Pod Debug 示例

发布时间: 更新时间: 总字数:436 阅读时间:1m 作者: IP上海 分享 网址


使用临时容器调试 pod

k8s 中,有些容器没有 sh、bash、ash 等命令,我们使用 exec 尝试调试容器时总是报错,下面介绍如上使用临时容器调试 pod

  • 环境准备
root@k8s-master:~# kubectl run ephemeral-demo --image=gcmirrors/pause:3.6 --restart=Never
pod/ephemeral-demo created
root@k8s-master:~# kubectl get pod
ephemeral-demo   1/1     Running   0          4s
  • 尝试使用 kubectl exec 来创建一个 shell 报如下错误:
root@k8s-master:~# kubectl exec -it ephemeral-demo -- sh
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "sh": executable file not found in $PATH: unknown
command terminated with exit code 126
  • 使用 kubectl debug 启动调试容器
root@k8s-master:~# kubectl debug -it ephemeral-demo --image=busybox --target=ephemeral-demo
Targeting container "ephemeral-demo". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
Defaulting debug container name to debugger-wj4h5.
If you don't see a command prompt, try pressing enter.
/ # whoami
/ #
  • 查看容器
$ kubectl describe pod ephemeral-demo
Ephemeral Containers:
    Container ID:   docker://331cfd3cec5d91f605cf8825751dca4cfefd4435d43d64cfe82b7db1ac450a2a
    Image:          busybox
    Image ID:       docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 27 Feb 2022 20:15:20 +0800
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:         <none>

Pod 副本调试

  • 模拟场景
kubectl run myapp --image=busybox --restart=Never -- sleep 1d
  • 通过运行以下命令,建立 myapp 的一个名为 myapp-debug 的副本, 新增了一个用于调试的 Ubuntu 容器
root@k8s-master:~# kubectl debug myapp -it --image=ubuntu --share-processes --copy-to=myadd-debug
Defaulting debug container name to debugger-jncbw.
If you don't see a command prompt, try pressing enter.
root@myadd-debug:/# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
65535          1       0  0 12:25 ?        00:00:00 /pause
root           7       0  0 12:25 ?        00:00:00 sleep 1d
root          14       0  0 12:26 pts/0    00:00:00 bash
root          23      14  0 12:26 pts/0    00:00:00 ps -ef
root@myadd-debug:/# cat /proc/7/cwd/
.dockerenv  dev/        home/       root/       tmp/        var/
bin/        etc/        proc/       sys/        usr/
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数