Cannot execute netcat inside a Docker/Kubernetes container

3/13/2019

I am trying to deploy a few Docker containers via Kubernetes within an AWS cluster. I have no problems with the deployment itself, but I am unable to execute netcat from inside a container.

If I enter the container (kubectl exec -it example-0 bash) and I attempt to execute: nc -w 1 -q 1 127.0.0.1 2181, it fails with:

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

This becomes a problem for some of my containers because I am using the netcat command to implement the corresponding readiness probes.

Example:

Deploying a Zookeeper container:

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

and it deploys fine, but the readiness probe fails with the following error:

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

I am new to Kubernetes, does anybody know what I am missing?

Thank you for your help.

-- João Matos
kubernetes
netcat

0 Answers