網絡插件Flannel介紹:https://www.kubernetes.org.cn/3682.htmlhtml
首先,flannel利用Kubernetes API或者etcd用於存儲整個集羣的網絡配置,其中最主要的內容爲設置集羣的網絡地址空間。例如,設定整個集羣內全部容器的IP都取自網段「10.1.0.0/16」。node
接着,flannel在每一個主機中運行flanneld做爲agent,它會爲所在主機從集羣的網絡地址空間中,獲取一個小的網段subnet,本主機內全部容器的IP地址都將從中分配。linux
而後,flanneld再將本主機獲取的subnet以及用於主機間通訊的Public IP,一樣經過kubernetes API或者etcd存儲起來。git
最後,flannel利用各類backend mechanism,例如udp,vxlan等等,跨主機轉發容器間的網絡流量,完成容器間的跨主機通訊。github
1、下載flannel插件docker
在全部node節點安裝flannel插件,本次環境在hdss7-21,hdss7-22上:api
# cd /opt/src
# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz
# mkdir /opt/flannel-v0.11.0
# tar xf flannel-v0.11.0-linux-amd64.tar.gz -C /opt/flannel-v0.11.0/
# ln -s /opt/flannel-v0.11.0/ /opt/flannel
# cd /opt/flannel
# mkdir cert
由於要和apiserver通訊,因此要配置client證書:網絡
cert]# scp hdss7-200:/opt/certs/ca.pem . cert]# scp hdss7-200:/opt/certs/client.pem . cert]# scp hdss7-200:/opt/certs/client-key.pem .
編輯環境變量env文件:紅色部分根據node節點信息修改post
# vi subnet.env FLANNEL_NETWORK=172.7.0.0/16 FLANNEL_SUBNET=172.7.21.1/24 FLANNEL_MTU=1500 FLANNEL_IPMASQ=false
編輯啓動腳本:紅色部分根據node節點信息修改,而且eth0信息根據本機網卡信息修改測試
# vi 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
# chmod u+x flanneld.sh
建立日誌存放目錄:
# mkdir -p /data/logs/flanneld
在etcd中增長網絡配置信息:
# cd /opt/etcd
測試使用 host-gw模型:
# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
查看網絡模型配置:
# ./etcdctl get /coreos.com/network/config
編輯supervisor啓動腳本:紅色部分記得修改
# vi /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)
更新supervisor配置:
# supervisorctl update
這裏須要修改iptables優化SNAT規則,不然在訪問時,其餘節點記錄的是node節點的ip 10.4.7.21,而不是pod集羣內部的172.7.21.x,紅色部分按需修改
# yum install iptables-services -y
# iptables -t nat -D POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
# iptables -t nat -I POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
# iptables-save |grep -i postrouting
# iptables-save > /etc/sysconfig/iptables
修改後會影響到docker本來的iptables鏈的規則,因此須要重啓docker服務
# systemctl restart docker
配置vxlan模型:
# cd /opt/etcd # ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "vxlan"}}'
重啓flanneld:
# supervisorctl restart flanneld-7-21
能夠發現多了一塊網卡,這塊網卡就是vxlan用於隧道通訊的虛擬網卡:
配置vxlan跟host-gw混合模型:
# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "vxlan","Directrouting": true}}'
重啓flanneld:
# supervisorctl restart flanneld-7-21