ubuntu@work-1:~$ sudo crictl ps CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID POD 64aad8b603123 985a52bfdfe21 13 minutes ago Running simple-http-server 0 0c2daa092abd3 shs-5698fcd598-dpxlg
ubuntu@work-1:~$ sudo crictl exec -it 64aad8b603123 bash bash-5.1# ls -l total 6352 -rwxr-xr-x 1 root root 6503679 Oct 28 05:48 simple-http-server
使用 kubectl kubectl exec -it <pod-name> -- bash
1 2 3 4 5
# ubuntu @ master in ~ [23:16:17] $ kubectl exec -it shs-5698fcd598-dpxlg -- bash bash-5.1# ls -l total 6352 -rwxr-xr-x 1 root root 6503679 Oct 28 05:48 simple-http-server
$ kubectl -n kube-system get pod -owide | grep coredns coredns-84b58f6b4-qd4vp 1/1 Running 11 (57m ago) 30d 172.20.251.235 192.168.64.5 $ kubectl -n kube-system exec -it coredns-84b58f6b4-qd4vp -- /bin/zsh error: Internal error occurred: error executing command in container: failed to exec in container: failed to start exec "...": OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/zsh": stat /bin/zsh: no such file or directory: unknown $ ps -ef | grep -i coredns | grep -v grep root 1738 1618 0 22:45 ? 00:00:11 /coredns -conf /etc/coredns/Corefile $ sudo nsenter -t 1738 -n /bin/zsh work-2# ip address show dev eth0 3: eth0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 66:b9:35:7b:77:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.20.251.235/32 brd 172.20.251.235 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::64b9:35ff:fe7b:7702/64 scope link valid_lft forever preferred_lft forever work-2# tcpdump -i eth0 tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
另外 nsenter 还可以单独进入其他的 namespace,分别如下:
1 2 3 4 5 6 7 8 9 10 11
-a, --all enter all namespaces -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 -C, --cgroup[=<file>] enter cgroup namespace -U, --user[=<file>] enter user namespace -T, --time[=<file>] enter time namespace -S, --setuid <uid> set uid in entered namespace -G, --setgid <gid> set gid in entered namespace