1、安裝etcd
yum install etcd systemctl stop etcd systemctl start etcd systemctl status etcd
systemctl enable etcd ##開機自啟動
查看etcd是否啟動成功,etcd默認端口為2379
$ lsof -i:2379
測試etcd可用性
$ etcd --version
$ etcdctl mkdir /test
$ etcdctl ls /
2、安裝flannel
$ wget https://github.com/coreos/flannel/releases/download/v0.5.5/flannel-0.5.5-linux-amd64.tar.gz
$ tar xvf flannel-0.5.5-linux-amd64.tar.gz
$ cd flannel-0.5.5
$ mv flanneld /usr/bin
$ mv mk-docker-opts.sh /usr/bin
啟動flannel方法一:
$ flanneld &
查看flannel是否啟動
$ ps -ef|grep flannel
啟動flannel方法二(推薦):添加到systemd服務中
a、創建配置/etc/flannel/flanneld.conf
$ mkdir -p /etc/flannel/
$ vim /etc/flannel/flanneld.conf
# Flanneld configuration options
# etcd url location. Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="-etcd-endpoints=http://localhost:2379"
# etcd集群配置
FLANNEL_ETCD_ENDPOINTS="-etcd-endpoints=http://172.16.0.6:2379,http://172.16.0.7:2379"
# etcd config key. This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="-etcd-prefix=/coreos.com/network"
# Any additional options that you want to pass
#FLANNEL_OPTIONS=""
創建flanneld服務文件/usr/lib/systemd/system/flanneld.service
$ vim /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
After=network-online.target
Wants=network-online.target
After=etcd.service
Before=docker.service
[Service]
Type=notify
EnvironmentFile=-/etc/flannel/flanneld.conf
ExecStart=/usr/bin/flanneld $FLANNEL_ETCD_ENDPOINTS $FLANNEL_ETCD_PREFIX $FLANNEL_OPTIONS
Restart=on-failure
[Install]
WantedBy=multi-user.target
RequiredBy=docker.service
啟動flanneld
$ systemctl daemon-reload
$ systemctl start flanneld
$ systemctl restart flanneld
$ systemctl enable flanneld ##開機自啟動
查看啟動日志:
$ systemctl status flanneld -l
發現報錯了:Couldn't fetch network config: 100: Key not found
想起來etcd里還沒寫入網絡配置信息呢;
flanneld網絡配置
docker默認用172.17.0.0/16這個網段,我想換個網段,以明確知道是在使用flanneld提供的網絡,於是就用了172.10.0.0/16。
$ etcdctl mk /coreos.com/network/config '{"Network":"172.10.0.0/16", "SubnetLen": 24, "SubnetMin": "172.10.1.0", "SubnetMax": "172.10.254.0", "Backend": {"Type": "vxlan"}}' $ etcdctl get /coreos.com/network/config
{"Network":"172.10.0.0/16", "SubnetLen": 24, "SubnetMin": "172.10.1.0", "SubnetMax": "172.10.254.0", "Backend": {"Type": "vxlan"}}
因為flannel一直在嘗試從etcd讀配置信息,所以不需要重啟flanneld,再看啟動日志,flanneld已經獲取到想要的子網地址了;
此時,在/run/flannel/subnet.env位置會生成這個host所分配到的子網信息:
FLANNEL_NETWORK=172.10.0.0/16 FLANNEL_SUBNET=172.10.60.1/24 FLANNEL_MTU=1450 FLANNEL_IPMASQ=false
正常情況下,執行ifconfig或ip a可以看到flannel0橋接網卡了,etcd上執行etcdctl ls /coreos.com/network/subnets也能夠看到flannel申請的網段。
而這個文件正是mk-docker-opts.sh腳本的輸入文件:
$ /usr/bin/mk-docker-opts.sh -c
$ cat /run/docker_opts.env
DOCKER_OPTS=" --bip=172.10.60.1/24 --ip-masq=true --mtu=1450 "
這時看一下etcd,多了一條信息:
$ etcdctl get /coreos.com/network/subnets/172.10.60.0-24
{"PublicIP":"172.16.92.255","BackendType":"vxlan","BackendData":{"VtepMAC":"1e:36:0a:72:6b:c7"}}
看一下主機網卡
$ ip a |grep flannel
2036: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
inet 172.10.60.0/16 scope global flannel.1
看一下網卡詳情
$ ip -d link show dev flannel.1
2036: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN mode DEFAULT group default
link/ether 1e:36:0a:72:6b:c7 brd ff:ff:ff:ff:ff:ff promiscuity 0
vxlan id 1 local 172.16.92.255 dev eth0 srcport 0 0 dstport 8472 nolearning ageing 300 noudpcsum noudp6zerocsumtx noudp6zerocsumrx addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
3、Docker使用flannel網絡
再重新生成一次docker_opts.env:
# /usr/bin/mk-docker-opts.sh -c # cat /run/docker_opts.env DOCKER_OPTS=" --bip=172.10.60.1/24 --ip-masq=true --mtu=1450 "
修改vim /lib/systemd/system/docker.service:
EnvironmentFile=/run/docker_opts.env # 在ExecStart參數之前新增這句話 ExecStart=/usr/bin/dockerd $DOCKER_OPTS #在ExecStart末尾添加$DOCKER_OPTS,$DOCKER_OPTS添加網橋的參數
重啟docker服務:
# systemctl daemon-reload
# systemctl restart docker
查看bridge網絡的信息:
# docker network ls |grep bridge
8d4e33066eb0 bridge bridge local
db61b72ccaa7 metabase_default bridge local
# docker network inspect 8d4e33066eb0 | grep Subnet
"Subnet": "172.18.0.0/16"
這就已經是在flannel提供的網段內了;
測試
然后在另一台主機上也照做一遍,分別啟動一個container,互ping一下,測試通過。
docker run -itd --name mycentos docker.io/centos /bin/bash
4、flannel生成的vxlan設備的刪除方法
停止flanneld服務並確認
# systemctl stop flanneld
# status flanneld
使用ifconfig將設備停止
# ip addr s flannel.1
# ifconfig flannel.1 down
使用ip link del刪除vxlan設備
# ip link del flannel.1
# ip addr s flannel.1
==========================================================================
溫馨提示:
如上面操作后,發現各容器內分配的ip之間相互ping不通,基本就是由於防火牆問題引起的!
可是明明已經在前面部署的時候,通過"systemctl stop firewalld.service"關閉了防火牆,為什么還有防火牆問題??
這是因為linux還有底層的iptables,所以解決辦法是在各節點上執行下面操作:
$ systemctl stop firewalld.service ##只關閉一次防火牆
$ systemctl disable firewalld.service ##永久關閉一次防火牆
[root@node-1 ~]# iptables -P INPUT ACCEPT [root@node-1 ~]# iptables -P FORWARD ACCEPT [root@node-1 ~]# iptables -F
執行上面操作后,基本各容器間就能相互ping通了。
docker通過Flannel可以實現各容器間的相互通信,即宿主機和容器,容器和容器之間都能相互通信。
如果服務器重啟,這時docker是基於flannel網絡的,docker自啟動會失敗,此時我們可以把etcd、flannel設置自啟動,然后執行如下命令進行docker重啟:
/usr/bin/mk-docker-opts.sh -c systemctl daemon-reload systemctl restart docker