首页 > 解决方案 > 无法在 Docker/Kubernetes 容器内执行 netcat

问题描述

我正在尝试通过 Kubernetes 在 AWS 集群中部署一些 Docker 容器。我对部署本身没有任何问题,但我无法从容器内执行 netcat。

如果我进入容器 ( kubectl exec -it example-0 bash) 并尝试执行: nc -w 1 -q 1 127.0.0.1 2181,它会失败并显示:

(UNKNOWN) [127.0.0.1] 2181 (?) : Connection refused

这对我的一些容器来说是个问题,因为我使用 netcat 命令来实现相应的就绪探测。

例子:

部署 Zookeeper 容器:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper
  labels:
    app: zookeeper
spec:
  serviceName: zookeeper
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      terminationGracePeriodSeconds: 1800
      nodeSelector:
        proc_host: "yes"
        host_role: "iw"
      containers:
      - name: zookeeper
        image: imageregistry:443/mydomain/zookeeper
        imagePullPolicy: Always
        ports:
        - containerPort: 2181
          name: client
        - containerPort: 2888
          name: peer
        - containerPort: 3888
          name: leader-election
        resources:
          limits:
            memory: 120Mi
          requests:
            cpu: "10m"
            memory: 100Mi
        lifecycle:
          preStop:
            exec:
              command: ["sh", "-ce", "kill -s TERM 1; while $(kill -0 1 2>/dev/null); do sleep 1; done"]
        env:
          - name: LOGGING_LEVEL
            value: WARN
          - name: ID_OFFSET
            value: "4"
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
        readinessProbe:
          exec:
            command:
            - /bin/bash
            - -c
            - '[ "imok" = "$(echo ruok | nc -w 1 -q 1 127.0.0.1 2181)" ]'
          initialDelaySeconds: 15
          timeoutSeconds: 5
        volumeMounts:
        - name: zookeeper
          mountPath: /tmp/zookeeper
      volumes:
        - name: zookeeper
          hostPath:
            path: /var/lib/kafka/zookeeper

并且部署良好,但就绪探测失败并出现以下错误:

Readiness probe failed: (UNKNOWN) [127.0.0.1] 2181 (?) : Connection refused

我是 Kubernetes 新手,有人知道我缺少什么吗?

谢谢您的帮助。

标签: kubernetesnetcat

解决方案


推荐阅读