rhel HA + HAProxy 实现高可用集群及负载均衡

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性负载均衡,以及基于TCPHTTP的应用程序代理

HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

包括 GitHubBitbucket[3]、Stack Overflow[4]、RedditTumblrTwitter[5][6]和 Tuenti[7]在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。

 

实验环境:

server1: ip:172.25.61.1

server2: ip:172.25.61.2

server3: ip:172.25.61.3

server4: ip:172.25.61.4

 

server1,server4 上安装rhel_ha高可用集群组件,安装haproxy负责负载均衡,来访问server2,server3上的服务

 

server1,server4:

yum install rpm-build -y    

yum install -y gcc pcre-devel     ##rpmbuild所依赖的包

rpmbuild -tb haproxy-1.6.11.tar.gz 

cd ~/rpmbuild/RPMS/x86_64/

rpm -ivh haproxy-1.6.11-1.x86_64.rpm 

rpm -ql haproxy-1.6.11-1.x86_64.rpm

tar  zxf haproxy-1.6.11.tar.gz

cp haproxy-1.6.11/examples/content-sw-sample.cfg /etc/haproxy/haproxy.cfg

groupadd -g 200 haproxy

useradd -u 200 -g 200 haproxy

cd /etc/haproxy/

vim haproxy.cfg

 

global
        maxconn         10000
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon


defaults

        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
        stats uri       /admin/stats
        option prefer-last-server
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s

# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear       ##监听80端口
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend static

# The static backend backend for 'Host: img', /img and /css.
backend static
        balance         roundrobin               ##负载均衡算法为轮循
        server          statsrv1 172.25.61.2:80 check inter 1000             check inter 1000:检测心跳频率
        server          statsrv2 172.25.61.3:80 check inter 1000

 

/etc/init.d/haproxy start

测试:

 

server1:

/etc/init.d/ricci start

/etc/init.d/luci start

 

server4:

/etc/init.d/ricci start

 

主机:

systemctl start fence_virtd

通过浏览器访问https://server1:8084

添加resource

添加service group

可以看到当前haproxy服务在server1上运行

测试:

将web服务切换到server4上

测试:

 

haproxy的相关设置

访问haproxy管理工具

显示haproxy服务是否正常

haproxy日志

vim /etc/rsyslog.conf

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

local0.*                                                /var/log/haproxy.log

*.info;mail.none;authpriv.none;cron.none;local0.none    /var/log/messages

/etc/init.d/rsyslog restart

vim /var/log/haproxy.log

Sep 25 09:17:05 localhost haproxy[13011]: 172.25.61.250:39100 [25/Sep/2018:09:17:05.974] public static/statsrv1 0/0/0/0/0 200 293 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Sep 25 09:17:08 localhost haproxy[13011]: 172.25.61.250:39100 [25/Sep/2018:09:17:05.975] public static/statsrv2 2361/0/1/0/2362 200 285 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Sep 25 09:17:09 localhost haproxy[13011]: 172.25.61.250:39100 [25/Sep/2018:09:17:08.336] public static/statsrv1 730/0/0/0/730 200 293 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"

 

当在defaults中加入  stats auth admin:westos  后,访问http://172.25.61.1/admin/stats时需要输入密码进行验证

 

当server1和server2上的服务都停止时,使用本机的服务

backend static
        balance         roundrobin
        server          statsrv1 172.25.61.2:80 check inter 1000
        server          statsrv2 172.25.61.3:80 check inter 1000
        server backup 127.0.0.1:8080 backup

haproxy 黑名单

frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }

        acl blacklist src 172.25.61.250
        http-request deny if blacklist   ##拒绝blacklist名单中的ip进行访问,当blacklist名单中的ip进行访问时,会出现403报错
        default_backend static

 

 

frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        
        acl blacklist src 172.25.61.250
        http-request deny if blacklist
        errorloc 403 http://172.25.61.1:8080         ##当出现403报错时,跳转到http://172.25.61.1:8080
        default_backend static

frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }

        acl blacklist src 172.25.61.250
        redirect location http://172.25.61.1:8080 if blacklist     ##当blacklist 名单中的ip进行访问时,直接重定向到http://172.25.61.1:8080
        default_backend static

 

根据不同条件访问不同页面

frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        use_backend dynamic if { path_end -i .php }        ##当网址最后为.php时访问dynamic
        default_backend static

# The static backend backend for 'Host: img', /img and /css.
backend static
        balance         roundrobin
        server          statsrv1 172.25.61.2:80 check inter 1000

        #server backup 127.0.0.1:8080 backup


backend dynamic
        balance         roundrobin
        server          statsrv2 172.25.61.3:80 check inter 1000

当网址不是以.php结尾时访问static中的server

当网址以.php结尾时访问dynamic中的server

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值