码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived+nginx的nginx高可用(个人简单不详细记录)

时间:2015-11-26 01:35:18      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:keepalived+nginx

1,安装都是用的yum

nginx用的是官方最新源

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

yum install nginx keepalived -y

2,keepalived配置文件.

主:配置文件内容

[root@t01 opt]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script chk_http_port {

    script "/opt/nginx_pid.sh"

    interval 2

    weight -5

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 81

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        chk_http_port

    }

    virtual_ipaddress {

        172.16.226.100

    }

    notify_master /tmp/keep.sh

    notify_backup /tmp/keep.sh

    notify_fault /tmp/keep.sh

    notify_stop /tmp/keep.sh

}

从:配置文件内容.

[root@226-129 ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script chk_http_port {

    script "/opt/nginx_pid.sh"

    interval 2

    weight -5

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 81

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        chk_http_port

    }

    virtual_ipaddress {

        172.16.226.100

    }

    notify_master /tmp/keep_master.sh

    notify_backup /tmp/keep_backup.sh

    notify_fault /tmp/keep_fault.sh

    notify_stop /tmp/keep_stop.sh

}

nginx相关脚本:

[root@226-129 ~]# cat /opt/nginx_pid.sh 

#!/bin/bash 


A=`ps -C nginx --no-header|wc -l`


if [ $A -eq 0 ];then

  /usr/sbin/nginx -c /etc/nginx/nginx.conf

  sleep 3 

  if [ `ps -C nginx --no-header|wc -l` -eq 0 ];then

    killall keepalived

  fi

fi

测试两台服务器vip切换很流畅,nginx相关脚本执行正常,需要注意的是脚本要有执行权限.

keepalived+nginx的nginx高可用(个人简单不详细记录)

标签:keepalived+nginx

原文地址:http://lsfeng.blog.51cto.com/7959286/1716880

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!