kubernetes-notes

1. CNI(Container Network Interface)

CNI(Container Network Interface)即容器网络接口,通过约定统一的容器网络接口,从而kubelet可以通过这个标准的API来调用不同的网络插件实现不同的网络功能。

kubelet启动参数–network-plugin=cni来指定CNI插件,kubelet从--cni-conf-dir (默认是 /etc/cni/net.d) 读取文件并使用 该文件中的 CNI 配置来设置各个 Pod 的网络。 CNI 配置文件必须与 CNI 规约 匹配,并且配置所引用的所有所需的 CNI 插件都应存在于 --cni-bin-dir(默认是 /opt/cni/bin)下。如果有多个CNI配置文件,kubelet 将会使用按文件名的字典顺序排列 的第一个作为配置文件。

CNI规范定义:

2. CNI配置文件格式

CNI配置文件的格式为JSON格式,配置文件的默认路径:/etc/cni/net.d。插件二进制默认的路径为:/opt/cni/bin。

2.1. 主配置的字段

2.2. 插件配置字段

根据不同的插件,插件配置所需的字段不同。

必选字段:

可选字段:

2.3. 配置文件示例

$ mkdir -p /etc/cni/net.d
$ cat >/etc/cni/net.d/10-mynet.conf <<EOF
{
  "cniVersion": "1.0.0",
  "name": "dbnet",
  "plugins": [
    {
      "type": "bridge",
      // plugin specific parameters
      "bridge": "cni0",
      "keyA": ["some more", "plugin specific", "configuration"],

      "ipam": {
        "type": "host-local",
        // ipam specific
        "subnet": "10.1.0.0/16",
        "gateway": "10.1.0.1",
        "routes": [
            {"dst": "0.0.0.0/0"}
        ]
      },
      "dns": {
        "nameservers": [ "10.1.0.1" ]
      }
    },
    {
      "type": "tuning",
      "capabilities": {
        "mac": true
      },
      "sysctl": {
        "net.core.somaxconn": "500"
      }
    },
    {
        "type": "portmap",
        "capabilities": {"portMappings": true}
    }
  ]
}

3. CNI插件

3.1. 安装插件

安装CNI二进制插件,插件下载地:https://github.com/containernetworking/plugins/releases

# 下载二进制
wget https://github.com/containernetworking/plugins/releases/download/v1.1.0/cni-plugins-linux-amd64-v1.1.0.tgz

# 解压文件
tar -zvxf cni-plugins-linux-amd64-v1.1.0.tgz -C /opt/cni/bin/

# 查看解压文件
# ll -h
总用量 63M
-rwxr-xr-x 1 root root 3.7M 2月  24 01:01 bandwidth
-rwxr-xr-x 1 root root 4.1M 2月  24 01:01 bridge
-rwxr-xr-x 1 root root 9.3M 2月  24 01:01 dhcp
-rwxr-xr-x 1 root root 4.2M 2月  24 01:01 firewall
-rwxr-xr-x 1 root root 3.7M 2月  24 01:01 host-device
-rwxr-xr-x 1 root root 3.1M 2月  24 01:01 host-local
-rwxr-xr-x 1 root root 3.8M 2月  24 01:01 ipvlan
-rwxr-xr-x 1 root root 3.2M 2月  24 01:01 loopback
-rwxr-xr-x 1 root root 3.8M 2月  24 01:01 macvlan
-rwxr-xr-x 1 root root 3.6M 2月  24 01:01 portmap
-rwxr-xr-x 1 root root 4.0M 2月  24 01:01 ptp
-rwxr-xr-x 1 root root 3.4M 2月  24 01:01 sbr
-rwxr-xr-x 1 root root 2.7M 2月  24 01:01 static
-rwxr-xr-x 1 root root 3.3M 2月  24 01:01 tuning
-rwxr-xr-x 1 root root 3.8M 2月  24 01:01 vlan
-rwxr-xr-x 1 root root 3.4M 2月  24 01:01 vrf

3.2. 插件分类

参考:https://www.cni.dev/plugins/current/

分类 插件 说明
main bridge Creates a bridge, adds the host and the container to it
  ipvlan Adds an ipvlan interface in the container
  macvlan Creates a new MAC address, forwards all traffic to that to the container
  ptp Creates a veth pair
  host-device Moves an already-existing device into a container
  vlan Creates a vlan interface off a master
IPAM dhcp Runs a daemon on the host to make DHCP requests on behalf of a container
  host-local Maintains a local database of allocated IPs
  static Allocates static IPv4/IPv6 addresses to containers
meta tuning Changes sysctl parameters of an existing interface
  portmap An iptables-based portmapping plugin. Maps ports from the host’s address space to the container
  bandwidth Allows bandwidth-limiting through use of traffic control tbf (ingress/egress)
  sbr A plugin that configures source based routing for an interface (from which it is chained)
  firewall A firewall plugin which uses iptables or firewalld to add rules to allow traffic to/from the container

4. CNI插件接口

具体可参考:https://github.com/containernetworking/cni/blob/master/SPEC.md#cni-operations

CNI定义的接口操作有:

这些操作通过CNI_COMMAND环境变量来传递给CNI插件二进制。

其中环境变量包括:

4.1. ADD接口:添加容器网络

在容器的网络命名空间CNI_NETNS中创建CNI_IFNAME网卡设备,或者调整网卡配置。

必选参数:

可选参数:

4.2. DEL接口:删除容器网络

删除容器网络命名空间CNI_NETNS中的容器网卡CNI_IFNAME,或者撤销ADD修改操作。

必选参数:

可选参数:

4.3. CHECK接口:检查容器网络

4.4. VERSION接口:输出CNI的版本

参考: