一、master编写flannel脚本:
vim /root/scripts/flannel.sh
#!/bin/bash
cd /opt/kubernetes/ssl/
#写入分配的子网网段
/opt/kubernetes/bin/etcdctl -ca-file=ca.pem -cert-file=server.pem -key-file=server-key.pem --endpoints=“https://192.168.1.250:2379,https://192.168.1.75:2379,https://192.168.1.76:2379,https://192.168.1.77:2379” set /coreos.com/network/config ‘{ “Network”: “172.17.0.0/16”, “Backend”: {“Type”: “vxlan”}}’
#下载flannel安装包
wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz
tar xf flannel-v0.11.0-linux-amd64.tar.gz
#将flannel命令和脚本发送到node节点
scp flanneld mk-docker-opts.sh 192.168.1.75:/opt/kubernetes/bin/
scp flanneld mk-docker-opts.sh 192.168.1.76:/opt/kubernetes/bin/
scp flanneld mk-docker-opts.sh 192.168.1.77:/opt/kubernetes/bin/

执行脚本:
sh /root/scripts/flannel.sh

查看添加的子网:
/opt/kubernetes/bin/etcdctl -ca-file=ca.pem -cert-file=server.pem -key-file=server-key.pem --endpoints=“https://192.168.1.250:2379,https://192.168.1.75:2379,https://192.168.1.76:2379,https://192.168.1.77:2379” get /coreos.com/network/config
在这里插入图片描述
如果返回Error: 100: Key not found (/coreos.com) [11],则代表没有设置网络

二、node节点编写flannel脚本(三台node节点脚本内容相同):
vim /root/scripts/flannel.sh
#!/bin/bash

#写入flannel配置文件
cat >/opt/kubernetes/cfg/flanneld <<EOF
FLANNEL_OPTIONS="–etcd-endpoints=https://192.168.1.250:2379,https://192.168.1.75:2379,https://192.168.1.76:2379,https://192.168.1.77:2379
-etcd-cafile=/opt/kubernetes/ssl/ca.pem
-etcd-certfile=/opt/kubernetes/ssl/server.pem
-etcd-keyfile=/opt/kubernetes/ssl/server-key.pem"
EOF

#systemd管理flannel
cat >/usr/lib/systemd/system/flanneld.service <<EOF
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service

[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

#启动flannel
systemctl daemon-reload
systemctl restart flanneld
systemctl enable flanneld

#配置docker启动指定子网段
cat >/usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF

#重启docker
systemctl daemon-reload
systemctl restart docker

执行脚本:
sh /root/scripts/flannel.sh

node02、node03都执行脚本:
在node01上把写好的脚本直接传过去
scp /root/scripts/flannel.sh 192.168.1.76:/root/scripts/
scp /root/scripts/flannel.sh 192.168.1.77:/root/scripts/

三台node节点上执行脚本:
sh /root/scripts/flannel.sh

查看node节点上网络:
在这里插入图片描述
在这里插入图片描述
可以看到node节点上的docker0和flannel网络的ip在同一个网段,在node01节点上ping节点node02上的docker0的ip是通,反之亦然:
在这里插入图片描述
在这里插入图片描述
注意:
如果开启了防火墙,建议每台机器上都放行内网网段访问:
iptables -I INPUT -s 192.168.1.0/24 -j ACCEPT

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐