007.Kubernetes二进制部署Flannel
一部署flannel
1.1 安装flannel
kubernetes 要求集群内各节点(包括 master 节点)能通过 Pod 网段互联互通。flannel 使用 vxlan 技术为各节点创建一个可以互通的 Pod 网络,使用的端口为 UDP 8472。
flanneld 第一次启动时,从 etcd 获取配置的 Pod 网段信息,为本节点分配一个未使用的地址段,然后创建 flannedl.1 网络接口(也可能是其它名称,如 flannel1 等)。
flannel 将分配给自己的 Pod 网段信息写入 /run/flannel/docker 文件,docker 后续使用这个文件中的环境变量设置 docker0 网桥,从而从这个地址段为本节点的所有 Pod 容器分配 IP。
更多flannel参考:《008.Docker Flannel+Etcd分布式网络部署》。
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分发flannel
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 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创建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 #创建flanneld的CA证书请求文件
解释:
该证书只会被 kubectl 当做 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分发证书和私钥
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 网段信息
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"}}'
注意:注意:本步骤只需执行一次。
提示:flanneld 当前版本 (v0.11.0) 不支持 etcd v3,故使用 etcd v2 API 写入配置 key 和网段数据;
写入的 Pod 网段 ${CLUSTER_CIDR} 地址段(如 /16)必须小于 SubnetLen,必须与 kube-controller-manager 的 --cluster-cidr 参数值一致。
1.6创建flanneld的systemd
1 [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 子网段信息写入 /run/flannel/docker 文件,后续 docker 启动时使用这个文件中的环境变量配置 docker0 网桥;
flanneld:使用系统缺省路由所在的接口与其它节点通信,对于有多个网络接口(如内网和公网)的节点,可以用 -iface 参数指定通信接口;
flanneld:运行时需要 root 权限;
-ip-masq: flanneld 为访问 Pod 网络外的流量设置 SNAT 规则,同时将传递给 Docker 的变量 --ip-masq(/run/flannel/docker 文件中)设置为 false,这样 Docker 将不再创建 SNAT 规则; Docker 的 --ip-masq 为 true 时,创建的 SNAT 规则比较“暴力”:将所有本节点 Pod 发起的、访问非 docker0 接口的请求做 SNAT,这样访问其他节点 Pod 的请求来源 IP 会被设置为 flannel.1 接口的 IP,导致目的 Pod 看不到真实的来源 Pod IP。 flanneld 创建的 SNAT 规则比较温和,只对访问非 Pod 网段的请求做 SNAT。
1.7分发flannel systemd
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 scp flanneld.service root@${master_ip}:/etc/systemd/system/ 7 done
二启动并验证
2.1启动flannel
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 daemon-reload && systemctl enable flanneld && systemctl restart flanneld" 6 done
2.2检查flannel启动
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网段信息
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 网段(/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 子网段列表(/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 网段对应的节点 IP 和 flannel 接口地址
解释:
172.30.32.0/21 被分配给节点 k8smaster01 (172.24.8.71);
VtepMAC 为 k8smaster01 节点的 flannel.1 网卡 MAC 地址。
2.4检查flannel网络信息
1 [root@k8smaster01 ~]# ip addr show
解释:flannel.1 网卡的地址为分配的 Pod 子网段的第一个 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
解释:
到其它节点 Pod 网段请求都被转发到 flannel.1 网卡;
flanneld 根据 etcd 中子网段的信息,如 ${FLANNEL_ETCD_PREFIX}/subnets/172.30.32.0-21 ,来决定进请求发送给哪个节点的互联 IP。
2.5验证各节点flannel
在各节点上部署 flannel 后,检查是否创建了 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
在各节点上 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
赞 (0)