目标
实现 2 台 docker 主机中的容器与容器之间、容器与主机之间的网络互通
资源准备
- 准备两台已经安装好 docker 的 Linux 服务器(docker 安装非常的简单,这里就不多介绍了):
- 192.168.9.1
- 192.168.9.2
- 下载 flannel-v0.8.0 ,这里我们是通过 CoreOS 的 flannel 解决网络通讯问题
- 准备一个 etcd 集群用于解决 ip 分配的唯一问题,查看集群准备方案
服务器初始化:
2 台服务器分别执行:mkdir -p /opt/flannel/{bin,conf}
开始实施
- 解压 flannel-v0.8.0-linux-amd64.tar.gz
将 flanneld 与 mk-docker-opts.sh 复制到 /opt/flannel/bin 目录
- 在 etcd 中申请 ip 段
在拥有 etcdctl 工具的机器上执行如下命令,这里的 backend 选用 vxlan,没有使用默认的 UDP
etcdctl --endpoints "http://你的etcd集群ip:2379" set /xieyanke/network/config '{"NetWork":"10.0.0.0/16", "SubnetMin": "10.0.1.0", "SubnetMax": "10.0.10.0", "Backend": {"Type": "vxlan"}}'
- 编辑 flanneld 的配置文件
vi /opt/flannel/conf/flanneld.conf
ETCD_ENDPOINTS="http://你的etcd1集群IP:2379,http://你的etcd2集群IP:2379,http://你的etcd3集群IP:2379"
ETCD_PREFIX="/xieyanke/network"
- 为了方便管理,这里配置一个 flanneld 的 systemd
vi /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld
Documentation=https://github.com/coreos/flannel
After=network.target
Before=docker.service
[Service]
EnvironmentFile=/opt/flannel/conf/flanneld.conf
User=root
ExecStart=/opt/flannel/bin/flanneld \
--etcd-endpoints=${ETCD_ENDPOINTS} \
--etcd-prefix=${ETCD_PREFIX} \
--iface=你要绑定网卡的IP \
--ip-masq
Restart=on-failure
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
- 启动 flanneld
systemctl daemon-reload
systemctl start flanneld
- 通过 mk-docker-opts.sh 创建 docker 启动参数
执行:/opt/flannel/bin/mk-docker-opts.sh -i
生成的参数会在 /run/docker_opts.env 中记录
- systemd 中添加 docker 的启动参数
这里主要添加的是:
EnvironmentFile=/run/docker_opts.env
ExecStart=/usr/bin/dockerd
$DOCKER_OPT_BIP
$DOCKER_OPT_IPMASQ
$DOCKER_OPT_MTU
vi /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
EnvironmentFile=/run/docker_opts.env
ExecStart=/usr/bin/dockerd \
$DOCKER_OPT_BIP \
$DOCKER_OPT_IPMASQ \
$DOCKER_OPT_MTU
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
- 重启 docker
systemctl daemon-reload
systemctl restart docker
- 查看 docker 与 flannel 的虚拟网卡是否在同一网段,如果在同一个网段则表明配置成功
ifconfig docker0
ifconfig flannel.1
实际测试
分别在 2 台docker 主机上创建 2 个容器
192.168.9.1# docker run -it busybox sh
# 查看容器IP
/ # cat /etc/hosts
10.0.2.2 44ceb6a97c90
192.168.9.2# docker run -it busybox sh
# 查看容器IP
/ # cat /etc/hosts
10.0.3.2 f914961f8d3c
分别在容器 10.0.2.2 与 10.0.3.2 中执行 ping 192.168.9.1、192.168.9.2、10.0.2.2、10.0.3.2,验证网络是否互通,如果没有 ping 成功,则表明主机防火墙开了,手工关闭一下防火墙即可