我们在诸多线上的环境中Pod都是推荐使用non-privileged mode运行的.这样对于在转型的公司来说:通常还是比较需要使用ip route arp等信息的处理来测试某些场景。但是在非特权模式下:################[root@k8s-1 ~]# cat cni_demo_template.yamlapiVersion: apps/v1kind: DaemonSetmetadata:labels:app: cniname: cnispec:selector:matchLabels:app: cnitemplate:metadata:labels:app: cnispec:containers:- image: burlyluo/nettoolboxname: nettoolbox[root@k8s-1 ~]#################bash-5.1# arp -nAddress HWtype HWaddress Flags Mask Iface172.12.1.11 ether ee:ee:ee:ee:ee:ee C eth0169.254.1.1 ether ee:ee:ee:ee:ee:ee C eth0bash-5.1# arp -d 169.254.1.1SIOCDARP(dontpub): Operation not permitted 我们这里可以发现,它此时是无法进行操作的。那该怎么办呢?bash-5.1#
https://dev.to/stefanjacobs/nsenter-entering-a-running-process-or-container-46a3
我们知道Pod的原理和Container的原理以后:我们可以尝试如果找到这个container所在的网络对应的ns,是不是就可以来操作了呢?我们来一起看看:
[root@k8s-1 ~]# kubectl get pods -owide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cni-7j2gh 1/1 Running 0 2d23h 10.244.200.196 k8s-2 <none> <none>
cni-kmslk 1/1 Running 1 2d23h 10.244.231.206 k8s-1 <none> <none> #我们以这个Pod作为demo:其中地址为:10.244.231.206
^C[root@k8s-1 ~]# docker ps | grep cni
6f565a8a8286 burlyluo/nettoolbox "/sbin/tini -g -- /e…" 45 hours ago Up 45 hours k8s_nettoolbox_cni-kmslk_default_1cff0682-ad96-4139-b733-1721ba7066c1_1
fd9d1e1cbea5 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 45 hours ago Up 45 hours k8s_POD_cni-kmslk_default_1cff0682-ad96-4139-b733-1721ba7066c1_1
[root@k8s-1 ~]# docker inspect 6f565a8a8286 | grep Pid
"Pid": 6373,
"PidMode": "",
"PidsLimit": null,
[root@k8s-1 ~]# nsenter -n -t 6373
[root@k8s-1 ~]# 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@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1480 qdisc noqueue state UP group default
link/ether fa:93:cf:ce:99:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.244.231.206/32 brd 10.244.231.206 scope global eth0 # 这里我们通过ip a 看到的地址:10.244.231.206和我们pod中看到是一致的,所以,这种方式是可以的:
valid_lft forever preferred_lft forever
[root@k8s-1 ~]#
我们再来看看ip route相关的:
[root@k8s-1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 169.254.1.1 0.0.0.0 UG 0 0 0 eth0
169.254.1.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
[root@k8s-1 ~]# arp -n
Address HWtype HWaddress Flags Mask Iface
172.12.1.11 ether ee:ee:ee:ee:ee:ee C eth0
169.254.1.1 ether ee:ee:ee:ee:ee:ee C eth0
[root@k8s-1 ~]# arp -d 169.254.1.1 # 此时这里我们发现:是可以来清理掉对应的arp table的。
[root@k8s-1 ~]# arp -n
Address HWtype HWaddress Flags Mask Iface
172.12.1.11 ether ee:ee:ee:ee:ee:ee C eth0
[root@k8s-1 ~]#
[root@k8s-1 ~]# exit
logout
[root@k8s-1 ~]#
[root@k8s-1 ~]# nsenter -h
Usage:
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
-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 do not fork before exec'ing <program>
-Z, --follow-context set SELinux context according to --target PID
-h, --help display this help and exit
-V, --version output version information and exit
For more details see nsenter(1).
[root@k8s-1 ~]#
