K8S调试工具之--nsenter
生活随笔
收集整理的這篇文章主要介紹了
K8S调试工具之--nsenter
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
注:本文基于K8S v1.21.2版本編寫
1、關(guān)于nsenter
nsenter就是namespace enter的意思,它可以進(jìn)入到目標(biāo)程序所在的namespace中,因此可以用來調(diào)試容器程序。我們都知道目前存在的幾個namespace,比如網(wǎng)絡(luò),用戶,pid等,nsenter都有對應(yīng)的參數(shù)可以指定,從而進(jìn)入該namespace,
[root@master ~]# nsenter -hUsage:nsenter [options] <program> [<argument>...]Run a program with namespaces of other processes.Options:-t, --target <pid> target process to get namespaces from-m, --mount[=<file>] enter mount namespace-u, --uts[=<file>] enter UTS namespace (hostname etc)-i, --ipc[=<file>] enter System V IPC namespace-n, --net[=<file>] enter network namespace-p, --pid[=<file>] enter pid namespace-U, --user[=<file>] enter user namespace ...2 常用場景
對于容器中有bash程序的場景,我們通過exec的方式就能進(jìn)入容器內(nèi)部,此時并不需要nsenter,但是對于那些沒有bash程序的,就沒法通過exec進(jìn)入容器了,比如我們嘗試進(jìn)入coredns容器,
[root@master ~]# kubectl exec -it -n kube-system coredns-74747c454d-w57p8 -- /bin/bash OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown command terminated with exit code 126這個時候nsenter就派上用場了,因為bash不存在,也就是文件系統(tǒng)層面的隔離導(dǎo)致的,我們可以不進(jìn)入這個namespace,
[root@master ~]# docker ps | grep coredns |grep -v pause 87c9da4bff1d 296a6d5035e2 "/coredns -conf /etc…" 3 days ago Up 3 days k8s_coredns_coredns-74747c454d-sshdk_kube-system_19246bc7-5c13-40b4-afbc-72a7b2fc687a_8 3dc5c132293d 296a6d5035e2 "/coredns -conf /etc…" 3 days ago Up 3 days k8s_coredns_coredns-74747c454d-8dttx_kube-system_de4a127f-811e-48d2-a22e-5de060a88c7c_8 b2aa0928b3d9 296a6d5035e2 "/coredns -conf /etc…" 3 days ago Up 3 days k8s_coredns_coredns-74747c454d-w57p8_kube-system_17ce6713-101a-4df2-a520-5c52563c5d0f_8 [root@master ~]# docker inspect --format "{{.State.Pid}}" 87c9da4bff1d 5157 [root@master ~]# nsenter -u -n -p -t 5157 [root@coredns-74747c454d-sshdk ~]# hostname coredns-74747c454d-sshdk [root@coredns-74747c454d-sshdk ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1480inet 10.244.219.73 netmask 255.255.255.255 broadcast 10.244.219.73ether 02:5e:0f:ac:5b:94 txqueuelen 0 (Ethernet)RX packets 78006 bytes 7194976 (6.8 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 78337 bytes 7492525 (7.1 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0我們首先通過docker inspect獲得對應(yīng)容器的pid,然后通過nsenter進(jìn)入utc、net、和pid namespace,這樣我們就能使用宿主機(jī)的調(diào)試工具,比如tcpdump,ip等命令。
總結(jié)
以上是生活随笔為你收集整理的K8S调试工具之--nsenter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv 使用 GPU 加速
- 下一篇: java短信接口_java调用短信接口代