kubernetes高可用集群的搭建
前面介绍过了k8s单master节点的安装部署
今天介绍一下k8s高可用集群搭建
环境准备:
vip :192.168.121.99 keeplive
master01:192.168.121.153 centos7
master02:192.168.121.154 centos7
master03:192.168.121.155 centos7
机器环境先准备三台干净的主机,跟着我前面介绍的k8s部署做前面k8s部署的文章做
我的前一篇文章已经介绍的很详细了,所以我这里就不介绍前期的环境配置了
跟着做到如下位置
下面这个也要安装
bash
yum install kubectl-1.26.3
然后前期的准备工作就差不多了
下面就是的k8s高可用集群和单机部署的区别
首先下载代理和高可用的工具
bash
yum install -y haproxy keepalived
然后编写haproxy的配置文件,记得修改自己的主机名
每个节点都一样的配置
bash
vim /etc/haproxy/haproxy.cfg
bash
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend kube-api
bind :16443
mode tcp
maxconn 50000
default_backend kube-master
backend kube-master
balance roundrobin
server master1 192.168.121.153:6443 weight 1 check inter 5s rise 2 fall 3
server master2 192.168.121.154:6443 weight 1 check inter 5s rise 2 fall 3
server master3 192.168.121.155:6443 weight 1 check inter 5s rise 2 fall 3
然后到keepalived的配置
bash
[root@node101 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id server153
vrrp_mcast_group4 224.5.5.5
script_user root
enable_script_security
}
vrrp_script chklive {
script /etc/keepalived/chkhaproxy.sh
interval 1
weight -3
timeout 5
}
vrrp_instance VI_1 {
nopreempt
state BACKUP
interface ens33
priority 100
virtual_router_id 51
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.121.99
}
track_script {
chklive
}
}
这里要记得修改两个地方,就是主机和虚拟VIP
然后就编写脚本监控haproxy的存活来实现vip的故障切换
bash
vim /etc/keepalived/chkhaproxy.sh
bash
#! /bin/bash
if ! netstat -nlpt | grep -w "16443" &> /dev/null
then
systemctl stop keepalived
fi
exit 0
然后记得赋予脚本执行权限
bash
chmod +x /etc/keepalived/chkhaproxy.sh
然后配置文件就配置好了
接下来启动haproxy和keepalived
bash
systemctl enable --now haproxy
systemctl enable --now keepalived
然后就查看启动情况
bash
systemctl status haproxy keepalived
都启动以后,看vip在三台机子中的某一台就可以了
然后就开始初始化集群,
随便在哪一台机子上都行,只在一台机子上操作就可以了
bash
kubeadm init --apiserver-advertise-address=192.168.121.153 --kubernetes-version=v1.26.3 --image-repository registry.aliyuncs.com/google_containers --cri-socket unix:///var/run/cri-dockerd.sock --service-cidr=10.1.0.0/16 --pod-network-cidr=10.2.0.0/16 --token-ttl=0 --control-plane-endpoint "192.168.121.99:16443" --upload-certs
和之前单节点集群的初始化有一点不同而已,原理都是差不多的
初始化完成以后就是这个样子的,然后我们去把另外两台主机也加进来
bash
kubeadm join 192.168.121.99:16443 --token 99wnxp.kulhlbpbzes315zl --discovery-token-ca-cert-hash sha256:037bfd86aa3f0f7dd011e49a4d0c167195116f44ee3f56bfa165ce569fa80df5 --control-plane --certificate-key cdf7e38d13711c37c032732843639f313ade8b1b041d63f4665679f8eef1dd82 --cri-socket unix:///var/run/cri-dockerd.sock
有几台master节点就加几台,都是一样的操作,当然也是得规划好的,不然vip分不到
然后每个节点都执行下面得命令将环境变量设置一下
bash
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
然后随便在一台master节点都可以查询节点情况
bash
[root@server153 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
server153 NotReady control-plane 11m v1.26.3
server154 NotReady control-plane 5m55s v1.26.3
server155 NotReady control-plane 2m25s v1.26.3
这样我们得k8s高可用集群就搭建好了
node节点加入得方式都是一样的,我的之前的博文介绍过了,网络配置也是,忘记了可以去看看
希望对大家有帮助