k8s pod网络故障注入,命令行实现

工具介绍

  • crictl工具:用于查询k8s pod和容器信息
  • nsenter工具:用于进入容器的网络命名空间
  • tc工具:注入网络故障

实现k8s pod级别网络故障

  1. 首先查找故障注入的pod的宿主机,并ssh登陆进去
bash 复制代码
kubectl get pod -n <namespace> -owide
  1. 使用 kubectl 获取 Pod 的容器 ID,例如第1个
bash 复制代码
kubectl get pod <pod-name> -o jsonpath='{.status.containerStatuses[0].containerID}' | cut -d'/' -f3
  1. 使用crictl获取容器的 PID
bash 复制代码
crictl inspect --output=go-template --template="{{.info.pid}}" <pod_container_id>
  1. 使用nsenter进入网络命名空间
bash 复制代码
nsenter -n -t $pod_pid

进入网络空间前后对比,进入网络空间后,eth0的ip应为pod ip

bash 复制代码
(tob_env) root@n73-001-014:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.72.3.100  netmask 255.255.255.0  broadcast 10.72.3.250
        inet6 fe50::216:3eff:fe50:fe50  prefixlen 64  scopeid 0x20<link>
        ether 00:36:3e:36:5b:36  txqueuelen 1000  (Ethernet)
        RX packets 38875633274  bytes 15496787166310 (14.0 TiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31492296646  bytes 17143859349471 (15.5 TiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(tob_env) root@n73-001-014:~# nsenter -n -t 501532

(tob_env) root@n73-001-014:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 172.26.25.6  netmask 255.255.255.255  broadcast 172.26.25.6
        inet6 fe80::b322:e9ff:fe7e:b322  prefixlen 64  scopeid 0x20<link>
        ether ba:22:e9:22:b3:22  txqueuelen 0  (Ethernet)
        RX packets 8077026811  bytes 1361131463473 (1.2 TiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6472821861  bytes 1134318477480 (1.0 TiB)
        TX errors 0  dropped 1 overruns 0  carrier 0  collisions 0
  1. 进入网络命名空间后,你就可以在该命名空间中执行各种网络相关的命令了

    • 通过tc工具注入网络故障
bash 复制代码
tc qdisc add dev eth0 root netem loss 100%
sleep 300
tc qdisc del dev eth0 root
相关推荐
Jewel Q9 分钟前
VRRP技术
网络·智能路由器
van叶~21 分钟前
Linux网络-------1.socket编程基础---(TCP-socket)
linux·网络·tcp/ip
两圆相切1 小时前
ICMPv4报文类型详解表
网络·智能路由器
zhezhi5011 小时前
OSPF多区域介绍
网络·ospf多区域
Z7676_2 小时前
OSPF开放式最短路径优先
运维·网络
两圆相切2 小时前
ICMPv6报文类型详解表
网络
极地星光2 小时前
TCP/IP 网络编程面试题及解答
网络·网络协议·tcp/ip
誰能久伴不乏3 小时前
Linux 系统调用详解:操作文件的常用系统调用
服务器·网络·servlet
呉師傅5 小时前
佳能iR-ADV C5560复印机如何扫描文件到电脑
运维·网络·windows·计算机外设·电脑
半梦半醒*5 小时前
Linux网络管理
linux·运维·网络·centos·运维开发