无法使用“kubectl debug”调试我的 Kubernetes pod

问题描述 投票:0回答:1

我有一个带有两个容器的 Kubernetes Pod,额外的容器遇到问题,导致崩溃循环退避。为了尝试调试,我使用 kubectl debug 命令。然而,我在识别和访问调试容器中特定的 sidecar 容器以进行调试方面面临着挑战。

这是我的 pod 清单的片段:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  #Main application container
  - name: nginx-container
    image: nginx:latest
    ports:
      - containerPort: 80
    volumeMounts:
      - name: logs
        mountPath: /var/log/nginx
  #This is sidecar container
  - name: sidecar-container
    image: busybox
    command: ["/bin/sh"]
    args: ["-c", "tail -f /var/log/access.log"]
    volumeMounts:
      - name: logs
        mountPath: /var/log/nginx
  volumes:
    - name: logs
      emptyDir: {}

我可以获得与上述问题相关的 sidecar-container 日志,但我想通过调试窗格对其进行调试。

kubectl logs nginx-pod  -c sidecar-container
tail: can't open '/var/log/access.log': No such file or directory
tail: no files

我故意在清单文件中为我的 sidecar 容器提供了错误的路径来模拟故障。这是为了使用 kubectl debug 进行调试。

这是我正在使用的 kubectl debug 命令,它将创建一个额外的 pod 来调试:

kubectl debug nginx-pod -it --image=busybox:1.28 --share-processes --copy-to=debug-pod

kubectl debug命令的输出:

kubectl debug nginx-pod -it --image=busybox:1.28 --share-processes --copy-to=debug-pod
Defaulting debug container name to debugger-qnnf7.
If you don't see a command prompt, try pressing enter.
/ #

Pod 状态:

k get po               
NAME        READY   STATUS             RESTARTS      AGE
debug-pod   2/3     CrashLoopBackOff   1 (12s ago)   15s
nginx-pod   1/2     CrashLoopBackOff   3 (27s ago)   80s

我能够在调试中找到主容器的位置,并能够访问与主应用程序容器相关的所有文件。

/ # 
/ # ps
PID   USER     TIME  COMMAND
    1 65535     0:00 /pause
    7 root      0:00 nginx: master process nginx -g daemon off;
   35 101       0:00 nginx: worker process
   36 101       0:00 nginx: worker process
   37 101       0:00 nginx: worker process
   38 101       0:00 nginx: worker process
   39 101       0:00 nginx: worker process
   40 101       0:00 nginx: worker process
   41 101       0:00 nginx: worker process
   42 101       0:00 nginx: worker process
   49 root      0:00 sh
   61 root      0:00 ps
/ # 
/ # cd proc/7/root/
(unreachable)/ # ls
bin                   docker-entrypoint.d   home                  mnt                   root                  srv                   usr
boot                  docker-entrypoint.sh  lib                   opt                   run                   sys                   var
dev                   etc                   media                 proc                  sbin                  tmp
(unreachable)/ # cat etc/hostname 
debug-pod
(unreachable)/ # cd var/log/nginx/
(unreachable)/var/log/nginx # ls
access.log  error.log
(unreachable)/var/log/nginx # 
(unreachable)/var/log/nginx #

尽管如此,我似乎无法在调试容器中找到 sidecar 容器。任何关于如何在这种情况下有效调试附加容器的见解或指导将不胜感激。 是否可以访问这里的sidecar-container文件进行调试?

kubernetes debugging containers devops kubectl
1个回答
0
投票

...我似乎无法在调试容器中找到 sidecar 容器。

你的命令是正确的。但请注意

sidecar-container
容器已按您的预期退出。要测试这一点,您可以切换到:
"-c", "sleep 1000"
,您将看到 sidecar 进程。

© www.soinside.com 2019 - 2024. All rights reserved.