当前位置: 代码迷 >> 综合 >> Linux:部署keepalived,实现nginx代理的高可用【keepalived+nginx+httpd】
  详细解决方案

Linux:部署keepalived,实现nginx代理的高可用【keepalived+nginx+httpd】

热度:69   发布时间:2023-12-18 11:53:06.0

部署keepalived,实现nginx代理的高可用

后端httpd:192.168.186.162 192.168.186.163
keepalived+nginx:192.168.186.161 192.168.186.160
客户端:192.168.186.123

实验拓扑图:
在这里插入图片描述
后端httpd:192.168.186.162 192.168.186.163

yum -y install httpd
192.168.186.162:  echo 162 > /var/www/html/index.html
192.168.186.163   echo 163 > /var/www/html/index.html
systemctl start httpd

nginx:192.168.186.161 192.168.186.160

yum -y install nginx
vim /etc/nginx/nginx.conf

配置如下:

.....include /etc/nginx/conf.d/*.conf;upstream webs {
    server 192.168.186.162;server 192.168.186.163;}server {
    listen       80 default_server;listen       [::]:80 default_server;server_name  _;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {
    proxy_pass http://webs;}location = /status {
    stub_status on;access_log off;allow 127.0.0.1;deny all;}error_page 404 /404.html;location = /40x.html {
    }error_page 500 502 503 504 /50x.html;location = /50x.html {
    }}
............
systemctl start nginx

keepalived:192.168.186.161 192.168.186.160

161配置:

! Configuration File for keepalivedglobal_defs {
    notification_email {
    acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script nginx {
    script "/usr/bin/curl -I 127.0.0.1 &> /dev/null"interval 3weight -30fall 2rise 1
}
vrrp_instance VI_1 {
    state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {
    auth_type PASSauth_pass 1111}virtual_ipaddress {
    192.168.186.100}track_script {
    nginx}
}

160配置:

! Configuration File for keepalivedglobal_defs {
    notification_email {
    acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 {
    state BACKUPinterface ens33virtual_router_id 51priority 80advert_int 1authentication {
    auth_type PASSauth_pass 1111}virtual_ipaddress {
    192.168.186.100}
}
systemctl start keepalived

=================================================
方法二:使用脚本进行判断

vim /etc/keepalived/nginx_keep.sh
#!/bin/bash
/usr/bin/curl -I 127.0.0.1 &> /dev/null || pkill keepalivedchmod +x  /etc/keepalived/nginx_keep.sh
此时161 的keepalived配置中vrrp_script模块可改为:
vrrp_script nginx {
    script "/etc/keepalived/nginx_keep.sh"interval 3
}

测试:
使用客户端进行访问:

curl 192.168.186.100

能实现对httpd的轮询
关闭161机器上的nginx,可以实现VIP的飘移
则说明实验成功!