012.Kubernetes二进制部署worker节点Flannel
一 部署flannel1.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分布式网络部署》。提示:k8smaster01节点已下载相应二进制,可直接分发至node节点。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 node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 scp flannel/{flanneld,mk-docker-opts.sh} root@${node_ip}:/opt/k8s/bin/
7 ssh root@${node_ip} "chmod +x /opt/k8s/bin/*"
8 done
1.3 创建flannel证书和密钥
提示:k8smaster01节点已创建flanneld的CA证书请求文件,可直接分发至node节点。1.4 分发证书和私钥
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 ssh root@${node_ip} "mkdir -p /etc/flanneld/cert"
7 scp flanneld*.pem root@${node_ip}:/etc/flanneld/cert
8 done
1.5 创建flanneld的systemd
提示:k8smaster01节点已创建创建flanneld的systemd,可直接分发至node节点。1.6 分发flannel systemd
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 scp flanneld.service root@${node_ip}:/etc/systemd/system/
7 done
二 启动并验证2.1 启动flannel
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for node_ip in ${NODE_IPS[@]}
3do
4 echo ">>> ${node_ip}"
5 ssh root@${node_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 node_ip in ${NODE_IPS[@]}
3do
4 echo ">>> ${node_ip}"
5 ssh root@${node_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 ~]# etcdctl
9 --endpoints=${ETCD_ENDPOINTS}
10 --ca-file=/etc/kubernetes/cert/ca.pem
11 --cert-file=/etc/flanneld/cert/flanneld.pem
12 --key-file=/etc/flanneld/cert/flanneld-key.pem
13 get ${FLANNEL_ETCD_PREFIX}/subnets/172.30.8.0-21 #查看某一 Pod 网段对应的节点 IP 和 flannel 接口地址
解释:172.30.8.0/21 被分配给节点 k8snode02 (172.24.8.75);VtepMAC 为 k8snode02 节点的 flannel.1 网卡 MAC 地址。2.4 检查flannel网络信息
1 [root@k8snode02 ~]# ip addr show
解释:flannel.1 网卡的地址为分配的 Pod 子网段的第一个 IP(.0),且是 /32 的地址。[root@k8smaster01 ~]# ip route show |grep flannel.1172.30.8.0/21 via 172.30.8.0 dev flannel.1 onlink 172.30.128.0/21 via 172.30.128.0 dev flannel.1 onlink 172.30.208.0/21 via 172.30.208.0 dev flannel.1 onlink 172.30.216.0/21 via 172.30.216.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 all_ip in ${ALL_IPS[@]}
3do
4 echo ">>> ${all_ip}"
5 ssh ${all_ip} "/usr/sbin/ip addr show flannel.1|grep -w inet"
6 done
输出:
在各节点上 ping 所有 flannel 接口 IP,确保能通: 1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh 2 [root@k8smaster01 ~]# for all_ip in ${ALL_IPS[@]}
3do
4 echo ">>> ${all_ip}"
5 ssh ${all_ip} "ping -c 1 172.30.8.0"
6 ssh ${all_ip} "ping -c 1 172.30.32.0"
7 ssh ${all_ip} "ping -c 1 172.30.128.0"
8 ssh ${all_ip} "ping -c 1 172.30.208.0"
9 ssh ${all_ip} "ping -c 1 172.30.216.0"
10 done
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 scp flannel/{flanneld,mk-docker-opts.sh} root@${node_ip}:/opt/k8s/bin/
7 ssh root@${node_ip} "chmod +x /opt/k8s/bin/*"
8 done
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 ssh root@${node_ip} "mkdir -p /etc/flanneld/cert"
7 scp flanneld*.pem root@${node_ip}:/etc/flanneld/cert
8 done
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for node_ip in ${NODE_IPS[@]}
4do
5 echo ">>> ${node_ip}"
6 scp flanneld.service root@${node_ip}:/etc/systemd/system/
7 done
2.1 启动flannel
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh2 [root@k8smaster01 ~]# for node_ip in ${NODE_IPS[@]}
3do
4 echo ">>> ${node_ip}"
5 ssh root@${node_ip} "systemctl daemon-reload && systemctl enable flanneld && systemctl restart flanneld"
6 done
2.2 检查flannel启动
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh2 [root@k8smaster01 ~]# for node_ip in ${NODE_IPS[@]}
3do
4 echo ">>> ${node_ip}"
5 ssh root@${node_ip} "systemctl status flanneld|grep Active"
6 done
2.3 检查pod网段信息
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh2 [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解释:172.30.8.0/21 被分配给节点 k8snode02 (172.24.8.75);VtepMAC 为 k8snode02 节点的 flannel.1 网卡 MAC 地址。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 ~]# etcdctl
9 --endpoints=${ETCD_ENDPOINTS}
10 --ca-file=/etc/kubernetes/cert/ca.pem
11 --cert-file=/etc/flanneld/cert/flanneld.pem
12 --key-file=/etc/flanneld/cert/flanneld-key.pem
13 get ${FLANNEL_ETCD_PREFIX}/subnets/172.30.8.0-21 #查看某一 Pod 网段对应的节点 IP 和 flannel 接口地址
2.4 检查flannel网络信息
1 [root@k8snode02 ~]# ip addr show
解释:flannel.1 网卡的地址为分配的 Pod 子网段的第一个 IP(.0),且是 /32 的地址。[root@k8smaster01 ~]# ip route show |grep flannel.1172.30.8.0/21 via 172.30.8.0 dev flannel.1 onlink 172.30.128.0/21 via 172.30.128.0 dev flannel.1 onlink 172.30.208.0/21 via 172.30.208.0 dev flannel.1 onlink 172.30.216.0/21 via 172.30.216.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 all_ip in ${ALL_IPS[@]}
3do
4 echo ">>> ${all_ip}"
5 ssh ${all_ip} "/usr/sbin/ip addr show flannel.1|grep -w inet"
6 done
在各节点上 ping 所有 flannel 接口 IP,确保能通:
1 [root@k8smaster01 ~]# source /opt/k8s/bin/environment.sh2 [root@k8smaster01 ~]# for all_ip in ${ALL_IPS[@]}
3do
4 echo ">>> ${all_ip}"
5 ssh ${all_ip} "ping -c 1 172.30.8.0"
6 ssh ${all_ip} "ping -c 1 172.30.32.0"
7 ssh ${all_ip} "ping -c 1 172.30.128.0"
8 ssh ${all_ip} "ping -c 1 172.30.208.0"
9 ssh ${all_ip} "ping -c 1 172.30.216.0"
10 done
以上是 012.Kubernetes二进制部署worker节点Flannel 的全部内容, 来源链接: utcz.com/z/510899.html