1. 程式人生 > 其它 >記一次使用Flannel外掛排錯歷程

記一次使用Flannel外掛排錯歷程

記一次使用Flannel外掛排錯歷程

原來使用的是Calico外掛,這不準備學習K8s的網路,就準備換成Flannel了,然後噩夢就開始了。。。

直接使用kubectl apply -f 安裝了flannel外掛,使用kubectl get pod -n kube-system檢視pod的執行狀態,一切都能美好,接著就準備直接執行個Pod看是否正常,結果等了半天還是在建立,這肯定不正常了,就使用kubectl describe pod命令檢視,結果發現報下面型別的錯誤:

Warning FailedCreatePodSandBox 7m48s kubelet, node1 Failed to create pod sandbox: rpc error: code = Unknown desc = [failed to set up sandbox container "c71c64ed91ac3f408b7809a4a98117714a15fc45282efb136affc2469a9f9b61" network for pod "coredns-7ff77c879f-544jh": networkPlugin cni failed to set up pod "coredns-7ff77c879f-544jh_kube-system" network: error getting ClusterInformation: Get https://[10.96.0.1]:443/apis/crd.projectcalico.org/v1/clusterinformations/default: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes"), failed to clean up sandbox container "c71c64ed91ac3f408b7809a4a98117714a15fc45282efb136affc2469a9f9b61" network for pod "coredns-7ff77c879f-544jh": networkPlugin cni failed to teardown pod "coredns-7ff77c879f-544jh_kube-system" network: error getting ClusterInformation: Get https://[10.96.0.1]:443/apis/crd.projectcalico.org/v1/clusterinformations/default: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes")]

嗯,看來是網路外掛的問題,初步分析應該是Calico沒有解除安裝乾淨。當初只是執行了kubeadm reset命令,接著就分別執行了下面的幾個命令:

$ ipvsadm --clear
$ rm -rf /etc/cni/net.d/
$  rm -rf $HOME/.kube/config

接著重新安裝flannel外掛,安裝完成後,執行kubectl get pod -n kube-system檢視,結果發現coredns一直起不來。使用describe命令檢視,發現報錯資訊和上面的類似。

繼續翻,原來kubelet會從預設目錄讀取配置檔案,如果有多個配置檔案,那麼它會應用按字母順序首先出現的配置檔案中的 CNI 外掛,CNI的配置檔案預設在/etc/cni/net.d/

目錄。

詳細可以參考官方文件:https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/

這時候感覺有些眉目了,前面我們一直在操作master,而沒有管node節點,進入node節點的/etc/cni/net.d/目,果然發現有calico的遺留。

刪除後進行kubeadm reset,然後重新加入master,在master上執行kubectl get pod -n kube-system,發現終於正常了。

接著呢,就有開始驗證是否能正常拉取映象,就執行了下面的命令:

kubectl create deployment nginx --image=nginx:1.14-alpine

檢視pod,結果發現還是不正常,這次報下面這種錯誤:

Warning FailedCreatePodSandBox 5m28s (x4 over 5m34s) kubelet, node1 (combined from similar events): Failed to create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "48c46e7e0d9198ff94721fba330cc9370f166c6d1c4181fb3543ef99461c4e8d" network for pod "nginx-55f8fd7cfc-4zbvl": networkPlugin cni failed to set up pod "nginx-55f8fd7cfc-4zbvl_default" network: failed to set bridge addr: "cni0" already has an IP address different from 10.244.3.1/24

從報錯資訊中看出,cni的ip地址不在10.244.3.1/24這個段。在node1上使用ip address檢視IP資訊。

從上圖中可以看到flannel.1的閘道器是10.244.3.9/32。接著使用cat /run/flannel/subnet.env檢視子網環境:

到這裡問題就明確了,我們使用的Overlay network為Flannel,也就是說Pod的IP地址段應該在Flannel的subnet下,而現在我們看到cni0的IP地址段與flannel subnet地址段不同,所以就出現了問題。

接下來直接刪除這個cni0這個錯誤網絡卡,刪除後會自動重建。

$ ifconfig cni0 down
$ ip link delete cni0

最後在master上進行驗證,問題解決。