Dannyvon 2020-02-22
Flannel是一种基于overlay网络的跨主机容器网络解决方案,也就是将TCP数据包封装在另一种网络包里面进行路由转发和通信,Flannel是CoreOS开发,专门用于docker多机互联的一个工具,让集群中的不同节点主机创建的容器都具有全集群唯一的虚拟ip地址,Flannel使用go语言编写
1、Flannel为每个host分配一个subnet,容器从这个subnet中分配IP,这些IP可以在host间路由,容器间无需使用nat和端口映射即可实现跨主机通信 2、每个subnet都是从一个更大的IP池中划分的,flannel会在每个主机上运行一个叫flanneld的agent,其职责就是从池子中分配subnet 3、Flannel使用etcd存放网络配置、已分配 的subnet、host的IP等信息 4、Flannel数据包在主机间转发是由backend实现的,目前已经支持UDP、VxLAN、host-gw、AWS VPC和GCE路由等多种backend
1、容器直接使用目标容器的ip访问,默认通过容器内部的eth0发送出去。 2、报文通过veth pair被发送到vethXXX。 3、ethXXX是直接连接到虚拟交换机docker0的,报文通过虚拟bridge docker0发送出去。 4、查找路由表,外部容器ip的报文都会转发到flannel0虚拟网卡,这是一个P2P的虚拟网卡,然后报文就被转发到监听在另一端的flanneld。 5、flanneld通过etcd维护了各个节点之间的路由表,把原来的报文UDP封装一层,通过配置的iface发送出去。 6、报文通过主机之间的网络找到目标主机。 7、报文继续往上,到传输层,交给监听在8285端口的flanneld程序处理。 8、数据被解包,然后发送给flannel0虚拟网卡。 9、查找路由表,发现对应容器的报文要交给docker0。 10、docker0找到连到自己的容器,把报文发送过去。
节点名称 | IP地址 | 安装软件 |
docker-01 | 192.168.1.220 | etcd |
docker-02 | 192.168.1.221 | etcd |
docker-03 | 192.168.1.222 | etcd |
[ ~]# yum -y install etcd
[ ~]# cp /usr/lib/systemd/system/etcd.service{,_bak} [ ~]# cat /etc/etcd/etcd.conf ETCD_NAME="docker-01" ETCD_DATA_DIR="/var/lib/etcd/docker-01.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.1.220:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.1.220:2379,http://127.0.0.1:2379" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.220:2380" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.220:2379" ETCD_INITIAL_CLUSTER="docker-01=http://192.168.1.220:2380,docker-02=http://192.168.1.221:2380,docker-03=http://192.168.1.222:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new"
[ ~]# cp /etc/etcd/etcd.conf{,_bak} [ ~]# cat /etc/etcd/etcd.conf ETCD_NAME="docker-02" ETCD_DATA_DIR="/var/lib/etcd/docker-02.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.1.221:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.1.221:2379,http://127.0.0.1:2379" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.221:2380" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.221:2379" ETCD_INITIAL_CLUSTER="docker-01=http://192.168.1.220:2380,docker-02=http://192.168.1.221:2380,docker-03=http://192.168.1.222:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new"
[ ~]# cp /etc/etcd/etcd.conf{,_bak} [ ~]# cat /etc/etcd/etcd.conf ETCD_NAME="docker-03" ETCD_DATA_DIR="/var/lib/etcd/docker-03.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.1.222:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.1.222:2379,http://127.0.0.1:2379" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.222:2380" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.222:2379" ETCD_INITIAL_CLUSTER="docker-01=http://192.168.1.220:2380,docker-02=http://192.168.1.221:2380,docker-03=http://192.168.1.222:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new"
[ ~]# cp /usr/lib/systemd/system/etcd.service{,_bak} [ ~]# cat /usr/lib/systemd/system/etcd.service [Service] Type=notify WorkingDirectory=/var/lib/etcd/ EnvironmentFile=-/etc/etcd/etcd.conf User=etcd # set GOMAXPROCS to number of processors ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\" --initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\"" Restart=on-failure LimitNOFILE=65536
[ ~]# systemctl start etcd.service [ ~]# etcdctl cluster-health member 164a311aff833bc1 is healthy: got healthy result from http://192.168.1.222:2379 member b1eeb25e6baf68e0 is healthy: got healthy result from http://192.168.1.221:2379 member e7c8f1a60e57abe4 is healthy: got healthy result from http://192.168.1.220:2379 cluster is healthy
# 查看cluster状态 [ ~]# etcdctl cluster-health member 164a311aff833bc1 is healthy: got healthy result from http://192.168.1.222:2379 member b1eeb25e6baf68e0 is healthy: got healthy result from http://192.168.1.221:2379 member e7c8f1a60e57abe4 is healthy: got healthy result from http://192.168.1.220:2379 cluster is healthy # 列出etcd服务状态,从列出信息可以看出,目前是docker-03为主节点。 [ ~]# etcdctl member list 164a311aff833bc1: name=docker-03 peerURLs=http://192.168.1.222:2380 clientURLs=http://192.168.1.222:2379 isLeader=false b1eeb25e6baf68e0: name=docker-02 peerURLs=http://192.168.1.221:2380 clientURLs=http://192.168.1.221:2379 isLeader=false e7c8f1a60e57abe4: name=docker-01 peerURLs=http://192.168.1.220:2380 clientURLs=http://192.168.1.220:2379 isLeader=true
【注释: 此(flannel_use)目录自己可以定义,但是此处设置的目录必须与flannel配置文件中FLANNEL_ETCD_PREFIX="/flannel_use/network"配置保持一致,flannel启动程序只认带“config”的key,否则会报错Not a directory (/flannel_use/network)】 # 固定配置方式 [ ~]# etcdctl set /flannel_use/network/config ‘{"Network":"10.10.0.0/16"}‘ {"Network":"10.10.0.0/16"}
[ ~]# yum install -y flannel