Linux下搭建keepalive+nginx

 

一、 安装nginx(略)

二、 安装keepalive

  1. 下载http://www.keepalived.org/download.html
  2. 安装依赖包 yum install –y popt* gcc* openssl* nmap
  3. tar fvxz keepalived-1.2.15.tar.gz
  4. cd keepalived-1.2.15
  5. ./configure –prefix=/usr/local/keepalived
  6. make && make installmkdir /etc/keepalived  ##为keepalived创建配置文件目录,默认配置文件从/etc/keepalived下读取
  7. cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ ##复制keepalived的二进制命令  
  8. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/  ##提供启动脚本
  9. cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ##keepalived的主
  10. cp /usr/local/keepalived/ety/sysconfig/keepalived /usr/sysconfig/

 

三、 配置keepalive

1.  打开配置文件vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     liumingtao@zhanghetianxia.com

     lmt122900@sina.com

   }

   notification_email_from pub@easymobi.cn

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

    script "/opt/nginx.sh"  #检测nginx是否存活脚本位置(下面会有定义脚本名)

    interval 2

    weight 2

}

 

vrrp_instance VI_1 {

    state MASTER # 主   (备用 BACKUP)

    interface eth0       #本机ip网卡名称

    virtual_router_id 10  # 每组keepalive routeid相同,不同组不能一样

    mcast_src_ip 192.168.102.79  #本机ip 备机用备机ip

    priority 100  #权重 备机数值要比主的数值小

    advert_int 1  # 检查间隔

    authentication {

        auth_type PASS

        auth_pass 1111222

    }

track_script {

    chk_http_port  #定义nginx检测脚本名称

}

    virtual_ipaddress {

        192.168.102.89  #虚拟ip

    }

}

启动主备的keepalived验证

nginx.sh 脚本 备机可以不用定义脚本相关选项

 

#!/bin/sh

# check nginx server status

NGINX=/usr/local/nginx/sbin/nginx

PORT=80

 

nmap localhost -p $PORT | grep "$PORT/tcp open"

#echo $?

if [ $? -ne 0 ];then

    $NGINX -s stop

    $NGINX

    sleep 3

    nmap localhost -p $PORT | grep "$PORT/tcp open"

    [ $? -ne 0 ] && /etc/init.d/keepalived stop

fi

把脚本加上执行权限后放到/opt/目录下

posted @ 2019-11-04 15:16  liumt  阅读(984)  评论(0编辑  收藏  举报