1. 程式人生 > 實用技巧 >容器網路(八)flannel 概述【53】

容器網路(八)flannel 概述【53】

(十一)flannel 概述

​ 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 等資訊。

​ 資料包如何在主機間轉發是由 backend 實現的。flannel 提供了多種 backend,最常用的有 vxlan 和 host-gw,我們將在本章討論這兩種 backend。其他 backend 請參考 https://github.com/coreos/flannel。接下來我們就開始實踐 flannel。

(1)實驗環境描述

本章實驗環境如圖所示:

flannel<etcd>flannel

etcd 部署在 10.0.0.20,host1 和 host2 上執行 flanneld,首先安裝配置 etcd。

(2)安裝配置 etcd

在 10.0.0.20上執行如下指令碼:

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://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#