K8S单master部署二:Flannel网络部署
更新:HHH   时间:2023-1-7


以下所有操作均在node节点进行


Flannel网络概述

Flannel通过给每台宿主机分配一个子网的方式为容器提供虚拟网络,它基于Linux TUN/TAP,使用UDP/VXLAN封装IP包来创建overlay网络,并借助etcd(也支持kubernetes)维护网络的分配情况。


服务器角色分配

角色 地址 安装组件
master 192.168.142.220 kube-apiserver kube-controller-manager kube-scheduler etcd
node1 192.168.142.136 kubelet kube-proxy docker flannel etcd
node2 192.168.142.132 kubelet kube-proxy docker flannel etcd

Flannel网络部署(所有node节点)

安装docker容器

docker引擎的安装比较简单这里就不多讲解了,直接贴脚本刷即可。

脚本如下:

#!/bin/bash
#Docker引擎部署
#安装依赖包
yum install yum-utils device-mapper-persistent-data lvm2 -y

#设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#安装Docker-ce
yum install -y docker-ce

#关闭防火墙及增强型安全功能
systemctl stop firewalld.service
setenforce 0

#启动Docker并设置为开机自启动
systemctl start docker.service
systemctl enable docker.service

#镜像加速服务(每个人的阿里加速地址都不同,请自行前往阿里官网获取)
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://123456.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload

#网络优化
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
service network restart 
systemctl restart docker

#检验容器是否启动完成
docker images
if [ $? -eq 0 ];then
    echo "Docker启动完成"
else
    echo "ERROR"
    systemctl status docker
    exit 1
fi

为flannel网络分配子网段,并注入etcd数据库中

[root@node2 bin]# /opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--endpoints="https://192.168.142.220:2379,https://192.168.142.136:2379,https://192.168.142.132:2379" set /coreos.com/network/config '{"Network":"172.17.0.0/16","Backend":{"Type":"vxlan"}}'

解压安装flannel二进制软件包

[root@node2 ~]# tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
flanneld
mk-docker-opts.sh
README.md

//建立工作目录
[root@node2 ~]# mkdir -p /opt/kubernetes/{cfg,ssl,bin}
//移动至工作目录
[root@node2 ~]# mv flanneld mk-docker-opts.sh /opt/kubernetes/bin/

更改flannel配置文件

[root@node2 ~]# cat <<EOF >/opt/kubernetes/cfg/flanneld.cfg
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.142.220:2379,https://192.168.142.132:2379,https://192.168.142.136:2379 \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem 
EOF

编写启动脚本

[root@node2 ~]# 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.cfg
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

启动服务

[root@node2 bin]# chmod +x /usr/lib/systemd/system/flanneld.service
[root@node2 bin]# systemctl start flanneld.service
[root@node2 bin]# systemctl status flanneld.service
[root@node2 bin]# systemctl enable flanneld.service

这里启动后会生成一个flannel的虚拟网卡

设置docker容器连接上flannel网络

[root@node2 bin]# vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env              //添加这一行
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS(添加该变量,加入flannel网络中) -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

检查自身分配的子网段(不是必须的)

[root@node2 bin]# cat /run/flannel/subnet.env
DOCKER_OPT_BIP="--bip=172.17.34.1/24"     //给docker分配的网关
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=172.17.34.1/24 --ip-masq=false --mtu=1450"

重启容器使配置文件生效

[root@node2 bin]# systemctl daemon-reload
[root@node2 bin]# systemctl restart docker

检查flannel网络互通性

node1→node2

node2→node1

未完待续~~~

返回云计算教程...