问题描述

添加k8s node2后,使用node2域名访问的时候一直提示timeout。网络是flannel插件。

查看详细

1
2
3
4
5
6
7
8
9
10
11
12
13
14
kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-7568f67dbd-5cfz7 1/1 Running 0 3d14h 10.244.0.231 master <none> <none>
coredns-7568f67dbd-hnhtq 1/1 Running 0 3d14h 10.244.0.232 master <none> <none>
etcd-master 1/1 Running 0 3d17h 10.0.16.13 master <none> <none>
kube-apiserver-master 1/1 Running 0 3d17h 10.0.16.13 master <none> <none>
kube-controller-manager-master 1/1 Running 0 3d17h 10.0.16.13 master <none> <none>
kube-flannel-ds-6s7kb 1/1 Running 0 17h 172.17.8.21 node2 <none> <none>
kube-flannel-ds-bm2pd 1/1 Running 0 3d13h 10.8.0.14 node1 <none> <none>
kube-flannel-ds-vp5ln 1/1 Running 0 3d13h 10.0.16.13 master <none> <none>
kube-proxy-dpc4t 1/1 Running 0 17h 172.17.8.21 node2 <none> <none>
kube-proxy-lqv8g 1/1 Running 0 3d13h 10.8.0.14 node1 <none> <none>
kube-proxy-nxtr2 1/1 Running 0 3d17h 10.0.16.13 master <none> <none>
kube-scheduler-master 1/1 Running 0 3d17h 10.0.16.13 master <none> <none>

我们先去查看下 CNI 的配置文件:

1
2
3
4
5
6
[root@node2 ~]# ls -la /etc/cni/net.d/
总用量 16
drwxr-xr-x 2 1001 116 4096 11月 19 18:31 .
drwxr-xr-x 3 1001 116 4096 7月 30 01:13 ..
-rw-r--r-- 1 1001 116 604 7月 30 01:13 10-containerd-net.conflist
-rw-r--r-- 1 root root 292 11月 19 18:31 10-flannel.conflist

可以看到里面包含两个配置,一个是 10-containerd-net.conflist,另外一个是我们上面创建的 Flannel 网络插件生成的配置,我们的需求肯定是想使用 Flannel 的这个配置,我们可以查看下 containerd 这个自带的 cni 插件配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@node2 ~]# cat /etc/cni/net.d/10-containerd-net.conflist
{
"cniVersion": "0.4.0",
"name": "containerd-net",
"plugins": [
{
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"promiscMode": true,
"ipam": {
"type": "host-local",
"ranges": [
[{
"subnet": "10.88.0.0/16"
}],
[{
"subnet": "2001:4860:4860::/64"
}]
],
"routes": [
{ "dst": "0.0.0.0/0" },
{ "dst": "::/0" }
]
}
},
{
"type": "portmap",
"capabilities": {"portMappings": true}
}
]
}

可以看到上面的 IP 段恰好就是 10.88.0.0/16,但是这个 cni 插件类型是 bridge 网络.

但是使用 bridge 网络的容器无法跨多个宿主机进行通信,跨主机通信需要借助其他的 cni 插件,比如上面我们安装的 Flannel,或者 Calico 等等,由于我们这里有两个 cni 配置,所以我们需要将 10-containerd-net.conflist 这个配置删除,因为如果这个目录中有多个 cni 配置文件,kubelet 将会使用按文件名的字典顺序排列的第一个作为配置文件,所以前面默认选择使用的是 containerd-net 这个插件

1
2
3
4
5
6
mv /etc/cni/net.d/10-containerd-net.conflist /etc/cni/net.d/10-containerd-net.conflist.bak
ifconfig cni0 down && ip link delete cni0
systemctl daemon-reload
systemctl restart containerd kubelet

kubectl delete pods kube-flannel-ds-6s7kb kube-proxy-dpc4t -n kube-system

然后就可以正常访问