Kubernetes中網絡相關知識

流量轉發和橋接

  • Kubernetes的核心是依靠Netfilter內核模塊來設置低級別的集羣IP負載均衡。須要兩個關鍵的模塊:IP轉發和橋接

IP轉發(IP Forward)

  • IP forward 是一種內核態設置,容許將一個接口的流量轉發到另一個接口,該配置是 Linux 內核將流量從容器路由到外部所必須的.
查看
sysctl net.ipv4.ip_forward
# 0意味着未開啓
設置
sysctl -w net.ipv4.ip_forward=1
echo net.ipv4.ip_forward=1 >> /usr/lib/sysctl.d/00-system.conf
sysctl -p

橋接

  • bridge-netfilter 設置能夠使 iptables 規則能夠在 Linux Bridges 上面工做,就像 Docker 和 Kubernetes 設置的那樣。網絡

  • 此設置對於 Linux 內核進行宿主機和容器之間進行數據包的地址轉換是必須的。負載均衡

查看
[root@t91 home]# sysctl net.bridge.bridge-nf-call-iptables
net.bridge.bridge-nf-call-iptables = 1
# 若是是0,則表示未開啓
設置
modprobe br_netfilter
sysctl -w net.bridge.bridge-nf-call-iptables=1
echo net.bridge.bridge-nf-call-iptables=1 >> /etc/sysconf.d/10-bridge-nf-call-iptables.conf
sysctl -p

防火牆規則

  • Kubernetes 提供了各類網絡插件來支持其集羣功能,同時也對傳統的基於 IP 和端口的應用程序提供了向後兼容的支持。工具

  • 最多見的 一種 Kubernetes 網絡方案就是利用 VxLan Overlay 網絡,其中的 IP 數據包被封裝在 UDP 中經過8472端口進行數據傳輸。插件

  • 這種狀況下會出現100%數據包丟失
$ ping 10.244.1.4 
PING 10.244.1.4 (10.244.1.4): 56 data bytes
^C--- 10.244.1.4 ping statistics ---
5 packets transmitted, 0 packets received, 100% packet loss
  • 最好的方式是使用相同的協議來傳輸數據,由於防火牆規則可能配置了特意的協議,好比可能會阻止 UDP 流量。
  • iperf 是一個很好的驗證工具
#  在服務端執行
iperf -s -p 8472 -u
# 在客戶端執行
iperf -c 172.28.128.103 -u -p 8472 -b 1K
相關文章
相關標籤/搜索