1

我创建了一个标准的 nginx pod,我想运行一个 sidecar 容器,但它在 Kubernetes 1.18 - 1.20 中不适合我。

# kubectl alpha debug nginx --image=busybox --target=nginx
Defaulting debug container name to debugger-6wlb5.

# kubectl attach nginx -c debugger-6wlb5
If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container debugger-6wlb5 not found in pod nginx_default 

我在 apiserver、调度程序和控制器管理器中启用了门功能。pod 上的描述不显示错误或其他内容:

Mounts:
    /var/run/secrets/kubernetes.io/serviceaccount from default-token-4jhz2 (ro)
Ephemeral Containers:
  debugger-6wlb5:
    Image:        busybox
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
Conditions:
  Type              Status

我在这里想念什么?

4

1 回答 1

3

背景

不幸的是,这仍然是alfa feature并且它不适合生产。如临时容器文档中所述。

本页概述了临时容器:一种特殊类型的容器,在现有 Pod 中临时运行以完成用户发起的操作,例如故障排除。您使用临时容器来检查服务而不是构建应用程序。

警告:临时容器处于早期 alpha 状态,不适合生产集群。

测试

我已经在KubeadmKubernetes 1.19 上对此进行了测试。Feature Gates标志在配置文件中设置,如apiserver,schedulercontroller-manager- --feature-gates=EphemeralContainers=true。也kubelet被修改了。因为这是alpha它需要特定的步骤才能使其工作。

选项1

此方法在Debug Running Pods - Debugging with an ephemeral debug container 中进行了描述。

$ kubectl run ephemeral-demo --image=k8s.gcr.io/pause:3.1 --restart=Never
pod/ephemeral-demo created
$ kubectl alpha debug -it ephemeral-demo --image=busybox --target=ephemeral-demo
Defaulting debug container name to debugger-hnr2w.
If you don't see a command prompt, try pressing enter.
/ # ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 /pause
    7 root      0:00 sh
   12 root      0:00 ps aux
/ # Now, you are in the debugger-hnr2w container which is in the same pod as container ephemeral-demo
/ # exit
$ 

描述吊舱

Containers:
  ephemeral-demo:
    Container ID:   docker://e24cf29efdab9fbf8180ec6c8a9539cdfbdfc490b3b4a4d5dd02d419887c8486
    Image:          k8s.gcr.io/pause:3.1
    ...
Ephemeral Containers:
  debugger-hnr2w:
    Container ID:   docker://649d020e92eedbe03d281fb2368f33ceec176eaaa340fb52af5bf59fc269c701
    Image:          busybox
    ...

在上面的示例中,attach是使用-i标志制作的。在添加新容器时复制 Pod 中有说明。

-i 标志默认情况下会导致 kubectl debug 附加到新容器。您可以通过指定 --attach=false 来防止这种情况。

边注

引用的第二部分是,If your session becomes disconnected you can reattach using kubectl attach.但是它适用于另一种方法,而不是Ephemeral Containers

选项 2

此选项和所有先决条件在Ephemeral Containers — Kubernetes 工作负载调试的未来一文中进行了描述。而不是Deployment我使用了nginxpod。

$ kubectl run nginx --image=nginx.

接下来,您必须.json使用以下配置创建文件。请更改metadata.name为您的 pod 名称。

{
    "apiVersion": "v1",
    "kind": "EphemeralContainers",
    "metadata": {
            "name": "nginx"
    },
    "ephemeralContainers": [{
        "command": [
            "sh"
        ],
        "image": "busybox:latest",
        "imagePullPolicy": "IfNotPresent",
        "name": "debugger",
        "stdin": true,
        "tty": true,
        "terminationMessagePolicy": "File"
    }]
}

重要的

你必须使用它来应用它kubectl replace --raw

$ kubectl replace --raw /api/v1/namespaces/default/pods/<podName>/ephemeralcontainers -f <jsonFileName>.json

否则会报错:

kubectl apply -f debug.json 
error: unable to recognize "debug.json": no matches for kind "EphemeralContainers" in version "v1"

应用正确的命令后:

$ kubectl replace --raw /api/v1/namespaces/default/pods/nginx/ephemeralcontainers -f debug.json
{"kind":"EphemeralContainers","apiVersion":"v1","metadata":{"name":"nginx","namespace":"default","selfLink":"/api/v1/namespaces/default/pods/nginx/ephemeralcontainers","uid":"...}

用于kubectl describe <podname>验证是否EphemeralContainer已创建。

$ kubectl describe po | grep 'Container ID' -B 2
Containers:
  nginx:
    Container ID:   docker://a410b326cdc3b95abb2edff8cdb4d7edca9498ba44b54ca6a448967596391813
--
Ephemeral Containers:
  debugger:
    Container ID:  docker://a1357c0daed0ad5664b8c838183a3eb0716339020e829077c14e7438fa5e1cf5

使用此方法,您将能够使用kubectl attach

$ kubectl attach -it nginx -c debugger
If you don't see a command prompt, try pressing enter.
/ # 

结论

Ephemeral Container是一种特殊类型的容器,它在现有 Pod 中临时运行,以完成用户发起的操作,例如故障排除。您将终止会话,您将无法再次连接。

你会得到错误:

$ kubectl attach -it nginx -c debugger
If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container debugger not found in pod nginx_default

将来可能会更改,但根据我的测试,您只能连接到此容器一次。

于 2020-12-11T15:22:40.943 回答