由於是筆記本虛擬機搭建因此很久沒打開k8s集羣了,開機後先是提示:Unable to connect to the server: x509: certificate has expired or is not yet,一會後就提示:The connection to the server 192.168.37.201:6443 was refused - did you specify the right host or port?,排查後發現爲證書過時。node
1:先查看集羣證書過時時間:docker
sudo openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ' 輸出: Not Before: May 24 03:32:37 2019 GMT Not After : May 23 03:32:38 2020 GMT
2:在當前目錄編輯kubeadm.conf配置文件:bootstrap
sion: kubeadm.k8s.io/v1beta1 kind: ClusterConfiguration kubernetesVersion: v1.15.0 # kubernetes 版本 apiServer: certSANs: - 192.168.10.xxx # master 全部節點IP地址,包括master和slave - 192.168.10.xxx # slave1 - 192.168.10.xxx # slave2 extraArgs: service-node-port-range: 80-32767 advertise-address: 0.0.0.0 controlPlaneEndpoint: "192.168.10.xxx:6443" # APIserver 地址,也就是master節點地址 imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers #這裏使用國內阿里雲的鏡像倉庫
3:更新證書api
kubeadm alpha certs renew all --config kubeadm.conf sudo openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ' 輸出: Not Before: Jun 24 10:55:40 2019 GMT Not After : Jul 27 08:37:35 2021 GMT
4:從新生成配置文件bash
mv /etc/kubernetes/*.conf ~/. kubeadm init phase kubeconfig all --config kubeadm.conf
5:更新.kube下的配置文件:app
mv $HOME/.kube/config $HOME/.kube/config.old sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
6:重啓kube-apiserver,kube-controller,kube-scheduler,etcd這4個容器:(必定要ps -a要不有可能服務容器沒啓動)ide
docker ps -a | grep -v pause | grep -E "etcd|scheduler|controller|apiserver" | awk '{print $1}' | awk '{print "docker","restart",$1}' | bash
7:重啓容器後發現節點都爲NotReady狀態google
[root@k8s-master kubernetes]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 384d v1.15.0 k8s-node1 NotReady node 384d v1.15.0 [root@k8s-master pki]# cd /var/lib/kubelet/pki/ [root@k8s-master pki]# ls kubelet-client-2019-12-17-18-50-01.pem kubelet-client-2019-12-17-18-50-52.pem kubelet-client-current.pem kubelet.crt kubelet.key
8:master節點中kubelet證書也須要更新,從新生成證書,刪除kubelet配置文件阿里雲
kubelet會向apiserver發起一個csr [root@k8s-master pki]# mkdir bakup [root@k8s-master pki]# mv kubelet* bakup/ [root@k8s-master pki]# ls bakup [root@k8s-master pki]# systemctl restart kubelet [root@k8s-master pki]# systemctl status kubelet [root@k8s-master pki]# openssl x509 -in kubelet.crt -noout -text |grep ' Not ' Not Before: Jan 5 09:25:07 2021 GMT Not After : Jan 5 09:25:07 2022 GMT //查看未受權的CSR請求: [root@k8s-master pki]# kubectl get csr<br> //approve CSR 請求,有則贊成,沒有則無論: [root@k8s-master pki]# kubectl certificate approve csr-4pw6g NAME AGE REQUESTOR CONDITION csr-4pw6g 1h kubelet-bootstrap Approved,Issued //驗證結果 [root@k8s-master kubernetes]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 384d v1.15.0
9:master節點就更新完成了,而後獲取token在更新slave節點時要用rest
[root@k8s-master kubernetes]# kubeadm token create [root@k8s-master kubernetes]# kubeadm token list TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS yszrm3.d1zaj2uwj1pfo627 23h 2021-01-06T18:14:57+08:00 authentication,signing <none> system:bootstrappers:kubeadm:default-node-token
10:node節點添加進集羣(需刪除原先kubelet配置文件,不然加入失敗)
[root@k8s-node2 ~]# rm -rf /etc/kubernetes/kubelet.conf [root@k8s-node2 ~]# rm -rf /etc/kubernetes/pki/ca.crt [root@k8s-node2 ~]# rm -rf /etc/kubernetes/bootstrap-kubelet.conf [root@k8s-node2 ~]# systemctl stop kubelet [root@k8s-node2 ~]# kubeadm join --token=yszrm3.d1zaj2uwj1pfo627 192.168.10.XXX:6443 --node-name k8s-node2 --discovery-token-unsafe-skip-ca-verification
11:驗證結果
[root@k8s-master kubernetes]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 384d v1.15.0 k8s-node1 Ready node 384d v1.15.0 k8s-node2 Ready <none> 11m v1.15.0