Kubernetes:NodePort 服务无法将负载平衡到其他节点上的 Pod

Kubernetes:NodePort 服务无法将负载平衡到其他节点上的 Pod

环境:

  • 1 名师傅,3 名工人
  • 操作系统:Ubuntu 18.04,由 Vagrant 配置 3 个网络接口(NAT、hostonly、bridge)
  • Kubernetes:1.21
  • Docker:20.10.5
  • Pod 网络:Weave Net

问题: NodePort 服务未将负载平衡到其他节点上的 pod。例如:

  • Worker1:pod1,pod2
  • Worker2:pod3、pod4
  • Worker3:没有 Pod

当我发出 curl 命令时:

  • http://worker1:30001:我只能在 pod1、pod2 上访问服务
  • http://worker2:30001:我只能在 pod3、pod4 上访问服务
  • http://worker3:30001: curl: (7) 无法连接到本地主机端口 30001:没有到主机的路由

附加信息:

k get nodes -o wide
NAME     STATUS   ROLES                  AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
vm-150   Ready    control-plane,master   35m   v1.21.0   10.0.2.15     <none>        Ubuntu 18.04.5 LTS   4.15.0-128-generic   docker://20.10.5
vm-163   Ready    <none>                 31m   v1.21.0   10.0.2.15     <none>        Ubuntu 18.04.5 LTS   4.15.0-128-generic   docker://20.10.5
vm-216   Ready    <none>                 31m   v1.21.0   10.0.2.15     <none>        Ubuntu 18.04.5 LTS   4.15.0-128-generic   docker://20.10.5
vm-217   Ready    <none>                 28m   v1.21.0   10.0.2.15     <none>        Ubuntu 18.04.5 LTS   4.15.0-128-generic   docker://20.10.5
k get pods --all-namespaces -o wide
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
default       hello-deploy-6575485494-4sstw    1/1     Running   0          33m   10.32.0.4   vm-216   <none>           <none>
default       hello-deploy-6575485494-69jdv    1/1     Running   0          33m   10.32.0.7   vm-163   <none>           <none>
default       hello-deploy-6575485494-9cshc    1/1     Running   0          33m   10.32.0.6   vm-216   <none>           <none>
default       hello-deploy-6575485494-bhsqr    1/1     Running   0          33m   10.32.0.6   vm-163   <none>           <none>
default       hello-deploy-6575485494-dwlfv    1/1     Running   0          33m   10.32.0.5   vm-216   <none>           <none>
default       hello-deploy-6575485494-kfwm7    1/1     Running   0          33m   10.32.0.3   vm-163   <none>           <none>
default       hello-deploy-6575485494-mxtrq    1/1     Running   0          33m   10.32.0.4   vm-163   <none>           <none>
default       hello-deploy-6575485494-qr458    1/1     Running   0          33m   10.32.0.5   vm-163   <none>           <none>
default       hello-deploy-6575485494-rg594    1/1     Running   0          33m   10.32.0.7   vm-216   <none>           <none>
default       hello-deploy-6575485494-vt6d8    1/1     Running   0          33m   10.32.0.3   vm-216   <none>           <none>
kube-system   coredns-558bd4d5db-bbxl7         1/1     Running   0          37m   10.32.0.2   vm-150   <none>           <none>
kube-system   coredns-558bd4d5db-kphk5         1/1     Running   0          37m   10.32.0.3   vm-150   <none>           <none>
kube-system   etcd-vm-150                      1/1     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   kube-apiserver-vm-150            1/1     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   kube-controller-manager-vm-150   1/1     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   kube-proxy-2j2f8                 1/1     Running   0          31m   10.0.2.15   vm-217   <none>           <none>
kube-system   kube-proxy-pbvvd                 1/1     Running   0          34m   10.0.2.15   vm-163   <none>           <none>
kube-system   kube-proxy-pwx7p                 1/1     Running   0          34m   10.0.2.15   vm-216   <none>           <none>
kube-system   kube-proxy-x9d8z                 1/1     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   kube-scheduler-vm-150            1/1     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   weave-net-jnkjp                  2/2     Running   1          31m   10.0.2.15   vm-217   <none>           <none>
kube-system   weave-net-l8fmm                  2/2     Running   0          34m   10.0.2.15   vm-163   <none>           <none>
kube-system   weave-net-lgzqp                  2/2     Running   0          37m   10.0.2.15   vm-150   <none>           <none>
kube-system   weave-net-mwvzj                  2/2     Running   0          34m   10.0.2.15   vm-216   <none>           <none>

部署清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deploy

spec:
  replicas: 10
  selector:
    matchLabels:
      app: hello-world
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-pod
        image: ngocchien/chien_test:1.0.0
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels:
    app: hello-world
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30001
    protocol: TCP
  selector:
    app: hello-world

相关内容