以下所有操作均在node节点进行
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 |
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
[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"}}'
[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/
[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的虚拟网卡
[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
node1→node2
node2→node1
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。