無法從 kubernetes 叢集中的 pod 存取主機

無法從 kubernetes 叢集中的 pod 存取主機

我在 Fedora Linux KVM 虛擬化環境上使用 kubeadm 設定了 1 個主節點和 1 個工作節點 kubernetes 集群,pod cidr 範圍為 - 10.244.0.0/16。使用 flannel 網路策略。

主節點:主機名稱 - fedkubemaster ip 位址 - 192.168.122.161 工作節點:主機名稱 - fedkubenode ip 位址 - 192.168.122.27 (注意 - 我的主機 FQDN 無法透過 DNS 解析)

$ kubectl get nodes -o wide
NAME            STATUS   ROLES                  AGE     VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                                KERNEL-VERSION            CONTAINER-RUNTIME
fedkubemaster   Ready    control-plane,master   2d20h   v1.23.3   192.168.122.161   <none>        Fedora Linux 35 (Workstation Edition)   5.15.16-200.fc35.x86_64   docker://20.10.12
fedkubenode     Ready    <none>                 2d6h    v1.23.3   192.168.122.27    <none>        Fedora Linux 35 (Workstation Edition)   5.15.16-200.fc35.x86_64   docker://20.10.12

這是我從主節點、工作節點的路線

[admin@fedkubemaster ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG    100    0        0 enp1s0
10.244.0.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
10.244.1.0      10.244.1.0      255.255.255.0   UG    0      0        0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-25b1faebd814
192.168.122.0   0.0.0.0         255.255.255.0   U     100    0        0 enp1s0
[admin@fedkubenode ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG    100    0        0 enp1s0
10.244.0.0      10.244.0.0      255.255.255.0   UG    0      0        0 flannel.1
10.244.1.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.122.0   0.0.0.0         255.255.255.0   U     100    0        0 enp1s0

我正在使用這個 dnsutil pod yml 定義來測試我與主機的連接

apiVersion: v1
kind: Pod
metadata:
  name: dnsutils
  namespace: default
spec:
  containers:
  - name: dnsutils
    image: k8s.gcr.io/e2e-test-images/jessie-dnsutils:1.3
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

這是 dnsutils pod 中我的 ip addr 和 ip Route show 輸出。

root@dnsutils:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default 
    link/ether 7a:50:37:bc:4b:45 brd ff:ff:ff:ff:ff:ff
    inet 10.244.1.2/24 brd 10.244.1.255 scope global eth0
       valid_lft forever preferred_lft forever
root@dnsutils:/# 
root@dnsutils:/# ip route show
default via 10.244.1.1 dev eth0 
10.244.0.0/16 via 10.244.1.1 dev eth0 
10.244.1.0/24 dev eth0  proto kernel  scope link  src 10.244.1.2 

我正在嘗試對我的主機 FQDN 進行 nslookup 和 ping,但它無法解析。然後我嘗試使用它們各自的 IP 位址執行 ping 操作,其中主節點將輸出顯示為封包已被過濾,並且工作節點能夠使用 IP 位址進行回應。

root@dnsutils:/# nslookup fedkubemaster
;; connection timed out; no servers could be reached

root@dnsutils:/# nslookup fedkubenode  
;; connection timed out; no servers could be reached
root@dnsutils:/# ping fedkubemaster
ping: unknown host fedkubemaster
root@dnsutils:/# ping fedkubenode  
ping: unknown host fedkubenode
root@dnsutils:/# ping 192.168.122.161
PING 192.168.122.161 (192.168.122.161) 56(84) bytes of data.
From 10.244.1.1 icmp_seq=1 Packet filtered
From 10.244.1.1 icmp_seq=2 Packet filtered
^C
--- 192.168.122.161 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1013ms

root@dnsutils:/# ping 192.168.122.27 
PING 192.168.122.27 (192.168.122.27) 56(84) bytes of data.
64 bytes from 192.168.122.27: icmp_seq=1 ttl=64 time=0.286 ms
64 bytes from 192.168.122.27: icmp_seq=2 ttl=64 time=0.145 ms

問題是我想讓我的主機 FQDN 可以從 Pod 內解析,但我無法理解如何修復它。似乎沒有路由可以從 pod 內解析我的主機 FQDN,這也反映在 coredns 日誌中。這是錯誤。

[admin@fedkubemaster networkutils]$ kubectl logs -f coredns-64897985d-8skq2 -n kube-system
.:53
[INFO] plugin/reload: Running configuration MD5 = db32ca3650231d74073ff4cf814959a7
CoreDNS-1.8.6
linux/amd64, go1.17.1, 13a9191
[ERROR] plugin/errors: 2 2603559064493035223.1593267795798361043. HINFO: read udp 10.244.0.2:38440->192.168.122.1:53: read: no route to host
[ERROR] plugin/errors: 2 2603559064493035223.1593267795798361043. HINFO: read udp 10.244.0.2:34275->192.168.122.1:53: read: no route to host

我正在嘗試弄清楚是否可以在預設情況下將路由新增至 Pod,但不太熟悉它們來修復它。

請建議。如果需要任何其他詳細信息,請告訴我。

謝謝蘇迪爾

答案1

我能夠透過暫時停用主伺服器和工作伺服器上的防火牆服務來解決我的問題。

[admin@fedkubemaster ~]$ sudo systemctl stop firewalld.service
[admin@fedkubemaster ~]$ sudo systemctl disable firewalld.service

[admin@fedkubenode ~]$ sudo systemctl stop firewalld.service
[admin@fedkubenode ~]$ sudo systemctl disable firewalld.service

但我仍然需要理解為什麼即使所有所需連接埠根據啟用 kubernetes 文件的原因,它導致了此問題。

相關內容