haproxy(單機)+mysql叢集負載均衡

張衝andy發表於2018-09-19

HAProxy是 七層代理 ,在使甠HAProxy後,在MySQL上 看不到Apps的源IP地址 ,看到的是HAProxy地址,而 MySQL的許可權訪問設定是和IP地址有關 ,這樣就導致了MySQL無法 針對應用 進行區分許可權了,所以使用的時候要注意。

1. HAProxy的安裝
Shell> yum install haproxy

2. HAProxy的配置
2.1. haproxy.cfg

將以下配置檔案儲存為 /etc/haproxy/haproxy.cfg

[root@mysql3 haproxy]# cat /etc/haproxy/haproxy.cfg 
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
#option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

# 繫結的IP和埠
listen MySQL 10.100.25.42:3308
mode tcp
maxconn 200
# 負載均衡演算法是 輪詢
balance roundrobin
# 透過mysql連線去檢測mysql是否可以訪問
option mysql-check user haproxy_check
server mysql_1 10.100.25.40:3308 inter 1s rise 2 fall 2
server mysql_3 10.100.25.41:3307 inter 1s rise 2 fall 2
server mysql_3 10.100.25.41:3308 inter 1s rise 2 fall 2

# 自帶的監控伺服器的配置
listen admin_status
mode http
bind 0.0.0.0:8899
option httplog
log global
stats enable
stats refresh 10s
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
# 監控的使用者名稱和密碼
stats auth myadmin:myadmin 
stats admin if TRUE

2.2. 新增haproxy_check使用者
將以下SQL語句在Master端執行,透過複製功能,傳遞到Slave上。
drop user haproxy_check@'XX';
create user haproxy_check@'XX';
grant usage on *.* to haproxy_check@'XX';


2.3. 配置日誌

注意:該方法僅在 CentOS 6.X 上使用, CentOS 7.x 安裝 HAProxy 後可甠 systemctl status haproxy 進行檢視。
將以下檔案儲存為 /etc/rsyslog.d/49-haproxy.conf
# Create an additional socket in haproxy's chroot in order to allow logging via
# /dev/log to chroot'ed HAProxy processes
$AddUnixListenSocket /var/lib/haproxy/dev/log
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
# Send HAProxy messages to a dedicated logfile
if $programname startswith 'haproxy' then /var/log/haproxy.log
&~


然後重啟 rsyslog 服務
Shell> service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]

3. 啟動HAProxy
Shell> service haproxy start
[root@mysql3 ~]# netstat -tunlp | grep haproxy
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 2583/haproxy 
tcp 0 0 0.0.0.0:8899 0.0.0.0:* LISTEN 2583/haproxy 
udp 0 0 0.0.0.0:33136 0.0.0.0:* 2583/haproxy

4. HAProxy測試

[root@mysql3 haproxy]# mysql -h 10.100.25.42 -P 3308 -uroot -pmysql -e "show variables like 'server_id'";
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 22 |
+---------------+-------+
[root@mysql3 haproxy]# mysql -h 10.100.25.42 -P 3308 -uroot -pmysql -e "show variables like 'server_id'";
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 33 |
+---------------+-------+
[root@mysql3 haproxy]# mysql -h 10.100.25.42 -P 3308 -uroot -pmysql -e "show variables like 'server_id'";
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 44 |
+---------------+-------+

 


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

相關文章