高可用keepalived + Nginx 负载均衡器

7 篇文章 0 订阅

准备操作:
[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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R1chArd_TvT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值