haproxy的web服务负载均衡、动静分离、 MySQL服务负载均衡、状态监控
实验环境:基于centos6.6
haproxy-Server:172.16.249.98 hostname:node1
upsteram server1:172.16.249.100 hostname:node2
upstream server2:172.16.249.99 hostname:node3
web服务的负载均衡以及状态监控:
设置记录haproxy日志的文件位置:
node1:
#vim /etc/rsyslog.conf (1)启用UDP: # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 (2)添加记录日志的文件位置 # Save boot messages also to boot.log local7.* /var/log/boot.log local2.* /var/log/haproxy.log #service rsyslog restart
node1:
#yum install haproxy -y #cd /etc/haproxy/ #cp haproxy.cfg{,.bak} #vim haproxy.cfg
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 30000 listen stats #此段为设置状态监控 mode http bind :8888 #生产环境中为保证安全性,为设置特殊端口,例如:8888 stats enable stats hide-version stats uri /haproxyadmin?stats stats realm Haproxy\ Statistics stats auth admin:admin #设置用户名以及密码 stats admin if TRUE frontend http-in bind *:80 mode http log global option httpclose option logasap option dontlognull capture request header Host len 20 capture request header Referer len 60 default_backend servers frontend healthcheck bind :1099 mode http option httpclose option forwardfor default_backend servers backend servers balance roundrobin server node2 172.16.249.100:80 check maxconn 2000 server node3 172.16.249.99:80 check maxconn 2000
#serivce haproxy start
node2,node3节点分别建立测试页面,并启动httpd服务;
node2:#vim /var/www/html/index.html <h1> httpd on node2 </h1> #service httpd start node3:#vim /var/www/html/index.html <h1> httpd on node3 </h1> #service httpd start
打开浏览器:172.16.249.98:
打开浏览器:172.16.249.98:8888/haproxy?stats
动静分离配置:
#vim /etc/haproxy/haproxy.cfg
#--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main *:80 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js .html .txt .htm use_backend static if url_static default_backend app backend static balance roundrobin server node2 172.16.249.100:80 check backend app balance roundrobin server node3 172.16.249.99:80 check
#serivce haproxy start #启动haproxy服务
所有静态页面请求代理至node2节点;所有动态页面请求代理至node3节点。
node2:#vim /var/www/html/index.html <h1> httpd on node2 </h1> #service httpd start node3:#vim /var/www/html/index.php <?php phpinfo(); ?> #service httpd start
打开浏览器:172.16.249.98/index.html
打开浏览器:172.16.249.98/index.php
负载均衡mysql服务:
node1:#yum install mysql -y
#vim /etc/haproxy/haproxy.cfg
#--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode tcp log global option dontlognull option http-server-close #option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend bd bind :3306 default_backend dbsrvs backend dbsrvs balance leastconn server node2 172.16.249.100:3306 check server node3 172.16.249.99:3306 check listen stats bind :80 mode http stats enable stats uri /haproxy?stats stats realm haproxy\ stats stats auth admin:admin stats admin if TRUE
分别在node2、node3节点建立数据库,并授权可远程连接:
node2:#yum install mysql-server -y
#serivce mysqld start
#mysql
node3:#yum install mysql-server -y
#serivce mysqld start
#mysql
node1:#service haproxy restart #重启服务,配置文件才能生效
#mysql -uroot -h172.16.249.98 -pmageedu
检测:
node2:#mysql -uroot -pmageedu
node3:#mysql -uroot -pmageedu;
说明:由于我们在node1节点调用了数据库并创建了表,node2节点查看有此表,node3节点无此表,因此我们刚才的测试中调用了node2节点的mysql数据库而非node3节点。
本文出自 “小二哥Linux” 博客,请务必保留此出处http://9612520.blog.51cto.com/9602520/1601566
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。