nginx+keepalived 主备搭建记录

nginx安装

1.软件安装

nginx version: nginx/1.16.1

nginx安装的服务器: 192.168.4.201/192.168.4.202

yum install -y nginx

2.软件启动

systemctl start nginx

3.查看nginx.conf位置

[root@localhost bin]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5.开机自启动配置

 systemctl enable nginx.service #设置nginx自启动
 systemctl status nginx   #查看nginx状态

七、keepalived安装

ipkeepalived角色虚拟ip
192.168.4.201BACKUP192.168.4.200
192.168.4.202MASTER192.168.4.200

1.安装必要的依赖

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2.安装keepalived

进入目录

cd /usr/local/src

下载keepalived

wget https://github.com/acassen/keepalived/archive/v2.0.18.tar.gz

解压

tar -zxvf v2.0.18.tar.gz

进入目录

cd keepalived-2.0.18

开始安装

./build_setup

如果出现报错,xxx未找到命令,则执行以下命令安装相应的工具

yum -y install aclocal autoheader automake autoreconf

再次执行命令

./build_setup

编译&&安装

./configure
make && make install

3.配置keepalived

复制相关配置文件到系统默认路径

mkdir /etc/keepalived
cp ./keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp ./keepalived/etc/init.d/keepalived /etc/init.d/
cp ./keepalived/etc/sysconfig/keepalived /etc/sysconfig/

修改配置文件

vim /usr/lib/systemd/system/keepalived.service

将PIDFile的值改为/var/run/keepalived.pid

修改keepalived的配置文件

vim /etc/keepalived/keepalived.conf

Master机器(192.168.4.202)的配置文件如下:

!configuration File for keepalived
global_defs {
   router_id nginx_ha
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   script_user root
   enable_script_security
   # notification_email
}
vrrp_script chk_nginx {
    # nginx检查脚本的目录
    script "/etc/keepalived/nginx_check.sh" interval 2
    weight 20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens32 
    virtual_router_id 51 
    priority 100 
    advert_int 1 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        # 虚拟ip地址
        192.168.4.200 
    }
    track_script {
        chk_nginx
    }
    notify_master "/usr/bin/python /etc/keepalived/send_email.py"
}

Backup(192.168.4.201)机器的配置文件如下:

!configuration File for keepalived
global_defs {
   router_id nginx_ha
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   script_user root
   enable_script_security
   # notification_email
}
vrrp_script chk_nginx {
    # nginx检查脚本的目录
    script "/etc/keepalived/nginx_check.sh" interval 2
    weight 20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens32 
    virtual_router_id 51 
    priority 100 
    advert_int 1 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.4.200
    }
    track_script {
        chk_nginx
    }
    notify_master "/usr/bin/python /etc/keepalived/send_email.py"
}

编写nginx_check脚本

#!/bin/bash
if [ $(ps aux|grep nginx|egrep '(master|worker)' | wc -l ) -eq 0 ];  then
    /usr/sbin/nginx
	sleep 2
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	      /etc/init.d/keepalived stop
	fi
else
	echo "nginx is running"

fi

为脚本添加可执行权限

chmod +x /etc/keepalived/nginx_check.sh

编写send_email.py脚本

#! /usr/bin/env python
# coding=utf-8

from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL

# 邮箱smtp服务器
host_server = '邮箱smtp服务器地址'
# sender为发件人的邮箱
sender = '发件人的邮箱'
# pwd 为邮箱密码
pwd = 'password'
# 发件人的邮箱
sender_cbi_mail = '发件人的邮箱'
# 收件人邮箱
receiver = '收件人的邮箱'

# 邮件的正文内容
mail_content = '你好,目前master的nginx已由172.16.254.102节点切换至本机,请迅速处理!\n \n  本邮件由keepalived自动发送,请勿回复!'
# 邮件标题
mail_title = 'ngxin主备切换告警!'

smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender, pwd)

msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_cbi_mail
msg["To"] = receiver

smtp.sendmail(sender_cbi_mail, receiver, msg.as_string())
smtp.quit()

启动Keepalived并添加到开机自启

systemctl start keepalived
systemctl enable keepalived

4.常用目录及命令

keepalived日志路径

/var/log/messages

keepalived配置文件路径

/etc/keepalived/keepalived.conf

启动keepalived的命令

systemctl start keepalived

重启keepalived的命令

systemctl restart keepalived

停止keepalived的命令

systemctl stop keepalived

keepalived的安装目录

/usr/local/src/keepalived-2.0.18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值