前言:
通过nginx+keepalived实现双机热备:
正文:
nginx、keepalived安装过程略。
服务器:
192.168.236.6:master(主机)
192.168.236.7:backup(备机)
开放端口:
停止firewall:systemctl stop firewalld.service
禁止firewalld:systemctl disable firewalld.service
查询防火墙状态:firewall-cmd --state
关闭防火墙:firewall-cmd --state
查询端口是否可用:firewall-cmd --query-port=80/tcp
查询已开放端口:firewall-cmd --list-port
开放端口:firewall-cmd --zone=public --add-port=112/tcp --permanent
keepalived需要开放的端口:112
keepalived:
版本:2.0.17
安装目录:/usr/local/src/keepalived-2.0.17/
配置文件:/etc/init.d/keepalived
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /usr/local/keepalived/etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/keepalived/etc/sysconfig/keepalived
keepalived=/usr/local/keepalived/sbin/keepalived
keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf
keepalived_pid=/usr/local/keepalived/run/keepalived.pid
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon $keepalived -f ${keepalived_config} ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc $keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
reload() {
echo -n $"Reloading $prog: "
killproc $keepalived -1
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
主机配置文件:/usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
419572719@qq.com # 收件人邮箱地址
}
notification_email_from 419572718@qq.com # 发件人邮箱地址
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id nginx_master # 设置nginx master的id,在一个网络应该是唯一的
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 # 检测脚本执行的间隔,单位是秒
weight 2
}
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,主机配置为MASTER
interface ens33 # 网卡
virtual_router_id 51 # 路由编号,同一个集群必须使用同一个路由编号
priority 100 # 优先级,值越大越优先。
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
authentication {
auth_type PASS
auth_pass 1111 # 不同集群的密码最好不同。
}
track_script{
chk_http_port
}
virtual_ipaddress { # 虚拟IP/VIP:可多设,每行一个
192.168.236.100 #这些IP地址会通过网卡广播到组网中,同一个集群使用相同VIP(看情况)
}
}
备机配置文件:/usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
419572719@qq.com
}
notification_email_from 419572718@qq.com
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id nginx_backup
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 指定keepalived的角色,备机配置为BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script{
chk_http_port
}
virtual_ipaddress {
192.168.236.100
}
}
nginx检测恢复脚本:/usr/local/src/check_nginx_pid.sh
#!/bin/bash
#日志名称
log="/var/log/keepalived.log" #操作日志存放路径
exec 2>>$log #如果执行过程中有错误信息均输出到日志文件中
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
nginx安装备注:
1、修改nginx权限,让sh脚本可以执行nginx。
2、如果有报错:nginx: [emerg] bind() to x.x.x.x:xxxx failed (13: Permision denied)
需要给nginx使用的端口添加到http允许列表:
查看http允许访问的端口:semanage port -l | grep http_port_t
将要启动的端口加入到如上端口列表中:
semanage port -a -t http_port_t -p tcp xxx (这里不仅要添加nginx的端口,还要添加nginx集群中各服务器的服务接口,所有节点都要添加。)
keepalived修改日志文件:(默认日志保存在linux系统日志中)
1、修改配置文件:/usr/local/keepalived/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
2、在/etc/rsyslog.conf 末尾添加:
local0.* /var/log/keepalived.log
3、重启日志记录服务
systemctl restart rsyslog
4、重启keepalived
service keepalived restart