您现在的位置是:首页 >技术杂谈 >高可用keepalived + Nginx 负载均衡器网站首页技术杂谈

高可用keepalived + Nginx 负载均衡器

R1chArd_TvT 2023-06-17 00:00:03
简介高可用keepalived + Nginx 负载均衡器

准备操作:
[root@localhost ~]# systemctl stop firewalld  # 或 systemctl disable --now firewalld
[root@localhost ~]# setenforce 0

[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost ~]# mv repo.bak/* ./
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum install -y keepalived nginx        

#epel下载的旧版nginx 没有stream模块,只有动态stream模块                      

 


两台都需要配置:
[root@localhost ~]# vim /etc/nginx/nginx.conf
在include /etc/nginx/conf.d/*.conf; 下面添加

upstream web_server {
server 192.168.179.22:80;
server 192.168.179.23:80;
}

在server块中 include下添加location

location  ~ /test {
        proxy_pass http://web_server;
}

[root@localhost ~]# nginx -t
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
客户端:curl 192.168.179.22/test/test.html   curl 192.168.179.23/test/test.html

 ----------------配置keepalived------------------------
[root@localhost ~]# cd /etc/keepalived

[root@localhost ~]# vim nginx.sh

#!/bin/bash
if ! killall -0 nginx &> /dev/null ;then
systemctl stop keepalived
fi

[root@localhost ~]# chmod +x nginx.sh

[root@localhost ~]# cp keepalived.conf keepalived.bak
[root@localhost ~]# vim keepalived.conf

smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_01
}

vrrp_script check_nginx {
      script "/etc/keepalived/nginx.sh"
      interval 2                               #2秒做一次健康检查
      weight 2
}

vrrp_instance VI_1 {
      state MASTER
      interface ens33
      virtual_router_id  51
      priority  100
      advert_int   1
      authentication {
             auth_type PASS
             auth_pass 1111
       }
        virtual_ipaddress
                192.168.179.188
       }
       track_script {
              check_nginx
        }
}

! 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 LVS_01
}

vrrp_script check_nginx {
      script "/etc/keepalived/nginx.sh"
      interval 2
      weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.179.188
    }
    track_script {
         check_nginx
    }
}

[root@localhost ~]# scp keepalived.conf nginx.sh 192.168.179.21:`pwd`

在192.168.179.21上
修改/etc/keepalived/keepalived.conf
router_id NGINX_02
state  BACKUP
priority 90

! 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 LVS_02
}

vrrp_script check_nginx {
      script "/etc/keepalived/nginx.sh"
      interval 2
      weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
        192.168.179.188
    track_script {
         check_nginx
        }
}
                           

wq! 保存退出

#ifdown停掉两台web服务器的lo:0

[root@localhost ~]# ifconfig
[root@localhost ~]# ifdown lo:0
[root@localhost ~]# ifconfig

[root@localhost ~]# systemctl status nginx 
[root@localhost ~]# systemctl start keepalived.service
[root@localhost ~]# systemctl enable keepalived.service

客户端访问:http://192.168.179.188/test/test.html

 Completed!

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。