kubernetes master 高可用一键部署
#地址见:https://github.com/SILLKY/kubernetes-pro/tree/master/Master-HA
#包括其他一些文件,适当版本1.6.1
#!/bin/bash host=master.k8s host1=master1.k8s host2=master2.k8s ip0=192.168.255.125 ip1=192.168.255.126 ip2=192.168.255.127 #etcd数据备份 kube::etcd_backup() { docker stop etcd_test&&docker rm etcd_test rm -rf /home/etcd && cp -r /var/lib/etcd /home docker run --name etcd_test -d -p 12379:2379 -p 12380:2380 -v /home/etcd:/data appcelerator/etcd:3.0.15 echo '备份数据完毕' } #复制需要的文件到node节点 kube::copy_file() { #修改本机的etcd和apiserver cp etcd.yaml /etc/kubernetes/manifests/ cp kube-apiserver.yaml /etc/kubernetes/manifests/kube-apiserver.yaml #修改mininode节点上的配置 scp /etc/hosts root@$host1:/etc/ scp /etc/hosts root@$host2:/etc/ scp -r /etc/kubernetes/manifests root@$host1:/etc/kubernetes/ scp -r /etc/kubernetes/pki root@$host1:/etc/kubernetes/ scp /etc/kubernetes/controller-manager.conf root@$host1:/etc/kubernetes/ scp /etc/kubernetes/scheduler.conf root@$host1:/etc/kubernetes/ scp /etc/kubernetes/admin.conf root@$host1:/etc/kubernetes/ scp etcd1.yaml root@$host1:/etc/kubernetes/manifests/etcd.yaml scp kube-apiserver1.yaml root@$host1:/etc/kubernetes/manifests/kube-apiserver.yaml scp create-pki1.sh root@$host1:/root/create-pki.sh scp -r /etc/kubernetes/manifests root@$host2:/etc/kubernetes/ scp -r /etc/kubernetes/pki root@$host2:/etc/kubernetes/ scp /etc/kubernetes/controller-manager.conf root@$host2:/etc/kubernetes/ scp /etc/kubernetes/scheduler.conf root@$host2:/etc/kubernetes/ scp /etc/kubernetes/admin.conf root@$host2:/etc/kubernetes/ scp etcd2.yaml root@$host2:/etc/kubernetes/manifests/etcd.yaml scp kube-apiserver2.yaml root@$host2:/etc/kubernetes/manifests/kube-apiserver.yaml scp create-pki2.sh root@$host2:/root/create-pki.sh } # 修改相应的配置 kube::edit_file() { sed -i "s/{{ip}}/$ip0/g" /etc/kubernetes/manifests/kube-apiserver.yaml rm -rf /var/lib/etcd ssh root@$host1 "sed -i \"s/{{ip}}/$ip1/g\"" /etc/kubernetes/manifests/kube-apiserver.yaml ssh root@$host1 "sed -i \"s/{{ip}}/$ip1/g\"" /root/create-pki.sh ssh root@$host1 "sed -i \"s/$ip0/$ip1/g\"" /etc/kubernetes/admin.conf ssh root@$host1 "sed -i \"s/$ip0/$ip1/g\"" /etc/kubernetes/kubelet.conf ssh root@$host1 "sed -i \"s/$ip0/$ip1/g\"" /etc/kubernetes/controller-manager.conf ssh root@$host1 "sed -i \"s/$ip0/$ip1/g\"" /etc/kubernetes/scheduler.conf ssh root@$host1 'cd /root &&chmod +x create-pki.sh&&./create-pki.sh' ssh root@$host1 'rm -rf /var/lib/etcd' ssh root@$host2 "sed -i \"s/{{ip}}/$ip2/g\"" /etc/kubernetes/manifests/kube-apiserver.yaml ssh root@$host2 "sed -i \"s/{{ip}}/$ip2/g\"" /root/create-pki.sh ssh root@$host2 "sed -i \"s/$ip0/$ip2/g\"" /etc/kubernetes/admin.conf ssh root@$host2 "sed -i \"s/$ip0/$ip2/g\"" /etc/kubernetes/kubelet.conf ssh root@$host2 "sed -i \"s/$ip0/$ip2/g\"" /etc/kubernetes/controller-manager.conf ssh root@$host2 "sed -i \"s/$ip0/$ip2/g\"" /etc/kubernetes/scheduler.conf ssh root@$host2 'cd /root &&chmod +x create-pki.sh&&./create-pki.sh' ssh root@$host2 'rm -rf /var/lib/etcd' } # 重启kubelet kube::reset_kubelet() { systemctl daemon-reload systemctl restart kubelet ssh root@$host2 'systemctl daemon-reload' ssh root@$host2 'systemctl restart kubelet' ssh root@$host1 'systemctl daemon-reload' ssh root@$host1 'systemctl restart kubelet' } # 恢复etcd的数据到新的集群中 kube::etcd_recover() { #等待ETCD集群启动完毕 while : do result=$(kubectl get componentstatuses) result0=$(echo $result | grep 'etcd-0 Healthy {"health": "true"}') result1=$(echo $result | grep 'etcd-1 Healthy {"health": "true"}') result2=$(echo $result | grep 'etcd-2 Healthy {"health": "true"}') if [[ $result0 != "" && $result1 != "" && $result2 != "" ]] then echo "etcd集群启动完毕" break else echo "等待etcd集群启动......" fi sleep 10 done sleep 30 echo '准备还原数据' echo "export ETCDCTL_API=3&&etcdctl make-mirror $ip0:2379 --endpoints=$ip0:12379" docker exec etcd_test sh -c "export ETCDCTL_API=3&&etcdctl make-mirror $ip0:2379 --endpoints=$ip0:12379"& echo '等待还原数据.....' sleep 60 docker stop etcd_test && docker rm etcd_test } # 最后设置loadBalace kube::loadBalace() { #设置代理为loadBalaceIp loadBalaceIp=kubernetes.default.svc kubectl get configmap/kube-proxy -n kube-system -o yaml > kube-proxy-configmap.yaml sed -i "s/$ip0/$loadBalaceIp/g" kube-proxy-configmap.yaml kubectl apply -f kube-proxy-configmap.yaml #删除flanne,kube-proxyl让其自动重建 kubectl delete pod -l app=flannel -n kube-system kubectl delete pod -l k8s-app=kube-proxy -n kube-system } main() { kube::etcd_backup kube::copy_file kube::edit_file kube::reset_kubelet kube::etcd_recover kube::loadBalace sleep 20 echo "-------------------------------------------------------" echo "已经完成master HA" echo "-------------------------------------------------------" kubectl get pods -n kube-system -o wide } main $@