1. 程式人生 > 實用技巧 >K8S網路模型

K8S網路模型

K8S網路模型

解決容器在叢集之間的互通。

K8S設計了網路模型,但卻將它的實現交給了網路外掛,CNI網路外掛最主要的功能就是實現POD資源能夠跨宿主機進行通訊。

常見的CNI網路外掛

  • Flannel(重點):依賴於etcd去儲存網路資訊的
  • Calico:網路限制,網路規則的內容
  • Canal:把上面兩個結合了,前半部分用F,後半部分用C(沒有啥用
  • Contiv:思科開源的
  • OpenContrail
  • NSX-T
  • kube-router:試圖取代kube-proxy

叢集規劃

主機名 角色 IP
HDSS7-21.host.com flannel 10.4.7.21
HDSS7-22.host.com flannel 10.4.7.22

下載軟體,解壓,做軟連結

HDSS7-21.host.com

[root@hdss7-21 src]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz

[root@hdss7-21 src]# mkdir /opt/flannel-v0.11.0
[root@hdss7-21 src]# tar -zxvf flannel-v0.11.0-linux-amd64.tar.gz -C /opt/flannel-v0.11.0
[root@hdss7-21 src]# ln -s /opt/flannel-v0.11.0/ /opt/flannel

做軟連結,方便以後版本升級,只需要移除舊的軟連結,建立新的軟連線即可。

[root@hdss7-21 opt]# cd flannel
[root@hdss7-21 flannel]# ll
total 34436
-rwxr-xr-x 1 root root 35249016 Jan 29  2019 flannel
-rwxr-xr-x 1 root root     2139 Oct 23  2018 mk-docker-opts.sh
-rw-r--r-- 1 root root     4300 Oct 23  2018 README.md

[root@hdss7-21 flannel]# mkdir cert
[root@hdss7-21 flannel]# scp hdss7-200:/opt/certs/ca.pem .
[root@hdss7-21 flannel]# scp hdss7-200:/opt/certs/client.pem .
[root@hdss7-21 flannel]# scp hdss7-200:/opt/certs/client-key.pem .

建立env配置檔案

[root@hdss7-21 cert]# cd ..
[root@hdss7-21 flannel]# vim subnet.env

FLANNEL_NETWORK=172.7.0.0/16
FLANNEL_SUBNET=172.7.21.1/24
FLANNEL_MTU=1500
FLANNEL_IPMASQ=false

建立啟動指令碼

HDSS7-21.host.com

[root@hdss7-21 flannel]# vim flanneld.sh

#!/bin/sh
./flanneld \
  --public-ip=10.4.7.21 \
  --etcd-endpoints=https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 \
  --etcd-keyfile=./cert/client-key.pem \
  --etcd-certfile=./cert/client.pem \
  --etcd-cafile=./cert/ca.pem \
  --iface=eth0 \
  --subnet-file=./subnet.env \
  --healthz-port=2401

授權

[root@hdss7-21 flannel]# chmod +x flanneld.sh

建立日誌目錄

[root@hdss7-21 flannel]# mkdir -p /data/logs/flanneld

建立supervisor配置

HDSS7-21.host.com

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

操作etcd,增加host-gw

[root@hdss7-21 flannel]# cd /opt/etcd/

[root@hdss7-21 etcd]# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

[root@hdss7-21 etcd]# ./etcdctl get /coreos.com/network/config
{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

檢視誰是master

[root@hdss7-21 etcd]# ./etcdctl member list
988139385f78284: name=etcd-server-7-22 peerURLs=https://10.4.7.22:2380 clientURLs=http://127.0.0.1:2379,https://10.4.7.22:2379 isLeader=false
5a0ef2a004fc4349: name=etcd-server-7-21 peerURLs=https://10.4.7.21:2380  clientURLs=http://127.0.0.1:2379,https://10.4.7.21:2379 isLeader=true
f4a0cb0a765574a8: name=etcd-server-7-12 peerURLs=https://10.4.7.12:2380 clientURLs=http://127.0.0.1:2379,https://10.4.7.12:2379 isLeader=false

啟動服務並檢查

[root@hdss7-21 etcd]# supervisorctl status
etcd-server-7-21                 RUNNING   pid 1029, uptime 4 days, 0:41:33
kube-apiserver-7-21              RUNNING   pid 1030, uptime 4 days, 0:41:33
kube-controller-manager-7-21     RUNNING   pid 89625, uptime 1 day, 4:53:09
kube-kubelet-7-21                RUNNING   pid 1027, uptime 4 days, 0:41:33
kube-proxy-7-21                  RUNNING   pid 1032, uptime 4 days, 0:41:33
kube-scheduler-7-21              RUNNING   pid 90832, uptime 1 day, 4:49:07

[root@hdss7-21 etcd]# supervisorctl  update
flanneld-7-21: added process group

[root@hdss7-21 flannel]# supervisorctl  status
etcd-server-7-21                 RUNNING   pid 1029, uptime 4 days, 1:03:48
flanneld-7-21                    RUNNING   pid 50922, uptime 0:01:44
kube-apiserver-7-21              RUNNING   pid 1030, uptime 4 days, 1:03:48
kube-controller-manager-7-21     RUNNING   pid 48165, uptime 0:11:30
kube-kubelet-7-21                RUNNING   pid 1027, uptime 4 days, 1:03:48
kube-proxy-7-21                  RUNNING   pid 1032, uptime 4 days, 1:03:48
kube-scheduler-7-21              RUNNING   pid 48158, uptime 0:11:30

ping

[root@hdss7-22 ~]# ping 172.7.21.2
PING 172.7.21.2 (172.7.21.2) 56(84) bytes of data.
64 bytes from 172.7.21.2: icmp_seq=1 ttl=63 time=0.710 ms
64 bytes from 172.7.21.2: icmp_seq=2 ttl=63 time=0.429 ms