本文将完成多master集群的部署,即部署master02,以及nginx负载均衡、keepalived高可用
多master集群架构图:
架构说明:
-
node节点的kubelet只能对接一个master节点的apiserver,不可能同时对接多个master节点的apiserver。简而言之,node节只能有一个master来领导。
-
kubelet和kube-proxy是通过kubelet.kubeconfig和kube-proxy.kubeconfig文件中的server参数进行对接 master节点的。
-
所以在多master节点的环境下,需要有nginx负载均衡器来进行调度,而且需要进行keepalived高可用的构建(主从两个节点) ,防止主节点宕机导致整个k8s集群的不可用。
部署两台或者三台master都可以:
- 三台:多层保障、负载均衡(减轻每台master的压力)、选举机制选出leader。
- 两台:也可实现负载均衡,负载压力没那么大的情况下可选两台,同时考虑到成本。
六、部署master02 节点
master02:192.168.126.21
##------------ 1、 master01节点,拷贝文件到master02 -------------------------------#从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点
scp -r /opt/etcd/ root@192.168.126.21:/opt/
scp -r /opt/kubernetes/ root@192.168.126.21:/opt
scp /usr/lib/systemd/system/kube-* root@192.168.126.21:/usr/lib/systemd/system//
[root@master01 opt]# cd
[root@master01 ~]# scp -r .kube/ 192.168.126.21:/root##----------- 2、 master02节点,修改配置文件并启动相关服务-------------------------#修改配置文件kube-apiserver中的IPvim /opt/kubernetes/cfg/kube-apiserverKUBE_APISERVER_OPTS="--logtostderr=true \ #输出日志,false表示标准错误不输出到屏幕,而是输出到日志中。true表示标准错误会输出到屏幕。--v=4 \ #日志级别--etcd-servers=https://192.168.126.27:2379,https://192.168.126.28:2379,https://192.168.126.29:2379 \ #etcd节点的IP通信地址--bind-address=192.168.126.21 \ #修改,当前绑定的内网IP监听的地址--secure-port=6443 \ #基于HPPTS开放端口--advertise-address=192.168.126.21 \ #修改,内网通告地址,让其他node节点地址通信......#在 master02 节点上启动各服务并设置开机自启systemctl enable --now kube-apiserver.servicesystemctl enable --now kube-controller-manager.servicesystemctl enable --now kube-scheduler.service#查看服务状态是否开启systemctl status kube-apiserver kube-controller-manager kube-scheduler#将可执行文件,创建软链接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节点都关联起来
七、部署负载均衡
配置load balancer集群双机热备负载均衡(nginx实现负载均衡,keepalived实现双机热备)。
load balancer01:192.168.126.22
load balancer01:192.168.126.23
VIP:192.168.126.88
7.1 在lb01、lb02节点上配置nginx和keepalived
#配置load balancer集群双机热备负载均衡(nginx实现负载均衡,keepalived实现双机热备)#----------------- 1、两台负载均衡器配置nginx -------------------------------------systemctl disable --now firewalldsetenforce 0vim /etc/selinux/configSELINUX=disabled#配置nginx的官方在线yum源,配置本地nginx的yum源cat > /etc/yum.repos.d/nginx.repo << 'EOF'[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0EOFyum install nginx -y#修改nginx配置文件,配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口cd /etc/nginx/vim /etc/nginx/nginx.confevents {worker_connections 1024;}#添加stream {upstream k8s-apiserver {server 192.168.126.21:6443; #master01server 192.168.126.27:6443; #master02}server {listen 6443;proxy_pass k8s-apiserver;}}http {......#检查配置文件语法nginx -t #启动nginx服务,查看已监听6443端口systemctl start nginxsystemctl enable nginxnetstat -natp | grep nginx
[root@lb01 nginx]# netstat -lntp | grep nginx
tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 43439/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 43439/nginx: master
[root@lb01 nginx]# scp nginx.conf 192.168.126.23:`pwd`
[root@lb02 ~]# systemctl enable --now nginx
[root@lb02 ~]# netstat -lntp | grep nginx#------------------ 2、两台负载均衡器配置keepalived ------------------------------#部署keepalived服务yum install keepalived -y//创建nginx状态检查脚本
cd /etc/keepalived
vim check_nginx.sh
#!/bin/bash
if ! killall -0 nginx &> /dev/null
thensystemctl stop keepalived
fichmod +x check_nginx.sh #或者
vim /etc/keepalived/check_nginx.sh
#!/bin/bash
#egrep -cv "grep|$$" 用于过滤掉包含grep 或者 $$ 表示的当前Shell进程ID
count=$(ps -ef | grep nginx | egrep -cv "grep|$$")if [ "$count" -eq 0 ];thensystemctl stop keepalived
fichmod +x /etc/keepalived/check_nginx.sh#修改keepalived配置文件vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {# 接收邮件地址notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}# 邮件发送地址notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1 #修改为本机回环地址smtp_connect_timeout 30router_id NGINX_01 #lb01节点的为 NGINX_01,lb02节点的为 NGINX_02}#添加一个周期性执行的脚本vrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh" #指定检查nginx存活的脚本路径interval 2 #指定每两秒检测一次,默认1秒}vrrp_instance VI_1 {state MASTER #lb01节点的为 MASTER,lb02节点的为 BACKUPinterface ens33 #指定网卡名称 ens33virtual_router_id 51 #指定vrid,两个节点要一致priority 100 #设置优先级,lb01节点的为 100,lb02节点的为 90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.126.88 #指定 VIP,需要与之前证书中认证的地址一致}track_script { #追踪脚本check_nginx #指定vrrp_script配置的脚本}}#启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务)systemctl start keepalivedsystemctl enable keepalivedip addr #查看主节点的VIP是否生成[root@lb01 keepalived]# scp keepalived.conf check_nginx.sh 192.168.126.23:`pwd` #配置文件和脚本复制到另一台上
vim keepalived.conf
router_id NGINX_02 #修改
state BACKUP #修改
priority 90 #修改systemctl enable --now keepalived.service#---------------- 3、关闭主节点的nginx服务,模拟故障,测试keepalived-----------------------#关闭主节点lb01的Nginx服务,模拟宕机,观察VIP是否漂移到备节点systemctl stop nginxip addrsystemctl status keepalived #此时keepalived被脚本杀掉了 #备节点查看是否生成了VIPip addr #此时VIP漂移到备节点lb02#恢复主节点systemctl start nginx #先启动nginxsystemctl start keepalived #再启动keepalivedip addr
7.2 所有node节点修改配置文件
#修改node1和node2节点上的bootstrap.kubeconfig,kubelet.kubeconfig配置文件为VIPcd /opt/kubernetes/cfg/vim bootstrap.kubeconfig server: https://192.168.126.88:6443vim kubelet.kubeconfigserver: https://192.168.126.88:6443vim kube-proxy.kubeconfigserver: https://192.168.126.88:6443#重启node1和node2的kubelet和kube-proxy服务systemctl restart kubelet.service systemctl restart kube-proxy.service#在lb01查看nginx的k8s日志tail /var/log/nginx/k8s-access.log#在 lb01 上查看 nginx 和 node 、 master 节点的连接状态
netstat -natp | grep nginx
tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 45691/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45691/nginx: master
tcp 0 0 192.168.126.22:39598 192.168.126.21:6443 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.22:37884 192.168.126.27:6443 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.88:6443 192.168.126.28:44564 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.88:6443 192.168.126.28:44558 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.88:6443 192.168.126.29:45366 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.22:37880 192.168.126.27:6443 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.22:39606 192.168.126.21:6443 ESTABLISHED 45693/nginx: worker
tcp 0 0 192.168.126.22:37888 192.168.126.27:6443 ESTABLISHED 45693/nginx: worker
7.3 在 master01 节点测试创建pod
#测试创建podkubectl run nginx --image=nginx#查看Pod的状态信息kubectl get pods
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 0/1 ContainerCreating 0 22s #正在创建中kubectl get pods
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx 1/1 Running 0 6m21s #创建完成,运行中kubectl get pods -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default nginx 1/1 Running 0 7m23s 10.244.0.2 192.168.126.28 <none> <none>#READY为1/1,表示这个Pod中有1个容器#在对应node节点上操作,可以直接使用浏览器或者curl命令访问
[root@node01 cfg]# curl 10.244.1.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>#这时在master01节点上查看nginx日志,发现没有权限查看kubectl logs nginx #并没有Pod的访问记录此时多master集群以及nginx负载均衡、keepalived高可用部署完毕
八、部署Dashboard
Dashboard 介绍:
仪表板是基于Web的Kubernetes用户界面。您可以使用仪表板将容器化应用程序部署到Kubernetes集群,对容器化应用程序进行故障排除,并管理集群本身及其伴随资源。您可以使用仪表板来概述群集上运行的应用程序,以及创建或修改单个Kubernetes资源(例如部署,作业,守护进程等)。例如,您可以使用部署向导扩展部署,启动滚动更新,重新启动Pod或部署新应用程序。仪表板还提供有关群集中Kubernetes资源状态以及可能发生的任何错误的信息。
#在 master01 节点上操作#上传 recommended.yaml 文件到 /opt/k8s 目录中cd /opt/k8svim recommended.yaml#默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部kind: ServiceapiVersion: v1metadata:labels:k8s-app: kubernetes-dashboardname: kubernetes-dashboardnamespace: kubernetes-dashboardspec:ports:- port: 443targetPort: 8443nodePort: 30001 #添加type: NodePort #添加selector:k8s-app: kubernetes-dashboard#通过recommended.yaml资源配置清单,使用kubectl apply创建资源,-f指定资源配置清单文件kubectl apply -f recommended.yaml#创建service account并绑定默认cluster-admin管理员集群角色kubectl create serviceaccount dashboard-admin -n kube-systemkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin#获取token值kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')#使用输出的token登录Dashboard#如果web页面提示不安全,敲thisisunsafe即可进入下一页面,之后将token值输入https://192.168.126.29:30001
总结:
多master部署步骤:
- 先部署master02等其它master节点
- 搭建Nginx/Haproxy + keepalived 高可用 负载均衡对接
- master节点群集
- 修改node 节点上的kubelet kube-proxy 的kubeconfig 配置文件对接vip
- kubectl的配置文件也要对接VIP或者当前节点的IP