Haproxy+Keepalived+Nginx

本文详细介绍了如何在 CentOS6.5 环境下通过 HAProxy、Keepalived 和 Nginx 实现高可用负载均衡,包括编译依赖库、编译安装、配置等关键步骤。重点阐述了如何设置配置文件以确保集群的稳定性和高效性。
摘要由CSDN通过智能技术生成

Haproxy+Keepalived+Nginx

高可用负载均衡


官方网站:
http://haproxy.com/
http://www.keepalived.org/
http://nginx.org/

实验环境
OS:CentOS6.5 x64
软件版本:
haproxy-1.5.3
keepalived-1.2.13
nginx-1.6.2
主机:
haproxy VIP(keepalived): 192.168.8.100
haproxy1+keepalive-master: 192.168.8.80,192.168.7.70
haproxy2+keepalive-backup: 192.168.8.81,192.168.7.71
nginx1: 192.168.7.100
nginx2: 192.168.7.200
Haproxy+Keepalived+Nginx



Nginx
请参看nginx-1.6.2+php-5.5.19+mariadb-10.0.14

HAProxy
一.安装编译依赖库
[root@haproxy1 ~]# yum -y install pcre-devel openssl-devel zlib-devel

二.编译安装
[root@haproxy1 ~]# tar -xvf haproxy-1.5.3.tar.gz -C /usr/local/src/
[root@haproxy1 ~]# cd /usr/local/src/haproxy-1.5.3/
[root@haproxy1 haproxy-1.5.3]# make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
[root@haproxy1 haproxy-1.5.3]# make install

三.配置
1.init控制脚本
[root@haproxy1 haproxy-1.5.3]# ln -s /usr/local/sbin/haproxy* /usr/sbin/
[root@haproxy1 haproxy-1.5.3]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy1 haproxy-1.5.3]# chmod 755 /etc/init.d/haproxy
[root@haproxy1 ~]# chkconfig --add haproxy
[root@haproxy1 ~]# chkconfig haproxy on
[root@haproxy1 ~]# chkconfig --list haproxy
haproxy            0:off    1:off    2:on    3:on    4:on    5:on    6:off

2.配置文件/etc/haproxy/haproxy.cfg

http://www.haproxy.org/download/1.7/doc/configuration.txt


[root@haproxy1 haproxy-1.5.3]# mkdir /etc/haproxy
[root@haproxy1 haproxy-1.5.3]# mkdir /usr/share/haproxy
[root@haproxy1 haproxy-1.5.3]# cp examples/haproxy.cfg /etc/haproxy
[root@haproxy1 haproxy-1.5.3]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3

       stats enable    
       stats uri /haproxy-stats
       stats refresh 10s
       stats realm Haproxy\ statistic
       stats auth hadmin:foo.123

#redispatch
        maxconn 2000
       timeout connect 5000
        timeout client  50000
        timeout server  50000


listen nginx-http 0.0.0.0:80
        cookie  SERVERID rewrite
        balance roundrobin
       server  nginx1 192.168.7.100:80 cookie nginx1 check inter 2000 rise 2 fall 5
        server  nginx2 192.168.7.200:80 cookie nginx2 check inter 2000 rise 2 fall 5

listen nginx-https 0.0.0.0:443
        cookie  SERVERID rewrite
        balance roundrobin
       server  nginx1 192.168.7.100:443 cookie nginx1 check inter 2000 rise 2 fall 5
        server  nginx2 192.168.7.200:443 cookie nginx2 check inter 2000 rise 2 fall 5

补充:如果是调度数据库,如Redis, MariaDB, MongoDB等非web应用时,则非要将模式改为mode tcp,同时cookie也会失效,重启服务后会提示,所以也要删除对应的cookie选项
[root@haproxy1 haproxy]# /etc/init.d/haproxy restart
Shutting down haproxy:                                     [  OK  ]
Starting haproxy:                                          [  OK  ]
[root@haproxy1 haproxy]# netstat -tunlp|grep haproxy
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3768/haproxy       
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      3768/haproxy       
udp        0      0 0.0.0.0:55509               0.0.0.0:*                      

转载于:https://www.cnblogs.com/lixuebin/p/10814449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值