Nsenter使用記錄篇
介紹
nsenter是用來進(jìn)入容器內(nèi)部的一個(gè)命令拴疤,它的優(yōu)勢之處在于可以自己選擇加載容器的那些namespaces
使用
$ nsenter --help
用法:
nsenter [options] <program> [<argument>...]
Run a program with namespaces of other processes.
選項(xiàng):
-t, --target <pid> 要獲取名字空間的目標(biāo)進(jìn)程
-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
-S, --setuid <uid> set uid in entered namespace
-G, --setgid <gid> set gid in entered namespace
--preserve-credentials do not touch uids or gids
-r, --root[=<dir>] set the root directory
-w, --wd[=<dir>] set the working directory
-F, --no-fork 執(zhí)行 <程序> 前不 fork
-Z, --follow-context set SELinux context according to --target PID
-h, --help 顯示此幫助并退出
-V, --version 輸出版本信息并退出
可以看到選項(xiàng)很多闸与,但是大致上都是進(jìn)入某個(gè)namespace
--mount 參數(shù)是進(jìn)去到mount namespace中
--uts 參數(shù)是進(jìn)入到uts namespace中
--ipc 參數(shù)是進(jìn)入到System V IPC namaspace中
--net 參數(shù)是進(jìn)入到network namespace中
--pid 參數(shù)是進(jìn)入到pid namespace中
--user 參數(shù)是進(jìn)入到user namespace中
在使用nsenter命令之前需要獲取到docker容器的進(jìn)程了赌,然后再使用nsenter工具進(jìn)去到docker容器中,具體的使用方法如下:
$ docker inspect -f {{.State.Pid}} 容器名或者容器id #查詢?nèi)萜鞯腜ID
$ nsenter -t 容器PID -m -u -i -n -p #輸入該命令進(jìn)入容器
在排查coredns問題時(shí)用到了此命令.Pod bash環(huán)境并沒有curl命令,無法在容器內(nèi)部使用curl命令來判斷pod能否訪問Kubernetes API,具體使用如下
nsenter -n -t $PID curl -kg -6 $URI
如果RUNC為Containerd不為docker 則可以使用 ps -ef來獲取Pod PID
Example:
$ ps -ef | grep core
root 16557 16338 0 Sep12 ? 00:13:13 /coredns -conf /etc/coredns/Corefile
root 16706 16505 0 Sep12 ? 00:13:31 /coredns -conf /etc/coredns/Corefile
root 17347 17266 0 Sep12 ? 00:13:48 /coredns -conf /etc/coredns/Corefile
nsenter機(jī)制是使用哪個(gè)參數(shù),就進(jìn)入該進(jìn)程的哪個(gè)namespce埃唯,不使用則使用宿主機(jī)的namespace。而-m是進(jìn)入mount namespace的害捕,這個(gè)名稱空間是用來文件系統(tǒng)名稱空間绿淋,所以當(dāng)我們不加-m的時(shí)候,使用的是宿主機(jī)的文件系統(tǒng)尝盼,可以使用宿主機(jī)內(nèi)的命令對容器進(jìn)行問題的排查吞滞,當(dāng)然你也可以選擇使用宿主機(jī)的其他namespace來幫助你排查問題