首页 > 解决方案 > 如何使用设置“hostNetwork:true”和“NET_BIND_SERVICE”调试“没有可用端口”错误

问题描述

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

我正在尝试使用 Kubernetes 的 Docker Desktop 风格在 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))

然后,我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

标签: kubernetesdocker-for-mac

解决方案


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


推荐阅读