k8s 简易HA

在前一章的基础上搭建k8s HA
环境 192.168.201.45
另一台master(apiserver、scheduler、controller-manager)
192.168.201.48
LB_master(nginx、keepalived)
192.168.201.46
LB_backup(nginx、keepalived)
192.168.201.50
VIP
k8s 简易HA
文章图片

另一台master

#scp -r /opt/kubernetes/ root@master2_ip:/opt #在单master基础上继续,将master1上的文件拷贝到master2上 #scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@master2_ip:/usr/lib/systemd/system/ #将服务一并拷贝过去 #scp -r /opt/etcd/ -r root@master2_ip:/opt #vi kube-apiserver #将master1_ip修改为master2_id #ntpdate time.windows.com #需要注意虚拟机里的时间要同步 #systemctl start kube-apiserver #systemctl start kube-controller-manager #systemctl start kube-scheduler #systemctl enable kube-apiserver #systemctl enable kube-controller-manager #systemctl enable kube-scheduler #kubectl get node #确认是否能够正常获取到node

LB_master
#nginx.sh #文件中第一部分($要转义)。生成安装yum源 #yum install nginx -y #vi /etc/nginx/nginx.conf #将nginx.conf加入http前,并修改相关ip为master1_ip与master2_ip,添加server。其中http是七层配置,加入的stream是四层配置。 #systemctl start nginx #yum install keepalived -y #cp keepalived.conf /etc/keepalived/keepalived.conf #上传配置文件覆盖自带配置文件 #vi /etc/keepalived/keepalived.conf #check_nginx文件,用于故障转移;配置文件中IP地址未被使用,会使用此IP作为VIP #bash -x /etc/nginx/check_nginx.sh #systemctl start keepalived #systemctl enable keepalived

nginx.sh
cat > /etc/yum.repos.d/nginx.repo << EOF [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 EOF

nginx.conf
stream {log_formatmain'$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent'; access_log/var/log/nginx/k8s-access.logmain; upstream k8s-apiserver { server 10.0.0.3:6443; server 10.0.0.8:6443; } server { listen 6443; proxy_pass k8s-apiserver; } }

LB_backup
#nginx.sh #文件中第一部分($要转义)。生成安装yum源,与LB_master一致。 #yum install nginx -y #yum install keepalived -y #在 LB_backup安装 #scp /etc/keepalived/keepalived.conf root@LB_backup_ip:/etc/keepalived/ #在LB上将配置文件复制到LB_backup上,并修改文件,state、interface、priority #scp /etc/nginx/check_nginx.sh root@ LB_backup_ip:/etc/nginx/ #systemctl start keepalived #在LB_backup上执行 #systemctl enable keepalived #vi /opt/kubernetes/cfg/bootstrap.kubeconfig #在node上修改将master_ip修改为VIP #vi /opt/kubernetes/cfg/kubelet.kubeconfig #在node上修改将master_ip修改为VIP #vi /opt/kubernetes/cfg/kube-proxy.kubeconfig #在node上修改将master_ip修改为VIP #systemctl restart kubelet #systemctl restart kube-proxy #重启服务,node2也同样

知识点1
配置文件中一个实例vrrp_instance就包括LB_master与LB_backup,所以virtual_router_id值,在一个实例中是一样的;配置文件中LB_master的state配置为master,LB_BACKUP配置为BACKUP;priority是优先级,备服务器要比主服务器数小。
k8s 简易HA
文章图片

知识点2
在LB_master上配置的VIP,会绑定到配置文件中网络接口上。此时此IP就是可正常PING通使用IP。
k8s 简易HA
文章图片

知识点3
验证VIP是否生效,就在LB_master上停掉nginx,查看VIP是否飘移到LB_backup(使用ip addr查看) 。
【k8s 简易HA】知识点4
VIP地址应该包含到server.pem与server-key.pem文件中。也就是在server-csr.json中hosts中。如果未包括重新编辑server-csr.json,然后重新生成server.pem与server-key.pem,并将文件放到master对应的ssl目录下。

    推荐阅读