Shell: 基于 nsenter 批量配置 Pod 容器的 iptables 规则

注意 nsenter 需要配合 EOF 完成标准输入,范式如下:

nsenter -t $pid -n bash <<EOF
something run in container namespace
exit
EOF

例如,给容器批量设置 iptables 规则的写法,如下:

for id in $(docker ps | grep k8s_POD | awk '{print $1}')
do
  pid=$(docker inspect $id -f '{
   
   {.State.Pid}}')
  nsenter -t $pid -n bash <<EOF
if ! iptables -t nat -S | grep OUTPUT | grep 192.168.0.1; then
    iptables -t nat -I OUTPUT -p all -d 1.2.3.4 -j DNAT --to-destination 192.168.0.1
    iptables -t nat -I OUTPUT -d 10.96.0.1 -p tcp --dport 443 -j DNAT --to-destination 192.168.0.1:6443
fi
exit
EOF
done

猜你喜欢

转载自blog.csdn.net/shida_csdn/article/details/119011307