Flannel 是CNI陣營裏標準的網絡插件,在沒有嚴格的網絡的要求下,它一般做爲kubernetes集羣裏網絡互聯比較經常使用的方案,固然還有其餘網絡方案,好比calico,weave,mxvlan等等,這裏主要使用到flannel做爲kubernetes的網絡方案;linux
kubernetes 要求集羣內各節點(包括 master 節點)能經過 Pod 網段互聯互通。flannel 使用 vxlan 技術爲各節點建立一個能夠互通的 Pod 網絡,使用的端口爲 UDP 8472(須要開放該端口,如公有云 AWS 等)。git
flanneld 第一次啓動時,從 etcd 獲取配置的 Pod 網段信息,爲本節點分配一個未使用的地址段,而後建立 flannedl.1 網絡接口(也多是其它名稱,如 flannel1 等)。github
flannel 將分配給本身的 Pod 網段信息寫入 /run/flannel/docker 文件,docker 後續使用這個文件中的環境變量設置 docker0 網橋,從而從這個地址段爲本節點的全部 Pod 容器分配 IP。docker
#################### Variable parameter setting ###################### FLANNEL_INSTALL_PATH=/data/apps/k8s/networks/flannel SOFTWARE=/root/software VERSION=v0.11.0 PACKAGE=flannel-${VERSION}-linux-amd64.tar.gz DOWNLOAD_URL=https://github.com/devops-apps/download/raw/master/network/$PACKAGE ETCD_ENPOINTS=https://10.10.10.22:2379,https://10.10.10.23:2379,https://10.10.10.24:2379 FLANNEL_ETCD_PREFIX=/k8s/network CA_DIR=/etc/k8s/ssl NETWORK_SUBNET=172.16.0.0/20 IFACE=eth0
登錄devops機器,訪問flannel github 官方地址下載穩定的 realease 包至本機;shell
wget $DOWNLOAD_URL -P $SOFTWARE
將flannel 軟件包分發到集羣全部節點服務器;api
sudo ansible master_k8s_vgs -m copy -a "src=${SOFTWARE}/$PACKAGE dest=${SOFTWARE}/" -b sudo ansible worker_k8s_vgs -m copy -a "src=${SOFTWARE}/$PACKAGE dest=${SOFTWARE}/" -b
### 1.Check if the install directory exists. if [ ! -d $FLANNEL_INSTALL_PATH/bin ]; then mkdir -p $FLANNEL_INSTALL_PATH/bin fi ### 2.Install binary of flannel. if [ ! -f "$SOFTWARE/flannel-${VERSION}-linux-amd64.tar.gz" ]; then wget $DOWNLOAD_URL -P $SOFTWARE >>/tmp/install.log 2>&1 fi cd $SOFTWARE && tar -xzf flannel-${VERSION}-linux-amd64.tar.gz -C ./ cp -fp ${SOFTWARE}/{flanneld,mk-docker-opts.sh} ${FLANNEL_INSTALL_PATH}/bin ln -sf ${FLANNEL_INSTALL_PATH}/bin/{flanneld,mk-docker-opts.sh} /usr/local/bin chmod -R 755 $FLANNEL_INSTALL_PATH
cd ${CA_DIR} sudo ansible master_k8s_vgs -m copy -a "src=ca.pem dest=${CA_DIR}/" -b sudo ansible master_k8s_vgs -m copy -a "src=flannel.pem dest=${CA_DIR}/" -b sudo ansible master_k8s_vgs -m copy -a "src=flannel-key.pem dest=${CA_DIR}/" -b
### Create network subnet of flannel . etcdctl --endpoint=$ETCD_ENPOINTS \ --ca-file=${CA_DIR}/ca.pem \ --cert-file=${CA_DIR}/etcd.pem \ --key-file=${CA_DIR}/etcd-key.pem \ mkdir $FLANNEL_ETCD_PREFIX etcdctl --endpoints=$ETCD_ENPOINTS \ --ca-file=${CA_DIR}/ca.pem \ --cert-file=${CA_DIR}/etcd.pem \ --key-file=${CA_DIR}/etcd-key.pem \ mk ${FLANNEL_ETCD_PREFIX}/config \ '{"Network":"'${NETWORK_SUBNET}'","SubnetLen":24,"Backend":{"Type":"$TYPE"}}'
cat >/usr/lib/systemd/system/flanneld.service<<EOF [Unit] Description=Flanneld overlay address etcd agent After=network.target After=network-online.target Wants=network-online.target After=etcd.service Before=docker.service [Service] Type=notify ExecStart=${FLANNEL_INSTALL_PATH}/bin/flanneld \\ -etcd-cafile=${CA_DIR}/ca.pem \\ -etcd-certfile=${CA_DIR}/flannel.pem \\ -etcd-keyfile=${CA_DIR}/flannel-key.pem \\ -etcd-endpoints=${ETCD_ENPOINTS} \\ -etcd-prefix=${FLANNEL_ETCD_PREFIX} \\ -iface=${IFACE} \\ -ip-masq ExecStartPost=${FLANNEL_INSTALL_PATH}/bin/mk-docker-opts.sh \\ -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker Restart=always RestartSec=5 StartLimitInterval=0 [Install] WantedBy=multi-user.target RequiredBy=docker.service EOF
啓動flannel服務並檢查服務器
sudo systemctl start flanneld sudo systemctl status flanneld |grep 'Active:'
確保狀態爲 active (running),不然查看日誌,確認緣由:網絡
sudo journalctl -u flanneld
### Create network subnet of flannel . etcdctl --endpoint=$ETCD_ENPOINTS \ --ca-file=${CA_DIR}/ca.pem \ --cert-file=${CA_DIR}/etcd.pem \ --key-file=${CA_DIR}/etcd-key.pem \ get ${FLANNEL_ETCD_PREFIX}/config
輸出:app
{"Network":"172.20.0.0/20", "SubnetLen": 21, "Backend": {"Type": "vxlan"}}
查看已分配的 Pod 子網段列表(/24):ide
etcdctl --endpoints=$ETCD_ENPOINTS \ --ca-file=${CA_DIR}/ca.pem \ --cert-file=${CA_DIR}/etcd.pem \ --key-file=${CA_DIR}/etcd-key.pem \ ls ${FLANNEL_ETCD_PREFIX}/subnets
輸出(結果視部署狀況而定):
/k8s/network/subnets/172.16.1.0-24 /k8s/network/subnets/172.16.3.0-24 /k8s/network/subnets/172.16.13.0-24 /k8s/network/subnets/172.16.15.0-24 /k8s/network/subnets/172.16.8.0-24
查看某一 Pod 網段對應的節點 IP 和 flannel 接口地址:
etcdctl --endpoints=$ETCD_ENPOINTS \ --ca-file=${CA_DIR}/ca.pem \ --cert-file=${CA_DIR}/etcd.pem \ --key-file=${CA_DIR}/etcd-key.pem \ ls ${FLANNEL_ETCD_PREFIX}/subnets
輸出(結果視部署狀況而定):
{"PublicIP":"192.168.20.24","BackendType":"vxlan","BackendData":{"VtepMAC":"a6:92:04:07"}}
隨機登錄一臺master節點,這裏以master-ks8-n03爲例
$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.20.24 netmask 255.255.255.0 broadcast 192.168.20.255 ether 00:50:56:b9:9c:b1 txqueuelen 1000 (Ethernet) RX packets 305607 bytes 52151972 (49.7 MiB) RX errors 0 dropped 1176 overruns 0 frame 0 TX packets 355321 bytes 58794327 (56.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.24 netmask 255.255.255.0 broadcast 10.10.10.255 ether 00:50:56:b9:2c:1e txqueuelen 1000 (Ethernet) RX packets 216915716 bytes 36273314852 (33.7 GiB) RX errors 0 dropped 1173 overruns 0 frame 0 TX packets 207708912 bytes 33334846046 (31.0 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet 172.16.1.0 netmask 255.255.255.255 broadcast 0.0.0.0 ether a6:92:cc:85:04:07 txqueuelen 0 (Ethernet) RX packets 260862 bytes 27280155 (26.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 309429 bytes 36792200 (35.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看路由
$ ip route show |grep flannel.1 172.16.3.0/24 via 172.16.3.0 dev flannel.1 onlink 172.16.8.0/24 via 172.16.8.0 dev flannel.1 onlink 172.16.13.0/24 via 172.16.13.0 dev flannel.1 onlink 172.16.15.0/24 via 172.16.15.0 dev flannel.1 onlink
在各節點上部署 flannel 後,檢查是否建立了 flannel 接口(名稱可能爲 flannel0、flannel.0、flannel.1 等):這裏爲flannel.1
ansible master_k8s_vgs -m shell -a ''ping 172.16.1.1 -c 3 && ping 172.16.3.1 -c 3 \ && ping 172.16.8.1 -c 3 && ping 172.16.13.1 -c 3 && ping 172.16.15.1 -c 3"
上述命令在devops機器上執行。正常狀況下,到集羣內每一個節點都是互通的,若是有不通狀況下,檢查防火牆設置;
安裝完flannel網絡插件後,整個集羣到這裏算大功告成,後面還須要安裝相關插件,如dashboard可視化,dns解析,以及監控等等;關於flannel腳本能夠今後處獲取