Kubernetes(五)部署Flannel网络

  • A+
所属分类:Kubernetes

以下操作在所有的node节点上均执行一次

写入分配的子网段到etcd,供flanneld使用

注意修改endpoint ip;172.17.0.0网络是flanneld内网,在node节点执行以下命令;

在一个etcd节点上执行就可以,会同步到其他etcd节点上。这里指定了flannel的网段为172.17网段,采用vxlan模式。

cd /opt/kubernetes/ssl
/opt/kubernetes/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379" \
set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'

master下载flannel软件

cd /root/tools
wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
tar zxvf flannel-v0.10.0-linux-amd64.tar.gz

flanneld和mk-docker-opts.sh脚本传输到node节点

scp -r flanneld mk-docker-opts.sh root@k8s-node01:/opt/kubernetes/bin/
scp -r flanneld mk-docker-opts.sh root@k8s-node02:/opt/kubernetes/bin/

参考课件flanneld.sh

node节点创建flanneld配置文件(主要包括endpoints和key)

[root@k8s-node01 cfg]# vim /opt/kubernetes/cfg/flanneld 
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379 \
-etcd-cafile=/opt/kubernetes/ssl/ca.pem \
-etcd-certfile=/opt/kubernetes/ssl/server.pem \
-etcd-keyfile=/opt/kubernetes/ssl/server-key.pem"

创建flanneld服务文件(注意可执行文件全放到opt)

[root@k8s-node01 cfg]# vim /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

启动flanneld,可以通过ifconfig 查看生成的虚拟网卡

systemctl start flanneld
ifconfig
[root@k8s-node01 ssl]# cat /run/flannel/subnet.env 
DOCKER_OPT_BIP="--bip=172.17.44.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=172.17.44.1/24 --ip-masq=false --mtu=1450"

参考flanneld.sh

Kubernetes(五)部署Flannel网络flanneld.zip

node节点创建flanneld配置文件(主要包括endpoints和key)

[root@k8s-node01 cfg]# vim /opt/kubernetes/cfg/flanneld 
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379 \
-etcd-cafile=/opt/kubernetes/ssl/ca.pem \
-etcd-certfile=/opt/kubernetes/ssl/server.pem \
-etcd-keyfile=/opt/kubernetes/ssl/server-key.pem"

创建flanneld服务文件(注意可执行文件全放到opt)

[root@k8s-node01 cfg]# vim /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

启动flanneld,可以通过ifconfig 查看生成的虚拟网卡

systemctl start flanneld
ifconfig
[root@k8s-node01 ssl]# cat /run/flannel/subnet.env 
DOCKER_OPT_BIP="--bip=172.17.44.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=172.17.44.1/24 --ip-masq=false --mtu=1450"

配置docker 服务

修改配置前,可以对比备份下配置;

cp /usr/lib/systemd/system/docker.service /root/bak/docker.service.bak
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=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

EOF

启动服务

systemctl daemon-reload
systemctl enable flanneld
systemctl list-unit-files|grep  flanneld
systemctl restart flanneld
systemctl restart docker

验证网络通讯

验证node节点的flannel网络是否联通,启动docker后,各节点相互ping flannel的ip和docker的ip;

node节点通过get可以获取网络地址,类型是vxlan

[root@k8s-node02 ssl]# /opt/kubernetes/bin/etcdctl \
> --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
> --endpoints="https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379" \
> get /coreos.com/network/config
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@k8s-node02 ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379" ls /coreos.com/network/subnets
/coreos.com/network/subnets/172.17.44.0-24
/coreos.com/network/subnets/172.17.5.0-24
[root@k8s-node02 ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.20.211:2379,https://192.168.20.212:2379,https://192.168.20.213:2379" get /coreos.com/network/subnets/172.17.44.0-24
{"PublicIP":"192.168.20.212","BackendType":"vxlan","BackendData":{"VtepMAC":"26:08:89:6d:cd:8a"}}

YaLei

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: