nginx_引入keepalived实现高可用

现有服务器两台

编号ip系统服务职能
1192.168.136.128centos 7.6 x64nginx、keepalived (yum安装)主机(master)
2192.168.136.129centos 7.6 x64nginx、keepalived (yum安装)备机(backup)

如列表展示,机器1作nginx集群的主机,机器2作nginx集群的备机,通过keepalived要两台机器实现下面功能 :

  • 正常情况只有主机接收、处理请求,当主机宕机或者nginx服务故障,自动切换到备机,让备机临时代替工作,
  • 主机上线后,自动切换回主机

配置

  • 机器1和机器2都要配置 /etc/keepalived/nginx_check.sh
  • chmod +x /etc/keepalived/nginx_check.sh   //不加执行权限,会出现重启无效,vip漂移无效等问题,此乃深坑
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
    /usr/sbin/nginx      #重启nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi
  • 配置发送报警邮件:

1、  安装sendmail或postfix (邮件传送代理MTA),现在使用sendmail软件。

(标注:如果直接使用外部邮箱【qq企业邮和网易企业邮等】发送邮件可以不需要配置sendmail或postfix,直接把这两个软件关掉,直接跳到第3步:配置mail即可实现) 

[root@ssticentos65 ~]# yum -y install sendmail                #在线yum安装sendmail
[root@ssticentos65 ~]# /etc/init.d/sendmail start             #启动sendmail服务,如果没有sendmail执行文件 直接service/systemctl 启动即可,下面直接略过
[root@ssticentos65 ~]# /etc/init.d/sendmail status          #查看sendmail启动情况
sendmail dead but subsys locked                            #sendmail进程锁住,原因是postfix服务启动导致sendmail服务进程锁住,需要关闭postfix服务。
sm-client (pid  1759) is running...
[root@ssticentos65 ~]# /etc/init.d/postfix stop                #暂停postfix服务提示失败,原因是postfix进程正在使用,需要使用kill命令杀掉postfix进程
Shutting down postfix:                                     [FAILED]
[root@ssticentos65 ~]# /etc/init.d/postfix status                #查看postfix服务可以查看到进程号
master (pid  1647) is running...
[root@ssticentos65 ~]# kill -9 1647                                     #杀掉postfix服务进程号
[root@ssticentos65 ~]# /etc/init.d/postfix status                #查看postfix状态
master dead but pid file exists
[root@ssticentos65 ~]# chkconfig postfix off                      #设置postfix开机不启动
[root@ssticentos65 ~]# /etc/init.d/sendmail stop               #暂停sendmail服务
Shutting down sm-client:                                   [  OK  ]
Shutting down sendmail:                                    [FAILED]
[root@ssticentos65 ~]# /etc/init.d/sendmail start                 #启动sendmail服务
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]

2、安装邮件发送工具mailx 。(邮件用户代理MUA)

[root@ssticentos65 ~]# yum -y install mailx                     #在线安装mailx

3、  配置mail

[root@ssticentos65 ~]# vim /etc/mail.rc
set  from=XX@nnv5.cn    ##设置发件人的邮箱地址
set  smtp=smtp.exmail.qq.com   ##设置发件人的smtp地址
set smtp-auth-user="XX@nnv5.cn" smtp-auth-password="XXXXXXX" ##设置发件的账号和密码(qq邮箱有可能会需要授权码,密码有时候不行)
set  smtp-auth=login     ##登录,默认即可
  • 机器1和机器2都要配置 /etc/keepalived/mail.sh
#!/bin/bash   //别把这行省掉,初用者注意,要不无法执行,log会显示 Error exec-ing command ...
#
contact='root@localhost(要发送的邮箱)'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac

同样设置权限:chmod +x /etc/keepalived/mail.sh 

结束后试着向一个邮箱发送测试邮件:echo " testing " | mail -s "test" 152xxxxyyyy@163.com

  • 机器1和机器2都要配置 /etc/keepalived/keepalived.conf

主机:

global_defs {
   router_id LVS_DEVEL_MASTER
}
vrrp_script chk_nginx {
 script "/etc/keepalived/nginx_check.sh"
 interval 2
 weight -20
}
vrrp_instance VI_1 {
    state MASTER        # 备机设置为 state BACKUP
    interface ens33
    virtual_router_id 131
    #smtp_alert   smtp提醒,这里没有使用,而是使用自定义smtp发送工具
    notify_master "/etc/keepalived/mail.sh master"
    notify_backup "/etc/keepalived/mail.sh backup"
    notify_fault "/etc/keepalived/mail.sh fault"
    priority 100
    advert_int 1
    nopreempt
    track_script{
        chk_nginx
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.131
    }
}

备机: 

#! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL_BACKUP
}

vrrp_script chk_nginx {
 script "/etc/keepalived/nginx_check.sh"
 interval 2
 weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 131
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script{
        chk_nginx
    }
    virtual_ipaddress {
        192.168.136.131
    }
    notify_master "/etc/keepalived/mail.sh master"
    notify_fault "/etc/keepalived/mail.sh fault"
    notify_backup "/etc/keepalived/mail.sh backup"
}
  • 这里重启:service keepalived restart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值