您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用MySQL+Keepalived+haproxy實現負載均衡”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用MySQL+Keepalived+haproxy實現負載均衡”吧!
數據庫環境:
slave2:192.168.8.59
slave3:192.168.8.61
工具包:
keepalived-1.4.5.tar.gz
haproxy-1.8.9.tar.gz
此次實驗中,slave2和slave3利用 keepalived和haproxy實現負載均衡,本文著重講解負載均衡的搭建過程。
一、修改內核參數
echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf sysctl -p
net.ipv4.ip_nonlocal_bind=1 意思是啟動haproxy的時候,允許忽視VIP的存在
net.ipv4.ip_forward = 1 haproxy代理服務器同時也要打開內核的轉發功能
二、安裝keepalived和haproxy
mount /dev/cdrom /media yum install kernel-devel openssl-devel popt-devel -y cd /software tar zxvf keepalived-1.4.5.tar.gz cd keepalived-1.4.5 ./configure --prefix=/usr/local/keepalived/ make && make install systemctl enable keepalived
vi /usr/local/keepalived/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
vi /etc/rsyslog.conf
local0.* /var/log/keepalived.log
systemctl restart rsyslog
cd /software tar zxvf haproxy-1.8.9.tar.gz cd haproxy-1.8.9 make TARGET=linux2628 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy /usr/local/haproxy/sbin/haproxy -v useradd -r -s /sbin/nologin haproxy touch /var/log/haproxy.log chmod 755 /var/log/haproxy.log
修改日志位置
vi /etc/rsyslog.conf local0.* /var/log/haproxy.log vi /etc/sysconfig/rsyslog SYSLOGD_OPTIONS="-r -m 0 -c 2" systemctl restart rsyslog
三、配置keepalived
192.168.8.59
/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id mysql-slave notification_email { zdd5503@163.com } notification_email_from zdd5503@163.com smtp_server stmp.163.com smtp_connect_timeout 30 } vrrp_script chk_haproxy { script "/etc/keepalived/scripts/check_haproxy.sh" interval 2 weight 2 } vrrp_instance v_mysql_slave_zdd01 { interface enp0s3 state MASTER virtual_router_id 98 priority 200 nopreempt virtual_ipaddress { 192.168.8.98/24 } track_script { chk_haproxy } notify_master /etc/keepalived/scripts/haproxy_master.sh notify_backup /etc/keepalived/scripts/haproxy_backup.sh notify_fault /etc/keepalived/scripts/haproxy_fault.sh notify_stop /etc/keepalived/scripts/haproxy_stop.sh }
192.168.8.61
/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id mysql-slave notification_email { zdd5503@163.com } notification_email_from zdd5503@163.com smtp_server stmp.163.com smtp_connect_timeout 30 } vrrp_script chk_haproxy { script "/etc/keepalived/scripts/check_haproxy.sh" interval 2 weight 2 } vrrp_instance v_mysql_slave_zdd01 { interface enp0s3 state BACKUP virtual_router_id 98 priority 150 nopreempt virtual_ipaddress { 192.168.8.98/24 } track_script { chk_haproxy } notify_master /etc/keepalived/scripts/haproxy_master.sh notify_backup /etc/keepalived/scripts/haproxy_backup.sh notify_fault /etc/keepalived/scripts/haproxy_fault.sh notify_stop /etc/keepalived/scripts/haproxy_stop.sh }
四、配置haproxy
192.168.8.59和192.168.8.61都做如下腳本配置:
mkdir -p /etc/keepalived/scripts
vi /etc/keepalived/scripts/check_haproxy.sh
#!/bin/bash STARTHAPROXY="/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg" #STOPKEEPALIVED="systemctl stop keepalived" LOGFILE="/etc/keepalived/scripts/keepalived-haproxy-state.log" echo "[check_haproxy status]" >> $LOGFILE A=`ps -C haproxy --no-header |wc -l` echo "[check_haproxy status]" >> $LOGFILE date >> $LOGFILE if [ $A -eq 0 ];then echo $STARTHAPROXY >> $LOGFILE $STARTHAPROXY >> $LOGFILE 2>&1 sleep 5 fi if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then exit 0 else exit 1 fi
vi /etc/keepalived/scripts/haproxy_master.sh
#!/bin/bash STARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg` STOPHAPROXY=`ps -ef |grep sbin/haproxy| grep -v grep|awk '{print $2}'|xargs kill -s 9` LOGFILE="/etc/keepalived/scripts/keepalived-haproxy-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "stop haproxy...." >> $LOGFILE 2>&1 $STOPHAPROXY >> $LOGFILE 2>&1 echo "start haproxy...." >> $LOGFILE 2>&1 $STARTHAPROXY >> $LOGFILE 2>&1 echo "haproxy stared ..." >> $LOGFILE
vi /etc/keepalived/scripts/haproxy_backup.sh
#!/bin/bash STARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg` STOPHAPROXY=`ps -ef |grep sbin/haproxy| grep -v grep|awk '{print $2}'|xargs kill -s 9` LOGFILE="/etc/keepalived/scripts/keepalived-haproxy-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "stop haproxy...." >> $LOGFILE 2>&1 $STOPHAPROXY >> $LOGFILE 2>&1 echo "start haproxy...." >> $LOGFILE 2>&1 $STARTHAPROXY >> $LOGFILE 2>&1 echo "haproxy stared ..." >> $LOGFILE
vi /etc/keepalived/scripts/haproxy_fault.sh
#!/bin/bash LOGFILE=/etc/keepalived/scripts/keepalived-haproxy-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE
vi /etc/keepalived/scripts/haproxy_stop.sh
#!/bin/bash LOGFILE=/etc/keepalived/scripts/keepalived-haproxy-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
更改腳本屬性
chmod 755 /etc/keepalived/scripts/*
配置haproxy
192.168.8.59
vi /usr/local/haproxy/haproxy.cfg
global #全局配置參數 log 127.0.0.1 local0 notice #日志 #user haproxy #group haproxy daemon #以后臺形式運行haproxy #quiet nbproc 1 # 進程數量,可以設置多個進程提高性能 pidfile /usr/local/haproxy/haproxy.pid #haproxy的pid存放路徑 defaults #一些默認參數 log global #全局日志 retries 3 # 3次連接失敗就認為服務器不可用 option dontlognull # 保存haproxy不記錄上次負載均衡發送過來的用于檢測狀態沒有數據的心跳包 option redispatch # 對應的服務器掛掉后,強制指向到其它正常的服務器 maxconn 2000 #默認最大連接數 timeout queue 1m # 等待最大時長 timeout http-request 10s #客戶端建立連接又不請求數據的時候超過10S,就關閉客戶端連接 timeout connect 10s # 定義haproxy將客戶端請求轉發至后端服務器所等待的超時時長 timeout server 1m # 服務端非活動狀態的超時時長 timeout client 1m # 客戶端非活動狀態的超時時長 timeout http-keep-alive 10s # 設置http-keep-alive超時時長 timeout check 10s #健康狀態監測時的超時時間 balance roundrobin #負載均衡方式,輪詢方式(保持會話session:源地址/cookies,針對web應用服務器) #定義負載均衡的配置 listen mysql_slave_wgpt_lb1 bind 192.168.8.98:3307 #綁定的IP與端口 (建議bind *:3306) mode tcp #模式是TCP,(7層http,4層tcp,如果是mysql用tcp,如果是weblogic/was/tomcat之類的就用http) option mysql-check user haproxy_check #通過mysql連接去檢測mysql是否可以訪問 stats hide-version #隱藏統計頁上的haproxy的版本信息 balance roundrobin #負載均衡方式,輪詢方式 server slave2 192.168.8.59:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300 server slave3 192.168.8.61:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300 #服務器定義,check inter 2000檢測心跳頻率,rise 2 2次正確認為服務器可用,fall 5 5次失敗認為服務器不可用, #maxconn 300 最大連接數300 #自帶的監控服務器的配置 listen haproxy_stats mode http bind *:8888 option httplog stats refresh 5s stats uri /haproxy-stat stats realm www.zdd.com monitor stats realm Haproxy Manager stats auth haproxy:haproxy
192.168.8.61
vi /usr/local/haproxy/haproxy.cfg
global #全局配置參數 log 127.0.0.1 local0 notice #日志 #user haproxy #group haproxy daemon #以后臺形式運行haproxy #quiet nbproc 1 # 進程數量,可以設置多個進程提高性能 pidfile /usr/local/haproxy/haproxy.pid #haproxy的pid存放路徑 defaults #一些默認參數 log global #全局日志 retries 3 # 3次連接失敗就認為服務器不可用 option dontlognull # 保存haproxy不記錄上次負載均衡發送過來的用于檢測狀態沒有數據的心跳包 option redispatch # 對應的服務器掛掉后,強制指向到其它正常的服務器 maxconn 2000 #默認最大連接數 timeout queue 1m # 等待最大時長 timeout http-request 10s #客戶端建立連接又不請求數據的時候超過10S,就關閉客戶端連接 timeout connect 10s # 定義haproxy將客戶端請求轉發至后端服務器所等待的超時時長 timeout server 1m # 服務端非活動狀態的超時時長 timeout client 1m # 客戶端非活動狀態的超時時長 timeout http-keep-alive 10s # 設置http-keep-alive超時時長 timeout check 10s #健康狀態監測時的超時時間 balance roundrobin #負載均衡方式,輪詢方式(保持會話session:源地址/cookies,針對web應用服務器) #定義負載均衡的配置 listen mysql_slave_wgpt_lb1 bind 192.168.8.98:3307 #綁定的IP與端口 (建議bind *:3306) mode tcp #模式是TCP,(7層http,4層tcp,如果是mysql用tcp,如果是weblogic/was/tomcat之類的就用http) option mysql-check user haproxy_check #通過mysql連接去檢測mysql是否可以訪問 stats hide-version #隱藏統計頁上的haproxy的版本信息 balance roundrobin #負載均衡方式,輪詢方式 server slave2 192.168.8.59:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300 server slave3 192.168.8.61:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300 #服務器定義,check inter 2000檢測心跳頻率,rise 2 2次正確認為服務器可用,fall 5 5次失敗認為服務器不可用, #maxconn 300 最大連接數300 #自帶的監控服務器的配置 listen haproxy_stats mode http bind *:8888 option httplog stats refresh 5s stats uri /haproxy-stat stats realm www.zdd.com monitor stats realm Haproxy Manager stats auth haproxy:haproxy
五、添加 haproxy_check 用戶
drop user haproxy_check@'%'; create user haproxy_check@'192.168.8.59'; create user haproxy_check@'192.168.8.61'; create user haproxy_check@'192.168.8.98'; grant usage on *.* to haproxy_check@'192.168.8.59'; grant usage on *.* to haproxy_check@'192.168.8.61'; grant usage on *.* to haproxy_check@'192.168.8.98';
mysql> select user,host from mysql.user; +---------------+--------------+ | user | host | +---------------+--------------+ | repl | 192.168.8.% | | root | 192.168.8.% | | haproxy_check | 192.168.8.59 | | haproxy_check | 192.168.8.61 | | haproxy_check | 192.168.8.98 | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+--------------+
六、啟動 keepalived
systemctl daemon-reload systemctl enable keepalived systemctl start keepalived
ip a |grep 98 ps -ef |grep keepalived
七、配置keepalived監控腳本
兩個節點都做如下配置:
vi /etc/keepalived/keepcheck.sh
#!/bin/bash while : do keepalivedcheck=`ps -C keepalived --no-header | wc -l` if [ $keepalivedcheck -eq 0 ];then systemctl start keepalived else echo "keepalived is running" fi sleep 5 done
chmod +x /etc/keepalived/keepcheck.sh nohup /etc/keepalived/keepcheck.sh & echo "nohup /etc/keepalived/keepcheck.sh &" >> /etc/rc.d/rc.local
八、配置haproxy 啟動腳本
vim /etc/rc.d/init.d/haproxy
#! /bin/sh set -e PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin PROGDIR=/usr/local/haproxy PROGNAME=haproxy DAEMON=$PROGDIR/sbin/$PROGNAME CONFIG=$PROGDIR/$PROGNAME.cfg PIDFILE=$PROGDIR/$PROGNAME.pid DESC="HAProxy daemon" SCRIPTNAME=/etc/init.d/$PROGNAME # Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 start() { echo -n "Starting $DESC: $PROGNAME" $DAEMON -f $CONFIG echo "." } stop() { echo -n "Stopping $DESC: $PROGNAME" haproxy_pid=`cat $PIDFILE` kill -9 $haproxy_pid echo "." } restart() { echo -n "Restarting $DESC: $PROGNAME" $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE) echo "." } case "$1" in start) start ;; stop) stop ;; restart) restart ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2 exit 1 ;; esac exit 0
chmod +x /etc/rc.d/init.d/haproxy echo "nohup /etc/rc.d/init.d/haproxy start &" >> /etc/rc.d/rc.local
systemctl stop keepalived systemctl start keepalived tail -f /var/log/keepalived.log
/etc/rc.d/init.d/haproxy start /etc/rc.d/init.d/haproxy stop tail -f /var/log/haproxy.log
haproxy控制臺訪問地址:
http://192.168.8.98:8888/haproxy-stat
九、測試負載均衡
[root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 59 | +---------------+-------+ [root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 61 | +---------------+-------+ [root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 59 | +---------------+-------+ [root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 61 | +---------------+-------+ [root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 59 | +---------------+-------+ [root@slave3 ~]# mysql -uroot -pmysql -h292.168.8.98 -P3307 -e "show variables like 'server_id';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 61 | +---------------+-------+
因為我們在haproxy配置文件中配置了權重為1:1,所以會話將交替分發到兩個節點上邊。
到此,相信大家對“怎么用MySQL+Keepalived+haproxy實現負載均衡”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。