mysql动静分离_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
?httpd?on?node2?
#service?httpd?start
node3:#vim?/var/www/html/index.html
?httpd?on?node3?
#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
?httpd?on?node2?
#service?httpd?start
node3:#vim?/var/www/html/index.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節點。
原文:http://9612520.blog.51cto.com/9602520/1601566
總結
以上是生活随笔為你收集整理的mysql动静分离_haproxy的web服务负载均衡、动静分离、 MySQL服务负载均衡、状态监控...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年净利暴涨227.8%!国产CP
- 下一篇: 特斯拉中国裁员 疑似赔偿方案出炉:N+3