Kubernetes集群部署之六Flannel网络部署

时间:2022-09-16 21:52:43

1.为Flannel生成证书:

[root@k8s-master ~]# cd /usr/local/src/ssl/
[root@k8s-master ssl]# cat > flanneld-csr.json <<EOF
{
  "CN": "flanneld",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

EOF

2.生成证书

[root@k8s-master ssl]# cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
   -ca-key=/opt/kubernetes/ssl/ca-key.pem \
   -config=/opt/kubernetes/ssl/ca-config.json \
   -profile=kubernetes flanneld-csr.json | cfssljson -bare flanneld
   

3.分发证书文件

[root@k8s-master ssl]# cp flanneld*.pem /opt/kubernetes/ssl/
[root@k8s-master ssl]# scp flanneld*.pem 10.200.3.106:/opt/kubernetes/ssl/
[root@k8s-master ssl]# scp flanneld*.pem 10.200.3.107:/opt/kubernetes/ssl/

4.下载Flannel软件包

[root@k8s-master ssl]# cd /usr/local/src
[root@k8s-master src]# wget \
 https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
[root@k8s-master src]# tar zxf flannel-v0.10.0-linux-amd64.tar.gz
[root@k8s-master src]# cp flanneld mk-docker-opts.sh /opt/kubernetes/bin/

分发文件到node节点

[root@k8s-master src]# scp flanneld mk-docker-opts.sh 10.200.3.106:/opt/kubernetes/bin/
[root@k8s-master src]# scp flanneld mk-docker-opts.sh 10.200.3.107:/opt/kubernetes/bin/

分发对应脚本到/opt/kubernetes/bin目录下

[root@k8s-master src]# cd /usr/local/src/kubernetes/cluster/centos/node/bin/
[root@k8s-master bin]# cp remove-docker0.sh /opt/kubernetes/bin/
[root@k8s-master bin]# scp remove-docker0.sh 10.200.3.106:/opt/kubernetes/bin/
remove-docker0.sh                                                                                  100%  850     0.8KB/s   00:00    
[root@k8s-master bin]# scp remove-docker0.sh 10.200.3.107:/opt/kubernetes/bin/
remove-docker0.sh                                                                                  100%  850     0.8KB/s   00:00    
[root@k8s-master bin]# 

5.配置Flannel

[root@k8s-master ~]# cat > /opt/kubernetes/cfg/flannel  <<EOF
FLANNEL_ETCD="-etcd-endpoints=https://10.200.3.105:2379,https://10.200.3.106:2379,https://10.200.3.107:2379"
FLANNEL_ETCD_KEY="-etcd-prefix=/kubernetes/network"
FLANNEL_ETCD_CAFILE="--etcd-cafile=/opt/kubernetes/ssl/ca.pem"
FLANNEL_ETCD_CERTFILE="--etcd-certfile=/opt/kubernetes/ssl/flanneld.pem"
FLANNEL_ETCD_KEYFILE="--etcd-keyfile=/opt/kubernetes/ssl/flanneld-key.pem"

EOF

分发配置到其它节点上

[root@k8s-master ~]# scp /opt/kubernetes/cfg/flannel 10.200.3.106:/opt/kubernetes/cfg/
[root@k8s-master ~]# scp /opt/kubernetes/cfg/flannel 10.200.3.107:/opt/kubernetes/cfg/

6.设置Flannel系统服务

[root@k8s-master ~]#vim /usr/lib/systemd/system/flannel.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
Before=docker.service

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/flannel
ExecStartPre=/opt/kubernetes/bin/remove-docker0.sh
ExecStart=/opt/kubernetes/bin/flanneld ${FLANNEL_ETCD} ${FLANNEL_ETCD_KEY} ${FLANNEL_ETCD_CAFILE} ${FLANNEL_ETCD_CERTFILE} ${FLANNEL_ETCD_KEYFILE}
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -d /run/flannel/docker

Type=notify

[Install]
WantedBy=multi-user.target
RequiredBy=docker.service

复制系统服务脚本到其它节点上

[root@k8s-master ~]# scp /usr/lib/systemd/system/flannel.service 10.200.3.106:/usr/lib/systemd/system/
[root@k8s-master ~]# scp /usr/lib/systemd/system/flannel.service 10.200.3.107:/usr/lib/systemd/system/

7.Flannel CNI集成软件
下载CNI插件 

git地址:https://github.com/containernetworking/plugins/releases

[root@k8s-master ~]# cd /usr/local/src/

[root@k8s-master src]# wget \
 https://github.com/containernetworking/plugins/releases/download/v0.7.1/cni-plugins-amd64-v0.7.1.tgz

在所有节点创建该目录,存放CNI插件文件.

[root@k8s-master src]# mkdir /opt/kubernetes/bin/cni
[root@k8s-master src]# scp -r /opt/kubernetes/bin/cni/* 10.200.3.106:/opt/kubernetes/bin/cni/
[root@k8s-master src]# scp -r /opt/kubernetes/bin/cni/* 10.200.3.107:/opt/kubernetes/bin/cni/

8.在master节点创建Etcd的key

/opt/kubernetes/bin/etcdctl --ca-file /opt/kubernetes/ssl/ca.pem --cert-file /opt/kubernetes/ssl/flanneld.pem --key-file /opt/kubernetes/ssl/flanneld-key.pem \
      --no-sync -C https://10.200.3.105:2379,https://10.200.3.106:2379,https://10.200.3.107:2379 \
mk /kubernetes/network/config '{ "Network": "10.2.0.0/16", "Backend": { "Type": "vxlan", "VNI": 1 }}'

9.启动flannel

# systemctl daemon-reload
# systemctl enable flannel
# chmod +x /opt/kubernetes/bin/*
# systemctl start flannel
查看服务状态
# systemctl status flannel

10.配置Docker使用Flannel

1.在Unit段中的After后面添加flannel.service参数,在Wants下面添加Requires=flannel.service.
2.[Service]段中Type后面添加EnvironmentFile=-/run/flannel/docker段,在ExecStart后面添加$DOCKER_OPTS参数.

配置如下:

[root@k8s-master ~]# vim /usr/lib/systemd/system/docker.service 
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service flannel.service
Wants=network-online.target
Requires=flannel.service

[Service]
Type=notify
EnvironmentFile=-/run/flannel/docker
ExecStart=/usr/bin/dockerd $DOCKER_OPTS
...

将配置分发到另外两个节点中

[root@k8s-master ~]# rsync -av  /usr/lib/systemd/system/docker.service  10.200.3.106:/usr/lib/systemd/system/docker.service 
[root@k8s-master ~]# rsync -av  /usr/lib/systemd/system/docker.service  10.200.3.107:/usr/lib/systemd/system/docker.service

重启Docker服务

# systemctl daemon-reload
# systemctl restart docker

如果docker0和flannel在一个网段,则表示正常.

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.2.1.1  netmask 255.255.255.0  broadcast 10.2.1.255
        ether 02:42:47:f1:94:9e  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 10.2.1.0  netmask 255.255.255.255  broadcast 0.0.0.0
        ether a2:5d:b6:0a:89:88  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 1 overruns 0  carrier 0  collisions 0

至此flannel网络配置完成.