> 网络镜像可以用这个封好加速的 ``` crpi-h1btrnvpx4qygkbr.cn-hangzhou.personal.cr.aliyuncs.com/ifbiu/nettoolbox:latest ``` - flannel hairpinMode 发卡模式(重要) - veth pair veth pair(虚拟以太网对) 是 Linux 内核提供的一种虚拟网络设备,由一对网卡组成,像一根网线的两头。 ```bash ┌─────────────────────────────────────────┐ │ 宿主机 │ │ ┌─────────┐ ┌─────────────────┐ │ │ │ 容器A │ │ 网桥 br0 │ │ │ │ eth0 │◄────►│ br-veth0 eth0 │ │ │ │(veth0) │veth │ │ │ │ │ │ └─────────┘pair └────┼───────┼─────┘ │ │ ↑ │ │ │ │ ns1 命名空间 veth 物理网卡 │ │ pair (连外部) │ │ ┌─────────┐ │ │ │ │ 容器B │◄────────┘ │ │ │ eth0 │ │ │ │(veth1) │ │ │ └─────────┘ │ │ ↑ │ │ ns2 命名空间 │ └─────────────────────────────────────────┘ ``` - 常用命令 ```bash # 查看系统的 IPv4 路由表 route -n # 查看系统上所有的 Linux 网桥及其端口成员 brctl show # Linux 网桥 cni0 上的 MAC 地址表 brctl showmacs cni0 # eth0 网卡的详细统计信息和驱动状态(peer_ifindex对应宿主机ip a的索引号) ethtool -S eth0 # 创建一个名为 br0 的 Linux 网桥设备 # 等价于 ip link add br0 type bridge ip l a br0 type bridge # 启用网桥 # 等价于 ip link set br0 up ip l s br0 up # 创建一个名为 ns1 的网络命名空间 ip netns a ns1 # 创建一对 veth(虚拟以太网)接口 # 效果如下 #┌─────────┐ ┌─────────────┐ #│ veth0 │◄───────►│ br-veth0 │ #│(ifindex)│ veth pair│(peer_ifindex)│ #└─────────┘ └─────────────┘ ip l a veth0 type veth peer br-veth0 # 将接口 veth0 移动到网络命名空间 ns1 中 ip l s veth0 netns ns1 # 在网络命名空间 ns1 中执行 ip a ip netns exec ns1 ip a # 将接口 br-veth0 挂载到网桥 br0 上,使其成为网桥的一个端口 #挂载前: #┌─────────────────┐ #│ br0 │ ┌─────────────┐ #│ (空网桥,无端口) │ │ br-veth0 │ #│ │ │ (悬空未连接) │ #└─────────────────┘ └─────────────┘ # #挂载后: #┌─────────────────┐ #│ br0 │ #│ ┌───────────┐ │ #│ │ br-veth0 │ │◄──── 成为网桥端口 #│ │ (端口) │ │ #│ └───────────┘ │ #└─────────────────┘ # │ # 数据包从 br-veth0 进入,由 br0 按 MAC 地址转发到其他端口 ip l s br-veth0 master br0 # 在网络命名空间 ns1 中启用接口 veth0 ip netns exec ns1 ip l s veth0 up # 在容器/命名空间 ns1 内配置网卡 IP 并查看网络状态 ip netns exec ns1 bash ifconfig veth0 192.168.4.41/24 ifconfig # 进入容器 ns2,给它的网卡 veth1 配置 IP,然后查看所有网卡状态 ip netns exec ns2 bash ip a a 192.168.4.42/24 dev veth1 ifconfig # 启用回环接口 lo ifconfig lo up # 在 br-veth0 接口上抓包 tcpdump -pne -i br-veth0 # 启用接口 br-beth0 ip l s br-beth0 up # 保存当前 iptables 防火墙规则 iptables-save # 以详细(detailed)格式显示所有网络接口的信息 ip -d link show ```