欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实战攻略:K8s在多节点环境下的部署与负载均衡技术实现

最编程 2024-08-02 18:56:09
...
配置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 EOF yum install nginx -y 修改nginx配置文件,配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口 vim /etc/nginx/nginx.conf events { 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.80.11:6443; server 192.168.80.14:6443; } server { listen 6443; proxy_pass k8s-apiserver; } } http { ......  检查配置文件语法 nginx -t 启动nginx服务,查看已监听6443端口 systemctl enable --now nginx netstat -natp | grep nginx 将nginx.conf传到20.0.0.90 scp /etc/nginx/nginx.conf 20.0.0.90:/etc/nginx/nginx.conf 部署keepalived服务 yum install keepalived -y 修改keepalived配置文件 vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { # 接收邮件地址 notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } # 邮件发送地址 notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id NGINX_MASTER #lb01节点的为 NGINX_MASTER,lb02节点的为 NGINX_BACKUP } #添加一个周期性执行的脚本 vrrp_script check_nginx { script "/etc/nginx/check_nginx.sh" #指定检查nginx存活的脚本路径 } vrrp_instance VI_1 { state MASTER #lb01节点的为 MASTER,lb02节点的为 BACKUP interface ens33 #指定网卡名称 ens33 virtual_router_id 51 #指定vrid,两个节点要一致 priority 100 #lb01节点的为 100,lb02节点的为 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.80.100/24 #指定 VIP } track_script { check_nginx #指定vrrp_script配置的脚本 } } 创建nginx状态检查脚本 vim /etc/nginx/check_nginx.sh #!/bin/bash #egrep -cv "grep|$$" 用于过滤掉包含grep 或者 $$ 表示的当前Shell进程ID count=$(ps -ef | grep nginx | egrep -cv "grep|$$") if [ "$count" -eq 0 ];then systemctl stop keepalived fi chmod +x /etc/nginx/check_nginx.sh 将keepalived.conf、check_nginx.sh传到20.0.0.90 scp check_nginx.sh keepalived.conf 20.0.0.90:`pwd` 启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务) systemctl start keepalived systemctl enable keepalived ip a 修改node节点上的bootstrap.kubeconfig,kubelet.kubeconfig配置文件为VIP cd /opt/kubernetes/cfg/ vim bootstrap.kubeconfig server: https://20.0.0.100:6443 vim kubelet.kubeconfig server: https://20.0.0.100:6443 vim kube-proxy.kubeconfig server: https://20.0.0.100:6443 //重启kubelet和kube-proxy服务 systemctl restart kubelet.service systemctl restart kube-proxy.service 创建pod测试 #master01上创建 kubectl create deploy nginx-text --image=nginx #master02上创建 kubectl create deploy nginx-master2 --image=nginx 在对应网段的node节点上操作,可以直接使用浏览器或者curl命令访问 curl 172.17.72.2 这时在master01节点上查看nginx日志,发现没有权限查看 kubectl logs nginx-text-78cc774878-4wv7n 在master01节点上,将cluster-admin角色授予用户system:anonymous kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=syst

推荐阅读