1、Linux网络虚拟化==ipvlan、macvlan

 

k8s网络实现不是集群内部自己实现,而是依赖于第三方网络插件---(CNI:Container Network Interface)。

flannel、calico、canel等是目前比较流行的第三方网络插件。

这三种的网络插件需要实现Pod网络方案的方式通常有以下几种:虚拟网桥、多路复用(MacVLAN)、硬件交换(SR-IOV) 

Pod网络的实现方式

每个Pod对象内的基础架构容器均使用一个独立的网络名称空间,并共享给同一Pod内的其他容器使用。每个名称空间均有其专用的独立网络协议栈及其相关的网络接口设备。一个网络接口仅能属于一个网络名称空间,于是,运行多个Pod必然要求使用多个网络名称空间,也就需要用到多个网络接口设备。不过,一个易于实现的方案是使用软件实现的伪网络接口及模拟线缆将其连接至物理接口。伪网络接口的实现方案常见的有虚拟网桥、多路复用及硬件交换三种。

1、虚拟网桥:创建一对虚拟以太网接口(veth),一个接入容器内部,另一个留置于根名称空间内并借助于Linux内核桥接功能OpenVSwitch(OVS)关联至真实的物理接口。

2、多路复用:多路复用可以由一个中间网络设备组成,它暴露了多个虚拟接口,可使用数据包转发规则来控制每个数据包转到的目标接口。MAC VLAN为每个虚拟接口配置一个MAC地址并基于此地址完成二层报文收发,而IP VLAN是基于IP地址的并使用单个MAC,从而使其更适合VM。

3、硬件交换:现今市面上的大多数NIC都支持单根I/O虚拟化(SR-IOV),它是创建虚拟设备的一种实现方式。每个虚拟设备自身均表现为一个独立的PCI设备,并有着自己的VLAN及与硬件强制关联的QoS。SR-IOV提供了接近硬件级别的性能,但在公共云中通常是不可用的。

SR-IOV 技术是一种基于硬件的虚拟化解决方案,可提高性能和可伸缩性。SR-IOV 标准允许在虚拟机之间高效共享 PCIe(Peripheral Component Interconnect Express,快速外设组件互连)设备,并且它是在硬件中实现的,可以获得能够与本机性能媲美的 I/O 性能。SR-IOV 规范定义了新的标准,根据该标准,创建的新设备可允许将虚拟机直接连接到 I/O 设备。

 

 参考:从 Bridge 到 OVS,探索虚拟交换机

无论是上面的哪种方式在容器当中实现,都需要大量的操作步骤,而K8S支持CNI插件进行编排网络,以实现Pod和集群网络管理功能的自动化。

每次Pod被初始化或删除,kubelet都会调用默认的CNI插件去创建一个虚拟设备接口附加到相关的底层网络,为Pod去配置IP地址、路由信息并映射到Pod对象的网络名称空间。

CNI的主要核心是:在创建容器时,先创建好网络名称空间(netns),然后调用CNI插件为这个netns配置网络,最后在启动容器内的进程

Flannel是一种基于overlay网络的跨主机容器网络解决方案,也就是将TCP数据包封装在另一种网络包里面进行路由转发和通信。

Flannel数据包在主机间转发是由backend实现的,目前已经支持UDP、VxLAN、host-gw等多种backend。

VxLAN:使用内核中的VxLAN模块进行封装报文。也是flannel推荐的方式

host-gw:即Host GateWay,通过在节点上创建目标容器地址的路由直接完成报文转发,要求各节点必须在同一个2层网络,对报文转发性能要求较高的场景使用

UDP:使用普通的UDP报文封装完成隧道转发。

工作原理

Flannel会为k8s集群中每个node端的host分配一个网段,Pod从这个网段中分配IP,这些IP可以在host间路由,Pod间无需使用nat和端口映射即可实现跨主机通信。如host1被分配的网段为172.17.1.0/16,host2被分配的网段为172.17.2.0/16。已分配 的subnet、host的IP等网络配置会被存放在ETCD中。

[root@VM-0-11-centos ~]# docker network create -d
bridge   ipvlan   macvlan  overlay

# Create the IPvlan L3 network
docker network create -d ipvlan \
--subnet=192.168.214.0/24 \
--subnet=10.1.214.0/24 \
-o ipvlan_mode=l3 ipnet210


# Test 192.168.214.0/24 connectivity
docker run --net=ipnet210 --ip=192.168.214.10 -itd alpine /bin/sh
docker run --net=ipnet210 --ip=10.1.214.10 -itd alpine /bin/sh


# Test L3 connectivity from 10.1.214.0/24 to 192.168.212.0/24
docker run --net=ipnet210 --ip=192.168.214.9 -it --rm alpine ping -c 2 10.1.214.10


# Test L3 connectivity from 192.168.212.0/24 to 10.1.214.0/24
docker run --net=ipnet210 --ip=10.1.214.9 -it --rm alpine ping -c 2 192.168.214.10

 

Network drivers

Docker’s networking subsystem is pluggable, using drivers. Several drivers exist by default, and provide core networking functionality:

bridge: The default network driver. If you don’t specify a driver, this is the type of network you are creating. Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks.默认网络驱动程序。如果您未指定驱动程序,则这是您正在创建的网络类型。当您的应用程序在需要通信的独立容器中运行时,通常会使用桥接网络。请参阅 桥接网络。

host: For standalone containers, remove network isolation between the container and the Docker host, and use the host’s networking directly. See use the host network.对于独立容器,去掉容器与 Docker 主机之间的网络隔离,直接使用主机的网络。请参阅 使用主机网络。

overlay: Overlay networks connect multiple Docker daemons together and enable swarm services to communicate with each other. You can also use overlay networks to facilitate communication between a swarm service and a standalone container, or between two standalone containers on different Docker daemons. This strategy removes the need to do OS-level routing between these containers. See overlay networks.Overlay网络将多个 Docker 守护进程连接在一起,使 swarm 服务能够相互通信。您还可以使用覆盖网络来促进 swarm 服务和独立容器之间的通信,或者不同 Docker 守护程序上的两个独立容器之间的通信。这种策略消除了在这些容器之间进行操作系统级路由的需要。请参阅覆盖网络。

ipvlan: IPvlan networks give users total control over both IPv4 and IPv6 addressing. The VLAN driver builds on top of that in giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration. See IPvlan networks.IPvlan网络使用户可以完全控制 IPv4 和 IPv6 寻址。VLAN 驱动程序建立在此之上,为对底层网络集成感兴趣的用户提供了对第 2 层 VLAN 标记甚至 IPvlan L3 路由的完全控制。请参阅IPvlan 网络。

macvlan: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Using the macvlan driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host’s network stack. See Macvlan networks.Macvlan网络允许您将 MAC 地址分配给容器,使其在您的网络上显示为物理设备。Docker 守护进程通过它们的 MAC 地址将流量路由到容器。macvlan 在处理期望直接连接到物理网络而不是通过 Docker 主机的网络堆栈路由的遗留应用程序时,使用驱动程序有时是最佳选择。请参阅 Macvlan 网络。

none: For this container, disable all networking. Usually used in conjunction with a custom network driver. none is not available for swarm services. See disable container networking.对于这个容器,禁用所有网络。通常与自定义网络驱动程序一起使用。none不适用于 swarm 服务。请参阅 禁用容器网络。

Network plugins: You can install and use third-party network plugins with Docker. These plugins are available from Docker Hub or from third-party vendors. See the vendor’s documentation for installing and using a given network plugin.您可以通过 Docker 安装和使用第三方网络插件。这些插件可从 Docker Hub 或第三方供应商处获得。请参阅供应商的文档以安装和使用给定的网络插件。

 

 

 

 

 

 

 

 

一、IPVLAN L2

 

 

 

 

 

二、IPVLAN L3

 

 

 

 1 # 创建网络空间
 2 ip netns add net1
 3 ip netns add net2
 4 # 创建子接口
 5 ip link add ipvlan1 link ens32 type ipvlan mode l3
 6 ip link add ipvlan2 link ens32 type ipvlan mode l3
 7 # 关联ipvlan l3与vm上的网络空间
 8 ip link set ipvlan1 netns net1
 9 ip link set ipvlan2 netns net2
10 # 设置不同网段
11 ip netns exec net1 ifconfig ipvlan1 192.168.10.135/24 up
12 ip netns exec net2 ifconfig ipvlan2 192.168.20.136/24 up
13 # 测试一下二则是否ping得通
14 ip netns exec net1 ping 192.168.20.136

 

三、MACVLAN

 

 

 

 1 ifconfig ens32 promisc
 2 ip netns list
 3 ip netns add net1
 4 ip netns add net2
 5 ip netns list
 6 ip link add link ens32 name macv1 type macvlan mode bridge
 7 ip link add link ens32 name macv2 type macvlan mode bridge
 8 ip link set macv1 netns net1
 9 ip link set macv2 netns net2
10 ip netns exec net1 ifconfig macv1 192.168.163.135/24 up
11 ip netns exec net2 ifconfig macv2 192.168.163.136/24 up

 

转载:Kubernetes网络(IPVLAN与MACVLAN)

posted @ 2022-10-26 17:31  重构者  阅读(1304)  评论(0编辑  收藏  举报