二、配置HAProxy负载平衡集群

2.1 要求
? 准备三台虚拟机
– 两台做Web服务器,一台安装HAProxy
? 安装并配置HAProxy
– 发往HAProxy的连接请求,分发到真正的Web服务器
– 把HAProxy设置为开机自动启动
? 设置HAProxy以实现监控,并查看监控信息
2.2 项目拓扑图
         client
             |
————————————
      |                          |
HAPorxy             Web Server
192.168.4.13   192.168.4.11-12/24
2.3 HAProxy安装
? RHEL7光盘中内置了HAProxy,只要配置好yum,可以直接安装
[root@svr1 ~]# yum -y install haproxy
[root@svr1 ~]# rpm -q haproxy
haproxy-1.5.14-3.el7.x86_64
2.4 修改配置文件
[root@svr1 ~]# vim /etc/haproxy/haproxy.cfg
12 global
26 log 127.0.0.1 local2 ###[err warning info debug]
28 chroot /var/lib/haproxy
29 pidfile /var/run/haproxy.pid ###haproxy的pid存放路径
30 maxconn 4000 ###最大连接数,默认4000
31 user haproxy
32 group haproxy
33 daemon ###创建1个进程进入deamon模式运行
35 # turn on stats unix socket
36 stats socket /var/lib/haproxy/stats
42 defaults #为后续的其他部分设置缺省参数;缺省参数可以被后续部分重置
43 mode http ###默认的模式mode { tcp|hEp|health } log global ###采用全局定义的日志
44 log global
45 option httplog
46 option dontlognull ###不记录健康检查的日志信息
47 option http-server-close
48 option forwardfor except 127.0.0.0/8 ###后端服务器可以从HEp Header中获得客户端ip
49 option redispatch ###serverid服务器挂掉后强制定向到其他健康服务器
50 retries 3
51 timeout http-request 10s
52 timeout queue 1m
53 timeout connect 10s #如果backend没有指定,默认为10s
54 timeout client 1m ###客户端连接超时
55 timeout server 1m ###服务器连接超时
56 timeout http-keep-alive 10s
57 timeout check 10s
58 maxconn 3000 ##最大连接数
将这个以下部分全部删除,
60 #---------------------------------------------------------------------
61 # main frontend which proxys to the backends
62 #---------------------------------------------------------------------
在最下面添加一下内容(上面配置根据需要更改,我这没做更改)
定义一个监控页面
listen stats
bind 0.0.0.0:1080
stats refresh 30s
stats uri /stats
stats realm haprox manager
stats auth admin:admin
listen web_backend 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server web1 192.168.4.11:80 cookie a1i1 check inter 2000 rise 2 fall 5
server web2 192.168.4.12:80 cookie a1i2 check inter 2000 rise 2 fall 5
2.5 起服务
[root@svr1 ~]# systemctl start haproxy
[root@svr1 ~]# systemctl status haproxy
2.6 访问验证
[root@svr1 ~]## firefox 192.168.4.13:1080/stats
用户名和密码都是admin

二、配置HAProxy负载平衡集群

 

 stop掉web服务器任何一台,刷新页面,查看页面颜色的变化

注,web服务器需要之前搭好
2.7 配置haproxy服务器为日志服务器以便于可以接收到haproxy通过网络发来的日志
2.7.1 配置svr1接受发来的日志(配置调度器本身也是日志服务器,可以接受网络发来的日志)
[root@svr1 ~]# netstat -nutlp |grep :514
[root@svr1 ~]# vim /etc/rsyslog.conf
去掉日志
15 $ModLoad imudp
16 $UDPServerRun 514
19 $ModLoad imtcp
20 $InputTCPServerRun 514
[root@svr1 ~]# netstat -nutlp |grep :514
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 5142/rsyslogd
tcp6 0 0 :::514 :::* LISTEN 5142/rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0:* 5142/rsyslogd
udp6 0 0 :::514 :::* 5142/rsyslogd
客户端访问:
[root@client ~]# firefox 192.168.4.13:1080/stats
[root@cilent ~]# firefox 192.168.4.13
这两个访问哪个都可以
查看haproxy的日志:
[root@svr1 ~]# tail -f /var/log/messages
客户端访问负载均衡,将有日志产生

 

二、配置HAProxy负载平衡集群

上一篇:汉诺塔


下一篇:吴恩达-特征缩放(feature scaling)和PCR降维