flannel 是 CoreOS 开发的容器网络解决方案。flannel 为每一个 host 分配一个 subnet,容器今后 subnet 中分配 IP,这些 IP 能够在 host 间路由,容器间无需 NAT 和 port mapping 就能够跨主机通讯。linux
每一个 subnet 都是从一个更大的 IP 池中划分的,flannel 会在每一个主机上运行一个叫 flanneld 的 agent,其职责就是从池子 中分配 subnet。为了在各个主机间共享信息,flannel 用 etcd(与 consul 相似的 key-value 分布式数据库)存放网络配置、已分配的 subnet、host 的 IP 等信息。git
数据包如何在主机间转发是由 backend 实现的。flannel 提供了多种 backend,最经常使用的有 vxlan 和 host-gw,咱们将在本章讨论这两种 backend。其余 backend 请参考 https://github.com/coreos/flannel。接下来咱们就开始实践 flannel。github
本章实验环境如图所示:数据库
flannel<etcd>flannelubuntu
etcd 部署在 10.0.0.20,host1 和 host2 上运行 flanneld,首先安装配置 etcd。网络
在 10.0.0.20上运行以下脚本:app
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 监听端口。curl
etcd -listen-client-urls http://10.0.0.20:2379 -advertise-client-urls http://10.0.0.20:2379
测试 etcd 是否可用:分布式
root@ubuntu-01:/tmp/test-etcd# etcdctl --endpoints=10.0.0.20:2379 set foo "bar" bar root@ubuntu-01:/tmp/test-etcd# etcdctl --endpoints=10.0.0.20:2379 get foo bar root@ubuntu-01:/tmp/test-etcd#