以前写过一篇,nginx+keepalived 双机互备的文章,写那篇文章的时候没有想过如果apache或者nginx 挂了,而 keepalived 或者 机器没有死,那么主辅是不会切换的,今天就研究了一下该如何监控 nginx进程呢,看官方站看到了。vrrp_script 功能,但是用他的方法实在形不通,可能是我的方法不对,或者是个BUG。所以后来我自己写了个小脚本来完成工作。
环境
Server 1  :  ubuntu-server 8.04.4          192.168.6.162
Server 2  :  userver-server 8.04.4          192.168.6.188
软件
Keepalived 1.1.15
nginx-0.8.35
pcre-8.02
1.分别在两台服务器上安装nginx
tar jxvf pcre-8.02.tar.bz2 

cd pcre-8.02 

./configure --prefix=/usr --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz 

make 

make install 

tar zxvf nginx-0.8.35.tar.gz 

cd nginx-0.8.35 

--prefix=/usr/local/nginx --with-pcre --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt=' -O3' 

make 

make install
2.分别在两台服务器编写配置文件
vim /usr/local/nginx/conf/nginx.conf

  1. user www www;
  2. worker_processes 1;
  3. error_log logs/error.log notice;
  4. pid logs/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. tcp_nopush on;
  13. keepalive_timeout 65;
  14. gzip on;
  15. server {
  16. listen 80;
  17. server_name localhost;
  18. index index.html index.htm;
  19. root /var/www;
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. root html;
  23. }
  24. }
  25. }

3.分别在两台机器创建测试文件

echo "192.168.6.162" > /var/www/index.html 

echo "192.168.6.188" > /var/www/index.html
4.安装 keepalived 
apt-get install keepalived
5.在server 1服务器编写配置文件
  1. vrrp_script chk_http_port {
  2. script "/opt/nginx_pid.sh" ###监控脚本
  3. interval 2 ###监控时间
  4. weight 2 ###目前搞不清楚
  5. }
  6. vrrp_instance VI_1 {
  7. state MASTER ### 设置为 主
  8. interface eth0 ### 监控网卡
  9. virtual_router_id 51 ### 这个两台服务器必须一样
  10. priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUP
  11. authentication {
  12. auth_type PASS ### 加密
  13. auth_pass eric ### 加密的密码,两台服务器一定要一样,不然会出错
  14. }
  15. track_script {
  16. chk_http_port ### 执行监控的服务
  17. }
  18. virtual_ipaddress {
  19. 192.168.6.7 ### VIP 地址
  20. }
  21. }

6.在 server 2 服务器 keepalived 配置

  1. vrrp_script chk_http_port {
  2. script "/opt/nginx_pid.sh"
  3. interval 2
  4. weight 2
  5. }
  6. vrrp_instance VI_1 {
  7. state BACKUP ### 设置为 辅机
  8. interface eth0
  9. virtual_router_id 51 ### 与 MASTRE 设置 值一样
  10. priority 100 ### 比 MASTRE权重值 低
  11. authentication {
  12. auth_type PASS
  13. auth_pass eric ### 密码 与 MASTRE 一样
  14. }
  15. track_script {
  16. chk_http_port
  17. }
  18. virtual_ipaddress {
  19. 192.168.6.7
  20. }
  21. }
7.编写监控nginx监控脚本
vim /opt/nginx_pid.sh 

#!/bin/bash
# varsion 0.0.2
# 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios 当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本
  1. A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
  2. if [ $A -eq 0 ];then ## 如果没有进程值得为 零
  3. /usr/local/nginx/sbin/nginx
  4. sleep 3
  5. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  6. killall keepalived ## 则结束 keepalived 进程
  7. fi
  8. fi
8、测试,分别在两个服务器 启动 nginx 和 keepalived
/usr/local/nginx/sbin/nginx 

/etc/init.d/keepalived start
监控 server 1 的日志
Apr 20 18:37:39 nginx Keepalived_vrrp: Registering Kernel netlink command channel 

Apr 20 18:37:39 nginx Keepalived_vrrp: Registering gratutious ARP shared channel 

Apr 20 18:37:39 nginx Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 

Apr 20 18:37:39 nginx Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. 

Apr 20 18:37:39 nginx Keepalived_healthcheckers: Configuration is using : 3401 Bytes 

Apr 20 18:37:39 nginx Keepalived_vrrp: Configuration is using : 35476 Bytes 

Apr 20 18:37:40 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE 

Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE 

Apr 20 18:37:41 nginx Keepalived_vrrp: Netlink: skipping nl_cmd msg... 

Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
监控 server 2的日志 
Apr2018:38:23 varnish Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. 

Apr 20 18:38:23 varnish Keepalived_healthcheckers: Configuration is using : 3405 Bytes 

Apr 20 18:38:23 varnish Keepalived_vrrp: Using MII-BMSR NIC polling thread... 

Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink reflector 

Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink command channel 

Apr 20 18:38:23 varnish Keepalived_vrrp: Registering gratutious ARP shared channel 

Apr 20 18:38:23 varnish Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 

Apr 20 18:38:23 varnish Keepalived_vrrp: Configuration is using : 35486 Bytes 

Apr 20 18:38:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE 

Apr 20 18:38:25 varnish Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded 
看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了
现在我们在 server 1 把 nginx 服务器停到
Server 1 $> killall nginx
这时候看server 1的日志
Apr 20 18:41:26 nginx Keepalived_healthcheckers: Terminating Healthchecker child process on signal 

Apr 20 18:41:26 nginx Keepalived_vrrp: Terminating VRRP child process on signal 
可以看出keepalived 的进程已经停到
这时候看server 2的日志,看是否已经接管
Apr 20 18:41:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE 

Apr 20 18:41:24 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE 

Apr 20 18:41:24 varnish Keepalived_vrrp: Netlink: skipping nl_cmd msg... 
很明显的看出 server 2 已经接管了,已经变为 MASTER 了

本文出自 “linuxer” 博客,请务必保留此出处http://deidara.blog.51cto.com/400447/302402

nginx+keepalived主辅切换(监控脚本在keepalived.conf中执行)的更多相关文章

  1. Nginx+Keepalived主备切换(包含nginx服务停止)

    原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4 ...

  2. keepalived当主节点切换时脚本通知 lvs

    脚本 在keepalived.conf中添加 mail  查看邮件 实验dr实验模型 给director 做主从 4台服务器 rip:192.168.0.103 rip2:192.168.0.104 ...

  3. keepalived主备切换后的arp问题【转】

    使用keepalived的时候主机挂了,备机显示绑定了VIP.但是此时实际还是不能访问.其实就是网关的arp缓存没有刷新.   在服务器上执行一下就行了 arping -I eth0 -c 5 -s ...

  4. 测试redis+keepalived实现简单的主备切换【转载】

    转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...

  5. Keepalived + HAProxy 搭建【第二篇】Keepalived 安装与配置

    第一步:准备 1. 简介 本文搭建的是利用 Keepalived 实现 HAProxy 的热备方案,即两台主机上的 HAProxy 实例同时运行,其中全总较高的实例为 MASTER,MASTER出现异 ...

  6. Keepalived + HAProxy 搭建【第二篇】Keepalived 的安装与配置

    第一步:准备 1. 简介 本文搭建的是利用 Keepalived 实现 HAProxy 的热备方案,即两台主机上的 HAProxy 实例同时运行,其中全总较高的实例为 MASTER,MASTER出现异 ...

  7. Nginx+Keepalived(带Nginx监控脚本)

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0220/151.html?1456381460 Keepalived+ nginx的安装部署 主机: ...

  8. JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

  9. nginx+keepalived实现nginx双主高可用的负载均衡

    http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...

随机推荐

  1. iOS组件化方案

    一.蘑菇街url-block方案 这是蘑菇街中应用的一种页面间调用的方式,通过在启动时注册组件提供的服务,把调用组件使用的url和组件提供的服务block对应起来,保存到内存中.在使用组件的服务时,通 ...

  2. C#中的线程池使用(二)

    线程池是后台线程.每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中.每个进程只有一个线程池对象. 下面说一下线程池中的异常,在线程池中未处理的异常将终止进程.以下为此规则的三种例外 ...

  3. Java实现终止线程池中正在运行的定时任务

    源于开发 最近项目中遇到了一个新的需求,就是实现一个可以动态添加定时任务的功能.说到这里,有人可能会说简单啊,使用quartz就好了,简单粗暴.然而quartz框架太重了,小项目根本不好操作啊.当然, ...

  4. 软件测试就业必备知识点&自学软件测试-Dotest-2019

    软件测试就业必备知识点&自学测试&教学大纲-Dotest-2019

  5. django内置服务器

    单进程多线程的 多线程用来并发,各个线程之间不会阻塞,每个线程对应一个连接

  6. 让函数的input、output更"函数化"

    前言 我们都知道函数的基本形式为:output f(input),且先按这种形式进行input与output的分析,我们的input与output可以有更好的设计方式,而我们的output是选择使用r ...

  7. Hanlp(汉语言处理包)配置、使用、官方文档

    配置使用教程:https://github.com/hankcs/HanLP Hanlp官方文档:http://www.hankcs.com/nlp/hanlp.html 参考API:http://h ...

  8. CSS+jQuery实现轮播

    CSS+jQuery实现轮播 CSS jQuery 前端  实现功能: 自动轮播: 鼠标放在上面停止轮播: 鼠标放在上面显示左右切换的按钮: 鼠标放在小圆圈上显示对应的图片: 轮播效果图 style. ...

  9. Kibana6.x.x——执行yarn build出现的警告信息记录

    Running "_build:installDependencies" task Warning: Command failed: /home/kibana_git/kibana ...

  10. 4 Values whose Sum is 0(枚举+二分)

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...