k8s集群的高可用实际是k8s各核心组件的高可用,这里使用主备模式,架构如下:
主备模式高可用架构说明: 核心组件 高可用模式 高可用实现方式
apiserver 主备 keepalived
controller-manager 主备 leader election
scheduler 主备 leader election
etcd 集群 kubeadm
apiserver 通过keepalived实现高可用,当某个节点故障时触发keepalived vip 转移;
controller-manager k8s内部通过选举方式产生领导者(由--leader-elect 选型控制,默认为true),同一时刻集群内只有一个controller-manager组件运行;
scheduler k8s内部通过选举方式产生领导者(由--leader-elect 选型控制,默认为true),同一时刻集群内只有一个scheduler组件运行;
etcd 通过运行kubeadm方式自动创建集群来实现高可用,部署的节点数为奇数,3节点方式最多容忍一台机器宕机。
部署环境 k8s版本 kubelet version kubeadm version kubectl versionv1.15.1 v1.15.1 v1.15.1
主机配置 Centos版本 系统内核 docker version flannel version Keepalived version
7.8.2003 4.4.223 19.03.9 v0.11.0 v1.3.5
主机列表 主机名 ip 主机配置 备注
master01 192.168.213.181 4U4G control plane
master02 192.168.213.182 4U4G control plane
master03 192.168.213.183 4U4G control plane
node01 192.168.213.192 2U2G node
node02 192.168.213.192 2U2G node
VIP 192.168.213.200 4U4G 在control plane上浮动
私有仓库 主机名 ip 主机配置 备注
docker-registry 192.168.213.129 2U1G reg.zhao.com
其他准备
系统初始化,docker安装,k8s(kubelet、kubeadm和kubectl)安装省略
kubelet 运行在集群所有节点上,用于启动Pod和容器
kubeadm 用于初始化集群,启动集群
kubectl 用于和集群通信,部署和管理应用,查看各种资源,创建、删除和更新各种组件
启动kubelet并设置开机启动 systemctl enable kubelet && systemctl start kubelet
keepalived安装在所有master节点上安装
安装keepalived [root@master01 ~]# yum -y install keepalived keepalived配置 master01 [root@master01 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id master01 } vrrp_instance VI_1 { state MASTER interface ens160 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.27.34.130 } } master02 [root@master02 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id master02 } vrrp_instance VI_1 { state BACKUP interface ens160 virtual_router_id 50 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.27.34.130 } } master03 [root@master03 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id master03 } vrrp_instance VI_1 { state BACKUP interface ens160 virtual_router_id 50 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.27.34.130 } } 启动keepalived并设置开机启动 [root@master01 ~]# systemctl start keepalived [root@master01 ~]# systemctl enable keepalived VIP查看 配置master节点 初始化master01节点 master01初始化 #初始化的配置文件 [root@master01 ~]# cat kubeadm-config.yaml apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration kubernetesVersion: v1.15.1 apiServer: certSANs: ##填写所有kube-apiserver节点的hostname、IP、VIP - master01 - master02 - master03 - node01 - node02 - 192.168.213.181 - 192.168.213.182 - 192.168.213.183 - 192.168.213.191 - 192.168.213.192 - 192.168.213.200 controlPlaneEndpoint: "192.168.213.200:6443" networking: podSubnet: "10.244.0.0/16" [root@master01 ~]# kubeadm init --config=kubeadm-config.yaml|tee kubeadim-init.log