k8s设计了网络模型,但却将他的实现交给了网络插件,CNI网络插件最主要的功能就是实现POD资源能够跨主机进行通信
种类众多,以**flannel**为例
三种常用工作模式
优化SNAT规则

常见的CNI网络插件:

  • Flannel
  • Calico
  • Canal
  • Contiv
  • OpenContrail
  • NSX-T
  • Kube-router

安装flannel

需要在所有运算节点上安装(k8s-5-138,k8s-5-139) 关于flannel的详细说明,可参考以下文章 https://blog.csdn.net/xxb249/article/details/85642172

注意: 由于 flannel 还不支持etcd 3, 所以先要将etcd开启v2,在etcd启动脚本中添加参数 —enable-v2

  1. [Unit]
  2. Description=etcd
  3. Documentation=https://github.com/coreos/etcd
  4. Conflicts=etcd.service
  5. Conflicts=etcd2.service
  6. [Service]
  7. Type=notify
  8. Restart=always
  9. RestartSec=5s
  10. LimitNOFILE=40000
  11. TimeoutStartSec=0
  12. ExecStart=/opt/etcd/etcd --name etcd139 \
  13. --data-dir /data/etcd-server \
  14. --listen-client-urls https://192.168.5.139:2379,http://127.0.0.1:2379 \
  15. --advertise-client-urls https://192.168.5.139:2379,http://127.0.0.1:2379 \
  16. --listen-peer-urls https://192.168.5.139:2380 \
  17. --initial-advertise-peer-urls https://192.168.5.139:2380 \
  18. --initial-cluster etcd137=https://192.168.5.137:2380,etcd138=https://192.168.5.138:2380,etcd139=https://192.168.5.139:2380 \
  19. --initial-cluster-token tkn \
  20. --initial-cluster-state new \
  21. --client-cert-auth \
  22. --trusted-ca-file /opt/etcd/certs/ca.pem \
  23. --cert-file /opt/etcd/certs/etcd-peer.pem \
  24. --key-file /opt/etcd/certs/etcd-peer-key.pem \
  25. --peer-client-cert-auth \
  26. --peer-trusted-ca-file /opt/etcd/certs/ca.pem \
  27. --peer-cert-file /opt/etcd/certs/etcd-peer.pem \
  28. --peer-key-file /opt/etcd/certs/etcd-peer-key.pem \
  29. --enable-v2

将flannel配置写到etcd时,需要使用ETCD_API=2 声明版本变量

  1. [root@k8s-5-137 ~]# ETCDCTL_API=2 /opt/etcd/etcdctl set /coreos.com/network/config '{"Network": "172.5.0.0/16", "Backend": {"Type": "host-gw"}}'
  2. # 用v2命令添加的key-value,使用v3命令get不到,必须使用v2命令才能查到
  3. [root@k8s-5-137 flannel]# /opt/etcd/etcdctl get /coreos.com/network/config
  4. [root@k8s-5-137 flannel]# ETCDCTL_API=2 /opt/etcd/etcdctl get /coreos.com/network/config
  5. {"Network": "172.5.0.0/16", "Backend": {"Type": "host-gw"}}

1. 下载二进制安装包

https://github.com/flannel-io/flannel/releases/tag/v0.12.0

2. 解压,配置证书

因为flannel需要使用etcd来存储一些配置,所以需要配置etcd的客户端证书

  1. [root@k8s-5-138 ~]# cd /opt/src
  2. [root@k8s-5-138 src]# ll
  3. total 381636
  4. -rw-r--r-- 1 root root 17280028 Mar 18 16:28 etcd-v3.4.3-linux-amd64.tar.gz
  5. -rw-r--r-- 1 root root 9565406 Mar 29 10:06 flannel-v0.12.0-linux-amd64.tar.gz
  6. -rw-r--r-- 1 root root 363943527 Mar 24 10:37 kubernetes-server-linux-amd64.tar.gz
  7. [root@k8s-5-138 src]# mkdir /opt/flanel-v0.12.0
  8. [root@k8s-5-138 src]# tar xf flannel-v0.12.0-linux-amd64.tar.gz -C /opt/flanel-v0.12.0/
  9. [root@k8s-5-138 src]# ln -s /opt/flanel-v0.12.0/ /opt/flanel
  10. [root@k8s-5-138 flannel]# mkdir cert
  11. [root@k8s-5-138 flannel]# cd cert
  12. [root@k8s-5-138 cert]# scp k8s-5-141.host.com:/opt/certs/ca.pem .
  13. [root@k8s-5-138 cert]# scp k8s-5-141.host.com:/opt/certs/kube-cert/client.pem .
  14. [root@k8s-5-138 cert]# scp k8s-5-141.host.com:/opt/certs/kube-cert/client-key.pem .
  15. [root@k8s-5-139 cert]# ll
  16. total 12
  17. -rw-r--r-- 1 root root 1387 Mar 29 11:05 ca.pem
  18. -rw------- 1 root root 1679 Mar 29 11:05 client-key.pem
  19. -rw-r--r-- 1 root root 1407 Mar 29 11:05 client.pem

3. 创建配置文件,添加启动服务

启动参数中的—public-ip 需要修改成当前服务器的内网IP

  1. #创建配置文件
  2. [root@k8s-5-138 flannel]# vim subnet.env
  3. FLANNEL_NETWORK=172.5.0.0/16
  4. FLANNEL_SUBNET=172.5.138.1/24
  5. FLANNEL_MTU=1500
  6. FLANNEL_IPMASQ=false
  7. #添加启动服务
  8. [root@k8s-5-138 flannel]# vim /etc/systemd/system/flanneld-app.service
  9. [Unit]
  10. Description=flanneld service
  11. Documentation=https://github.com/kubernetes
  12. Conflicts=flanneld-app
  13. [Service]
  14. Type=notify
  15. Restart=always
  16. RestartSec=5s
  17. LimitNOFILE=40000
  18. TimeoutStartSec=0
  19. ExecStart=/opt/flannel/flanneld \
  20. --public-ip=192.168.5.138 \
  21. --etcd-endpoints=https://192.168.5.137:2379,https://192.168.5.139:2379,https://192.168.5.139:2379 \
  22. --etcd-keyfile=/opt/flannel/cert/client-key.pem \
  23. --etcd-certfile=/opt/flannel/cert/client.pem \
  24. --etcd-cafile=/opt/flannel/cert/ca.pem \
  25. --iface=ens33 \
  26. --subnet-file=/opt/flannel/subnet.env \
  27. --healthz-port=2401
  28. [Install]
  29. WantedBy=multi-user.target

4. 添加etcd配置(在etcd主节点服务器上执行)

  1. # 先找出etcd leader 服务器
  2. [root@k8s-5-138 etcd]# /opt/etcd/etcdctl \
  3. > --endpoints https://192.168.5.137:2379,https://192.168.5.138:2379,https://192.168.5.139:2379 \
  4. > --cacert /opt/etcd/certs/ca.pem \
  5. > --cert /opt/etcd/certs/etcd-peer.pem \
  6. > --key /opt/etcd/certs/etcd-peer-key.pem \
  7. > endpoint status
  8. https://192.168.5.137:2379, 8538572bbe136754, 3.4.3, 2.6 MB, true, false, 168690, 788172, 788172,
  9. https://192.168.5.138:2379, e66eb4f1843fc910, 3.4.3, 2.6 MB, false, false, 168690, 788172, 788172,
  10. https://192.168.5.139:2379, 2fb7bc8fa69bd29b, 3.4.3, 2.5 MB, false, false, 168690, 788172, 788172,
  11. # 切换到137服务器
  12. [root@k8s-5-137 ~]# ETCDCTL_API=2 /opt/etcd/etcdctl set /coreos.com/network/config '{"Network": "172.5.0.0/16", "Backend": {"Type": "host-gw"}}'
  13. # 用v2命令添加的key-value,使用v3命令get不到,必须使用v2命令才能查到
  14. [root@k8s-5-137 flannel]# /opt/etcd/etcdctl get /coreos.com/network/config
  15. [root@k8s-5-137 flannel]# ETCDCTL_API=2 /opt/etcd/etcdctl get /coreos.com/network/config
  16. {"Network": "172.5.0.0/16", "Backend": {"Type": "host-gw"}}

5. 启动falnnel服务

  1. [root@k8s-5-138 ~]# systemctl daemon-reload
  2. [root@k8s-5-138 ~]# systemctl enable flannel-app
  3. [root@k8s-5-138 ~]# systemctl start flannel-app

6. 验证容器网络是否互通

  1. #先查看当前集群内有哪些Pod
  2. [root@k8s-5-138 /]# kubectl get pods -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. nginx-ds-2v724 1/1 Running 0 3d4h 172.5.138.2 k8s-5-138.host.com <none> <none>
  5. nginx-ds-85l6c 1/1 Running 0 3d4h 172.5.139.2 k8s-5-139.host.com <none> <none>
  6. #可以看到当前集群内有两个pod,分别运行在 k8s-5-138 和 k8s-5-139 节点上,如果在任何一个k8s节点服务器都可以ping通非当前节点下的pod的ip,就表示flannel服务配置成功了
  7. [root@k8s-5-138 /]# ping 172.5.138.2
  8. PING 172.5.138.2 (172.5.138.2) 56(84) bytes of data.
  9. 64 bytes from 172.5.138.2: icmp_seq=1 ttl=64 time=0.100 ms
  10. 64 bytes from 172.5.138.2: icmp_seq=2 ttl=64 time=0.049 ms
  11. ^C
  12. --- 172.5.138.2 ping statistics ---
  13. 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  14. rtt min/avg/max/mdev = 0.049/0.074/0.100/0.026 ms
  15. [root@k8s-5-138 /]# ping 172.5.139.2
  16. PING 172.5.139.2 (172.5.139.2) 56(84) bytes of data.
  17. 64 bytes from 172.5.139.2: icmp_seq=1 ttl=63 time=0.307 ms
  18. 64 bytes from 172.5.139.2: icmp_seq=2 ttl=63 time=0.333 ms
  19. ^C
  20. --- 172.5.139.2 ping statistics ---
  21. 2 packets transmitted, 2 received, 0% packet loss, time 999ms
  22. rtt min/avg/max/mdev = 0.307/0.320/0.333/0.013 ms
  23. [root@k8s-5-138 /]# curl 172.5.138.2
  24. <!DOCTYPE html>
  25. <html>
  26. <head>
  27. <title>Welcome to nginx!</title>
  28. <style>
  29. body {
  30. width: 35em;
  31. margin: 0 auto;
  32. font-family: Tahoma, Verdana, Arial, sans-serif;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <h1>Welcome to nginx!</h1>
  38. <p>If you see this page, the nginx web server is successfully installed and
  39. working. Further configuration is required.</p>
  40. <p>For online documentation and support please refer to
  41. <a href="http://nginx.org/">nginx.org</a>.<br/>
  42. Commercial support is available at
  43. <a href="http://nginx.com/">nginx.com</a>.</p>
  44. <p><em>Thank you for using nginx.</em></p>
  45. </body>
  46. </html>
  47. [root@k8s-5-138 /]# curl 172.5.139.2
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <title>Welcome to nginx!</title>
  52. <style>
  53. body {
  54. width: 35em;
  55. margin: 0 auto;
  56. font-family: Tahoma, Verdana, Arial, sans-serif;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <h1>Welcome to nginx!</h1>
  62. <p>If you see this page, the nginx web server is successfully installed and
  63. working. Further configuration is required.</p>
  64. <p>For online documentation and support please refer to
  65. <a href="http://nginx.org/">nginx.org</a>.<br/>
  66. Commercial support is available at
  67. <a href="http://nginx.com/">nginx.com</a>.</p>
  68. <p><em>Thank you for using nginx.</em></p>
  69. </body>
  70. </html>