1 准备环境
确保在集群环境中安装了etcd,在各node中安装了docker
2 安装flannel
2.1 flannel节点IP地址段分配
flannel依赖etcd分配不冲突的集群IP,首先在etcd上设置flannel节点使用的IP地址段,本文的etcd节点的ip为192.168.100.20
etcdctl --endpoints "http://192.168.100.20:2379" set /coreos.com/network/config '{"NetWork":"172.17.0.0/16","SubnetMin":"172.17.0.0","SubnetMax":"172.17.20.0","Backend":{"Type":"vxlan"}}'
2.2 minion节点安装flannel
下载flannel软件包
wget https://github.com/coreos/flannel/releases/download/v0.9.0/flannel-v0.9.0-linux-amd64.tar.gz
解压并将解压出的flanneld,mk-docker-opts.sh复制到/usr/local/bin下
cp flanneld,mk-docker-opts.sh /usr/local/bin
2.3 创建flannel的systemd服务
cat /lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld
Documentation=https://github.com/coreos/flannel
After=network.target
Before=docker.service
[Service]
ExecStart=/usr/local/bin/flanneld --etcd-endpoints=http://192.168.100.20:2379 --iface=enp0s3 --ip-masq=true
Restart=on-failure
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
启动flannel服务
systemctl start flanneld.service
flannel服务启动时主要做了以下几步的工作:
- 从etcd中获取network的配置信息。
- 划分subnet,并在etcd中进行注册。
- 将子网信息记录到/run/flannel/subnet.env中
cat /run/flannel/subnet.env
FLANNEL_NETWORK=172.17.0.0/16
FLANNEL_SUBNET=172.17.2.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
3 配置docker启动参数
使用flannel提供的脚本将subnet.env转写成Docker启动参数,创建好的启动参数位于/run/docker_opts.env文件中
root@wangpei-VM:~# sh /usr/local/bin/mk-docker-opts.sh -d /run/docker_opts.env -c
root@wangpei-VM:~# cat /run/docker_opts.env
DOCKER_OPTS=" --bip=172.17.2.1/24 --ip-masq=false --mtu=1450"
将这条语句写入 /etc/default/docker,作为docker启动的环境变量
cat /etc/default/docker |grep DOCKER_OPTS
DOCKER_OPTS=" --bip=172.17.2.1/24 --ip-masq=false --mtu=1450"
docker中的启动参数
cat /lib/systemd/system/docker.service |egrep 'EnvironmentFile|ExecStart'
EnvironmentFile=-/etc/default/docker
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
加载配置,启动docker
root@wangpei-VM:~# systemctl daemon-reload
root@wangpei-VM:~# systemctl restart docker
为了方便,在flanneld启动后直接执行mk-docker-opts.sh脚本,docker启动脚本中加入
EnvironmentFile=-/run/docker_opts.env
改后的flanneld.service 和docker.service如下
cat /lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld
Documentation=https://github.com/coreos/flannel
After=network.target
Before=docker.service
[Service]
ExecStart=/usr/local/bin/flanneld --etcd-endpoints=http://192.168.100.20:2379 --iface=enp0s3 --ip-masq=true
ExecStartPost=/usr/local/bin/mk-docker-opts.sh -d /run/docker_opts.env -c
Restart=on-failure
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
grep -v "^#" /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket
Requires=docker.socket
[Service]
Type=notify
EnvironmentFile=-/etc/default/docker
EnvironmentFile=-/run/docker_opts.env
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
现在启动flaneld和docker就会自动分配好ip