第 8 章 容器網絡 - 058 - flannel 概述
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 請參考 https://github.com/coreos/flannel。)
flannel實驗環境描述如圖:
etcd 部署在 10.12.31.21上,host1(10.12.31.22) 和 host2(10.12.31.23) 上運行 flanneld,首先安裝配置 etcd。
1)安裝配置 etcd
1 [root@ubuntu ~]$cat etcd_install.sh 2 ETCD_VER=v2.3.73 DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download 4 curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz 5 mkdir -p /tmp/test-etcd && tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/test-etcd --strip-components=1 6 cp/tmp/test-etcd/etcd* /usr/local/bin/
1 [root@ubuntu ~]$sh etcd_install.sh 2 % Total % Received % Xferd Average Speed Time Time Time Current 3 Dload Upload Total Spent Left Speed 4 100 153 0 153 0 0 141 0 --:--:-- 0:00:01 --:--:-- 141 5 100 620 0 620 0 0 405 0 --:--:-- 0:00:01 --:--:-- 100k 6 100 8347k 100 8347k 0 0 222k 0 0:00:37 0:00:37 --:--:-- 250k 7 etcd-v2.3.7-linux-amd64/Documentation/ 8 etcd-v2.3.7-linux-amd64/Documentation/runtime-configuration.md 9 etcd-v2.3.7-linux-amd64/Documentation/admin_guide.md 10 etcd-v2.3.7-linux-amd64/Documentation/tuning.md 11 etcd-v2.3.7-linux-amd64/Documentation/glossary.md 12 etcd-v2.3.7-linux-amd64/Documentation/rfc/ 13 etcd-v2.3.7-linux-amd64/Documentation/rfc/v3api.md 14 etcd-v2.3.7-linux-amd64/Documentation/discovery_protocol.md 15 etcd-v2.3.7-linux-amd64/Documentation/errorcode.md 16 etcd-v2.3.7-linux-amd64/Documentation/metrics.md 17 etcd-v2.3.7-linux-amd64/Documentation/security.md 18 etcd-v2.3.7-linux-amd64/Documentation/configuration.md 19 etcd-v2.3.7-linux-amd64/Documentation/docker_guide.md 20 etcd-v2.3.7-linux-amd64/Documentation/dev/ 21 etcd-v2.3.7-linux-amd64/Documentation/dev/release.md 22 etcd-v2.3.7-linux-amd64/Documentation/members_api.md 23 etcd-v2.3.7-linux-amd64/Documentation/auth_api.md 24 etcd-v2.3.7-linux-amd64/Documentation/backward_compatibility.md 25 etcd-v2.3.7-linux-amd64/Documentation/platforms/ 26 etcd-v2.3.7-linux-amd64/Documentation/platforms/freebsd.md 27 etcd-v2.3.7-linux-amd64/Documentation/libraries-and-tools.md 28 etcd-v2.3.7-linux-amd64/Documentation/implementation-faq.md 29 etcd-v2.3.7-linux-amd64/Documentation/reporting_bugs.md 30 etcd-v2.3.7-linux-amd64/Documentation/upgrade_2_2.md 31 etcd-v2.3.7-linux-amd64/Documentation/internal-protocol-versioning.md 32 etcd-v2.3.7-linux-amd64/Documentation/upgrade_2_1.md 33 etcd-v2.3.7-linux-amd64/Documentation/faq.md 34 etcd-v2.3.7-linux-amd64/Documentation/api_v3.md 35 etcd-v2.3.7-linux-amd64/Documentation/runtime-reconf-design.md 36 etcd-v2.3.7-linux-amd64/Documentation/clustering.md 37 etcd-v2.3.7-linux-amd64/Documentation/proxy.md 38 etcd-v2.3.7-linux-amd64/Documentation/branch_management.md 39 etcd-v2.3.7-linux-amd64/Documentation/other_apis.md 40 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/ 41 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-2-2-0-rc-memory-benchmarks.md 42 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-3-demo-benchmarks.md 43 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-2-2-0-benchmarks.md 44 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-2-1-0-alpha-benchmarks.md 45 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-storage-memory-benchmark.md 46 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/README.md 47 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-2-2-0-rc-benchmarks.md 48 etcd-v2.3.7-linux-amd64/Documentation/benchmarks/etcd-3-watch-memory-benchmark.md 49 etcd-v2.3.7-linux-amd64/Documentation/api.md 50 etcd-v2.3.7-linux-amd64/Documentation/authentication.md 51 etcd-v2.3.7-linux-amd64/Documentation/04_to_2_snapshot_migration.md 52 etcd-v2.3.7-linux-amd64/Documentation/upgrade_2_3.md 53 etcd-v2.3.7-linux-amd64/Documentation/production-users.md 54 etcd-v2.3.7-linux-amd64/README-etcdctl.md 55 etcd-v2.3.7-linux-amd64/etcdctl 56 etcd-v2.3.7-linux-amd64/etcd 57 etcd-v2.3.7-linux-amd64/README.md 58 [root@ubuntu ~]$
該腳本從 github 上下載 etcd 的可執行文件並保存到 /usr/local/bin/,啟動 etcd 並打開 2379 監聽端口。
etcd -listen-client-urls http://10.12.31.21:2379 -advertise-client-urls http://10.12.31.21:2379
1 [root@ubuntu ~]$etcd -listen-client-urls http://10.12.31.21:2379 -advertise-client-urls http://10.12.31.21:2379 2 2019-03-27 23:22:33.110359 I | etcdmain: etcd Version: 2.3.7 3 2019-03-27 23:22:33.110422 I | etcdmain: Git SHA: fd17c91 4 2019-03-27 23:22:33.110430 I | etcdmain: Go Version: go1.6.2 5 2019-03-27 23:22:33.110437 I | etcdmain: Go OS/Arch: linux/amd64 6 2019-03-27 23:22:33.110444 I | etcdmain: setting maximum number of CPUs to 2, total number of available CPUs is 2 7 2019-03-27 23:22:33.110451 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd 8 2019-03-27 23:22:33.110705 I | etcdmain: listening for peers on http://localhost:2380 9 2019-03-27 23:22:33.110802 I | etcdmain: listening for peers on http://localhost:7001 10 2019-03-27 23:22:33.110840 I | etcdmain: listening for client requests on http://10.12.31.21:2379 11 2019-03-27 23:22:33.114237 I | etcdserver: name = default 12 2019-03-27 23:22:33.114254 I | etcdserver: data dir = default.etcd 13 2019-03-27 23:22:33.114259 I | etcdserver: member dir = default.etcd/member 14 2019-03-27 23:22:33.114264 I | etcdserver: heartbeat = 100ms 15 2019-03-27 23:22:33.114271 I | etcdserver: election = 1000ms 16 2019-03-27 23:22:33.114278 I | etcdserver: snapshot count = 10000 17 2019-03-27 23:22:33.114288 I | etcdserver: advertise client URLs = http://10.12.31.21:2379 18 2019-03-27 23:22:33.114297 I | etcdserver: initial advertise peer URLs = http://localhost:2380,http://localhost:7001 19 2019-03-27 23:22:33.114310 I | etcdserver: initial cluster = default=http://localhost:2380,default=http://localhost:7001 20 2019-03-27 23:22:33.141747 I | etcdserver: starting member ce2a822cea30bfca in cluster 7e27652122e8b2ae 21 2019-03-27 23:22:33.141853 I | raft: ce2a822cea30bfca became follower at term 0 22 2019-03-27 23:22:33.141900 I | raft: newRaft ce2a822cea30bfca [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0] 23 2019-03-27 23:22:33.141915 I | raft: ce2a822cea30bfca became follower at term 1 24 2019-03-27 23:22:33.142350 I | etcdserver: starting server... [version: 2.3.7, cluster version: to_be_decided] 25 2019-03-27 23:22:33.143380 N | etcdserver: added local member ce2a822cea30bfca [http://localhost:2380 http://localhost:7001] to cluster 7e27652122e8b2ae 26 2019-03-27 23:22:33.143968 E | etcdmain: failed to notify systemd for readiness: No socket 27 2019-03-27 23:22:33.144218 E | etcdmain: forgot to set Type=notify in systemd service file? 28 2019-03-27 23:22:33.542474 I | raft: ce2a822cea30bfca is starting a new election at term 1 29 2019-03-27 23:22:33.542972 I | raft: ce2a822cea30bfca became candidate at term 2 30 2019-03-27 23:22:33.543300 I | raft: ce2a822cea30bfca received vote from ce2a822cea30bfca at term 2 31 2019-03-27 23:22:33.543575 I | raft: ce2a822cea30bfca became leader at term 2 32 2019-03-27 23:22:33.543814 I | raft: raft.node: ce2a822cea30bfca elected leader ce2a822cea30bfca at term 2 33 2019-03-27 23:22:33.544664 I | etcdserver: published {Name:default ClientURLs:[http://10.12.31.21:2379]} to cluster 7e27652122e8b2ae 34 2019-03-27 23:22:33.544753 I | etcdserver: setting up the initial cluster version to 2.3 35 2019-03-27 23:22:33.557561 N | etcdserver: set the initial cluster version to 2.3
測試 etcd 是否可用:
etcdctl --endpoints=10.12.31.21:2379 set foo "bar"
etcdctl --endpoints=10.12.31.21:2379 get foo
可以正常在 etcd 中存取數據了。
-----------------------------引用來自--------------------------------------
https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587799&idx=1&sn=a26c6c4a9681997031cd346224d01822&chksm=8d30814eba47085870ac9695411bc1943ee054baeb6920094256215112f5817b7a3874388c89&scene=21#wechat_redirect
第 8 章 容器網絡 - 058 - flannel 概述