重新初始化k8s叢集

2023-06-26 18:00:23
執行如下命令,所有節點都執行
kubeadm reset
初始化叢集,僅在master(centos01)上執行
[root@centos01 opt]# kubeadm init --apiserver-advertise-address 192.168.109.130 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.27.1 --pod-network-cidr=10.244.0.0/16

...
...
...

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.109.130:6443 --token osh87v.zvo010kamsr8esmp \
	--discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230

其中,
–apiserver-advertise-address:通告偵聽地址
–image-repository:指定映象地址使用阿里雲的,預設會使用谷歌映象
–kubernetes-version:指定當前的kubernetes的版本
–pod-network-cidr=10.244.0.0/16:flannel網路的固定地址範圍

仔細閱讀kubeadm init執行的結果,根據提示,還需要進行3步操作

1.筆者用的是root使用者,僅在master節點執行

[root@centos01 opt]# vim /etc/profile
#在最後一行增加
export KUBECONFIG=/etc/kubernetes/admin.conf

生效環境變數

[root@centos01 opt]# source /etc/profile

2.安裝網路外掛,可以選擇calico或flannel,這裡選擇安裝flannel,僅在master節點執行

下載安裝檔案

[root@centos01 opt]# wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

修改設定

[root@centos01 opt]# vim kube-flannel.yml
#修改Network項的值,改為和--pod-network-cidr一樣的值
"Network": "10.244.0.0/16"
#由於有時國內網路的問題,需要修改image的地址,把所有的docker.io改為dockerproxy.com
#共需要修改3處,兩個值
image: dockerproxy.com/flannel/flannel:v0.22.0
image: dockerproxy.com/flannel/flannel-cni-plugin:v1.1.2

安裝網路外掛

[root@centos01 opt]# kubectl apply -f kube-flannel.yml

驗證安裝

[root@centos01 opt]# kubectl get pods -n kube-flannel
NAME                    READY   STATUS    RESTARTS       AGE
kube-flannel-ds-dfngh   1/1     Running   17 (13m ago)   6d1h
kube-flannel-ds-qll8g   1/1     Running   12 (13m ago)   6d1h

3.其他節點加入叢集,非master節點都執行

[root@centos02 opt]# kubeadm join 192.168.109.130:6443 --token osh87v.zvo010kamsr8esmp --discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230
驗證安裝結果,僅在master節點執行
[root@centos01 opt]# kubectl get nodes
NAME       STATUS   ROLES           AGE    VERSION
centos01   Ready    control-plane   134m   v1.27.1
centos02   Ready    <none>          133m   v1.27.1
[root@centos01 opt]# kubectl get pods -n kube-system
NAME                               READY   STATUS    RESTARTS   AGE
coredns-7bdc4cb885-l4vs2           1/1     Running   0          9m3s
coredns-7bdc4cb885-wzc8x           1/1     Running   0          9m3s
etcd-centos01                      1/1     Running   0          9m18s
kube-apiserver-centos01            1/1     Running   0          9m18s
kube-controller-manager-centos01   1/1     Running   0          9m19s
kube-proxy-m92hr                   1/1     Running   0          28s
kube-proxy-pv4hw                   1/1     Running   0          9m3s
kube-scheduler-centos01            1/1     Running   0          9m18s

重啟一遍所有伺服器節點,有可能會暴露出來隱藏的問題,再次驗證以上結果,仍然正常。

最後,推薦一個釋出應用的平臺https://gitee.com/i512team/dhorse,是一個以應用為中心的雲原生DevOps系統,能夠實現持續整合、持續部署、微服務治理等功能,主要特點:部署簡單、操作簡潔、功能快速。