#地址見: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 $@