参考资料:
https://cizixs.com/2017/05/23/container-network-cni/
https://thenewstack.io/container-networking-landscape-cni-coreos-cnm-docker/
https://murat1985.github.io/kubernetes/cni/2016/05/14/netns-and-cni.html
1.什么是CNI
CNI是Container Network Interface的简称
容器网络接口(CNI)是coreos提出的一种容器网络规范,并被ApacheMeos、CloudFoundry、Kubernetes、Kurma和Rkt等项目所采用。还有由Contiv Networking、Project Calico和Weave等项目创建的插件。
2.CNI模型
3.CNI干的工作
CNI连接了两个组件:容器管理系统和网络插件。它们之间通过 JSON 格式的文件进行通信,实现容器的网络功能。具体的事情都是插件来实现的,包括:创建容器网络空间(network namespace)、把网络接口(interface)放到对应的网络空间、给网络接口分配 IP 等等。
4.CNI设计文档
https://github.com/containernetworking/cni/blob/master/SPEC.md
5.CNI与CNM的关系
docker 也提出了 CNM 标准,它要解决的问题和 CNI 是重合的,也就是说目前两者是竞争关系。目前 CNM 只能使用在 docker 中,而 CNI 可以使用在任何容器运行时。CNM 主要用来实现 docker 自身的网络问题,也就是 docker network 子命令提供的功能
CNM模型图
6.CNI的实现
具体参考: https://github.com/containernetworking/cni
容器运行时:
- rkt - container engine
- Kubernetes - a system to simplify container operations
- OpenShift - Kubernetes with additional enterprise features
- Cloud Foundry - a platform for cloud applications
- Apache Mesos - a distributed systems kernel
- Amazon ECS - a highly scalable, high performance container management service
- Singularity - container platform optimized for HPC, EPC, and AI
- OpenSVC - orchestrator for legacy and containerized application stacks
三方插件:
- Project Calico - a layer 3 virtual network
- Weave - a multi-host Docker network
- Contiv Networking - policy networking for various use cases
- SR-IOV
- Cilium - BPF & XDP for containers
- Infoblox - enterprise IP address management for containers
- Multus - a Multi plugin
- Romana - Layer 3 CNI plugin supporting network policy for Kubernetes
- CNI-Genie - generic CNI network plugin
- Nuage CNI - Nuage Networks SDN plugin for network policy kubernetes support
- Silk - a CNI plugin designed for Cloud Foundry
- Linen - a CNI plugin designed for overlay networks with Open vSwitch and fit in —SDN/OpenFlow network environment
- Vhostuser - a Dataplane network plugin - Supports OVS-DPDK & VPP
- Amazon ECS CNI Plugins - a collection of CNI Plugins to configure containers with —Amazon EC2 elastic network interfaces (ENIs)
- Bonding CNI - a Link aggregating plugin to address failover and high availability network
- ovn-kubernetes - an container network plugin built on Open vSwitch (OVS) and Open - Virtual Networking (OVN) with support for both Linux and Windows
- Juniper Contrail / TungstenFabric - Provides overlay SDN solution, delivering -multicloud networking, hybrid cloud networking, simultaneous overlay-underlay support, - network policy enforcement, network isolation, service chaining and flexible load balancing
- Knitter - a CNI plugin supporting multiple networking for Kubernetes
- DANM - a CNI-compliant networking solution for TelCo workloads running on – Kubernetes
- VMware NSX – a CNI plugin that enables automated NSX L2/L3 networking and L4/L7 -Load Balancing; network isolation at the pod, node, and cluster level; and zero-trust -security policy for your Kubernetes cluster.
- cni-route-override - a meta CNI plugin that override route information
- Terway - a collection of CNI Plugins based on alibaba cloud VPC/ECS network product
官方网络插件:
官方提供的插件目前分成三类:main、meta 和 ipam。main 是主要的实现了某种特定网络功能的插件;meta 本身并不会提供具体的网络功能,它会调用其他插件,或者单纯是为了测试;ipam 是分配 IP 地址的插件。
ipam 并不提供某种网络功能,只是为了灵活性把它单独抽象出来,这样不同的网络插件可以根据需求选择 ipam,或者实现自己的 ipam。
这些插件的功能说明如下:
main
loopback:这个插件很简单,负责生成 lo 网卡,并配置上 127.0.0.1/8 地址
bridge:和 docker 默认的网络模型很像,把所有的容器连接到虚拟交换机上
macvlan:使用 macvlan 技术,从某个物理网卡虚拟出多个虚拟网卡,它们有独立的 ip 和 mac 地址
ipvlan:和 macvlan 类似,区别是虚拟网卡有着相同的 mac 地址
ptp:通过 veth pair 在容器和主机之间建立通道
meta
flannel:结合 bridge 插件使用,根据 flannel 分配的网段信息,调用 bridge 插件,保证多主机情况下容器
ipam
host-local:基于本地文件的 ip 分配和管理,把分配的 IP 地址保存在文件中
dhcp:从已经运行的 DHCP 服务器中获取 ip 地址
7.在 kubernetes 中的使用
kubernetes 使用了 CNI 网络插件之后,工作过程是这样的:
- kubernetes 先创建 pause 容器生成对应的 network namespace
- 调用网络 driver(因为配置的是 CNI,所以会调用 CNI 相关代码)
- CNI driver 根据配置调用具体的 cni 插件
- cni 插件给 pause 容器配置正确的网络
- pod 中其他的容器都是用 pause 的网络