內網不在一個網段的兩臺雲服務器搭建K8S 遇到的坑及解決方案

     這兩天在搭建K8S服務器,因爲我的是兩臺不在同一網段的服務器,遇到了不少大大小小的坑,在多次想放棄的情況下,還是一步步艱難排除萬難,取得了最終勝利!言歸正傳,下面一一列舉一下,希望對大家有所幫助。

1、kube init初始化master

在初始化時,遇到了一個坑:

kubeadm init --kubernetes-version=1.14.0 --apiserver-advertise-address=192.168.0.1 --pod-network-cidr=10.244.0.0/16

在--apiserver-advertise-address的這個ip地址我使用的是服務器的外網地址,導致出錯,這裏要寫成內網地址

2、加入work節點

在初始化master節點之後獲取到:

kubeadm join 192.168.0.1:6443 --token 51ezko.v8bf2b5yqqd5lufy \
    --discovery-token-ca-cert-hash sha256:95407ad19119963699661fb7474d2446cd1c0440abc07a5071b78bf193630c68
    

在work機器上執行該語句,如下:

排查:

第一步:

1、直接ping一下master服務器:外網地址可以ping通,內網ping不通;

2、telnet 一下master服務器6443端口(api-server),發現不通;

解決方案:

1>先關閉了master節點的防火牆(也可以不關閉,只暴露需要對外開放的端口即可),但是仍然ping不通,有點尷尬了;

2>後來查了相關資料,發現還要在騰訊雲服務器管理界面打開端口;

經過以上兩步便可以正常ping通了6443端口。

第二步:

繼續執行:

kubeadm join 192.168.0.1:6443 --token 51ezko.v8bf2b5yqqd5lufy \
    --discovery-token-ca-cert-hash sha256:95407ad19119963699661fb7474d2446cd1c0440abc07a5071b78bf193630c68

執行結果:

 

[root@VM_0_9_centos ~]# kubeadm join 135.232.168.96:6443 --token 51ezko.v8bf2b5yqqd5lufy \
>     --discovery-token-ca-cert-hash sha256:95407ad19119963699661fb7474d2446cd1c0440abc07a5071b78bf193630c68
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
error execution phase preflight: unable to fetch the kubeadm-config ConfigMap: failed to get config map: Get https://122.127.0.13:6443/api/v1/namespaces/kube-system/configmaps/kubeadm-config: dial tcp 122.127.0.13:6443: i/o timeout

排查問題:

1、先在work服務器ping一下master的內網地址,發現內網地址ping不通;

解決方案:在work服務器上執行如下語句

iptables -t nat -A OUTPUT -d 192.168.0.1 -j DNAT --to-destination 152.132.125.96

注:第一個ip是master節點的內網ip,後一個ip是外網Ip

第三步:大功告成!

[root@VM_0_9_centos ~]# kubeadm join 192.168.0.1:6443 --token 51ezko.v8bf2b5yqqd5lufy     --discovery-token-ca-cert-hash sha256:95407ad19119963699661fb7474d2446cd1c04407a5071b78bf193630c68
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

最後

我們在master主機上查看一下k8s的節點就可以了

 

[root@VM_0_13_centos ~]# kubectl get nodes
NAME   STATUS   ROLES    AGE     VERSION
m      Ready    master   4h54m   v1.14.0
w1     Ready    <none>   23m     v1.14.0

 

發佈了27 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章