Keepalived+HAProxy实现MySQL高可用负载均衡的配置

2019-01-04 16:01:32王旭

2.安装xinetd

root@10.1.6.203:~# apt-get install xinetd

3.在每个节点添加xinetd服务脚本和mysqlchk端口号

root@10.1.6.203:~# vim /etc/xinetd.d/mysqlchk # default: on # description: mysqlchk service mysqlchk #需要在servive定义 { flags = REUSE socket_type = stream port = 9222 wait = no user = nobody server = /opt/mysqlchk log_on_failure += USERID disable = no per_source = UNLIMITED bind = 10.1.6.173 } root@10.1.6.203:~# vim /etc/services mysqlchk 9222/tcp # mysqlchk

4.编写mysqlchk监控服务脚本

root@10.1.6.203:~# ls -l /opt/mysqlchk -rwxr--r-- 1 nobody root 1994 2013-09-17 11:27 /opt/mysqlchk root@10.1.6.203:~# cat /opt/mysqlchk #!/bin/bash # # This script checks if a mysql server is healthy running on localhost. It will # return: # "HTTP/1.x 200 OKr" (if mysql is running smoothly) # - OR - # "HTTP/1.x 500 Internal Server Errorr" (else) # # The purpose of this script is make haproxy capable of monitoring mysql properly # MYSQL_HOST="localhost" MYSQL_SOCKET="/var/run/mysqld/mysqld.sock" MYSQL_USERNAME="mysqlchkusr" #该账户密码需要在mysql里添加 MYSQL_PASSWORD="secret" MYSQL_OPTS="-N -q -A" TMP_FILE="/dev/shm/mysqlchk.$$.out" ERR_FILE="/dev/shm/mysqlchk.$$.err" FORCE_FAIL="/dev/shm/proxyoff" MYSQL_BIN="/opt/mysqlcluster/mysql-cluster-gpl-7.2.6-linux2.6-x86_64/bin/mysql" CHECK_QUERY="select 1" preflight_check() { for I in "$TMP_FILE" "$ERR_FILE"; do if [ -f "$I" ]; then if [ ! -w $I ]; then echo -e "HTTP/1.1 503 Service Unavailablern" echo -e "Content-Type: Content-Type: text/plainrn" echo -e "rn" echo -e "Cannot write to $Irn" echo -e "rn" exit 1 fi fi done } return_ok() { echo -e "HTTP/1.1 200 OKrn" echo -e "Content-Type: text/htmlrn" echo -e "Content-Length: 43rn" echo -e "rn" echo -e "<html><body>MySQL is running.</body></html>rn" echo -e "rn" rm $ERR_FILE $TMP_FILE exit 0 } return_fail() { echo -e "HTTP/1.1 503 Service Unavailablern" echo -e "Content-Type: text/htmlrn" echo -e "Content-Length: 42rn" echo -e "rn" echo -e "<html><body>MySQL is *down*.</body></html>rn" sed -e 's/n$/rn/' $ERR_FILE echo -e "rn" rm $ERR_FILE $TMP_FILE exit 1 } preflight_check if [ -f "$FORCE_FAIL" ]; then echo "$FORCE_FAIL found" > $ERR_FILE return_fail; fi $MYSQL_BIN $MYSQL_OPTS --host=$MYSQL_HOST --socket=$MYSQL_SOCKET --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "$CHECK_QUERY" > $TMP_FILE 2> $ERR_FILE if [ $? -ne 0 ]; then return_fail; fi return_ok;

测试

2个节点开启keepalived(主节点会获得vip,自动拉起haproxy),xinetd

root@10.1.6.203:~# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:26:b9:36:0f:81 brd ff:ff:ff:ff:ff:ff inet 211.151.105.186/26 brd 211.151.105.191 scope global eth0 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:26:b9:36:0f:83 brd ff:ff:ff:ff:ff:ff inet 10.1.6.203/24 brd 10.1.6.255 scope global eth1 inet 10.1.6.173/32 scope global eth1 4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:26:b9:36:0f:85 brd ff:ff:ff:ff:ff:ff 5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:26:b9:36:0f:87 brd ff:ff:ff:ff:ff:ff root@10.1.6.203:~# netstat -tunlp | grep ha tcp 0 0 10.1.6.173:3366 0.0.0.0:* LISTEN 1042/haproxy tcp 0 0 10.1.6.203:8888 0.0.0.0:* LISTEN 1042/haproxy udp 0 0 0.0.0.0:56562 0.0.0.0:* 1042/haproxy root@10.1.6.203:~# netstat -tunlp | grep xine tcp 0 0 10.1.6.203:9222 0.0.0.0:* LISTEN 30897/xinetd root@10.1.6.203:~# ps -ef | grep haproxy root 1042 1 0 Sep17 ? 00:00:00 /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg