docker_flannel

目录

一.安装etcd

ETCD

安装

选择一个虚拟机,运行以下脚本

ETCD_VER=v2.3.7
DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download
curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
mkdir -p /tmp/test-etcd && tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/test-etcd --strip-components=1
cp /tmp/test-etcd/etcd* /usr/local/bin/

运行

该脚本从 github 上下载 etcd 的可执行文件并保存到 /usr/local/bin/

启动 etcd 并打开 2379 监听端口。

etcd -listen-client-urls http://192.168.0.104:2379 -advertise-client-urls http://192.168.0.104:2379

验证

测试 etcd 是否可用:

etcdctl --endpoints=192.168.0.104:2379 set foo "bar"

etcdctl --endpoints=1192.168.0.104:2379 get foo

二.安装和配置 flannel

  1. 下载并重命名 image。

    docker pull cloudman6/kube-cross:v1.6.2-2

    docker tag cloudman6/kube-cross:v1.6.2-2 gcr.io/google_containers/kube-cross:v1.6.2-2

  2. 下载 flannel 源码。

    git clone https://github.com/coreos/flannel.git

  3. 开始构建。

    cd flannel

    make dist/flanneld-amd64

  4. 将 flanneld 执行文件拷贝到 host1 和 host2(可以先设置免密登录)。

    scp dist/flanneld-amd64 node1:/usr/local/bin/flanneld

    scp dist/flanneld-amd64 node2:/usr/local/bin/flanneld

将 flannel 网络的配置信息保存到 etcd

先将配置信息写到文件 flannel-config.json 中,内容为:

{
  "Network": "10.2.0.0/16",
  "SubnetLen": 24,
  "Backend": {
    "Type": "vxlan"
  }
}
  1. Network 定义该网络的 IP 池为 10.2.0.0/16
  2. SubnetLen 指定每个主机分配到的 subnet 大小为 24 位,即10.2.X.0/24
  3. Backendvxlan,即主机间通过 vxlan 通信,后面我们还会讨论host-gw

将配置存入 etcd:
etcdctl --endpoints=192.168.180.5:2379 set /docker-test/network/config < flannel-config.json

/docker-test/network/config 是此 etcd 数据项的 key,其 value 为 flannel-config.json 的内容。key 可以任意指定,这个 key 后面会作为 flanneld 的一个启动参数。执行 etcdctl get 确保设置成功。

ps关闭防火墙

启动 flannel

在 host1 和 host2 上执行如下命令:
flanneld -etcd-endpoints=http://192.168.180.5:2379 -iface=enp0s8 -etcd-prefix=/docker-test/network

-etcd-endpoints 指定 etcd url。

-iface 指定主机间数据传输使用的 interface。

-etcd-prefix 指定 etcd 存放 flannel 网络配置信息的 key。

node2上输出如下:

① enp0s8 被选作与外部主机通信的 interface。

② 识别 flannel 网络池 10.2.0.0/16。

③ 分配的 subnet 为 10.2.40.0/24。

flanneld 启动后,node2 内部网络会发生一些变化

一个新的 interface flannel.1 被创建,而且配置上 subnet 的第一个 IP 10.2.40.0。

node2 添加了一条路由:目的地址为 flannel 网络 10.2.0.0/24 的数据包都由 flannel.1 转发。

host1输出类似,主要区别是 host2 的 subnet 为 10.2.17.0/24:

三. Docker 中使用 flannel

配置 Docker 连接 flannel

1.编辑docker配置文件,配置--bip和--mtu

vim /lib/systemd/system/docker.service

其中这两个参数的值必须与 /run/flannel/subnet.env 中 FLANNEL_SUBNETFLANNEL_MTU 一致。

2.重启docker服务

root@node-1:~# systemctl daemon-reload
root@node-1:~# systemctl restart docker.service
root@node-1:~# ip r

Docker 会将 10.2.10.1 配置到 Linux bridge docker0 上,并添加 10.2.10.0/24 的路由

同样配置node2

容器连接到 flannel 网络

在 node1 中运行容器 bbox11:

docker run -itd --name bbox11 busybox

在 node2 中运行容器 bbox22:

docker run -itd --name bbox22 busybox

四.flannel 网络连通性

flannel 网络连通性

bbox11 能够 ping 到位于不同 subnet 的 bbox22,通过 traceroute 分析一下 bbox11 到 bbox22 的路径。

  1. bbox11 与 bbox22 不是一个 subnet,数据包发送给默认网关 10.2.10.1(docker0)。

  2. 根据 node1 的路由表(下图),数据包会发给 flannel.1。

  3. flannel.1 将数据包封装成 VxLAN,通过 enp0s8 发送给 node2。

  4. node2收到包解封装,发现数据包目的地址为 10.2.17.2,根据路由表(下图)将数据包发送给 flannel.1,并通过 docker0 到达 bbox22。

flannel 网络隔离

flannel 为每个主机分配了独立的 subnet,但 flannel.1 将这些 subnet 连接起来了,相互之间可以路由。本质上,flannel 将各主机上相互独立的 docker0 容器网络组成了一个互通的大网络,实现了容器跨主机通信。flannel 没有提供隔离。

flannel 与外网连通性

因为 flannel 网络利用的是默认的 bridge 网络,所以容器与外网的连通方式与 bridge 网络一样,即:

  1. 容器通过 docker0 NAT 访问外网
  2. 通过主机端口映射,外网可以访问容器

五.使用flannel host-gw backend

flannel 支持多种 backend,如 vxlan,host-gw

配置host-gw

1.与 vxlan 不同,host-gw 不会封装数据包,而是在主机的路由表中创建到其他主机 subnet 的路由条目,从而实现容器跨主机通信。要使用 host-gw 首先修改 flannel 的配置 flannel-config.json:

{

 "Network": "10.2.0.0/16",

 "SubnetLen": 24,

 "Backend": {

   "Type": "host-gw"

 }

}

2.etcd节点上ctrl +c 结束flannel进程,然后重新执行

  1. 重新设置监听etcd -listen-client-urls http://192.168.180.5:2379 -advertise-client-urls http://192.168.180.5:2379

    etcdctl --endpoints=192.168.180.5:2379 set /docker-test/network/config < flannel-config.json

4.重启node1,node2

5.分别执行flanneld -etcd-endpoints=http://192.168.180.5:2379 -iface=enp0s8 -etcd-prefix=/docker-test/network

6.etcd -listen-client-urls http://192.168.180.5:2379 -advertise-client-urls http://192.168.180.5:2379

① flanneld 检查到原先已分配的 subnet 10.2.51.0/24,重用之。

② flanneld 从 etcd 数据库中检索到node1的 subnet 10.2.10.0/24,但因为其 type=vxlan,立即忽略。

③ 两分钟后,再次发现 subnet 10.2.10.0/24,将其加到路由表中。这次没有忽略 subnet 的原因是此时我们在 host2 上重启了 flanneld,根据当前 etcd 的配置使用 host-gw backend。

查看node2的路由表,增加了一条到 10.2.10.0/24 的路由,网关为 node1 的 IP 192.168.180.3

node1 启动 flanneld 时会重用 subnet10.2.10.0/24 ,并将node2 的 subnet 10.2.51.0/24,添加到路由表中,网关为 node2的IP 192.168.180.4

从 /run/flannel/subnet.env 可以看到 host-gw 使用的 MTU 为 1500:

修改 docker 启动参数 --mtu=1500并重启 docker daemon。

host-gw 和 vxlan 简单比较。

  1. host-gw 把每个主机都配置成网关,主机知道其他主机的 subnet 和转发地址。vxlan 则在主机间建立隧道,不同主机的容器都在一个大的网段内(比如 10.2.0.0/16)。
  2. 虽然 vxlan 与 host-gw 使用不同的机制建立主机之间连接,但对于容器则无需任何改变,bbox1 仍然可以与 bbox2 通信。
  3. 由于 vxlan 需要对数据进行额外打包和拆包,性能会稍逊于 host-gw。

猜你喜欢

转载自www.cnblogs.com/mrwuzs/p/9648499.html
今日推荐