菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
112
0

容器网络(八)安装配置 flannel【54】

原创
05/13 14:22
阅读数 15733

(十二)安装配置 flannel

上一节我们部署了 etcd,本节安装和配置 flannel。

(1)build flannel

​ flannel 没有现成的执行文件可用,必须自己 build,最可靠的方法是在 Docker 容器中 build。不过用于做 build 的 docker 镜像托管在 gcr.io,国内可能无法直接访问,为方便大家,我把它 mirror 到了 docker hub,构建步骤如下:

①下载并重命名 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

②下载 flannel 源码。

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

③开始构建。

cd flannel
apt-getp install make -y
make dist/flanneld-amd64

④将 flanneld 执行文件拷贝到 host1 和 host2。

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

scp dist/flanneld-amd64 10.0.0.22:/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=10.0.0.20:2379 set /docker-test/network/config < flannel-config.json

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

(2)启动 flannel

在 host1 和 host2 上执行如下命令:

flanneld -etcd-endpoints=http://10.0.0.20:2379 -iface=ens33 -etcd-prefix=/docker-test/network

root@host2:~# flanneld -etcd-endpoints=http://10.0.0.20:2379 -iface=ens33 -etcd-prefix=/docker-test/network
I1111 01:40:17.088705    4345 main.go:531] Using interface with name ens33 and address 10.0.0.22
I1111 01:40:17.088757    4345 main.go:548] Defaulting external address to interface address (10.0.0.22)
I1111 01:40:17.088814    4345 main.go:246] Created subnet manager: Etcd Local Manager with Previous Subnet: None
I1111 01:40:17.088835    4345 main.go:249] Installing signal handlers
I1111 01:40:17.093353    4345 main.go:390] Found network config - Backend type: vxlan
I1111 01:40:17.093427    4345 vxlan.go:123] VXLAN config: VNI=1 Port=0 GBP=false Learning=false DirectRouting=false
I1111 01:40:17.210075    4345 local_manager.go:234] Picking subnet in range 10.2.1.0 ... 10.2.255.0
I1111 01:40:17.221419    4345 local_manager.go:220] Allocated lease (10.2.2.0/24) to current node (10.0.0.22) 
I1111 01:40:17.222192    4345 main.go:313] Changing default FORWARD chain policy to ACCEPT
I1111 01:40:17.222320    4345 main.go:321] Wrote subnet file to /run/flannel/subnet.env
I1111 01:40:17.222347    4345 main.go:325] Running backend.
I1111 01:40:17.222589    4345 vxlan_network.go:60] watching for new subnet leases
I1111 01:40:17.226075    4345 iptables.go:150] Some iptables rules are missing; deleting and recreating rules
I1111 01:40:17.226119    4345 iptables.go:174] Deleting iptables rule: -s 10.2.0.0/16 -j ACCEPT
I1111 01:40:17.227113    4345 iptables.go:174] Deleting iptables rule: -d 10.2.0.0/16 -j ACCEPT
I1111 01:40:17.228307    4345 iptables.go:162] Adding iptables rule: -s 10.2.0.0/16 -j ACCEPT
I1111 01:40:17.232472    4345 iptables.go:162] Adding iptables rule: -d 10.2.0.0/16 -j ACCEPT
I1111 01:40:17.233198    4345 main.go:433] Waiting for 23h0m0.025590569s to renew lease


-etcd-endpoints 指定 etcd url。

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

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

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

② 识别 flannel 网络池 10.2.0.0/16。

③ 分配的 subnet 为 10.2.40.0/24。

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

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

ip addr show interface flannel.1
root@host1:~# ip addr show flannel.1 
13: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default 
    link/ether da:44:3b:16:ff:2e brd ff:ff:ff:ff:ff:ff
    inet 10.2.50.0/32 brd 10.2.50.0 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::d844:3bff:fe16:ff2e/64 scope link 
       valid_lft forever preferred_lft forever
root@host1:~# 

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

root@host1:~# ip route 
default via 10.0.0.254 dev ens33 proto static 
10.0.0.0/24 dev ens33 proto kernel scope link src 10.0.0.21 
10.2.2.0/24 via 10.2.2.0 dev flannel.1 onlink 
root@host1:~# 

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

发表评论

0/200
112 点赞
0 评论
收藏