Docker容器纵横研究院微服务&容器专题社区计算机网络技术分享

Container Networking From Scratc

2019-06-29  本文已影响3人  Mr_Hospital

最近观看了《Container Networking From Scratch》这个视频,看完之后,需要记录一下。

Q: Kubernetes对于网络的要求?

A: kubernetes的官方描述:

Kubernetes imposes the following fundamental requirements on any networking implementation (barring any intentional network segmentation policies):

  • pods on a node can communicate with all pods on all nodes without NAT
  • agents on a node (e.g. system daemons, kubelet) can communicate with all pods on that node

Note: For those platforms that support Pods running in the host network (e.g. Linux):

  • pods in the host network of a node can communicate with all pods on all nodes without NAT

简单来说,k8s对于网络的要求是:

这里,我有两个疑问:

视频里面还有一条:

就是说一个容器自己的ip同其他容器看到的值是一样的。

视频里面从什么都没有到完成一个overlay网络,走了四部:

具体情况,下面记录。

基础知识

第一步,Single network namespace

先看图:


step1.png

解释一下:

该图表示了流量是如何从节点通过route流向veth1,再到容器中的。同时也表明了流量如何从容器,通过容器内的route,veth2流向节点的。

接下来看看代码:

#!/bin/bash -e

. env.sh

echo "Creating the namespace"
sudo ip netns add $CON

echo "Creating the veth pair"
sudo ip link add veth1 type veth peer name veth2

echo "Adding one end of the veth pair to the namespace"
sudo ip link set veth2 netns $CON

echo "Configuring the interface in the network namespace with an IP address"
sudo ip netns exec $CON ip addr add $IP dev veth2

echo "Enabling the interface inside the network namespace"
sudo ip netns exec $CON ip link set dev veth2 up

echo "Enabling the interface on the node"
sudo ip link set dev veth1 up

echo "Setting the loopback interface in the network namespace"
sudo ip netns exec $CON ip link set lo up

echo "Setting the routes on the node"
sudo ip route add $IP/32 dev veth1

echo "Setting the default route in the network namespaces"
sudo ip netns exec $CON ip route add default via $IP dev veth2                                                                                                              

大概流程为:

第二步,Single node, 2 network namespaces.

先看图:


step2.jpg

解释一下:

看看代码:

#!/bin/bash -e

. env.sh

echo "Creating the namespaces"
sudo ip netns add $CON1
sudo ip netns add $CON2

echo "Creating the veth pairs"
sudo ip link add veth10 type veth peer name veth11
sudo ip link add veth20 type veth peer name veth21

echo "Adding the veth pairs to the namespaces"
sudo ip link set veth11 netns $CON1
sudo ip link set veth21 netns $CON2

echo "Configuring the interfaces in the network namespaces with IP address"
sudo ip netns exec $CON1 ip addr add $IP1/24 dev veth11
sudo ip netns exec $CON2 ip addr add $IP2/24 dev veth21

echo "Enabling the interfaces inside the network namespaces"
sudo ip netns exec $CON1 ip link set dev veth11 up
sudo ip netns exec $CON2 ip link set dev veth21 up

echo "Creating the bridge"
sudo ip link add name br0 type bridge

echo "Adding the network namespaces interfaces to the bridge"
sudo ip link set dev veth10 master br0
sudo ip link set dev veth20 master br0

echo "Assigning the IP address to the bridge"
sudo ip addr add $BRIDGE_IP/24 dev br0

echo "Enabling the bridge"
sudo ip link set dev br0 up

echo "Enabling the interfaces connected to the bridge"
sudo ip link set dev veth10 up
sudo ip link set dev veth20 up

echo "Setting the loopback interfaces in the network namespaces"
sudo ip netns exec $CON1 ip link set lo up
sudo ip netns exec $CON2 ip link set lo up

echo "Setting the default route in the network namespaces"
sudo ip netns exec $CON1 ip route add default via $BRIDGE_IP dev veth11
sudo ip netns exec $CON2 ip route add default via $BRIDGE_IP dev veth21

相对上一步来说:

第三步,Multiple nodes, same L2 network.

先看图:


step3.jpg

解释一下:

看看代码:

#!/bin/bash -e

. env.sh

echo "Creating the namespaces"
sudo ip netns add $CON1
sudo ip netns add $CON2

echo "Creating the veth pairs"
sudo ip link add veth10 type veth peer name veth11
sudo ip link add veth20 type veth peer name veth21

echo "Adding the veth pairs to the namespaces"
sudo ip link set veth11 netns $CON1
sudo ip link set veth21 netns $CON2

echo "Configuring the interfaces in the network namespaces with IP address"
sudo ip netns exec $CON1 ip addr add $IP1/24 dev veth11
sudo ip netns exec $CON2 ip addr add $IP2/24 dev veth21

echo "Enabling the interfaces inside the network namespaces"
sudo ip netns exec $CON1 ip link set dev veth11 up
sudo ip netns exec $CON2 ip link set dev veth21 up

echo "Creating the bridge"
sudo ip link add name br0 type bridge

echo "Adding the network namespaces interfaces to the bridge"
sudo ip link set dev veth10 master br0
sudo ip link set dev veth20 master br0

echo "Assigning the IP address to the bridge"
sudo ip addr add $BRIDGE_IP/24 dev br0

echo "Enabling the bridge"
sudo ip link set dev br0 up

echo "Enabling the interfaces connected to the bridge"
sudo ip link set dev veth10 up
sudo ip link set dev veth20 up

echo "Setting the loopback interfaces in the network namespaces"
sudo ip netns exec $CON1 ip link set lo up
sudo ip netns exec $CON2 ip link set lo up

echo "Setting the default route in the network namespaces"
sudo ip netns exec $CON1 ip route add default via $BRIDGE_IP dev veth11
sudo ip netns exec $CON2 ip route add default via $BRIDGE_IP dev veth21

# ------------------- Step 3 Specific Setup --------------------- #

echo "Setting the route on the node to reach the network namespaces on the other node"
sudo ip route add $TO_BRIDGE_SUBNET via $TO_NODE_IP dev enp0s8

echo "Enables IP forwarding on the node"
sudo sysctl -w net.ipv4.ip_forward=1

同上一步不同的是:

第四步,Multiple nodes, overlay network.

先看图:


step4.jpg

解释一下:

看看代码:

#!/bin/bash -e

. env.sh

echo "Creating the namespaces"
sudo ip netns add $CON1
sudo ip netns add $CON2

echo "Creating the veth pairs"
sudo ip link add veth10 type veth peer name veth11
sudo ip link add veth20 type veth peer name veth21

echo "Adding the veth pairs to the namespaces"
sudo ip link set veth11 netns $CON1
sudo ip link set veth21 netns $CON2

echo "Configuring the interfaces in the network namespaces with IP address"
sudo ip netns exec $CON1 ip addr add $IP1/24 dev veth11
sudo ip netns exec $CON2 ip addr add $IP2/24 dev veth21

echo "Enabling the interfaces inside the network namespaces"
sudo ip netns exec $CON1 ip link set dev veth11 up
sudo ip netns exec $CON2 ip link set dev veth21 up

echo "Creating the bridge"
sudo ip link add name br0 type bridge

echo "Adding the network namespaces interfaces to the bridge"
sudo ip link set dev veth10 master br0
sudo ip link set dev veth20 master br0

echo "Assigning the IP address to the bridge"
sudo ip addr add $BRIDGE_IP/24 dev br0

echo "Enabling the bridge"
sudo ip link set dev br0 up

echo "Enabling the interfaces connected to the bridge"
sudo ip link set dev veth10 up
sudo ip link set dev veth20 up

echo "Setting the loopback interfaces in the network namespaces"
sudo ip netns exec $CON1 ip link set lo up
sudo ip netns exec $CON2 ip link set lo up

echo "Setting the default route in the network namespaces"
sudo ip netns exec $CON1 ip route add default via $BRIDGE_IP dev veth11
sudo ip netns exec $CON2 ip route add default via $BRIDGE_IP dev veth21

echo "Enables IP forwarding on the node"
sudo sysctl -w net.ipv4.ip_forward=1

# ------------------- Step 4 Specific Setup --------------------- #

echo "Starts the UDP tunnel in the background"
sudo socat TUN:$TUNNEL_IP/16,iff-up UDP:$TO_NODE_IP:9000,bind=$NODE_IP:9000 &

echo "Setting the MTU on the tun interface"
sudo ip link set dev tun0 mtu 1492

echo "Disables reverse path filtering"
sudo bash -c 'echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter'
sudo bash -c 'echo 0 > /proc/sys/net/ipv4/conf/enp0s8/rp_filter'
sudo bash -c 'echo 0 > /proc/sys/net/ipv4/conf/br0/rp_filter'
sudo bash -c 'echo 0 > /proc/sys/net/ipv4/conf/tun0/rp_filter'

同上一步不同的地方在于:

参考资料:
[1] https://www.youtube.com/watch?v=6v_BDHIgOY8
[2] https://github.com/kristenjacobs/container-networking
[3] https://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/

上一篇 下一篇

猜你喜欢

热点阅读