如何使用设置`hostNetwork: true`和`NET_BIND_SERVICE`调试“没有空闲端口”错误

skw*_*kie 5 kubernetes docker-for-mac

我需要一些调试错误的帮助: 0/1 nodes are available: 1 node(s) didn't have free ports for the requested pod ports.帮助有人可以帮忙吗?

我正在尝试使用 Kubernetes 的 Docker 桌面风格在 Mac 上运行一个 pod(第一个),版本是 2.1.0.1 (37199)。我想尝试使用 hostNetwork 模式,因为它的效率和需要打开的端口数量(以千计)。只有hostNetwork: true设置,没有错误,但我也没有看到主机上打开的端口,也没有看到容器内的主机网络接口。由于我还需要打开443端口,所以我添加了NET_BIND_SERVICE那是它开始抛出错误的时候。

lsof -i在容器内运行(ubuntu:18.04) 然后sudo lsof -i在我的 Mac 上运行,我没有看到冲突。然后,我也看了看,/var/lib/log/containers/kube-apiserver-docker-desktop_kube-system_kube-apiserver-*.log我没有看到任何线索。谢谢!

附加信息:我在容器内运行了以下内容:

# ss -nltp
State  Recv-Q  Send-Q     Local Address:Port      Peer Address:Port
LISTEN 0       5                0.0.0.0:10024          0.0.0.0:*      users:(("pnnsvr",pid=1,fd=28))
LISTEN 0       5                0.0.0.0:2443           0.0.0.0:*      users:(("pnnsvr",pid=1,fd=24))
LISTEN 0       5                0.0.0.0:10000          0.0.0.0:*      users:(("pnnsvr",pid=1,fd=27))
LISTEN 0       50               0.0.0.0:6800           0.0.0.0:*      users:(("pnnsvr",pid=1,fd=14))
LISTEN 0       1                0.0.0.0:6802           0.0.0.0:*      users:(("pnnsvr",pid=1,fd=13))
LISTEN 0       50               0.0.0.0:443            0.0.0.0:*      users:(("pnnsvr",pid=1,fd=15))
Run Code Online (Sandbox Code Playgroud)

然后,我跑了 netstat在我的 Mac(主机)上运行并搜索这些端口,但我找不到冲突。如果需要,我很乐意提供 netstat(767 行)的输出。

这是yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pnnsvr
  labels:
    app: pnnsvr
    env: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pnnsvr
      env: dev
  template:
    metadata:
      labels:
        app: pnnsvr
        env: dev
    spec:
      hostNetwork: true
      containers:
      - name: pnnsvr
        image: dev-pnnsvr:0.92
        args: ["--root_ip=192.168.15.194"]
        # for using local images
        imagePullPolicy: Never
        ports:
        - name: https
          containerPort: 443
          hostPort: 443
        - name: cport6800tcp
          containerPort: 6800
          hostPort:  6800
          protocol: TCP
        - name: cport10000tcp
          containerPort: 10000
          hostPort: 10000
          protocol: TCP
        - name: cport10000udp
          containerPort: 10000
          hostPort: 10000
          protocol: UDP
        - name: cport10001udp
          containerPort: 10001
          hostPort: 10001
          protocol: UDP
        #test
        - name: cport23456udp
          containerPort: 23456
          hostPort: 23456
          protocol: UDP
        securityContext:
          capabilities:
            add:
              - SYS_NICE
              - NET_BIND_SERVICE
              - CAP_SYS_ADMIN
Run Code Online (Sandbox Code Playgroud)

skw*_*kie 6

我无意中解决了这个问题,我是通过弹跳 pod 而不是使用kubectl apply -f .... 弹跳 Pod 后不久,新的 Pod 就会投入使用。我的理论是,Kubernetes 将启动一个新的 pod,并在杀死旧 pod 之前先做好一切准备。由于旧 Pod 仍然打开了端口,因此新 Pod 将看到这些端口已被占用,从而0/1 nodes are available: 1 node(s) didn't have free ports for the requested pod ports触发错误:。

  • 这个问题可以通过设置 maxSurge: 1 来解决,这样部署就会销毁旧的并立即创建新的。请参阅配置示例:````策略:类型:RollingUpdaterollingUpdate:maxSurge:1 maxUnavailable:1```` (2认同)