高可用笔记(6)keepalived+nginx

    xiaoxiao2026-10-10  2

    前面讲到了部署多个tomcat和多个rabbitmq用nginx做代理服务器,这种情况,tomcat和rabbitmq既实现了高可用又实现了负载均衡。但是,nginx确成了单点。在一个HA环境中,任何一个点都必须实现高可用,这里就需要借助keepalived来实现nginx的高可用。

    测试环境

    host1 192.168.30.1 (nginx, keepalived/master)host2 192.168.30.2 (nginx, keepalived/backup)

    安装keepalived

    host1和host2分别安装keepalived, host2安装nginx(过程略)

    $ yum install keepalived

    准备nginx状态检测脚本

    /etc/keepalived/check_nginx.sh

    #!/bin/bash if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] then /usr/local/nginx/sbin/nginx sleep 5 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] then killall keepalived fi fi

    拷贝到host2

    $ scp /etc/keepalived/check_nginx.sh root@192.168.30.2:/etc/keepalived/check_nginx.sh

    修改host1的keepalived配置

    ! Configuration File for keepalived global_defs { notification_email { test@sanlogic.com } notification_email_from test@test.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.30.30 } }

    修改host2的keepalived配置文件

    ! Configuration File for keepalived global_defs { notification_email { test@sanlogic.com } notification_email_from test@test.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.30.30 } }

    启动keepalived

    $ systemctl enable keepalived $ systemctl start keepalived

    测试

    用浏览器访问http://192.168.30.30/hellonginx,出现如下界面

    这时关闭host1

    $ halt -p

    用浏览器访问http://192.168.30.30/hellonginx,仍然是如下界面

    Done !

    最新回复(0)