flannel 是 CoreOS 開發的容器網絡解決方案。flannel 為每個 host 配置設定一個 subnet,容器從此 subnet 中配置設定 IP,這些 IP 可以在 host 間路由,容器間無需 NAT 和 port mapping 就可以跨主機通信。
每個 subnet 都是從一個更大的 IP 池中劃分的,flannel 會在每個主機上運作一個叫 flanneld 的 agent,其職責就是從池子中配置設定 subnet。為了在各個主機間共享資訊,flannel 用 etcd(與 consul 類似的 key-value 分布式資料庫)存放網絡配置、已配置設定的 subnet、host 的 IP 等資訊。
接下來我們就開始實踐 flannel。
本章實驗環境如圖所示:

etcd 部署在 192.168.56.101,host1 和 host2 上運作 flanneld,首先安裝配置 etcd。
在 192.168.56.101 上運作如下腳本:
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.56.101:2379 -advertise-client-urls http://192.168.56.101:2379
測試 etcd 是否可用:
etcdctl --endpoints=192.168.56.101:2379 set foo "bar"
etcdctl --endpoints=192.168.56.101:2379 get foo
可以正常在 etcd 中存取資料了。
接下來需要安裝和配置 flannel,這個工作略微複雜,我們下節完成。