1.安装
-
下载 http://pan.baidu.com/s/1i3SYsVN
-
# cd haproxy-1.4.20
-
# make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
-
# make install PREFIX=/usr/local/haproxy
2.配置
安装完毕后,进入安装目录配置文件,默认情况下目录里是没有.cfg配置文件的,可以回到安装文件目录下将examples下的haproxy.cfg拷贝到usr/local/haproxy下。
# cd /usr/local/haproxy
# vi haproxy.cfg
3配置tcp负载均衡
-
[root@localhost conf]# vi haproxy.cfg
-
global
-
log 127.0.0.1 local0
-
maxconn 4096
-
stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
-
# nbproc 2
-
pidfile /tmp/haproxy.pid
-
daemon
-
-
defaults
-
log global
-
log 127.0.0.1 local0
-
mode tcp
-
option tcplog
-
option dontlognull
-
retries 3
-
option redispatch
-
maxconn 2000
-
timeout connect 5s
-
timeout client 120s
-
timeout server 120s
-
-
listen mysql_proxy :23306 ---分发均衡的端口
-
mode tcp ---类型
-
balance roundrobin
-
server mysql_65 192.168.6.119:3306 check inter 5000 rise 2 fall 3 ###需要分发的地址
-
server mysql_67 192.168.6.122:3306 check inter 5000 rise 2 fall 3
-
-
listen private_monitoring :8101 ----web页面查看的端口
-
mode http
-
option httplog
-
stats enable
-
stats uri /stats ####web访问页面
-
stats refresh 5s
-
stats auth Frank:Frank #设置监控页面的用户和密码:Frank
-
stats hide-version #隐藏统计页面的HAproxy版本信息
我们启动haproxy命令
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
4 测试tcp分发:
-
#第一次访问:访问到了119数据库
-
[root@localhost conf]# mysql -uroot -pESBecs00 -P23306 -h192.168.6.121
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| hhhh |
| mysql |
| performance_schema |
| test |
| test1 |
| test2 |
| xdy_link |
+--------------------+
11 rows in set (0.07 sec)
-
#第二次访问:访问到了122数据库
-
[root@localhost conf]# mysql -uroot -pESBecs00 -P23306 -h192.168.6.121
Warning: Using a password on the command line interface can be insecure.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| mysql |
| performance_schema |
| test |
| test1 |
| test2 |
+--------------------+
9 rows in set (0.09 sec)
tcp分发成功
-
登录web页面检查
-

5 配置http负载均衡
-
[root@localhost conf]# vi haproxy.cfg
-
global
-
log 127.0.0.1 local0
-
maxconn 4096
-
stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
-
# nbproc 2
-
pidfile /tmp/haproxy.pid
-
daemon
-
-
defaults
-
log global
-
log 127.0.0.1 local0
-
mode tcp
-
option tcplog
-
option dontlognull
-
retries 3
-
option redispatch
-
maxconn 2000
-
timeout connect 5s
-
timeout client 120s
-
timeout server 120s
-
-
listen mysql_proxy :23306
-
mode http ---这个地方不一样
-
balance roundrobin
-
server mysql_65 192.168.6.119:80 check inter 5000 rise 2 fall 3
-
server mysql_67 192.168.6.122:80 check inter 5000 rise 2 fall 3
-
-
listen private_monitoring :8101
-
mode http
-
option httplog
-
stats enable
-
stats uri /stats
-
stats refresh 5s
-
stats auth Frank:Frank #设置监控页面的用户和密码:Frank
-
-
stats hide-version #隐藏统计页面的HAproxy版本信息
6测试http分发
-
[root@localhost conf]# curl 192.168.6.121:23306
-
1234 this is 192.168.6.119
-
[root@localhost conf]# curl 192.168.6.121:23306
-
thisi is 192.168.6.122
-
[root@localhost conf]# curl 192.168.6.121:23306
-
1234 this is 192.168.6.119
-
[root@localhost conf]# curl 192.168.6.121:23306
-
thisi is 192.168.6.122
-
成功
7 还可以通过域名配置分发:(因为我们网站可能对应多个域名)
-
global
log 127.0.0.1 local0 info #[err warning info debug] //日志位置
maxconn 4096
daemon #设置成后台运行
nbproc 1 #进程数量
defaults
log global
mode http #默认模式
option httplog #http日志格式
option dontlognull
retries 3 #三次失败后认为服务器不可用
option redispatch #如果cookie写入了serverId而客户端不会刷新cookie,当serverId对应的服务器挂掉后,强制定向到其他健康的服务
器
maxconn 2000 #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接默认的最大连接数
contimeout 5000 #连接超时
clitimeout 30000 #客户端超时
srvtimeout 30000 #服务器超时
frontend web_in
mode http
maxconn 1000
bind :80
acl is_a hdr_beg(host) -i www.test1.com #判断域名是不是www.test1,是则给与a服务器集群服务
acl is_b hdr_beg(host) -i www.test2.com #判断域名是不是www.test2,是则给与b服务器集群服务
use_backend a_server if is_a
use_backend b_server if is_b
backend a_server
mode http #http 模式
stats uri /haproxy
balance roundrobin
cookie JSESSIONID prefix
stats hide-version
option httpclose
server web1 218.78.186.162:8000 check
server web1 218.78.186.163:8000 check
backend b_server
mode http #http 模式
stats uri /haproxy
balance roundrobin
cookie JSESSIONID prefix
stats hide-version
option httpclose
server web1 218.78.186.162:8080 check
参考文档
http://blog.csdn.net/youyudehexie/article/details/7606504
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1990588/
,如需转载,请注明出处,否则将追究法律责任。