haproxy安装
yum安装
[root@localhost ~]# yum -y install haproxy
源码安装
haproxy配置
负载均衡配置
实验逻辑图:
![在这里插入图片描述](https://img.php1.cn/3cd4a/189d8/978/7dbdf0f38ad53545.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2Njc0NzM1,size_16,color_FFFFFF,t_70#pic_center)
实验环境:
haproxy服务器和两个webserver服务器均为centos6操作系统
均关闭防火墙和selinux
[root@localhost ~]# service iptables stop
[root@localhost ~]# setenforce 0
haproxy负载均衡器配置
yum安装haproxy
[root@localhost ~]# yum -y install haproxy
修改配置文件
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
配置文件修改如下:
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/3e641eb5f5c8cd90.webp#pic_center)
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/857a46d091981bac.webp#pic_center)
保存退出开启haproxy服务
[root@localhost haproxy]# service haproxy start
Stopping haproxy: [ OK ]
Starting haproxy: [ OK ]
查看监听的端口验证
[root@localhost haproxy]# !n
netstat -nltp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1815/haproxy
webserver1配置
安装Apache服务
[root@localhost ~]# yum -y install httpd
创建测试页面
[root@localhost ~]# echo web111 > /var/www/html/index.html
开启Apache服务
[root@localhost ~]# service httpd start
webserver2配置
安装Apache服务
[root@localhost ~]# yum -y install httpd
创建测试页面
[root@localhost ~]# echo web222 > /var/www/html/index.html
开启Apache服务
[root@localhost ~]# service httpd start
验证:
访问haproxy负载均衡服务器,实现负载均衡
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/086aec93f5e1e9b2.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2Njc0NzM1,size_16,color_FFFFFF,t_70#pic_center)
监控页面
修改haproxy配置文件
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
在配置文件最后添加以下几行
listen admin_statsstats enablebind *:8080 #监听的ip端口号mode http #开关option httploglog globalmaxconn 10stats refresh 30s #统计页面自动刷新时间stats uri /admin #访问的uri ip:8080/adminstats realm haproxystats auth admin:admin #认证用户名和密码stats hide-version #隐藏HAProxy的版本号stats admin if TRUE #管理界面,如果认证成功了,可通过webui管理节点
保存退出后重启haproxy服务
[root@localhost ~]# service haproxy restart
Stopping haproxy: [ OK ]
Starting haproxy: [WARNING] 321/194154 (1631) : parsing [/etc/haproxy/haproxy.cfg:76] : backend 'nginx', another server named 'static' was defined without an explicit ID at line 75, this is not recommended.[ OK ]
然后访问 http://ip:8080/admin
用户名:admin 密码:admin
输入以后可以成功登陆haproxy监控页面
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/7494af3c1cda418d.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2Njc0NzM1,size_16,color_FFFFFF,t_70)
页面详细参数解释
Queue
Cur: current queued requests
Max:max queued requests
Limit:
Session rate(每秒的连接回话)列表:
scur: current sessions
smax: max sessions
slim: sessions limit Sessions
Total: Cur:
Max:
Limit:
Lbtot: total number of times a server was selected
Bytes
In:
Out: Denied
Req: denied requestsResp:denied responses
Errors
Req:request errors
Conn:connection errors
Resp: response errors (among which srv_abrt) Warnings
Retr: retries (warning)
Redis:redispatches (warning)
Server列表:
Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
LastChk: 持续检查后端服务器的时间
Wght: (weight) : 权重
Act: server is active (server), number of active servers (backend)
Bck: server is backup (server), number of backup servers (backend)
Down:
Downtime: downtime: total downtime (in seconds)
Throttle: warm up status