Skip to content

kubectl debug工具

通常情况下,业务容器所使用的镜像是非常精简的,而一旦业务容器出现问题,通过kubectl exec进入到容器时,我们会发现自己需要使用的工具都没有,也无法通过apt, apt-get, yum等包管理工具,下载需要的工具

1.使用

bash
$ kubectl debug --help
$ kubectl debug --help
[root@kube-master-01 ~]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS         AGE
busybox                                   1/1     Running   110 (46m ago)    32d
demoapp-7cc687d895-8xltm                  1/1     Running   4 (5h47m ago)    6d5h
[root@kube-master-01 ~]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS         AGE
busybox                                   1/1     Running   110 (46m ago)    32d
demoapp-7cc687d895-8xltm                  1/1     Running   4 (5h47m ago)    6d5h

主要是利用--target参数,这个参数主要用于指定debug Pod中的哪个容器;--image参数就是用于指定使用哪个镜像来debug,这个镜像包含我们需要使用的工具即可

bash
$ kubectl debug demoapp-7cc687d895-8xltm --image=ubuntu:20.04 -it --target=demoapp
Targeting container "demoapp". If you don't see processes from this container it may be because the container runtimedoesn't support this feature.
Defaulting debug container name to debugger-vrpd6.


If you don't see a command prompt, try pressing enter.

root@demoapp-7cc687d895-8xltm:/#
#此处多回车几次就可以进去

root@demoapp-7cc687d895-8xltm:/# ps uax|grep python
root           1  0.0  0.5  27288 23192 ?        Ss   02:47   0:13 python3 /app/demo.py
root@demoapp-7cc687d895-8xltm:/# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1115792   1664 1753772    0    0    36    48  477  920  1 10 89  1  0
$ kubectl debug demoapp-7cc687d895-8xltm --image=ubuntu:20.04 -it --target=demoapp
Targeting container "demoapp". If you don't see processes from this container it may be because the container runtimedoesn't support this feature.
Defaulting debug container name to debugger-vrpd6.


If you don't see a command prompt, try pressing enter.

root@demoapp-7cc687d895-8xltm:/#
#此处多回车几次就可以进去

root@demoapp-7cc687d895-8xltm:/# ps uax|grep python
root           1  0.0  0.5  27288 23192 ?        Ss   02:47   0:13 python3 /app/demo.py
root@demoapp-7cc687d895-8xltm:/# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1115792   1664 1753772    0    0    36    48  477  920  1 10 89  1  0