keepalived+nginx+脚本监测服务

来源:互联网 发布:电视mac认证状态异常 编辑:程序博客网 时间:2024/06/02 06:24
 ######keepalived+nginx#####
环境:selinux :disabled
     iptables:disabled
     server1 server4
两台主机已经安装好keepalived,可参考http://blog.csdn.net/csdn066/article/details/76223609
[root@server1 ~]# which keepalived                    ##确定两台主机的keepalived已经安装好
/sbin/keepalived
[root@server4 ~]# which keepalived
/sbin/keepalived
[root@server1 ~]# tar zxf nginx-1.12.0.tar.gz                 ##安装nginx
[root@server1 ~]# cd nginx-1.12.0
[root@server1 nginx-1.12.0]# vim auto/cc/gcc
将debag下面的那行备注
[root@server1 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx    ##源码安装

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

[root@server1 nginx-1.12.0]# yum  install -y pcre-devel            ##解决依赖性
[root@server1 nginx-1.12.0]# make && make install            ##编译安装
[root@server4 ~]# scp /usr/local/nginx/ server4:/usr/local/    ##避免麻烦,这里我们直接给另一台主机直接复制
[root@server1 ~]# vim /opt/nginx_check.sh                 ##nginx控制脚本
#!/bin/bash
curl http://127.0.0.1/index.html -o /dev/null -s || /usr/local/nginx/sbin/nginx

if  [ $? -ne 0 ];then
/etc/init.d/keepalived  stop &> /dev/null
fi
##脚本的意思时:执行curl http://127.0.0.1/index.html -o /dev/null -s 这个命令判断nginx是否正常工作,如果不能则尝试开启nginx服务,如果$?不等于0也就是curl http://127.0.0.1/index.html -o /dev/null -s是错误输出,则关闭keepalived服务,以达到通知另外一台主机接管工作的目的。
[root@server1 ~]# chmod +x /opt/nginx_check.sh                ##给脚本执行权限

[root@server1 ~]# vim  /etc/keepalived/keepalived.conf
  1 ! Configuration File for keepalived
  2
  3 vrrp_script nginx_check {
  4         script /opt/nginx_check.sh                    ##监控服务脚步
  5         interval 2                            ##监测时间间隔
  6 }
  7
  8 global_defs {
  9    notification_email {
 10      root@localhost            ##发送邮件目标地址                                            
 11         }
 12    notification_email_from keepalived@server1            ##发送邮件源地址
 13    smtp_server 127.0.0.1                        ##snmp服务器
 14    smtp_connect_timeout 30
 15    router_id LVS_DEVEL
 16    vrrp_skip_check_adv_addr
 17    vrrp_strict
 18 }
 19
 20 vrrp_instance VI_1 {
 21     state MASTER                            ##主备的区别点标示
 22     interface eth0                            ##对应的网卡
 23     virtual_router_id 51
 24     priority 100                            ##优先级,值越大优先级越高
 25     advert_int 1
 26     authentication {                        ##验证方式
 27         auth_type PASS
 28         auth_pass 1111
 29     }
 30     virtual_ipaddress {
 31         172.25.66.100/24
 32         }
 33         track_script {                        ##以脚本为监控
 34                 nginx_check
 35         }
 36 }
 37 }

[root@server4 ~]# vim /etc/keepalived/keepalived.conf
 1 ! Configuration File for keepalived
  2
  3 vrrp_script nginx_check {
  4         script /opt/nginx_check.sh
  5         interval 2
  6 }
  7
  8 global_defs {
  9    notification_email {
 10      root@localhost
 11         }
 12    notification_email_from keepalived@server1
 13    smtp_server 127.0.0.1
 14    smtp_connect_timeout 30
 15    router_id LVS_DEVEL
 16    vrrp_skip_check_adv_addr
 17    vrrp_strict
 18 }
 19
 20 vrrp_instance VI_1 {
 21     state BACKUP                            
 22     interface eth0
 23     virtual_router_id 51
 24     priority 100
 25     advert_int 1
 26     authentication {
 27         auth_type PASS
 28         auth_pass 1111
 29     }
 30     virtual_ipaddress {
 31         172.25.66.100/24
 32         }
 33         track_script {
 34                 nginx_check
 35         }
 36 }
 37 }
##备机除了要将MASTER改为BACKUP,及优先级小于主机外,其他配置与主机相同
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
 17 http {
 18         upstream westos {
 19         server 172.25.66.2:80;        ##主机
 20         server 172.25.66.3:80;        ##热备
 21         }
121 server {
122         listen 80;                ##监听端口
123         server_name www.westos.org;        ##监听地址
124         location / {            ##请求的url过滤
125                 proxy_pass http://westos;    ##请求转向westos定义的服务器列表
126                 }
127 }
128 }
##http块可以嵌套很多个server,配置代理、缓存、日志定义等绝大树功能和第三方模块的配置
[root@server4 ~]# vim /usr/local/nginx/conf/nginx.conf
 17 http {
 18         upstream westos {
 19         server 172.25.66.2:80;
 20         server 172.25.66.3:80;
 21         }
121 server {
122         listen 80;
123         server_name www.westos.org;
124         location / {
125                 proxy_pass http://westos;
126                 }
127 }
128 }

测试:

[root@server1 ~]# killall -9 nginx
[root@server1 ~]# ps ax
.......
 1974 ?        S      0:00 /usr/lib64/heartbeat/ipfail
 2682 ?        Ss     0:00 sshd: root@pts/0
 2693 pts/0    Ss     0:00 -bash
13019 ?        Ss     0:00 keepalived -D
13021 ?        S      0:00 keepalived -D
13022 ?        S      0:00 keepalived -D
18503 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
18504 ?        S      0:00 nginx: worker process      
18509 ?        S      0:00 sleep 1
18510 pts/0    R+     0:00 ps ax
##强制关闭nginx进程后,脚本会检测到并开启nginx服务
[root@server1 ~]# /etc/init.d/keepalived status
keepalived (pid  13019) is running...
[root@server1 ~]# mv /usr/local/nginx/sbin/nginx /mnt/        
##为了达到nginx服务无法开启的效果,将nginx移动到/mnt
[root@server1 ~]# killall -9 nginx
[root@server1 ~]# ps ax
 1826 tty5     Ss+    0:00 /sbin/mingetty /dev/tty5
 1828 tty6     Ss+    0:00 /sbin/mingetty /dev/tty6
 1974 ?        S      0:00 /usr/lib64/heartbeat/ipfail
 2682 ?        Ss     0:00 sshd: root@pts/0
 2693 pts/0    Ss     0:00 -bash
19131 ?        S      0:00 sleep 1
19132 pts/0    R+     0:00 ps ax
[root@server1 ~]# /etc/init.d/keepalived status
keepalived is stopped
##此时脚本检测到错误输出,并尝试开启ngnx服务,无法开启时,关闭该主机的keepalived服务,另外一台主机就会接替工作
[root@server4 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:64:7b:42 brd ff:ff:ff:ff:ff:ff
    inet 172.25.66.4/24 brd 172.25.66.255 scope global eth0
    inet 172.25.66.100/24 scope global secondary eth0
    inet6 fe80::5054:ff:fe64:7b42/64 scope link
       valid_lft forever preferred_lft forever

[kiosk@foundation6 Desktop]$ curl www.westos.org
<h1>server3-www.westos.org</h1>
[kiosk@foundation6 Desktop]$ curl www.westos.org
<h1>server2-www.westos.org</h1>
##服务正常时实现负载均衡