Flannel工作原理
Flannel是一种覆盖网络(overlay network),将TCP数据包装在另一种网络包里面进行路由转发和通讯,转发方式:UDP 、VxLAN 、GCE、AWS VPC等。
专门为kubernetes定制的三层网络解决方案,用于解决容器的跨主机通讯。
Flannel通过ETCD维护一张节点间路由表,详细记录了各节点子网网段。
FLannel配置
1.添加flannel网络配置信息到etcd
#/opt/kubernetes/bin/etcdctl --ca-file /opt/kubernetes/ssl/ca.pem --cert-file /opt/kubernetes/ssl/flanneld.pem --key-file /opt/kubernetes/ssl/flanneld-key.pem --no-sync -C https://192.168.56.11:2379,https://192.168.56.14:2379 mk /kubernetes/network/config '{"Network": "10.3.0.0/16","Backend":{"Type": "vxlan", "VNI": 1}}'
2.安装flannel
wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
tar zxf flannel-v0.10.0-linux-amd64.tar.gz
cp flanneld mk-docker-opts.sh /opt/kubernetes/bin/
3.配置flannel
cat /opt/kubernetes/cfg/flannel
FLANNEL_ETCD="-etcd-endpoints=https://192.168.56.11:2379,https://192.168.56.14:2379"FLANNEL_ETCD_KEY="-etcd-prefix=/kubernetes/network"FLANNEL_ETCD_CAFILE="--etcd-cafile=/opt/kubernetes/ssl/ca.pem"FLANNEL_ETCD_CERTFILE="--etcd-certfile=/opt/kubernetes/ssl/flanneld.pem"FLANNEL_ETCD_KEYFILE="--etcd-keyfile=/opt/kubernetes/ssl/flanneld-key.pem"cat /usr/lib/systemd/system/flannel.service [Unit]Description=Flanneld overlay address etcd agentAfter=network.targetBefore=docker.service[Service]EnvironmentFile=-/opt/kubernetes/cfg/flannel#ExecStartPre=/opt/kubernetes/bin/remove-docker0.shExecStart=/opt/kubernetes/bin/flanneld ${FLANNEL_ETCD} ${FLANNEL_ETCD_KEY} ${FLANNEL_ETCD_CAFILE} ${FLANNEL_ETCD_CERTFILE} ${FLANNEL_ETCD_KEYFILE}ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -d /run/flannel/dockerType=notify[Install]WantedBy=multi-user.targetRequiredBy=docker.service
4.配置docker使用flannel
vim /usr/lib/systemd/system/docker.service[Unit] #在Unit下面修改After和增加RequiresAfter=network-online.target firewalld.service flannel.serviceWants=network-online.targetRequires=flannel.service[Service] #增加EnvironmentFile=-/run/flannel/dockerType=notifyEnvironmentFile=-/run/flannel/dockerExecStart=/usr/bin/dockerd $DOCKER_OPTS
5.启动服务
启动flannel之后,重启docker
systemctl daemon-reload
systemctl start flannel
systemctl restart flannel
ifconfig查看ip,docker0和flannel.1在同一网段即可。