http://www.cnblogs.com/tae44/p/4717334.htmlhtml
實驗系統:CentOS 6.6_x86_64(2.6.32-504.30.3.el6.x86_64)mysql
實驗前提:防火牆和selinux都關閉linux
實驗說明:本實驗共有4臺主機,IP分配如拓撲redis
實驗軟件:keepalived-1.2.19 haproxy-1.5.14 mariadb-10.0.20sql
下載地址:http://pan.baidu.com/s/1bnnYiMr數據庫
實驗拓撲:vim
1、安裝mariadb後端
1.在兩臺數據庫服務器安裝:瀏覽器
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.配置主主複製:bash
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.建立具備複製權限的用戶:
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;
2、編譯安裝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 //根據本身主機設定 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 0 exec="/usr/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
3.提供配置文件:
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy vim /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 *:3306 //這裏爲了實驗方便,使用3306端口 default_backend mysql //後端服務器組名 backend mysql balance leastconn //使用最少鏈接方式調度
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監聽 $ModLoad imudp $UDPServerRun 514 local2.* /var/log/haproxy.log //添加此行
<-----------------------------------------------------
service rsyslog restart
5.啓動測試haproxy:
service haproxy start
netstat -tnlp
6.在19.74上建立遠程登陸帳號:
GRANT ALL ON *.* TO 'jason'@'192.168.19.%' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;
7.分別在19.66和19.79上登陸mysql,若都能鏈接成功則繼續往下:
yum -y install mysql //若是沒有mysql客戶端則運行此命令
mysql -ujason -p123456 -h192.168.19.66 //在19.66上登陸
mysql -ujason -p123456 -h192.168.19.79 //在19.79上登陸
3、安裝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/ //內核版本換成本身主機的 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節點 nopreempt //開啓不搶佔 interface eth0 virtual_router_id 51 priority 100 //開啓了不搶佔,因此此處優先級必須高於另外一臺 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.19.150 //配置VIP }
track_script {
chk_haproxy //調用檢查腳本
}
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 BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_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.在兩臺機器上建立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;
後端數據庫服務器抓包:
停掉19.66的keepalived服務,讓VIP轉移到19.79上,再進行測試:
service keepalived stop //停掉19.66的keepalived服務
mysql -ujason -p123456 -h192.168.19.150
------------------------------------------->
SHOW DATABASES;
後端數據庫服務器抓包:
6.在瀏覽器打開http://192.168.19.150:6677/haproxyadmin?stats,打開haproxy狀態頁:
在19.74上關閉mysql服務,能夠看到haproxy對於後端服務器的檢測是很迅速的:
service mysqld stop
7.額外說明:
繼續以前的實驗,將19.66上的keepalived服務再次啓動,能夠發現,VIP仍然在19.79上,這就是以前爲何要配置不搶佔的緣由。若是按照正常的配置,將19.66配置爲MASTER,當它重啓keepalived服務後,則必定會將VIP搶回。但實際上咱們並不但願這樣,由於19.79仍在正常工做,19.66沒有理由去搶奪資源,形成不必的資源切換。實驗演示就到這裏,謝謝你們!