nginx +keepalived 高可用

本文详细介绍了如何在CentOS上安装配置Nginx和Keepalived,实现高可用性的负载均衡集群。包括Nginx的安装、开机启动、服务启动;Keepalived的安装、设置为主从节点、配置邮件通知、脚本监控等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx 安装
------------------------------------------------------------------------
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

#设置开机启动
systemctl enable nginx.service

#启动服务
systemctl start nginx.service

-----------------------------------------------------------------------
keepalived 安装
#安装keepavlived
yum -y install keepalived

#设置开机启动
systemctl enable keepalived.service

#启动服务
service keepalived start

-----------------------------------------------------------------------
keepavlived mater设置


global_defs {
   notification_email {
     localhost@163.com
   }
   notification_email_from Keepalived@localhost
   smtp_server 163@smtp.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "/etc/keepalived/chk_nginx.sh"
    interval 3
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    lvs_sync_daemon_interface eth0
    virtual_router_id 151
    priority 100
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
#VIP
    virtual_ipaddress {
    172.16.101.193
        }
  track_script {
       chk_nginx
    }
}

------------------------------------------------------------------------
keepavlived backup 设置
cat keepalived.conf

global_defs {
   notification_email {
     localhost@163.com
   }
   notification_email_from Keepalived@localhost
   smtp_server 163@smtp.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "/etc/keepalived/chk_nginx.sh"
    interval 3    #这个必须要大于脚本里sleep 2 要不脚本不生效。
    weight 2
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    lvs_sync_daemon_interface eth0
    virtual_router_id 151
    priority 90
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
#VIP
    virtual_ipaddress {
    172.16.101.193
        }
  track_script {
       chk_nginx
    }
}

-------------------------------------------------------------
脚本:
cat /etc/keepalived/chk_nginx.sh

#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
    systemctl start nginx.service
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        systemctl stop keepalived
    fi
fi


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值