对容器进行抓包
业务镜像往往是非常精简、基础(处于安全考虑也不应该安装非业务内容)。不含常见的curl、ping、tcping、tcpdump等工具,甚至没有sh,kubectl-debug太重,需要在node安装agent,有更简单的方法:nsenter,是大多数系统自带的用于进入指定命令空间的工具。
安装nsenter
# centos $ yum install util-linux # ubuntu $ apt install util-linux
用法
$ nsenter [options] [program [arguments]] options: -a, --all enter all namespaces of the target process by the default /proc/[pid]/ns/* namespace paths. -m, --mount[=<file>]:进入 mount 命令空间。如果指定了 file,则进入 file 的命名空间 -u, --uts[=<file>]:进入 UTS 命名空间。如果指定了 file,则进入 file 的命名空间 -i, --ipc[=<file>]:进入 System V IPC 命名空间。如果指定了 file,则进入 file 的命名空间 -n, --net[=<file>]:进入 net 命名空间。如果指定了 file,则进入 file 的命名空间 -p, --pid[=<file>:进入 pid 命名空间。如果指定了 file,则进入 file 的命名空间 -U, --user[=<file>:进入 user 命名空间。如果指定了 file,则进入 file 的命名空间 -t, --target <pid> # 指定被进入命名空间的目标进程的 pid -G, --setgid gid:设置运行程序的 GID -S, --setuid uid:设置运行程序的 UID -r, --root[=directory]:设置根目录 -w, --wd[=directory]:设置工作目录
案例:
查看容器位置
查看容器id
[root@k8s-master01 ~]# kubectl -n nfs-provisioner describe pod nfs-client-provisioner-864bcc4c95-4s6cf |grep Container Containers: Container ID: containerd://3fb3c2a18159d308ea8a4d77204509cd7ad86274c80e3c84e4d20c43b7730055 ContainersReady True
在容器所在物理机上查看容器pid
[root@k8s-worker02 ~]# crictl inspect 3fb3c2a1 |grep -2 network }, { "type": "network", "path": "/proc/2830/ns/net" }
使用相应参数进入程序所在的network命名空间,可以使用宿主机的所有命令
[root@k8s-worker02 ~]# nsenter -n -t 2830 [root@k8s-worker02 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 4: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1480 qdisc noqueue state UP group default link/ether 3a:f5:49:2b:68:70 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.244.69.196/32 scope global eth0 valid_lft forever preferred_lft forever ## Ctrl+D 退出 [root@k8s-worker02 ~]# 登出