一部署flannel1.1 安裝flannelkubernetes 要求集群內(nèi)各節(jié)點(diǎn)(包括 master 節(jié)點(diǎn))能通過(guò) Pod 網(wǎng)段互聯(lián)互通。flannel 使用 vxlan 技術(shù)為各節(jié)點(diǎn)創(chuàng)建一個(gè)可以互通的 Pod 網(wǎng)絡(luò),使用的端口為 UDP 8472。
flanneld 第一次啟動(dòng)時(shí),從 etcd 獲取配置的 Pod 網(wǎng)段信息,為本節(jié)點(diǎn)分配一個(gè)未使用的地址段,然后創(chuàng)建 flannedl.1 網(wǎng)絡(luò)接口(也可能是其它名稱,如 flannel1 等)。
flannel 將分配給自己的 Pod 網(wǎng)段信息寫入 /run/flannel/docker 文件,docker 后續(xù)使用這個(gè)文件中的環(huán)境變量設(shè)置 docker0 網(wǎng)橋,從而從這個(gè)地址段為本節(jié)點(diǎn)的所有 Pod 容器分配 IP。
更多flannel參考:《008.Docker Flannel+Etcd分布式網(wǎng)絡(luò)部署》。
1 [root@k8smaster01 ~]# cd /opt/k8s/work/ 2 [root@k8smaster01 work]# mkdir flannel 3 [root@k8smaster01 work]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz 4 [root@k8smaster01 work]# tar -xzvf flannel-v0.11.0-linux-amd64.tar.gz -C flannel 1.2分發(fā)flannel1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 scp flannel/{flanneld,mk-docker-opts.sh} root@${master_ip}:/opt/k8s/bin/ 7 ssh root@${master_ip} "chmod +x /opt/k8s/bin/*" 8 done 1.3創(chuàng)建flannel證書和密鑰1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# cat > flanneld-csr.json <<EOF 3 { 4 "CN": "flanneld", 5 "hosts": [], 6 "key": { 7 "algo": "rsa", 8 "size": 2048 9 }, 10 "names": [ 11 { 12 "C": "CN", 13 "ST": "Shanghai", 14 "L": "Shanghai", 15 "O": "k8s", 16 "OU": "System" 17 } 18 ] 19 } 20 EOF 21 #創(chuàng)建flanneld的CA證書請(qǐng)求文件 解釋:
該證書只會(huì)被 kubectl 當(dāng)做 client 證書使用,所以 hosts 字段為空。
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem 3 -ca-key=/opt/k8s/work/ca-key.pem -config=/opt/k8s/work/ca-config.json 4 -profile=kubernetes flanneld-csr.json | cfssljson -bare flanneld#生成CA密鑰(ca-key.pem)和證書(ca.pem) 1.4分發(fā)證書和私鑰1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "mkdir -p /etc/flanneld/cert" 7 scp flanneld*.pem root@${master_ip}:/etc/flanneld/cert 8 done 1.5寫入集群 Pod 網(wǎng)段信息1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# etcdctl 4 --endpoints=${ETCD_ENDPOINTS} 5 --ca-file=/opt/k8s/work/ca.pem 6 --cert-file=/opt/k8s/work/flanneld.pem 7 --key-file=/opt/k8s/work/flanneld-key.pem 8 mk ${FLANNEL_ETCD_PREFIX}/config '{"Network":"'${CLUSTER_CIDR}'", "SubnetLen": 21, "Backend": {"Type": "vxlan"}}' 注意:注意:本步驟只需執(zhí)行一次。
提示:flanneld 當(dāng)前版本 (v0.11.0) 不支持 etcd v3,故使用 etcd v2 API 寫入配置 key 和網(wǎng)段數(shù)據(jù);
寫入的 Pod 網(wǎng)段 ${CLUSTER_CIDR} 地址段(如 /16)必須小于 SubnetLen,必須與 kube-controller-manager 的 --cluster-cidr 參數(shù)值一致。
1.6創(chuàng)建flanneld的systemd1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# cat > flanneld.service << EOF 4 [Unit] 5 Description=Flanneld overlay address etcd agent 6 After=network.target 7 After=network-online.target 8 Wants=network-online.target 9 After=etcd.service 10 Before=docker.service 11 12 [Service] 13 Type=notify 14 ExecStart=/opt/k8s/bin/flanneld \ 15 -etcd-cafile=/etc/kubernetes/cert/ca.pem \ 16 -etcd-certfile=/etc/flanneld/cert/flanneld.pem \ 17 -etcd-keyfile=/etc/flanneld/cert/flanneld-key.pem \ 18 -etcd-endpoints=${ETCD_ENDPOINTS} \ 19 -etcd-prefix=${FLANNEL_ETCD_PREFIX} \ 20 -iface=${IFACE} \ 21 -ip-masq 22 ExecStartPost=/opt/k8s/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker 23 Restart=always 24 RestartSec=5 25 StartLimitInterval=0 26 27 [Install] 28 WantedBy=multi-user.target 29 RequiredBy=docker.service 30 EOF 解釋:
mk-docker-opts.sh:該腳本將分配給 flanneld 的 Pod 子網(wǎng)段信息寫入 /run/flannel/docker 文件,后續(xù) docker 啟動(dòng)時(shí)使用這個(gè)文件中的環(huán)境變量配置 docker0 網(wǎng)橋;
flanneld:使用系統(tǒng)缺省路由所在的接口與其它節(jié)點(diǎn)通信,對(duì)于有多個(gè)網(wǎng)絡(luò)接口(如內(nèi)網(wǎng)和公網(wǎng))的節(jié)點(diǎn),可以用 -iface 參數(shù)指定通信接口;
flanneld:運(yùn)行時(shí)需要 root 權(quán)限;
-ip-masq: flanneld 為訪問(wèn) Pod 網(wǎng)絡(luò)外的流量設(shè)置 SNAT 規(guī)則,同時(shí)將傳遞給 Docker 的變量 --ip-masq(/run/flannel/docker 文件中)設(shè)置為 false,這樣 Docker 將不再創(chuàng)建 SNAT 規(guī)則; Docker 的 --ip-masq 為 true 時(shí),創(chuàng)建的 SNAT 規(guī)則比較“暴力”:將所有本節(jié)點(diǎn) Pod 發(fā)起的、訪問(wèn)非 docker0 接口的請(qǐng)求做 SNAT,這樣訪問(wèn)其他節(jié)點(diǎn) Pod 的請(qǐng)求來(lái)源 IP 會(huì)被設(shè)置為 flannel.1 接口的 IP,導(dǎo)致目的 Pod 看不到真實(shí)的來(lái)源 Pod IP。 flanneld 創(chuàng)建的 SNAT 規(guī)則比較溫和,只對(duì)訪問(wèn)非 Pod 網(wǎng)段的請(qǐng)求做 SNAT。
1.7分發(fā)flannel systemd1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 scp flanneld.service root@${master_ip}:/etc/systemd/system/ 7 done 二啟動(dòng)并驗(yàn)證2.1啟動(dòng)flannel1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for master_ip in ${MASTER_IPS[@]} 3 do 4 echo ">>> ${master_ip}" 5 ssh root@${master_ip} "systemctl daemon-reload && systemctl enable flanneld && systemctl restart flanneld" 6 done 2.2檢查flannel啟動(dòng)1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for master_ip in ${MASTER_IPS[@]} 3 do 4 echo ">>> ${master_ip}" 5 ssh root@${master_ip} "systemctl status flanneld|grep Active" 6 done 2.3檢查pod網(wǎng)段信息1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# etcdctl 3 --endpoints=${ETCD_ENDPOINTS} 4 --ca-file=/etc/kubernetes/cert/ca.pem 5 --cert-file=/etc/flanneld/cert/flanneld.pem 6 --key-file=/etc/flanneld/cert/flanneld-key.pem 7 get ${FLANNEL_ETCD_PREFIX}/config#查看集群 Pod 網(wǎng)段(/16) 1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# etcdctl 3 --endpoints=${ETCD_ENDPOINTS} 4 --ca-file=/etc/kubernetes/cert/ca.pem 5 --cert-file=/etc/flanneld/cert/flanneld.pem 6 --key-file=/etc/flanneld/cert/flanneld-key.pem 7 ls ${FLANNEL_ETCD_PREFIX}/subnets#查看已分配的 Pod 子網(wǎng)段列表(/24) 8 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 9 [root@k8smaster01 ~]# etcdctl 10 --endpoints=${ETCD_ENDPOINTS} 11 --ca-file=/etc/kubernetes/cert/ca.pem 12 --cert-file=/etc/flanneld/cert/flanneld.pem 13 --key-file=/etc/flanneld/cert/flanneld-key.pem 14 get ${FLANNEL_ETCD_PREFIX}/subnets/172.30.32.0-21#查看某一 Pod 網(wǎng)段對(duì)應(yīng)的節(jié)點(diǎn) IP 和 flannel 接口地址 解釋:
172.30.32.0/21 被分配給節(jié)點(diǎn) k8smaster01 (172.24.8.71);
VtepMAC 為 k8smaster01 節(jié)點(diǎn)的 flannel.1 網(wǎng)卡 MAC 地址。
2.4檢查flannel網(wǎng)絡(luò)信息 1 [root@k8smaster01 ~]# ip addr show 解釋:flannel.1 網(wǎng)卡的地址為分配的 Pod 子網(wǎng)段的第一個(gè) IP(.0),且是 /32 的地址。
1 [root@k8smaster01 ~]# ip route show |grep flannel.1 2 172.30.128.0/21 via 172.30.128.0 dev flannel.1 onlink 3 172.30.208.0/21 via 172.30.208.0 dev flannel.1 onlink 解釋:
到其它節(jié)點(diǎn) Pod 網(wǎng)段請(qǐng)求都被轉(zhuǎn)發(fā)到 flannel.1 網(wǎng)卡;
flanneld 根據(jù) etcd 中子網(wǎng)段的信息,如 ${FLANNEL_ETCD_PREFIX}/subnets/172.30.32.0-21 ,來(lái)決定進(jìn)請(qǐng)求發(fā)送給哪個(gè)節(jié)點(diǎn)的互聯(lián) IP。
2.5驗(yàn)證各節(jié)點(diǎn)flannel在各節(jié)點(diǎn)上部署 flannel 后,檢查是否創(chuàng)建了 flannel 接口(名稱可能為 flannel0、flannel.0、flannel.1 等):
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for master_ip in ${MASTER_IPS[@]} 3 do 4 echo ">>> ${master_ip}" 5 ssh ${master_ip} "/usr/sbin/ip addr show flannel.1|grep -w inet" 6 done 輸出:
1 >>> 172.24.8.71 2 inet 172.30.32.0/32 scope global flannel.1 3 >>> 172.24.8.72 4 inet 172.30.128.0/32 scope global flannel.1 5 >>> 172.24.8.73 6 inet 172.30.208.0/32 scope global flannel.1 在各節(jié)點(diǎn)上 ping 所有 flannel 接口 IP,確保能通:
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for master_ip in ${MASTER_IPS[@]} 3 do 4 echo ">>> ${master_ip}" 5 ssh ${master_ip} "ping -c 1 172.30.32.0" 6 ssh ${master_ip} "ping -c 1 172.30.128.0" 7 ssh ${master_ip} "ping -c 1 172.30.208.0" 8 done |
|