確定veth pair在容器和宿主機的對應關係
阿新 • • 發佈:2021-07-08
一、veth pair的對應關係
1、建立網路名稱空間ns1
# ip netns add ns1 # ip netns list ns1
2、建立veth pair
# ip link add veth1 type veth peer name veth2 # ip a 21: veth2@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 0a:73:e3:2e:49:08 brd ff:ff:ff:ff:ff:ff 22: veth1@veth2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500qdisc noop state DOWN group default qlen 1000 link/ether f2:20:e4:20:f6:ca brd ff:ff:ff:ff:ff:ff
從網絡卡名稱veth2@veth1和veth1@veth2就可以看到他們的對應關係
3、將veth2放入ns1名稱空間# ip link set veth2 netns ns1 up # ip a 22: veth1@if21: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether f2:20:e4:20:f6:ca brd ff:ff:ff:ff:ff:ff link-netnsid 2 # ip netns list ns1 (id: 2) //ns1的Id為2
veth1@if21:對端在所在網路名稱空間的21號網絡卡
link-netnsid 2:對端在netnsid為2的網路名稱空間裡# ip netns exec ns1 ip a 21: veth2@if22: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group defaultqlen 1000 link/ether 0a:73:e3:2e:49:08 brd ff:ff:ff:ff:ff:ff link-netnsid 0
veth1@if22:對端在所在網路名稱空間的22號網絡卡
link-netnsid 0:對端在netnsid為0的網路名稱空間裡 二、通過容器查詢宿主機上的對應網絡卡# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deployment-6dd86d77d-njhfk 1/1 Running 0 55m 10.2.199.3 10.30.20.106 <none> <none> # kubectl exec nginx-deployment-6dd86d77d-njhfk -it -- /bin/bash root@nginx-deployment-6dd86d77d-njhfk:/# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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 3: eth0@if20: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 52:86:34:64:46:1c brd ff:ff:ff:ff:ff:ff inet 10.2.199.3/32 brd 10.2.199.3 scope global eth0 valid_lft forever preferred_lft forever
eth0@if20:說明對應的為宿主機10.30.20.106上的20編號的網絡卡
在宿主機10.30.20.106# ip a ...... 20: calicde22b5f907@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether ee:ee:ee:ee:ee:ee brd ff:ff:ff:ff:ff:ff link-netnsid 1三、在宿主機上查詢 1、查詢網絡卡對應端所在的網路名稱空間的id
# ip a ...... 20: calicde22b5f907@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether ee:ee:ee:ee:ee:ee brd ff:ff:ff:ff:ff:ff link-netnsid 1 ......
link-netnsid 1:所在網路名稱空間的id為1
2、檢視對應網路名稱空間# ln -s /var/run/docker/netns /var/run/netns # ip netns list 72c4d4eeedf0 (id: 1) 41bb71485c50 (id: 0) default # ip netns exec 72c4d4eeedf0 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 3: eth0@if20: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 52:86:34:64:46:1c brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.2.199.3/32 brd 10.2.199.3 scope global eth0 valid_lft forever preferred_lft forever
對應IP地址為10.2.199.3