[root@k8s-master src]# wget https://github.com/coreos/flannel/releases/download/v0.9.1/flannel-v0.9.1-linux-amd64.tar.gz

[root@k8s-master src]# tar xf flannel-v0.9.1-linux-amd64.tar.gz

[root@k8s-master src]# scp flanneld mk-docker-opts.sh k8s-node01:/opt/kubernetes/bin/
flanneld 100% 33MB 134.8MB/s 00:00
mk-docker-opts.sh 100% 2139 3.9MB/s 00:00
[root@k8s-master src]# scp flanneld mk-docker-opts.sh k8s-node02:/opt/kubernetes/bin/
flanneld 100% 33MB 140.6MB/s 00:00
mk-docker-opts.sh 100% 2139 76.1KB/s 00:00

############

配置Flannel之前,先用etcd给Flannel分配网段

[root@k8s-master ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.56.243:2379,https://192.168.56.244:2379,https://192.168.56.245:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@k8s-master ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.56.243:2379,https://192.168.56.244:2379,https://192.168.56.245:2379" get /coreos.com/network/config
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@k8s-master ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.56.243:2379,https://192.168.56.244:2379,https://192.168.56.245:2379" ls /coreos.com/network/subnets
/coreos.com/network/subnets/172.17.7.0-
/coreos.com/network/subnets/172.17.9.0-
[root@k8s-master ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.56.243:2379,https://192.168.56.244:2379,https://192.168.56.245:2379" get /coreos.com/network/subnets/172.17.7.0-
{"PublicIP":"192.168.56.245","BackendType":"vxlan","BackendData":{"VtepMAC":"d2:ba:51:66:61:9f"}}
[root@k8s-master ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.56.243:2379,https://192.168.56.244:2379,https://192.168.56.245:2379" get /coreos.com/network/subnets/172.17.9.0-
{"PublicIP":"192.168.56.244","BackendType":"vxlan","BackendData":{"VtepMAC":"d2:cd:86:e7:54:7c"}}

##############flannel配置脚本

#!/bin/bash

ETCD_ENDPOINTS=${:-"http://127.0.0.1:2379"}

cat <<EOF >/opt/kubernetes/cfg/flanneld

FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/kubernetes/ssl/ca.pem \
-etcd-certfile=/opt/kubernetes/ssl/server.pem \
-etcd-keyfile=/opt/kubernetes/ssl/server-key.pem" EOF cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service [Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure [Install]
WantedBy=multi-user.target EOF cat <<EOF >/usr/lib/systemd/system/docker.service [Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target [Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd \$DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP \$MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=
StartLimitInterval=60s [Install]
WantedBy=multi-user.target EOF systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
systemctl restart docker

把配置脚本发送到节点上:

[root@k8s-master src]# scp flannel.sh k8s-node01:/usr/local/src/
flannel.sh 100% 1461 2.8MB/s 00:00
[root@k8s-master src]# scp flannel.sh k8s-node02:/usr/local/src/

[root@k8s-node01 ssl]# cd /usr/local/src/
[root@k8s-node01 src]# ll
total
-rw-r--r-- root root May : flannel.sh
[root@k8s-node01 src]# sh flannel.sh
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
[root@k8s-node01 src]# systemctl start flanneld
[root@k8s-node01 src]# ip add
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc mq state UP group default qlen
link/ether ::::8f:f2 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.244/ brd 192.168.56.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 fe80:::56ff:fe98:8ff2/ scope link
valid_lft forever preferred_lft forever
: flannel.: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default
link/ether d2:cd::e7::7c brd ff:ff:ff:ff:ff:ff
inet 172.17.9.0/ scope global flannel.
valid_lft forever preferred_lft forever
inet6 fe80::d0cd:86ff:fee7:547c/ scope link
valid_lft forever preferred_lft forever
: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu qdisc noqueue state DOWN group default
link/ether ::8f:ae:d2: brd ff:ff:ff:ff:ff:ff
inet 172.17.9.1/ brd 172.17.9.255 scope global docker0
valid_lft forever preferred_lft forever
[root@k8s-node02 cfg]# cd /usr/local/src/
[root@k8s-node02 src]# sh flannel.sh
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
[root@k8s-node02 src]# ip add
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc mq state UP group default qlen
link/ether ::::a5: brd ff:ff:ff:ff:ff:ff
inet 192.168.56.245/ brd 192.168.56.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 fe80:::56ff:fe98:a502/ scope link
valid_lft forever preferred_lft forever
: flannel.: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default
link/ether d2:ba::::9f brd ff:ff:ff:ff:ff:ff
inet 172.17.7.0/ scope global flannel.
valid_lft forever preferred_lft forever
inet6 fe80::d0ba:51ff:fe66:619f/ scope link
valid_lft forever preferred_lft forever
: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu qdisc noqueue state DOWN group default
link/ether ::b1:6c:a6:c9 brd ff:ff:ff:ff:ff:ff
inet 172.17.7.1/ brd 172.17.7.255 scope global docker0
valid_lft forever preferred_lft forever
05-28 03:03