Kubectl exec to the specific container in deployment

2/17/2020

Running a deployment with three pod.

NAME                        READY   STATUS    RESTARTS   AGE
my-api-XXX                  3/3     Running   0          4h

Containers:
  zipkin:
    Container ID:   docker://XXX
    Image:          openzipkin/zipkin:2.11
    Image ID:       docker-pullable://openzipkin/zipkin@sha256:XXX
    Port:           8611/TCP
    Host Port:      8611/TCP
    State:          Running
      Started:      Mon, 17 Feb 2020 12:13:03 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      XXX
  my-api:
    Container ID:   docker://XXX
    Image:          XXX
    Image ID:       XXX
    Ports:          5000/TCP, 6000/TCP
    Host Ports:     5000/TCP, 6000/TCP
    State:          Running
      Started:      Mon, 17 Feb 2020 12:13:04 +0800
    Ready:          True
    Restart Count:  0
    Mounts:
      XXX
  my-metrics:
    Container ID:   docker://XXX
    Image:          XXX
    Image ID:       XXX
    Ports:          5001/TCP, 6001/TCP
    Host Ports:     5001/TCP, 6001/TCP
    State:          Running
      Started:      Mon, 17 Feb 2020 12:13:04 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      XXX
    Mounts:
      XXX

The only one pod container I can connect is zipkin with kubectl exec -it my-api-XXX -- /bin/bash.

If I want to access the my-api container using kubectl exec -it my-api-XXX -c <my-api container ID> -- /bin/bash.

It report a error show the container is not in that pod.

Error from server (BadRequest): container my-api_containerID is not valid for pod my-api-XXX

-- ccd
kubectl
kubernetes

1 Answer

2/17/2020

kubectl exec -it "pod-name" -c "container-name" -n "namespace"

Here only the container name is needed. In your case it will be:

kubectl exec -it my-api-XXX -c my-api -- /bin/bash

You can exec to Zipkin because exec is taking zipkin as the default container.

-- shashank tyagi
Source: StackOverflow