[kubernetes]-k8s网络排查利器nsenter

导语:看到有用nsenter在docker运行时的情况下,排查网络问题的脚本。学习了一下顺便增加了一个containerd版本

containerd版本

#!/usr/bin/env bash

function e_net() {
  set -eu
  pod=$(kubectl get pod "${pod_name}" -n "${namespace}" -o jsonpath='{.status.containerStatuses[0].containerID}' | cut -d '/' -f 3)
  # 去掉pid1
  pid=$(crictl inspect "${pod}" | jq -r '.info.pid')
  #pid=$(crictl inspect "${pod}" | grep -oP '"pid": \K\d+' |head -n 1)
  echo -e "\033[32m Entering pod netns for ${namespace}/${pod_name} \033[0m\n"
  cmd="nsenter -n -t ${pid}"
  echo -e "\033[32m Execute the command: ${cmd} \033[0m"
  ${cmd}
}

# 运行函数
pod_name=$1
namespace=${2:-"default"}
e_net

执行脚本

image-20240705163435714

验证是否已经进入容器网络。在nsenter中telnet localhost 2181是可以的。但是有缺陷,无法用service名称通讯 。需要手动修改/etc/resolv.conf

cp  /etc/resolv.conf /tmp/
cat > /etc/resolv.conf <<EOF
search deepwise.svc.cluster.local svc.cluster.local cluster.local
nameserver 100.64.0.10
options ndots:5
EOF

image-20240705163539206

# 抓包命令
tcpdump -nnnvv -As 0 -i eth0 port 80 -w demo2.pcap

docker版本


#!/usr/bin/env bash

function e_net() {
  set -eu
  pod=`kubectl get pod ${pod_name} -n ${namespace} -o template --template='{{range .status.containerStatuses}}{{.containerID}}{{end}}' | sed 's/docker:\/\/\(.*\)$/\1/'`
  pid=`docker inspect -f {{.State.Pid}} $pod`
  echo -e "\033[32m Entering pod netns for ${namespace}/${pod_name} \033[0m\n"
  cmd="nsenter -n -t ${pid}"
  echo -e "\033[32m Execute the command: ${cmd} \033[0m"
  ${cmd}
}

# 运行函数
pod_name=$1
namespace=${2-"default"}
e_net

https://cloud.tencent.com/developer/article/1638871

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值