一.环境介绍

系统 IP VIP tengine版本 keepalived版本

centos7

192.168.171.8 192.168.171.44 2.2.0 1.3.5-6
centos7 192.168.171.10 192.168.171.44 2.2.0 1.3.5-6

 二.tengine安装

1.tengine2.2.0软件包下载地址http://tengine.taobao.org/download_cn.html

2.下载tcp代理需要的模块,下载地址https://codeload.github.com/yaoweibin/nginx_tcp_proxy_module/zip/master

3.安装

[root@bogon tengine-2.2.0]# cp nginx_tcp_porxy_module-master.zip /root
[root@bogon tengine-2.2.0]# unzip nginx_tcp_proxy_module-master.zip
[root@bogon tengine-2.2.0]# cd /root/tengine-2.2.0
[root@bogon tengine-2.2.0]# patch -p1 < /root/nginx_tcp_proxy_module-master/tcp.patch ##打补丁
[root@bogon tengine-2.2.0]# ./configure --prefix=/root/tengine --add-module=/root/nginx_tcp_proxy_module-master/
[root@bogon tengine-2.2.0]# make && make install

4.编辑配置文件

#vim /root/tengine/conf/nginx.conf
#user  nobody;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared   (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}
tcp {
#反向代理
   upstream reverse {
        server 192.168.143.156:6101 weight=5 fail_timeout=10s;
        server 192.168.143.160:6101 weight=5 fail_timeout=10s;
   }
   server {
        listen 16101;
        proxy_connect_timeout 60s;
        proxy_pass reverse;
   }
#正向代理
   upstream forward {
        server 192.168.143.152:17890;
   }
   server {
        listen 27890;
        proxy_connect_timeout 60s;
        proxy_pass forward;
   }
}

5.启动

 

三.Keepalived

1.用yum安装

2.更改两台机子的keepalived配置文件

# 192.168.171.8为vip的主
# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id 171_8
}
vrrp_  chk_nginx {
      \"/etc/keepalived/nginx_check.sh\"  #nginx检测脚本
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state MASTER            #设置为主
    interface eno16777984
    virtual_router_id 51    #主备机router_id需一致
    priority 100            #优先级为100
    vrrp_unicast_bind 192.168.171.8  #单播方式绑定在本地
    vrrp_unicast_peer 192.168.171.10 #单播方式绑定对端ip
    advert_int 1  #检测间隔
    authentication {
        auth_type PASS
        auth_pass 1111 
    }
    virtual_ipaddress {
        192.168.171.44  #vip设置
    }
    track_  {
        chk_nginx
    }
}
# 192.168.171.10为vip的备
# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id 171_10
}
vrrp_  chk_nginx {
      \"/etc/keepalived/nginx_check.sh\"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    vrrp_unicast_bind 192.168.171.10
    vrrp_unicast_peer 192.168.171.8
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_  {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.171.44
    }
}

3.nginx检测脚本配置,脚本防在/etc/keepalived/下

#! /bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then
/root/tengine/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
        killall keepalived
fi
fi

 4.启动

systemctl start keepalived

 

收藏 打印