日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

利用keepalived和haproxy配置mysql的高可用负载均衡

發(fā)布時間:2025/4/14 数据库 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用keepalived和haproxy配置mysql的高可用负载均衡 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

http://www.cnblogs.com/tae44/p/4717334.html

實驗系統(tǒng):CentOS 6.6_x86_64(2.6.32-504.30.3.el6.x86_64)

實驗前提:防火墻和selinux都關(guān)閉

實驗說明:本實驗共有4臺主機,IP分配如拓?fù)?/p>

實驗軟件:keepalived-1.2.19  haproxy-1.5.14  mariadb-10.0.20

下載地址:http://pan.baidu.com/s/1bnnYiMr

實驗拓?fù)?#xff1a;

    

一、安裝mariadb

  1.在兩臺數(shù)據(jù)庫服務(wù)器安裝:

tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -sv mariadb-10.0.20-linux-x86_64 mysql useradd -r mysql mkdir -pv /mydata/data chown -R mysql.mysql /mydata/data/ cd mysql/ chown -R root.mysql . scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ cp support-files/my-large.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on

  2.配置主主復(fù)制:

    19.74:

vim /etc/my.cnf -----------------------------------------------> [mysqld] server-id = 1 datadir = /mydata/data log-bin = /mydata/data/mysql1-bin binlog_format = ROW relay_log = /mydata/data/relay-log auto-increment-increment = 2 auto-increment-offset = 1 sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1

    19.76:

vim /etc/my.cnf -----------------------------------------------> [mysqld] server-id = 2 datadir = /mydata/data log-bin = /mydata/data/mysql2-bin binlog_format = ROW relay_log = /mydata/data/relay-log auto-increment-increment = 2 auto-increment-offset = 2 sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1

  3.創(chuàng)建具有復(fù)制權(quán)限的用戶:

    19.74:

service mysqld start /usr/local/mysql/bin/mysql ------------------------------------------> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'master'@'192.168.19.76' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;

    19.76:

service mysqld start /usr/local/mysql/bin/mysql ------------------------------------------> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'master'@'192.168.19.74' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;

  4.查看二進制位置:

    19.74:

SHOW MASTER LOGS;

    

    19.76上使用相同命令:

    

  5.配置雙主:

    19.74:

CHANGE MASTER TO MASTER_HOST='192.168.19.76',MASTER_USER='master',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql2-bin.000001',MASTER_LOG_POS=1112; START SLAVE;

    19.76:

CHANGE MASTER TO MASTER_HOST='192.168.19.74',MASTER_USER='master',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql1-bin.000001',MASTER_LOG_POS=1112; START SLAVE;

二、編譯安裝haproxy

  1.在19.66和19.79上編譯安裝haproxy:

tar xf haproxy-1.5.14.tar.gz cd haproxy-1.5.14 make TARGET=linux2628 ARCH=x86_64 //根據(jù)自己主機設(shè)定 make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/

  2.提供啟動腳本:

vim /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 0exec="/usr/sbin/haproxy" prog=$(basename $exec)[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$progcfgfile=/etc/haproxy/haproxy.cfg pidfile=/var/run/haproxy.pid lockfile=/var/lock/subsys/haproxycheck() {$exec -c -V -f $cfgfile $OPTIONS }start() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fiecho -n $"Starting $prog: "# start it up here, usually something like "daemon $exec"daemon $exec -D -f $cfgfile -p $pidfile $OPTIONSretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval }stop() {echo -n $"Stopping $prog: "# stop it here, often "killproc $prog"killproc $progretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval }restart() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fistopstart }reload() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fiecho -n $"Reloading $prog: "$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)retval=$?echoreturn $retval }force_reload() {restart }fdr_status() {status $prog }case "$1" instart|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

  3.提供配置文件:

mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy vim /etc/haproxy/haproxy.cfg ----------------------------------------------------------------------->
globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemonstats socket /var/lib/haproxy/statsdefaultsmode tcp //haproxy運行模式log globaloption dontlognulloption redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 600 //最大連接數(shù)

listen stats //配置haproxy狀態(tài)頁
??? mode http
??? bind :6677 //找一個比較特殊的端口
??? stats enable
??? stats hide-version //隱藏haproxy版本號
??? stats uri???? /haproxyadmin?stats //一會用于打開狀態(tài)頁的uri
??? stats realm?? Haproxy\ Statistics //輸入賬戶密碼時的提示文字
??? stats auth??? admin:admin //用戶名:密碼
??? stats admin if TRUE //開啟狀態(tài)頁的管理功能
frontend main *:3306 //這里為了實驗方便,使用3306端口default_backend mysql //后端服務(wù)器組名backend mysqlbalance???? leastconn //使用最少連接方式調(diào)度
??? server m1 192.168.19.74:3306 check port 3306 maxconn 300
??? server m2 192.168.19.76:3306 check port 3306 maxconn 300

?  4.啟動日志:

vim /etc/rsyslog.conf -----------------------------------------------------> # Provides UDP syslog reception //去掉下面兩行注釋,開啟UDP監(jiān)聽 $ModLoad imudp $UDPServerRun 514local2.* /var/log/haproxy.log //添加此行
<-----------------------------------------------------
service rsyslog restart

?  5.啟動測試haproxy:

service haproxy start
netstat -tnlp

 

  6.在19.74上創(chuàng)建遠(yuǎn)程登錄賬號:

GRANT ALL ON *.* TO 'jason'@'192.168.19.%' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;

  7.分別在19.66和19.79上登錄mysql,若都能連接成功則繼續(xù)往下:

yum -y install mysql //如果沒有mysql客戶端則運行此命令
mysql -ujason -p123456 -h192.168.19.66 //在19.66上登錄
mysql -ujason -p123456 -h192.168.19.79 //在19.79上登錄

三、安裝keepalived

  1.在19.66和19.79上編譯安裝keepalived:

tar xf keepalived-1.2.19.tar.gz cd keepalived-1.2.19 ./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.30.3.el6.x86_64/ //內(nèi)核版本換成自己主機的 make && make install chkconfig --add keepalived chkconfig keepalived on

?  2.在19.66上配置:

vim /etc/keepalived/keepalived.conf ----------------------------------------------------->
! Configuration File for keepalived

global_defs { //此段暫時略過,下同
?? notification_email {
???? acassen@firewall.loc
???? failover@firewall.loc
???? sysadmin@firewall.loc
?? }
?? notification_email_from Alexandre.Cassen@firewall.loc
?? smtp_server 192.168.200.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
}
vrrp_script chk_haproxy {script "/etc/keepalived/chk.sh" //檢查haproxy的腳本interval 2 //每兩秒檢查一次 }vrrp_instance VI_1 {state BACKUP //定義為BACKUP節(jié)點nopreempt //開啟不搶占interface eth0virtual_router_id 51priority 100 //開啟了不搶占,所以此處優(yōu)先級必須高于另一臺advert_int 1authentication {auth_type PASSauth_pass abcd}virtual_ipaddress {192.168.19.150 //配置VIP}
??? track_script {
??????? chk_haproxy //調(diào)用檢查腳本
??? }
notify_backup "/etc/init.d/haproxy restart"notify_fault "/etc/init.d/haproxy stop" }

  3.在19.79上配置:

vim /etc/keepalived/keepalived.conf ----------------------------------------------------->
! Configuration File for keepalived

global_defs {
?? notification_email {
???? acassen@firewall.loc
???? failover@firewall.loc
???? sysadmin@firewall.loc
?? }
?? notification_email_from Alexandre.Cassen@firewall.loc
?? smtp_server 192.168.200.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
}
vrrp_script chk_haproxy {script "/etc/keepalived/chk.sh"interval 2 }vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51priority 99advert_int 1authentication {auth_type PASSauth_pass abcd}virtual_ipaddress {192.168.19.150}
??? track_script {
??????? chk_haproxy
??? }notify_backup "/etc/init.d/haproxy restart"notify_fault "/etc/init.d/haproxy stop" }

  4.在兩臺機器上創(chuàng)建chk.sh文件:

vim /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

  5.在19.66和19.79上進行測試:

service keepalived start

    此處兩臺主機均配置為BACKUP,因此哪臺先運行keepalived,VIP就在哪臺上。我這里剛開始VIP運行在19.66上,然后進行連接測試:

    

mysql -ujason -p123456 -h192.168.19.150
------------------------------------------->
CREATE DATABASE bokeyuan;

    后端數(shù)據(jù)庫服務(wù)器抓包:

    

    停掉19.66的keepalived服務(wù),讓VIP轉(zhuǎn)移到19.79上,再進行測試:

service keepalived stop //停掉19.66的keepalived服務(wù)
mysql -ujason -p123456 -h192.168.19.150
------------------------------------------->
SHOW DATABASES;

    后端數(shù)據(jù)庫服務(wù)器抓包:

    

  6.在瀏覽器打開http://192.168.19.150:6677/haproxyadmin?stats,打開haproxy狀態(tài)頁:

    在19.74上關(guān)閉mysql服務(wù),可以看到haproxy對于后端服務(wù)器的檢測是很迅速的:

service mysqld stop

  7.額外說明:

    繼續(xù)之前的實驗,將19.66上的keepalived服務(wù)再次啟動,可以發(fā)現(xiàn),VIP仍然在19.79上,這就是之前為什么要配置不搶占的原因。如果按照正常的配置,將19.66配置為MASTER,當(dāng)它重啟keepalived服務(wù)后,則一定會將VIP搶回。但實際上我們并不希望這樣,因為19.79仍在正常工作,19.66沒有理由去搶奪資源,造成沒必要的資源切換。實驗演示就到這里,謝謝大家!

轉(zhuǎn)載于:https://www.cnblogs.com/hyl8218/p/7612512.html

總結(jié)

以上是生活随笔為你收集整理的利用keepalived和haproxy配置mysql的高可用负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。