k8s中找出pod在node节点上对应的网卡名称

创建pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 先通过yaml文件创建pod
root@k8s-master-51:~# vim nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: myserver
spec:
replicas: 2
selector:
#app: ng-deploy-80 #rc
matchLabels:
app: ng-deploy-80
template:
metadata:
labels:
app: ng-deploy-80
spec:
containers:
- name: ng-deploy-80
image: 172.20.17.24/base-image/nginx:1.27.4
ports:
- containerPort: 80

root@k8s-master-51:~# kubectl apply -f nginx.yaml

查看pod运行状态
root@k8s-master-51:~# kubectl get pod -o wide -n myserver
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-55fb9b765f-jhkv5 1/1 Running 0 5s 10.200.229.77 k8s-node-55 <none> <none>
nginx-deployment-55fb9b765f-lb7h6 1/1 Running 0 5s 10.200.22.143 k8s-node-54 <none> <none>

进入pod里

进到pod里安装好ethtool net-tools这两个工具后续用到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@k8s-master-51 # kubectl exec -it nginx-deployment-55fb9b765f-lb7h6 -n myserver -- /bin/bash 
# 修改apt源改成国内源
root@nginx-deployment-55fb9b765f-lb7h6:/# cd /etc/apt/sources.list.d
root@nginx-deployment-55fb9b765f-lb7h6:/etc/apt/sources.list.d# sed -i 's/deb.debian.org/mirrors.huaweicloud.com/g' /etc/apt/sources.list.d/debian.sources
root@nginx-deployment-55fb9b765f-lb7h6:/etc/apt/sources.list.d# apt update
root@nginx-deployment-55fb9b765f-lb7h6:~# apt -y install ethtool net-tools
root@nginx-deployment-55fb9b765f-lb7h6:~# ethtool -S eth0
NIC statistics:
# 看这块的peer编号,然后再在node机器上找到相对应的编号
peer_ifindex: 14
rx_queue_0_xdp_packets: 0
rx_queue_0_xdp_bytes: 0
rx_queue_0_drops: 0
rx_queue_0_xdp_redirect: 0
rx_queue_0_xdp_drops: 0
rx_queue_0_xdp_tx: 0
rx_queue_0_xdp_tx_errors: 0
tx_queue_0_xdp_xmit: 0
tx_queue_0_xdp_xmit_errors: 0

切换node节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 根据上面kubectl get pod -o wide -n myserver查找出nginx-deployment-55fb9b765f-lb7h6这个pod运行在k8s-node-54的节点上然后去k8s-node-54找网卡名称
# 在对应的node上使用ip link show
root@k8s-node-54:~# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:22:87:0e brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
3: kube-ipvs0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default
link/ether 46:36:1a:dd:5f:be brd ff:ff:ff:ff:ff:ff
4: cali073b411309a@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether ee:ee:ee:ee:ee:ee brd ff:ff:ff:ff:ff:ff link-netns cni-afaee581-c78f-3fe1-3c7e-5b6beeba720d
5: calie16db487b47@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether ee:ee:ee:ee:ee:ee brd ff:ff:ff:ff:ff:ff link-netns cni-8eab9086-4c7e-2f50-4592-3fcd86fad9b8
6: tunl0@NONE: <NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0

# 跟上面peer的编号一样也是14就是上面那个pod要找的网卡,这块calib485d2442ae就是上面那个pod在node节点上的对应网卡名称
14: calib485d2442ae@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether ee:ee:ee:ee:ee:ee brd ff:ff:ff:ff:ff:ff link-netns cni-fb06b0a4-6ae7-20e4-1435-c5c792e0dba3
# containerd通过sudo nsenter --net=/run/netns/xxxxx ip a 查看ip是否和上面一致
# docker通过sudo nsenter --net=/run/docker/netns/xxxxx ip a
root@k8s-node-54:~# sudo nsenter --net=/run/netns/cni-fb06b0a4-6ae7-20e4-1435-c5c792e0dba3 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
inet6 ::1/128 scope host
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
3: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether ce:f8:d8:a9:b4:e5 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.200.22.143/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ccf8:d8ff:fea9:b4e5/64 scope link
valid_lft forever preferred_lft forever

找到之后后续有什么问题就可以对此pod进行网络抓包进行分析对应的问题故障!!!


k8s中找出pod在node节点上对应的网卡名称
https://www.dklwj.com/2025/02/k8s-find-pod-in-node-network-card-name.html
作者
阿伟
发布于
2025年2月6日
许可协议