一:环境介绍:
Nginx Master: 122.112.X.XNginx Backup: 211.101.X.XVIP:Keepalived 1.2.13Nginx安装见nginx安装配置文档Master与Backup的Nginx配置一致.在MASTER跟BACKUP上安装Keepalived: 二:安 装:\\ 两台接入服务器分别安装NginX和keepalived: #tar zxvf keepalived-1.2.13.tar.gz #cd /usr/local/keepalived-1.2.13 #./configure && make && make install Keepalived configuration ------------------------ Keepalived version : 1.2.13 Compiler : gcc Compiler flags : -g -O2 Extra Lib : -lssl -lcrypto -lcrypt Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes SNMP support : No SHA1 support : No Use Debug flags : No ( 出现以上提示则安装成功 ) #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/#mkdir /etc/keepalived#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/#cp /usr/local/sbin/keepalived /usr/sbin/ #service keepalived start|stop #做成系统启动服务方便管理. #echo "/etc/init.d/keepalived start" >> /etc/rc.local #加入启动服务 三:配置:\\修改MASTER 的keepalived.conf #vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id nginx_master}vrrp_script chk_http_port { script "/usr/local/scripts/nginx_pid.sh" ###监控脚本 interval 2 ###监控时间 weight 2 } vrrp_instance VI_1 { state MASTER ### 设置为主 interface eth0 ### 监控网卡 virtual_router_id 51 ### 这个两台服务器必须一样 priority 101 ### 权重值MASTRE一定要高于BAUCKUP authentication { auth_type PASS ### 加密 auth_pass test ### 加密的密码,两台服务器一定要一样,不然会出错 } track_script { chk_http_port ### 执行监控的服务 } virtual_ipaddress { 192.168.128.177 ### VIP 地址 } } \\修改BACKUP keepalived.conf #vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id nginx_backup}vrrp_script chk_http_port { script "/opt/nginx_pid.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP ### 设置为备份机 interface eth0 virtual_router_id 51 ### 与 MASTRE 设置值一样 priority 80 ### 比 MASTRE权重值低 authentication { auth_type PASS auth_pass test ### 密码 与 MASTRE 一样 } track_script { chk_http_port } virtual_ipaddress { 192.168.128.177 } }
\\编写监控nginx监控脚本 #vim /usr/local/scripts/nginx_pid.sh #!/bin/bash A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A if [ $A -eq 0 ];then <span style="white-space:pre"> </span>## 如果没有进程值得为 零 <span style="white-space:pre"> </span>/usr/local/nginx/sbin/nginx <span style="white-space:pre"> </span>sleep 3 <span style="white-space:pre"> </span>if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then <span style="white-space:pre"> </span>killall keepalived ## 则结束 keepalived 进程 <span style="white-space:pre"> </span>fi fi \\测试,分别在两个服务器 启动 nginx 和 keepalived 1>先后在主、从服务器上启动keepalived: /etc/init.d/keepalived start2>在主服务器上查看是否已经绑定了虚拟IP: ip addr3>停止主服务器上的keepalived: /etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟IP:4>启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP\\监控 Nginx Mastaer 的日志 May 12 17:33:44 localhost Keepalived_vrrp: Configuration is using : 35676 Bytes May 12 17:33:44 localhost Keepalived: Starting VRRP child process, pid=1245 May 12 17:33:44 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)] May 12 17:33:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177 May 12 17:33:46 localhost avahi-daemon[2344]: Registering new address record for 192.168.128.177 on eth0. May 12 17:33:46 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.128.177 added \\监控 Nginx Backup 的日志 May 11 22:28:21 localhost Keepalived: Starting Keepalived v1.1.15 (05/11,2010) May 11 22:28:21 localhost Keepalived_vrrp: Using MII-BMSR NIC polling thread... May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink reflector May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink command channel May 11 22:28:21 localhost Keepalived_vrrp: Registering gratutious ARP shared channel May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040 May 11 22:28:21 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘. May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)] May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded 看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了 现在我们把Master的Nginx停掉.查看Backup的日志 [plain] view plaincopyMay 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040 May 11 22:28:21 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘. May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)] May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded May 11 22:29:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177 可以看出backup已经变成master 现在再启动原来master的nginx,再查看backup的日志 [plain] view plaincopyMay 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs. May 11 22:30:32 localhost avahi-daemon[2409]: Withdrawing address record for 192.168.128.177 on eth0. 可以看出又变回了backup.
时间: 2024-11-08 21:46:16