nginx主备模式笔记-LMLPHP

(1)两台服务器 192.168.17.129 和 192.168.17.131
(2)在两台服务器安装 keepalived 

安装 keepalived
(1)使用 yum 命令进行安装  
yum install keepalived –y
(2)安装之后,在 etc 里面生成目录 keepalived,有文件 keepalived.conf 
#rpm -q -a keepalived

主从配置
(1)修改/etc/keepalived/keepalivec.conf 配置文件 
 

global_defs {
	notification_email {
		[email protected]
		[email protected]
		[email protected]
	}
	notification_email_from [email protected]
	smtp_server 192.168.17.129
	smtp_connect_timeout 30
	router_id LVS_DEVEL 	#访问的主机名
}

vrrp_script chk_http_port {

	script "/usr/local/src/nginx_check.sh"
    interval 2      #(检测脚本执行的间隔)单位秒
	weight 2 	#权重
}

vrrp_instance VI_1 {
    state BACKUP   # 备份服务器上将 MASTER 改为 BACKUP
	interface ens33  //绑定的网卡
	virtual_router_id 51   # 主、备机的 virtual_router_id 必须相同
	priority 90     # 主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1 	# 每隔1秒发送一个心跳
	authentication {
        auth_type PASS	#权限检验方式,使用密码
        auth_pass 1111 #密码
    }
    virtual_ipaddress {
		192.168.17.50 // VRRP H 虚拟地址
    }
} 

(2)添加检测脚本/usr/local/src/nginx_check.sh
 

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
	/usr/local/nginx/sbin/nginx
	sleep 2
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	killall keepalived
	fi
fi

(3)把两台服务器上 nginx 和 keepalived 启动
启动 nginx:./nginx
启动 keepalived:systemctl start keepalived.service 
 
最终测试
(1)在浏览器地址栏输入 虚拟 ip 地址 192.168.17.50
(2)把主服务器(192.168.17.129)nginx 和 keepalived 停止,再输入 192.168.17.50 

12-09 14:58