Kubernetes设计了网络模型,但却将它的实现交给了网络插件,CNI网络插件最主要的功能就是实现POD资源能够跨宿主机进行通信。

常见的CNI网络插件:

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

在node中的pod不能互相通信前:

Kubelet—-pod—-apiserver—-scheduler—-controller-manager—apiserver—-VIP(10.4.7.10:7443)—-Node2中的kubelet—pod

1.集群架构

主机名 角色 IP地址
k8s-40.host.com flannel 192.168.10.40
k8s-50.host.com flannel 192.168.10.50

部署方法以k8s-40.host.com为例

1.1.下载软件,解压,做软链接

  1. [root@k8s-40 ~]# cd /opt/src/
  2. [root@k8s-40 src]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz
  3. [root@k8s-40 src]# mkdir /opt/flannel-v0.11.0
  4. [root@k8s-40 src]# tar xf flannel-v0.11.0-linux-amd64.tar.gz -C /opt/flannel-v0.11.0/
  5. [root@k8s-40 src]# ln -s /opt/flannel-v0.11.0/ /opt/flannel

1.2.创建目录

  1. [root@k8s-40 src]# cd /opt/flannel
  2. [root@k8s-40 flannel]# mkdir /opt/flannel/cert

1.3.拷贝证书文件

  1. 到管理主机上传送证书
  2. [root@k8s-60 certs]# scp ca.pem client.pem client-key.pem k8s-40:/opt/flannel/cert
  3. [root@k8s-60 certs]# scp ca.pem client.pem client-key.pem k8s-50:/opt/flannel/cert

1.4.创建配置

  1. [root@k8s-40 cert]# cd ..
  2. [root@k8s-40 flannel]# vi subnet.env
  3. FLANNEL_NETWORK=172.7.0.0/16
  4. FLANNEL_SUBNET=172.7.40.1/24
  5. FLANNEL_MTU=1500
  6. FLANNEL_IPMASQ=false

1.5.创建启动脚本

  1. [root@k8s-40 flannel]# vi flanneld.sh
  2. #!/bin/sh
  3. ./flanneld \
  4. --public-ip=192.168.10.40 \
  5. --etcd-endpoints=https://192.168.10.30:2379,https://192.168.10.40:2379,https://192.168.10.50:2379 \
  6. --etcd-keyfile=./cert/client-key.pem \
  7. --etcd-certfile=./cert/client.pem \
  8. --etcd-cafile=./cert/ca.pem \
  9. --iface=ens33 \ # 注意本机网卡名
  10. --subnet-file=./subnet.env \
  11. --healthz-port=2401

注意:flannel集群各主机的启动脚本略有不同,部署其它脚本时注意修改

1.6.授权和创建日志目录

  1. [root@k8s-40 flannel]# chmod +x flanneld.sh
  2. [root@k8s-40 flannel]# mkdir -p /data/logs/flanneld

1.7.创建supervisor配置

  1. [root@k8s-40 flannel]# vi /etc/supervisord.d/flannel.ini
  2. [program:flanneld-10-40]
  3. command=/opt/flannel/flanneld.sh ; the program (relative uses PATH, can take args)
  4. numprocs=1 ; number of processes copies to start (def 1)
  5. directory=/opt/flannel ; directory to cwd to before exec (def no cwd)
  6. autostart=true ; start at supervisord start (default: true)
  7. autorestart=true ; retstart at unexpected quit (default: true)
  8. startsecs=30 ; number of secs prog must stay running (def. 1)
  9. startretries=3 ; max # of serial start failures (default 3)
  10. exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  11. stopsignal=QUIT ; signal used to kill process (default TERM)
  12. stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  13. user=root ; setuid to this UNIX account to run the program
  14. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  15. stdout_logfile=/data/logs/flanneld/flanneld.stdout.log ; stderr log path, NONE for none; default AUTO
  16. stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB)
  17. stdout_logfile_backups=4 ; # of stdout logfile backups (default 10)
  18. stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  19. stdout_events_enabled=false ; emit events on stdout writes (default false)

1.8 操作etcd,增加host-gw

flannel是依赖etcd存储网络信息的

2.flannel host-gw模型(三选一)

  1. # 在etcd集群任意一个节点执行就可以
  2. [root@k8s-40 etcd]# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
  3. 验证:
  4. [root@k8s-40 etcd]# ./etcdctl get /coreos.com/network/config
  5. {"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

2.1.启动服务并检查

  1. [root@k8s-21 flannel]# supervisorctl update
  2. [root@k8s-21 flannel]# supervisorctl status

3.flannel vxlan模型(三选一)

  1. 1.supervisor stop flanneld-7-[21.22]
  2. 2.删除host-gw模型创建的路由
  3. route del -net 172.7.21.0/24 gw 10.4.7.21 hdss7-22.host.com
  4. route del -net 172.7.22.0/24 gw 10.4.7.22 hdss7-21.host.com
  5. 3.etcd修改
  6. ./etcdctl get /coreos.com/network/config
  7. ./etcdctl rm /coreos.com/network/config # 先删除host-gw模式设置的
  8. ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN"}}'
  9. 4.supervisor start flanneld-7-[21.22]

4.flannel 直接路由模型(三选一)

  1. '{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN","Directrouting": true}}'

5.flannel之SNAT规则优化

5.1.优化iptables规则

5.1.1.安装iptables-services并设置开机启动

  1. [root@k8s-21 ~]# yum install iptables-services -y
  2. [root@k8s-21 ~]# systemctl start iptables
  3. [root@k8s-21 ~]# systemctl enable iptables

5.1.2.优化各运算节点的SNAT规则

  1. iptables-save | grep -i postrouting
  2. [root@k8s-21 ~]# iptables -t nat -D POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
  3. [root@k8s-21 ~]# iptables -t nat -I POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
  4. ## 注意来源地址172.7.21.0/24,目的地址是除172.7.0.0/16和docker0网卡出网的 地址
  5. ## 即容器之间不用转换 真实ip

5.2.保存iptables规则

5.2.1.各运算节点保存iptables规则

  1. service iptables save

如果不行,查看拒绝规则,再删掉

  1. iptables-save | grep -i reject
  2. iptables -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited
  3. iptables -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited

再保存

  1. iptables-save > /etc/sysconfig/iptables

视频:https://www.bilibili.com/video/BV1PJ411h7Sw?p=33

生产经验

k8s集群中的一定要做SNAT规则优化,容器、Pod之间要互相看到真实ip

如果是小集群,不用安装flannel,也可以实现pod与pod通信,加路由和防火墙规则就行

需要172.7.40.0/24和172.7.50.0/24通信,则需要两条静态路由

node-40加172.7.50.0/24的静态路由

route add -net 172.7.50.0/24 gw 192.168.10.50 dev ens33

加了之后可以curl通:curl 172.7.50.2

再添加一条防火墙规则

iptables -t filter -I FORWARD -d 172.7.50.0/24 -j ACCEPT

node-50加172.7.40.0/24的静态路由

route add -net 172.7.40.0/24 gw 192.168.10.40 dev ens33

加了之后可以curl通:curl 172.7.40.2

再添加一条防火墙规则

iptables -t filter -I FORWARD -d 172.7.50.0/24 -j ACCEPT

flannel原理也是类似的,就是添加了路由

二层网络:网络中的服务器,指向同一个网关