CNI Plugin Chains

CNI还支持Plugin Chains,即指定一个插件列表,由Runtime依次执行每个插件。这对支持portmapping、vm等非常有帮助。

Network Configuration Lists

CNI SPEC支持指定网络配置列表,包含多个网络插件,由Runtime依次执行。注意

  • ADD操作,按顺序依次调用每个插件;而DEL操作调用顺序相反
  • ADD操作,除最后一个插件,前面每个插件需要增加prevResult传递给其后的插件
  • 第一个插件必须要包含ipam插件

示例

下面的例子展示了bridge+portmap插件的用法。

首先,配置CNI网络使用bridge+portmap插件:

  1. # cat /root/mynet.conflist
  2. {
  3. "name": "mynet",
  4. "cniVersion": "0.3.0",
  5. "plugins": [
  6. {
  7. "type": "bridge",
  8. "bridge": "mynet",
  9. "ipMasq": true,
  10. "isGateway": true,
  11. "ipam": {
  12. "type": "host-local",
  13. "subnet": "10.244.10.0/24",
  14. "routes": [
  15. { "dst": "0.0.0.0/0" }
  16. ]
  17. }
  18. },
  19. {
  20. "type": "portmap",
  21. "capabilities": {"portMappings": true}
  22. }
  23. ]
  24. }

然后通过CAP_ARGS设置端口映射参数:

  1. # export CAP_ARGS='{
  2. "portMappings": [
  3. {
  4. "hostPort": 9090,
  5. "containerPort": 80,
  6. "protocol": "tcp",
  7. "hostIP": "127.0.0.1"
  8. }
  9. ]
  10. }'

测试添加网络接口:

  1. # ip netns add test
  2. # CNI_PATH=/opt/cni/bin NETCONFPATH=/root ./cnitool add mynet /var/run/netns/test
  3. {
  4. "interfaces": [
  5. {
  6. "name": "mynet",
  7. "mac": "0a:58:0a:f4:0a:01"
  8. },
  9. {
  10. "name": "veth2cfb1d64",
  11. "mac": "4a:dc:1f:b7:56:b1"
  12. },
  13. {
  14. "name": "eth0",
  15. "mac": "0a:58:0a:f4:0a:07",
  16. "sandbox": "/var/run/netns/test"
  17. }
  18. ],
  19. "ips": [
  20. {
  21. "version": "4",
  22. "interface": 2,
  23. "address": "10.244.10.7/24",
  24. "gateway": "10.244.10.1"
  25. }
  26. ],
  27. "routes": [
  28. {
  29. "dst": "0.0.0.0/0"
  30. }
  31. ],
  32. "dns": {}
  33. }

可以从iptables规则中看到添加的规则:

  1. # iptables-save | grep 10.244.10.7
  2. -A CNI-DN-be1eedf7a76853f303ebd -d 127.0.0.1/32 -p tcp -m tcp --dport 9090 -j DNAT --to-destination 10.244.10.7:80
  3. -A CNI-SN-be1eedf7a76853f303ebd -s 127.0.0.1/32 -d 10.244.10.7/32 -p tcp -m tcp --dport 80 -j MASQUERADE

最后,清理网络接口:

  1. # CNI_PATH=/opt/cni/bin NETCONFPATH=/root ./cnitool del mynet /var/run/netns/test