haproxy高可用代理

haproxy高可用代理

1.haproxy简介

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

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

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

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

2.haproxy高可用代理的实现

1.实验环境

三台centos7服务器

一台win10虚拟机

2.我们需要用到的包

haproxy:

链接:https://pan.baidu.com/s/1qDJ47-t-iFoE8NwHe1-2yw
提取码:cyfh

nginx:

链接:https://pan.baidu.com/s/1n_wndg19LKJDBIYwQouK5Q
提取码:8v9l

3.配置主机名

haproxy:

[root@localhost ~]# hostnamectl set-hostname haproxy
[root@localhost ~]# su
[root@haproxy ~]# 

nginx1:

[root@localhost ~]# hostnamectl set-hostname nginx1
[root@localhost ~]# su
[root@nginx1 ~]# 

nginx2:

[root@localhost ~]# hostnamectl set-hostname nginx2
[root@localhost ~]# su
[root@nginx2 ~]# 

4.安装nginx

nginx1:

[root@nginx1 ~]# tar -zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx1 opt]# cd nginx-1.12.0/
[root@nginx1 nginx-1.12.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx1 nginx-1.12.0]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@nginx1 nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[root@nginx1 nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@nginx1 nginx-1.12.0]# make && make install
[root@nginx1 nginx-1.12.0]# cd /usr/local/nginx/html
[root@nginx1 html]# echo "this is nginx1" > test.html
[root@nginx1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx1 html]# nginx
[root@nginx1 html]# systemctl stop firewalld
[root@nginx1 html]# setenforce 0

nginx2:

[root@nginx2 ~]# tar -zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx2 opt]# cd nginx-1.12.0/
[root@nginx2 nginx-1.12.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx2 nginx-1.12.0]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@nginx2 nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[root@nginx2 nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@nginx2 nginx-1.12.0]# make && make install
[root@nginx2 nginx-1.12.0]# cd /usr/local/nginx/html
[root@nginx2 html]# echo "this is nginx2" > test.html
[root@nginx2 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx2 html]# nginx
[root@nginx2 html]# systemctl stop firewalld
[root@nginx2 html]# setenforce 0

5.在win10主机里面测试是否能够访问

测试nginx1

在这里插入图片描述
测试nginx2

在这里插入图片描述

6.配置haproxy服务器

[root@haproxy ~]# tar -zxvf haproxy-1.5.19.tar.gz -C /opt/
[root@haproxy haproxy-1.5.19]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@haproxy haproxy-1.5.19]# useradd -M -s /sbin/nologin nginx
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg 
        #chroot /usr/share/haproxy
        #redispatch
//把所有的listen都删除,添加下面的内容
listen  webcluster 0.0.0.0:80
        option httpchk GET /test.html
        balance roundrobin
        server inst1 192.168.73.174:80 check inter 2000 fall 3
        server inst2 192.168.73.175:80 check inter 2000 fall 3
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy 
[root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/
[root@haproxy haproxy]# service haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
[root@haproxy haproxy]# netstat -ntap | grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      41180/haproxy
[root@haproxy haproxy]# systemctl stop firewalld
[root@haproxy haproxy]# setenforce 0

7.在win10主机里面测试

在这里插入图片描述

在这里插入图片描述
8.对haproxy进行优化

[root@haproxy haproxy]# vim /etc/haproxy/haproxy.cfg 
        log /dev/log    local0 info
        log /dev/log    local1 notice
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy haproxy]# service haproxy restart
Restarting haproxy (via systemctl):                        [  确定  ]
[root@haproxy haproxy]# systemctl restart rsyslog.service

9.在win10主机里面刷新几次,在haproxy的日志里面看看

[root@haproxy log]# ls
anaconda  dmesg               lastlog   qemu-ga            sssd                    wtmp
audit     dmesg.old           libvirt   rhsm               tallylog                Xorg.0.log
boot.log  firewalld           maillog   sa                 tuned                   Xorg.0.log.old
btmp      gdm                 messages  samba              vmware-vgauthsvc.log.0  Xorg.9.log
chrony    glusterfs           ntpstats  secure             vmware-vmsvc.log        yum.log
cron      grubby_prune_debug  pluto     speech-dispatcher  vmware-vmusr.log
cups      haproxy             ppp       spooler            wpa_supplicant.log
[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log
[root@haproxy haproxy]# cat haproxy-info.log 
Jan 29 13:41:17 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:17.734] webcluster webcluster/inst1 4/0/1/2/7 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:17 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:17.742] webcluster webcluster/inst2 171/0/1/0/172 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:18 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:17.915] webcluster webcluster/inst1 199/0/1/0/201 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:18 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:18.115] webcluster webcluster/inst2 182/0/1/0/183 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:18 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:18.298] webcluster webcluster/inst1 160/0/1/1/162 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:18 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:18.461] webcluster webcluster/inst2 174/0/2/1/177 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:18 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:18.638] webcluster webcluster/inst1 188/0/1/1/190 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:19 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:18.829] webcluster webcluster/inst2 382/0/1/0/383 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:19 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:19.212] webcluster webcluster/inst1 222/0/0/1/223 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:19 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:19.435] webcluster webcluster/inst2 190/0/1/2/193 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:19 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:19.629] webcluster webcluster/inst1 195/0/0/1/196 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:20 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:19.825] webcluster webcluster/inst2 186/0/0/2/188 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:20 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:20.013] webcluster webcluster/inst1 175/0/1/0/176 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:20 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:20.189] webcluster webcluster/inst2 204/0/2/0/206 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:20 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:20.396] webcluster webcluster/inst1 183/0/1/0/184 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jan 29 13:41:20 haproxy haproxy[41965]: 192.168.73.136:51501 [29/Jan/2020:13:41:20.581] webcluster webcluster/inst2 193/0/1/0/195 200 250 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
发布了129 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/double_happy111/article/details/104106383