mairadb+galera+haproxy+keepalived實現mysql負載均衡與高可用

百聯達發表於2017-04-15
一:伺服器資源

Haproxy+keepalived    node1   10.116.19.251

Haproxy+keepalived    node2   10.44.90.201

mariadb+galera    node1  10.44.66.2

mariadb+galera    node2 10.116.148.103

mariadb+galera    node3 10.170.142.108

二:mariadb 叢集安裝

請參照上一篇博文,安裝成功後,叢集中新增haproxy訪問使用者

mysql -u root -p
CREATE USER 'haproxy'@' 10.116.19.251';
CREATE USER 'haproxy'@' 10.44.90.201';
FLUSH PRIVILEGES;

三:haproxy 安裝

sudo yum install haproxy

配置haproxy vi /etc/haproxy/haproxy.cfg


點選(此處)摺疊或開啟

  1. #---------------------------------------------------------------------
  2. # Example configuration for a possible web application. See the
  3. # full configuration options online.
  4. #
  5. # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
  6. #
  7. #---------------------------------------------------------------------

  8. #---------------------------------------------------------------------
  9. # Global settings
  10. #---------------------------------------------------------------------
  11. global
  12.     # to have these messages end up in /var/log/haproxy.log you will
  13.     # need to:
  14.     #
  15.     # 1) configure syslog to accept network log events. This is done
  16.     # by adding the '-r' option to the SYSLOGD_OPTIONS in
  17.     # /etc/sysconfig/syslog
  18.     #
  19.     # 2) configure local2 events to go to the /var/log/haproxy.log
  20.     # file. A line like the following can be added to
  21.     # /etc/sysconfig/syslog
  22.     #
  23.     # local2.* /var/log/haproxy.log
  24.     #
  25.     log 127.0.0.1 local2

  26.     chroot /var/lib/haproxy
  27.     pidfile /var/run/haproxy.pid
  28.     maxconn 4000
  29.     user haproxy
  30.     group haproxy
  31.     daemon

  32.     # turn on stats unix socket
  33.     stats socket /var/lib/haproxy/stats

  34. #---------------------------------------------------------------------
  35. # common defaults that all the 'listen' and 'backend' sections will
  36. # use if not designated in their block
  37. #---------------------------------------------------------------------
  38. defaults
  39.     mode http
  40.     log global
  41.     option httplog
  42.     option dontlognull
  43.     option http-server-close
  44.     option forwardfor except 127.0.0.0/8
  45.     option redispatch
  46.     retries 3
  47.     timeout http-request 10s
  48.     timeout queue 1m
  49.     timeout connect 10s
  50.     timeout client 1m
  51.     timeout server 1m
  52.     timeout http-keep-alive 10s
  53.     timeout check 10s
  54.     maxconn 3000

  55. #---------------------------------------------------------------------
  56. # main frontend which proxys to the backends
  57. #---------------------------------------------------------------------
  58. frontend main *:5000
  59.     acl url_static path_beg -i /static /images /javascript /stylesheets
  60.     acl url_static path_end -i .jpg .gif .png .css .js

  61.     use_backend static if url_static
  62.     default_backend app

  63. #---------------------------------------------------------------------
  64. # static backend for serving up images, stylesheets and such
  65. #---------------------------------------------------------------------
  66. backend static
  67.     balance roundrobin
  68.     server static 127.0.0.1:4331 check

  69. #---------------------------------------------------------------------
  70. # round robin balancing between the various backends
  71. #---------------------------------------------------------------------
  72. backend app
  73.     balance roundrobin
  74.     server app1 127.0.0.1:5001 check
  75.     server app2 127.0.0.1:5002 check
  76.     server app3 127.0.0.1:5003 check
  77.     server app4 127.0.0.1:5004 check

  78. listen mariadb-cluster
  79. bind 0.0.0.0:3306
  80. mode tcp
  81. option tcplog
  82. option mysql-check user haproxy
  83. balance roundrobin
  84. server mdb01 10.44.66.2:3306 check
  85. server mdb02 10.116.148.103:3306 check
  86. server mdb03 10.170.142.108:3306 check

  87. listen web-interface
  88. bind 0.0.0.0:8888
  89. mode http
  90. stats enable
  91. stats uri /
  92. stats realm Strictly\ Private
  93. stats auth admin:admin

啟動haproxy

chkconfig haproxy on
/etc/init.d/haproxy start

haproxy 監控結果



四:安裝keepalived

sudo yum install keepalived -y

配置haproxy+keepalived node1   vi /etc/keepalived/keepalived.conf


點選(此處)摺疊或開啟

  1. global_defs {
  2.         lvs_id LBL01
  3. }
  4.  
  5. vrrp_sync_group SyncGroup01 {
  6.         group {
  7.                 FloatIP1
  8.         }
  9. }
  10.  
  11. vrrp_script check_haproxy {
  12.         script "killall -0 haproxy"
  13.         interval 2
  14.         weight 2
  15. }
  16.  
  17. vrrp_instance FloatIP1 {
  18.         state MASTER
  19.         interface eth0
  20.         virtual_router_id 10
  21.         priority 101
  22.         advert_int 1
  23.         virtual_ipaddress {
  24.                 192.168.1.60
  25.         }
  26.         track_script {
  27.                 check_haproxy
  28.         }
  29. }

配置haproxy+keepalived node2  
 vi /etc/keepalived/keepalived.conf


點選(此處)摺疊或開啟

  1. global_defs {
  2.         lvs_id LBL01
  3. }
  4.  
  5. vrrp_sync_group SyncGroup01 {
  6.         group {
  7.                 FloatIP1
  8.         }
  9. }
  10.  
  11. vrrp_script check_haproxy {
  12.         script "killall -0 haproxy"
  13.         interval 2
  14.         weight 2
  15. }
  16.  
  17. vrrp_instance FloatIP1 {
  18.         state BACKUP
  19.         interface eth0
  20.         virtual_router_id 10
  21.         priority 100
  22.         advert_int 1
  23.         virtual_ipaddress {
  24.                 192.168.1.60
  25.         }
  26.         track_script {
  27.                 check_haproxy
  28.         }
  29. }


啟動keepalived   

chkconfig keepalived on


/etc/init.d/keepalived start

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2137324/,如需轉載,請註明出處,否則將追究法律責任。

相關文章