一.介绍
由于互联网系统读的压力要远大于写的压力,因此该软件主要实现分散压力,负载均衡的功能。
- 四层负载均衡(TCPIP协议的负载均衡)
haproxy lvs
- 七层负载均衡(http协议的负载均衡)
nginx
二.结构图
三.安装配置haproxy
1.将安装包上传
haproxy-1.7.9.tar.gz
2.编译安装
tar -xzvf haproxy-1.7.9.tar.gz
- 查看内核版本
# uname -r
2.6.32-696.el6.x86_64
- 根据内核版本进行编译
yum install gcc*
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=X86_64
make install PREFIX=/usr/local/haproxy
# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.7.9 2017/08/18
Copyright 2000-2017 Willy Tarreau
3.建立haproxy用户
useradd haproxy
chown -R haproxy.haproxy /usr/local/haproxy
4.配置
- 复制haproxy文件到/usr/sbin下
因为下面的haproxy.init启动脚本默认会去/usr/sbin下找,当然你也可以修改,不过比较麻烦。
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
- 复制haproxy脚本,到/etc/init.d下
cp /tmp/haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
- 创建配置文件
mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg
(1).roundrobin:轮询,轮流分配到后端服务器;
(2).static-rr:根据后端服务器性能分配;
(3).leastconn:最小连接者优先处理;
(4).source:根据请求源IP,与Nginx的IP_Hash类似
- 配置日志
vi /etc/rsyslog .conf 去掉下面两列的注释
$ModLoad imudp
$UDPServerRun 514
然后添加下面的行
local1 . * / var / log /haproxy . log
重启rsyslog
# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
5.启动haproxy
在管理server上添加vip
ifconfig eth0:0 192.168.1.207
service haproxy start
四.测试负载均衡
1.数据库连接
2.登陆网页
http://192.168.1.208:48800/admin-status
由于互联网系统读的压力要远大于写的压力,因此该软件主要实现分散压力,负载均衡的功能。
- 四层负载均衡(TCPIP协议的负载均衡)
haproxy lvs
- 七层负载均衡(http协议的负载均衡)
nginx
二.结构图
三.安装配置haproxy
1.将安装包上传
haproxy-1.7.9.tar.gz
2.编译安装
tar -xzvf haproxy-1.7.9.tar.gz
- 查看内核版本
# uname -r
2.6.32-696.el6.x86_64
- 根据内核版本进行编译
yum install gcc*
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=X86_64
make install PREFIX=/usr/local/haproxy
# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.7.9 2017/08/18
Copyright 2000-2017 Willy Tarreau
3.建立haproxy用户
useradd haproxy
chown -R haproxy.haproxy /usr/local/haproxy
4.配置
- 复制haproxy文件到/usr/sbin下
因为下面的haproxy.init启动脚本默认会去/usr/sbin下找,当然你也可以修改,不过比较麻烦。
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
- 复制haproxy脚本,到/etc/init.d下
cp /tmp/haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
- 创建配置文件
mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg
点击(此处)折叠或打开
- # this config needs haproxy-1.1.28 or haproxy-1.2.1
- global
- maxconn 4096
- log 127.0.0.1 local1 notice
- chroot /usr/local/haproxy
- user haproxy
- group haproxy
- daemon
- nbproc 1
- pidfile /usr/local/haproxy/logs/haproxy.pid
- defaults
- log global
- retries 3
- option redispatch
- maxconn 4096
- timeout http-keep-alive 10s
- timeout check 10s
- timeout connect 5000
- timeout client 50000
- timeout server 50000
- listen admin_stats
- bind 192.168.1.208:48800
- stats enable
- stats hide-version
- stats realm <realm>
- stats refresh 5s
- stats uri /admin-status
- stats auth admin:admin
- stats admin if TRUE
- mode http
- option httplog
- timeout connect 50000
- timeout check 50000
- timeout client 300000
- timeout server 300000
- listen mysql_proxy
- bind 192.168.1.208:3307
- mode tcp
- balance roundrobin
- option tcpka
- option tcplog
- server mysql5.7 192.168.1.203:3306 weight 1 check inter 2000 rise 2 fall 5
- server mysql5.7-2 192.168.1.204:3306 weight 1 check inter 2000 rise 2 fall 5
- server mysql5.7-3 192.168.1.205:3306 weight 1 check inter 2000 rise 2 fall 5
(2).static-rr:根据后端服务器性能分配;
(3).leastconn:最小连接者优先处理;
(4).source:根据请求源IP,与Nginx的IP_Hash类似
- 配置日志
vi /etc/rsyslog .conf 去掉下面两列的注释
$ModLoad imudp
$UDPServerRun 514
然后添加下面的行
local1 . * / var / log /haproxy . log
重启rsyslog
# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
5.启动haproxy
在管理server上添加vip
ifconfig eth0:0 192.168.1.207
service haproxy start
四.测试负载均衡
1.数据库连接
点击(此处)折叠或打开
- [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
- Enter password:
- +------------+
- | @@hostname |
- +------------+
- | mysql5.7 |
- +------------+
- [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
- Enter password:
- +------------+
- | @@hostname |
- +------------+
- | mysql5.7-2 |
- +------------+
- [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
- Enter password:
- +------------+
- | @@hostname |
- +------------+
- | mysql5.7-3 |
- +------------+
- [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
- Enter password:
- +------------+
- | @@hostname |
- +------------+
- | mysql5.7 |
- +------------+
2.登陆网页
http://192.168.1.208:48800/admin-status
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15412087/viewspace-2157512/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15412087/viewspace-2157512/