tengine+keepalived实现高可用tcp正、反向代理

一.环境介绍

系统IPVIPtengine版本keepalived版本

centos7

192.168.171.8192.168.171.442.2.01.3.5-6
centos7192.168.171.10192.168.171.442.2.01.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 Object (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_script chk_nginx {
    script "/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_script {
        chk_nginx
    }
}
# 192.168.171.10为vip的备
# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id 171_10
}
vrrp_script chk_nginx {
    script "/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_script {
        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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值