安装说明:
curl -L -O https://github.com/flannel-io/flannel/releases/download/v0.18.0/flannel-v0.18.0-linux-amd64.tar.gz
mkdir flannel
tar -xzvf flannel-v0.18.0-linux-amd64.tar.gz -C flannel
mv ./flannel/flanneld /opt/bin/
chmod -R o+x /opt/bin/flanneld
Make sure a flanneld binary exists at /opt/bin/flanneld on each node
#kubectl启动网络
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
#设置网络 添加flannel网络配置信息到etcd
etcdctl --endpoints "http://127.0.0.1:2379" put /coreos.com/network/config '{"Network": "10.0.0.0/16", "SubnetLen": 24, "SubnetMin": "10.0.1.0","SubnetMax": "10.0.20.0", "Backend": {"Type": "vxlan"}}'
Network:用于指定Flannel地址池
SubnetLen:用于指定分配给单个宿主机的docker0的ip段的子网掩码的长度
SubnetMin:用于指定最小能够分配的ip段
SudbnetMax:用于指定最大能够分配的ip段,在上面的示例中,表示每个宿主机可以分配一个24位掩码长度的子网,可以分配的子网从10.0.1.0/24到10.0.20.0/24,也就意味着在这个网段中,最多只能有20台宿主机
Backend:用于指定数据包以什么方式转发,默认为udp模式,host-gw模式性能最好,但不能跨宿主机网络
#配置backend为host-gw
etcdctl --endpoints http://127.0.0.1:2379 set /coreos.com/network/config '{"Network": "10.0.0.0/16", "SubnetLen": 24, "SubnetMin": "10.0.1.0","SubnetMax": "10.0.20.0", "Backend": {"Type": "host-gw"}}'
host-gw bakcend是flannel的另一个backend。与vxlan不同,host-gw不会封装数据包
#启动Flannel服务
#配置服务vim /etc/systemd/system/flanneld.service
systemctl daemon-reload
systemctl start flanneld
systemctl enable flanneld
systemctl status flanneld #journalctl -xe
ps -ef |grep flanneld
#验证Flannel网络
etcdctl get /coreos.com/network/subnets
查看docker1的flannel网卡信息:
ip a #可以看到flannel0网卡的地址和etcd存储的地址一样
cat /run/flannel/subnet.env
cat /run/docker_opts.env
可以在宿主机上查看到路由条目
ip route
cat /etc/systemd/system/flanneld.service
[Unit]
Description=Flanneld
Documentation=https://github.com/coreos/flannel
After=network.target
Before=docker.service
[Service]
User=root
ExecStartPost=/opt/flannel/mk-docker-opts.sh
ExecStart=/opt/flannel/flanneld \
--etcd-endpoints="http://10.161.137.56:2379" \
--iface=10.161.137.56 \
--ip-masq=true \
--etcd-prefix=/coreos.com/network
Restart=on-failure
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
命令行启动flannel测试
/opt/flannel/flanneld --etcd-endpoints="http://172.17.214.105:2379" --ip-masq=true >> /var/log/flanneld.log 2>&1 &
#命令行启动flannel2
/opt/flannel/flanneld -etcd-endpoints http://172.17.214.105:2379 -etcd-prefix /snbcloud/network1 &
/opt/flannel/mk-docker-opt.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker