Kubectl exec 到部署中的特定容器

ccd*_*ccd 2 kubernetes kubectl

运行具有三个 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
Run Code Online (Sandbox Code Playgroud)

我唯一可以连接的 pod 容器是 zipkin 和kubectl exec -it my-api-XXX -- /bin/bash.

如果我想使用kubectl exec -it my-api-XXX -c <my-api container ID> -- /bin/bash.

它报告错误,显示容器不在该 Pod 中。

来自服务器的错误 (BadRequest):容器 my-api_containerID 对于 pod my-api-XXX 无效

sha*_*agi 8

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

这里只需要容器名称。在你的情况下它将是:

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

您可以执行 Zipkin,因为exec将 zipkin 作为默认容器。