kubernetes使用临时容器排查pod异常问题

概念

    临时容器ephemeral:一种特殊的容器,该容器在现有 Pod 中临时运行,以便完成用户发起的操作,例如故障排查。 你会使用临时容器来检查服务,而不是用它来构建应用程序。

   临时容器与其他容器的不同之处在于,它们缺少对资源或执行的保证,并且永远不会自动重启, 因此不适用于构建应用程序。 临时容器使用与常规容器相同的 ContainerSpec 节来描述,但许多字段是不兼容和不允许的。

  • 临时容器没有端口配置,因此像 portslivenessProbereadinessProbe 这样的字段是不允许的。
  • Pod 资源分配是不可变的,因此 resources 配置是不允许的。
  • 有关允许字段的完整列表,请参见 EphemeralContainer 参考文档

临时容器是使用 API 中的一种特殊的 ephemeralcontainers 处理器进行创建的, 而不是直接添加到 pod.spec 段,因此无法使用 kubectl edit 来添加一个临时容器。

与常规容器一样,将临时容器添加到 Pod 后,将不能更改或删除临时容器。

临时容器的用途

当由于容器崩溃或容器镜像不包含调试工具而导致 kubectl exec 无用时, 临时容器对于交互式故障排查很有用。

尤其是,Distroless 镜像 允许用户部署最小的容器镜像,从而减少攻击面并减少故障和漏洞的暴露。 由于 distroless 镜像不包含 Shell 或任何的调试工具,因此很难单独使用 kubectl exec 命令进行故障排查。

使用临时容器时,启用 进程名字空间共享 很有帮助,可以查看其他容器中的进程。

开启特性支持临时容器

修改kube-apiserver、kube-scheduler、kubelet启动配置参数,添加--feature-gates=EphemeralContainers=true

通过kubectl debug mypod -it --image=busybox测试是否开启临时容器特性,未启用会报如下错误

Defaulting debug container name to debugger-ddw5.
error: ephemeral containers are disabled for this cluster (error from server: "the server could not find the requested resource").

使用临时容器调试

1.创建一个标准pod,作为临时容器故障排查的对象

kubectl create deployment my-nginx --image=nginx

2.获取容器的名字

3. 查看pod的yaml文件,注意对比目前没有临时容器的相关内容

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2022-04-19T07:59:03Z"
  generateName: my-nginx-779fb75d77-
  labels:
    app: my-nginx
    pod-template-hash: 779fb75d77
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:generateName: {}
        f:labels:
          .: {}
          f:app: {}
          f:pod-template-hash: {}
        f:ownerReferences:
          .: {}
          k:{"uid":"ab0bb326-2330-45b2-8746-75505610d432"}:
            .: {}
            f:apiVersion: {}
            f:blockOwnerDeletion: {}
            f:controller: {}
            f:kind: {}
            f:name: {}
            f:uid: {}
      f:spec:
        f:affinity: {}
        f:containers:
          k:{"name":"nginx"}:
            .: {}
            f:image: {}
            f:imagePullPolicy: {}
            f:lifecycle: {}
            f:name: {}
            f:resources:
              .: {}
              f:requests:
                .: {}
                f:cpu: {}
                f:memory: {}
            f:terminationMessagePath: {}
            f:terminationMessagePolicy: {}
        f:dnsPolicy: {}
        f:enableServiceLinks: {}
        f:restartPolicy: {}
        f:schedulerName: {}
        f:securityContext: {}
        f:terminationGracePeriodSeconds: {}
        f:volumes:
          .: {}
          k:{"name":"nfs-test"}:
            .: {}
            f:name: {}
            f:nfs:
              .: {}
              f:path: {}
              f:readOnly: {}
              f:server: {}
    manager: kube-controller-manager
    operation: Update
    time: "2022-04-19T07:59:03Z"
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:conditions:
          k:{"type":"ContainersReady"}:
            .: {}
            f:lastProbeTime: {}
            f:lastTransitionTime: {}
            f:status: {}
            f:type: {}
          k:{"type":"Initialized"}:
            .: {}
            f:lastProbeTime: {}
            f:lastTransitionTime: {}
            f:status: {}
            f:type: {}
          k:{"type":"Ready"}:
            .: {}
            f:lastProbeTime: {}
            f:lastTransitionTime: {}
            f:status: {}
            f:type: {}
        f:containerStatuses: {}
        f:hostIP: {}
        f:phase: {}
        f:podIP: {}
        f:podIPs:
          .: {}
          k:{"ip":"10.244.159.154"}:
            .: {}
            f:ip: {}
        f:startTime: {}
    manager: kubelet
    operation: Update
    time: "2022-05-07T14:52:01Z"
  name: my-nginx-779fb75d77-cs2fw
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: my-nginx-779fb75d77
    uid: ab0bb326-2330-45b2-8746-75505610d432
  resourceVersion: "13056554"
  selfLink: /api/v1/namespaces/default/pods/my-nginx-779fb75d77-cs2fw
  uid: 3e3b17df-0764-49f8-8b57-f59e93193354
spec:
  affinity: {}
  containers:
  - image: nginx:1.15.2
    imagePullPolicy: IfNotPresent
    lifecycle: {}
    name: nginx
    resources:
      requests:
        cpu: 10m
        memory: 10Mi
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-vmxk4
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: k8s-master1
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: nfs-test
    nfs:
      path: /data/nfs
      readOnly: true
      server: 10.10.20.5
  - name: default-token-vmxk4
    secret:
      defaultMode: 420
      secretName: default-token-vmxk4
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2022-04-19T07:59:03Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2022-05-07T14:52:01Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2022-05-07T14:52:01Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2022-04-19T07:59:03Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://ef580e03155572004c52f8e7f0f85c02b8bff26f3adb8cd04ca2b649c979c31a
    image: nginx:1.15.2
    imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
    lastState:
      terminated:
        containerID: docker://e00d837a4a2aa9f6893886ca6abc3b284c88fc3f65cca68e37dfc799dfb603ae
        exitCode: 255
        finishedAt: "2022-05-07T13:34:30Z"
        reason: Error
        startedAt: "2022-04-19T07:59:05Z"
    name: nginx
    ready: true
    restartCount: 1
    started: true
    state:
      running:
        startedAt: "2022-05-07T14:52:01Z"
  hostIP: 10.10.20.201
  phase: Running
  podIP: 10.244.159.154
  podIPs:
  - ip: 10.244.159.154
  qosClass: Burstable
  startTime: "2022-04-19T07:59:03Z"

4.执行kubectl debug创建临时容器排查pod的故障

kubectl debug -it pod/my-nginx-779fb75d77-cs2fw --image=busybox:1.28

当使用 kubectl describe pod my-nginx-779fb75d77-cs2fw 命令时,可以看到一个新字段 "Ephemeral Containers",此部分包含临时容器及其属性。

使用临时容器其他方式

1.创建一个使用指定镜像,名称为debugger的临时容器

 kubectl debug --image=myproj/debug-tools -c debugger mypod

2.创建一个mypod的副本,新增临时容器并挂载到副本上

kubectl debug mypod -it --image=busybox --copy-to=my-debugger

3.进程命名空间的共享

kubectl debug -it <POD_NAME> --image=busybox --share-processes --copy-to=debug-pod

4.创建一个临时容器,重定向进程到目标容器中

此命令添加一个新的 busybox 容器并将其挂接到该容器。--target 参数指定另一个容器的进程命名空间。

kubectl debug -it tomcat-test --image=busybox:1.28 --target=tomcat-java

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忍冬行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值