目录
1.新master节点的搭建
对master02进行初始化配置(192.168.88.31)
将master01的配置移植到master02
修改master02配置文件
2.负载均衡的部署
两台负载均衡器配置nginx
部署keepalived服务
所有node节点操作
总结
实验准备:
k8s集群master01:192.168.88.22 kube-apiserver kube-controller-manager kube-scheduler etcd
k8s集群master02:192.168.88.31
k8s集群node01:192.168.88.40 kubelet kube-proxy docker
k8s集群node02:192.168.88.13etcd集群节点1:192.168.88.22 etcd
etcd集群节点2:192.168.88.40
etcd集群节点3:192.168.88.13负载均衡
192.168.88.50 nginx/haproxy
192.168.88.51 nginx/haproxy
架构说明:
- node节点的kubelet只能对接一个master节点的apiserver,不可能同时对接多个master节点的apiserver。简而言之,node节只能有一个master来领导。
- kubelet和kube-proxy是通过kubelet.kubeconfig和kube-proxy.kubeconfig文件中的server参数进行对接 master节点的。
- 所以在多master节点的环境下,需要有nginx负载均衡器来进行调度,而且需要进行keepalived高可用的构建(主从两个节点) ,防止主节点宕机导致整个k8s集群的不可用。
1.新master节点的搭建
对master02进行初始化配置(192.168.88.31)
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X#关闭selinux
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config#关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab #根据规划设置主机名
hostnamectl set-hostname master02
su
#在master添加hosts(添加到整个k8s集群的主机上,保证其他主机均有该映射)
cat >> /etc/hosts << EOF
192.168.88.22 master01
192.168.88.31 master02
192.168.88.40 node01
192.168.88.13 node02
EOF#调整内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
#开启网桥模式,可将网桥的流量传递给iptables链
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
#关闭ipv6协议
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOFsysctl --system#时间同步
yum install ntpdate -y
ntpdate ntp.aliyun.com#将时间同步的操作添加到计划性任务,确保所有节点保证时间的同步
crontab -e
*/30 * * * * /usr/sbin/ntpdate ntp.aliyun.com
crontab -l
将master01的配置移植到master02
##------------ 1、 master01节点,拷贝文件到master02 -------------------------------#从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点scp -r etcd/ master02:`pwd`scp -r kubernetes/ master02:`pwd`scp /usr/lib/systemd/system/kube-* master02:/usr/lib/systemd/system/scp -r /root/.kube/ master02:/root/
修改master02配置文件
##----------- 2、 master02节点,修改配置文件并启动相关服务-------------------------#修改配置文件kube-apiserver中的IPvim /opt/kubernetes/cfg/kube-apiserverKUBE_APISERVER_OPTS="--logtostderr=true \ #输出日志,false表示标准错误不输出到屏幕,而是输出到日志中。true表示标准错误会输出到屏幕。--v=4 \ #日志级别--etcd-servers=https://192.168.88.22:2379,https://192.168.88.40:2379,https://192.168.88.13:2379 \ #etcd节点的IP通信地址--bind-address=192.168.88.31 \ #修改,当前绑定的内网IP监听的地址--secure-port=6443 \ #基于HPPTS开放端口--advertise-address=192.168.88.31 \ #修改,内网通告地址,让其他node节点地址通信......vim kube-controller-manager
vim kube-scheduler
#在 master02 节点上启动各服务并设置开机自启systemctl enable --now kube-apiserver.servicesystemctl enable --now kube-controller-manager.servicesystemctl enable --now kube-scheduler.service#将可执行文件,创建软链接ln -s /opt/kubernetes/bin/* /usr/local/bin/#查看node节点状态kubectl get nodeskubectl get nodes -o wide #-o=wide:输出额外信息;对于Pod,将输出Pod所在的Node名#此时在master02节点查到的node节点状态仅是从etcd查询到的信息,而此时node节点实际上并未与master02节点建立通信连接,因此需要使用一个VIP把node节点与master节点都关联起来
2.负载均衡的部署
负载均衡
192.168.88.50 nginx/haproxy
192.168.88.51 nginx/haproxy
两台负载均衡器配置nginx
#配置nginx的官方在线yum源,配置本地nginx的yum源
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOFyum install nginx -y#修改nginx配置文件,配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口vim /etc/nginx/nginx.confevents {worker_connections 1024;}#添加stream {log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';access_log /var/log/nginx/k8s-access.log main;upstream k8s-apiserver {server 192.168.88.22:6443; #master01server 192.168.88.31:6443; #master02}server {listen 6443;proxy_pass k8s-apiserver;}}http {......#检查配置文件语法nginx -t #启动nginx服务,查看已监听6443端口systemctl start nginxsystemctl enable nginxss -lntp|grep nginx
部署keepalived服务
#部署keepalived服务yum install keepalived -y#修改keepalived配置文件vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id nginx_master
}vrrp_script check_nginx {script "/etc/nginx/check_nginx.sh" #指定检测脚本的路径,并且该脚本充当心跳检测脚本
}vrrp_instance VI_1 {state MASTER #指定状态为master节点,109为BACKUP备用节点interface ens33virtual_router_id 51priority 100 #108优先级为100 109为90,优先级决定着主备的位置advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.88.250}track_script {check_nginx #追踪脚本的进程}
}
#创建nginx状态检查脚本
vim /etc/nginx/check_nginx.sh#!/bin/bash
killall -0 nginx &>/dev/null
if [ $? -ne 0 ];thensystemctl stop keepalived
fichmod +x /etc/nginx/check_nginx.sh #为脚本增加执行权限#两台主备调度器启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务)systemctl start keepalivedsystemctl enable keepalivedip addr #查看主节点的VIP是否生成
所有node节点操作
//修改node节点上的bootstrap.kubeconfig,kubelet.kubeconfig配置文件为VIP
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig
server: https://192.168.88.250:6443vim kubelet.kubeconfig
server: https://192.168.88.250:6443vim kube-proxy.kubeconfig
server: https://192.168.88.250:6443//重启kubelet和kube-proxy服务
systemctl restart kubelet.service
systemctl restart kube-proxy.service//在 lb01 上查看 nginx 和 node 、 master 节点的连接状态
netstat -natp | grep nginx
总结
部署多master高可用
- 负责master组件相关的二进制文件、证书、私钥、启动参数配置文件、kubeconfig集群引导配置文件和etcd的证书、私钥文件
- 修改 kube-apiserver kube-controller-manager kube-scheduler 启动参数配置文件里的监听地址和通告地址,再依次重启服务进程
- 部署 nginx/haproxy 负载均衡器和 keepalived 高可用
- 修改 kubelet kube-proxy kubectl 的kubeconfig集群引导配置文件里的server参数都指向keepalived的VIP地址,再重启 kubelet kube-proxy 服务进程
- 修改其它master节点上的 kube-controller-manager kube-scheduler 的kubeconfig集群引导配置文件里的server参数都指向各自本机的apiserver地址,再重启服务进程