雙主+keepalived+haproxy配置(負載均衡)mysql
實驗系統:CentOS 6.5_x86_64
實驗前提:防火牆和selinux都關閉
實驗軟件:keepalived-1.2.13 haproxy-1.8.13 mysql—5.7.21
主1 ip:192.168.226.134
主2 ip:192.168.226.135
vip 192.168.226.150linux
1、安裝mysqlredis
獲取mysql安裝包:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
安裝mysql過程省略sql
2、配置主主複製後端
命令 service iptables stop
檢查:service iptables status
關閉vi /etc/selinux/config
SELINUX=disabled瀏覽器
兩臺服務器上都要執行:
建立複製用戶
grant replication slave on *.* to 'repl'@'%' identified by '123';bash
進入從服務器mysql
命令: # mysql -uroot -p
關閉slave
命令:stop slave;
開始配置:
輸入下面代碼便可:
CHANGE MASTER TO MASTER_HOST='192.168.226.135', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=681;
先在從服務器配置完成,啓動從服務器:
命令: start slave;
反向再配置一次服務器
3、安裝haproxy(兩臺節點都要安裝)app
tar -zxvxf haproxy-1.8.13.tar.gz
cd haproxy-1.8.13
make TARGET=linux2628 //根據本身主機設定
make install 負載均衡
提供啓動腳本
vi /etc/init.d/haproxy
#!/bin/sh
#
# haproxy
#
# chkconfig: - 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/local/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
chkconfig --add haproxy
chkconfig haproxy on
chmod +x /etc/init.d/haproxy
提供配置文件
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp //haproxy運行模式
log global
option dontlognull
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 600 //最大鏈接數
listen stats //配置haproxy狀態頁
mode http
bind :6677 //找一個比較特殊的端口
stats enable
stats hide-version //隱藏haproxy版本號
stats uri /haproxyadmin?stats //一會用於打開狀態頁的uri
stats realm Haproxy\ Statistics //輸入帳戶密碼時的提示文字
stats auth admin:admin //用戶名:密碼
stats admin if TRUE //開啓狀態頁的管理功能
frontend main
bind *:80
default_backend mysql //後端服務器組名
backend mysql
balance leastconn //使用最少鏈接方式調度
server m1 192.168.226.134:80 check port 80 maxconn 300
server m2 192.168.226.135:80 check port 80 maxconn 300
啓動日誌:
vi /etc/rsyslog.conf
# Provides UDP syslog reception //去掉下面兩行註釋,開啓UDP監聽
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log //添加此行
service rsyslog restart
啓動測試haproxy:
service haproxy start
netstat -tnlp
4、安裝keepalived (兩臺服務器都要執行)
yum install -y keepalived
chkconfig --add keepalived
chkconfig keepalived on
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vi /etc/keepalived/keepalived.conf
粘貼以下內容
! Configuration File for keepalived
global_defs {
router_id Mysql_ha
}
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 5
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh"
interval 2
weight 5
}
vrrp_instance mysql-instance {
state MASTER #另外一臺爲BACKUP
interface enp4s0 #與網卡名稱對應
virtual_router_id 11 #每個IP惟一,另外一臺綁定相同IP要與整個ID一致
priority 10 #另外一臺爲9
advert_int 1
authentication {
auth_type PASS
auth_pass password321
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.226.150/24
}
}
vrrp_instance mysql-ha {
state MASTER
interface enp4s0
virtual_router_id 13
priority 10
advert_int 1
# nopreempt
authentication {
auth_type PASS
auth_pass password321
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.226.14/24
}
notify_backup "/etc/init.d/haproxy restart"
notify_fault "/etc/init.d/haproxy stop"
}
在兩臺機器上建立chk.sh文件:
vi /etc/keepalived/chk.sh
#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/chk.sh
vi /etc/keepalived/check_mysql.sh
#!/bin/bash
MYSQL_PORT=:3306
alive=$(netstat -apn |grep $MYSQL_PORT|grep LISTEN)
if [ -z "$alive" ]
then
exit 1
else
exit 0
fi
5、在兩臺機器上測試
.在瀏覽器打開http://192.168.226.150:6677/haproxyadmin?stats,打開haproxy狀態頁: