首先在两台后端backend服务器上安装mysql,并启动服务,配置mysql相关参数,测试mysql链接是否正常
[root@BYQ ~]#service mysqld start
Starting mysqld: [ OK ]
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> grant all on mydb.* to 'test'@'%' identified by 'testpass';
Query OK, 0 rows affected (0.00 sec)
在客户端主机上测试mysql的的链接是否正常
[root@localhost ~]# mysql -utest -ptestpass -h172.18.10.10
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@localhost ~]# mysql -utest -ptestpass -h172.18.10.11
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
两台后端主机mysql连接均正常
接下来在haproxy服务器端进行配置
[root@localhost ~]# cd /etc/haproxy/
[root@localhost haproxy]# ls
haproxy.cfg haproxy.cfg.bak
[root@localhost haproxy]# cp haproxy.cfg{,.web}
[root@localhost haproxy]# ls
haproxy.cfg haproxy.cfg.bak haproxy.cfg.web
[root@localhost haproxy]# vim haproxy.cfg
frontend web
mode tcp
bind *:3306
default_backend websrvs
backend websrvs
balance leastconn
server mysql1 172.18.10.10:3306 check
server mysql2 172.18.10.11:3306 check
简单配置完毕
实验1.3配置haproxy rslog
[root@localhost ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local2.* /var/log/haproxy.log
保存退出并重启动rsyslog服务,查看日志监听端口是否启动
[root@localhost ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@localhost ~]# ss -unl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:514 *:*
UNCONN 0 0 *:57263 *:*
UNCONN 0 0 :::514 :::*
实验1.4配置source算法实现hash-type绑定
编辑haproxy.cfg,更改添加如下配置
backend websrvs
balance source
server srv1 172.18.10.10:80 check
server srv2 172.18.10.11:80 check
hash-type map-based
重启服务
[root@localhost haproxy]# service haproxy restart
Stopping haproxy: [ OK ]
Starting haproxy: [ OK ]
在客户端测试
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100;done
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
Backend Server 2 172.18.10.11
实验1.5配置uri算法提高命中。绑定一致性hash consistent
编辑haproxy.cfg文件
backend websrvs
balance uri
server srv1 172.18.10.10:80 check
server srv2 172.18.10.11:80 check
hash-type consistent
保存退出重启服务,结论不管哪台服务器,只要uri相同,便访问同一个后端主机,若uri不同也另当别论
例如随机在两台后端主机生成20个页面文件
[root@localhost ~]# for i in {1..20};do echo "Test Page $i (BE 1)" > /var/www/html/test$i.html;done
[root@localhost ~]# for i in {1..20};do echo "Test Page $i (BE 2)" > /var/www/html/test$i.html;done
在客户端请求,则发现test1在BE1,而test2在BE2
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test1.html;done
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
Test Page 1 (BE 1)
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test2.html;done
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
Test Page 2 (BE 1)
因此得出结论和绑定的客户端没关系,只和绑定的uri有关系,因此能够极大的提高缓存命中率
实验1.6采用roundrobin调度规则,且增加权重 进行测试
编辑haproxy.cfg
backend websrvs
balance roundrobin
server srv1 172.18.10.10:80 check weigth 2
server srv2 172.18.10.11:80 check weigth 1
hash-type consistent
保存并退出
重启服务
[root@localhost haproxy]# service haproxy restart
Stopping haproxy: [ OK ]
Starting haproxy: [ OK ]
在客户端访问同一个URL
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done
Test Page 5 (BE 2)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
将其中一个服务器下线,等3秒 实现fall (默认三秒)
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
再将下线的服务器上线,等2秒,实现rise (默认两秒)恢复轮询
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
Test Page 5 (BE 1)
Test Page 5 (BE 1)
Test Page 5 (BE 2)
Test Page 5 (BE 1)
结论:实现健康状态检查
也可以自己设置 check inter(时间间隔) rise 次数 fall 次数 和 maxconn(最大并发连接数)、 以及backlog(后援队列不指定值的话,就用maxconn的值代替),如下所示
backend websrvs
balance roundrobin
server srv1 172.18.10.10:80 check inter 1000 rise 1 fall 2 maxconn 2000 weight 2
server srv2 172.18.10.11:80 check weight 1
hash-type consistent
首先进入后端服务器,修改httpd关于日志段的配置
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
从启httpd服务,再次在客户端访问
[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/index.html;done
Backend Server 1 172.18.10.10
Backend Server 2 172.18.10.11
Backend Server 1 172.18.10.10
Backend Server 2 172.18.10.11
Backend Server 1 172.18.10.10
Backend Server 2 172.18.10.11
Backend Server 1 172.18.10.10
Backend Server 2 172.18.10.11
Backend Server 1 172.18.10.10
Backend Server 2 172.18.10.11