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
相关推荐
lichenyang4534 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4534 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang4534 天前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
运维开发故事6 天前
基于 Arthas 的多集群在线诊断系统设计与实现
kubernetes
Patrick_Wilson8 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
探索云原生8 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
云恒要逆袭9 天前
运行你的第一个Docker容器
后端·docker·容器
Java之美10 天前
一次k8s升级引发的DevicePlugin注册失败
云原生·kubernetes
程序员老赵10 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程