【问题标题】:Ephemeral containers in KubernetesKubernetes 中的临时容器
【发布时间】:2020-12-11 06:03:13
【问题描述】:

我创建了一个标准的 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

我错过了什么?

【问题讨论】:

    标签: kubernetes sidecar


    【解决方案1】:

    背景

    不幸的是,这仍然是alfa feature,它不适合生产。如Ephemeral Containers 文档中所述。

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

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

    测试

    我已经在 Kubeadm 上使用 Kubernetes 1.19 进行了测试。 Feature Gates 标志在配置文件中设置,例如 apiserverschedulercontroller-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
    $ 
    

    描述 pod

    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 标志创建的。在Copying a Pod while adding a new container 中有说明。

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

    旁注

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

    选项 2

    Ephemeral Containers — the future of Kubernetes workload debugging 文章中描述了此选项和所有先决条件。而不是Deployment,我使用了nginx pod。

    $ 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 &lt;podname&gt; 验证是否创建了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
    

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

    【讨论】:

    • 嗨,PjoterS!谢谢。还有其他方法可以将边车添加到 RUNNING 吊舱吗?
    • 如果 pod 正在运行,则只能使用 ephemeral sidecar。如果您将 pod 与另一个容器作为 sidecar,您可以 copy 现有的一个并将新容器添加为 sidecar。它被描述为here。另一种选择是使用一些 Istio 功能 - Sidecar Injection
    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多