1. 程式人生 > >Mysql+keepalived

Mysql+keepalived

需求說明

mysql+keepalived實現高可用

環境說明

ip 伺服器型別
172.16.11.19 mysql(master)
172.16.11.21 mysql(backup)

①.關閉防火牆

[[email protected] ~]# systemctl stop firewalld.service 

②.關閉selinux

[[email protected]
~]# setenforce 0 [[email protected] ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

③.配置網路源

[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo 

操作步驟

  • 172.16.11.19

①.安裝mariadb+keepalived

[[email protected] ~]# yum -y install mariadb mariadb-server keepalived

②.給mysql設定密碼並授權給測試使用者

[[email protected] ~]# mysqladmin -uroot password lzh123!
[[email protected] ~]# mysql -uroot -plzh123!
MariaDB [(none)]> grant all privileges on *.* to 
[email protected]
'%' identified by 'test123!'; MariaDB [(none)]> flush privileges;

③.寫控制keepalived的指令碼

[[email protected] ~]# mkdir /scripts/
[[email protected] ~]# vim /scripts/check_my.sh
#!/bin/bash

mysql_status=`ps -ef | grep mysql | grep -Ev "grep|$0" | wc -l`

if [ $mysql_status -eq 0 ];
    systemctl stop keepalived
fi
[[email protected] ~]# chmod +x /scripts/check_my.sh 

④.寫傳送郵件的指令碼

[[email protected] ~]# vim /scripts/notify.sh
#!/bin/bash

VIP=$1
sendmail (){
        subject="${VIP} ip address drift"        content="`date +'%F-%H%M%S'`:`ip a | grep eth0 | grep inet | head -1 | awk -F '[ /]+' '{print $3}'` change to master"
        echo $conter | mail -s "$subject" [email protected]
}

sendmail
[[email protected] ~]# chmod +x /scripts/notify.sh 
[[email protected] ~]# yum -y install mailx

⑤.修改keepalived配置檔案

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {

   router_id lb_01
}
vrrp_script mysql_check {
    script "/scripts/check_my.sh"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.11.200
    }
    track_script {
        mysql_check
    }   
    notify_master "/scripts/notify.sh 172.16.11.200"
}

virtual_server 172.16.11.200 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 172.16.11.19 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.11.21 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}

  • 172.16.11.21
    ①.安裝mariadb+keepalived
[[email protected] ~]# yum -y install mariadb mariadb-server keepalived

②.給mysql設定密碼並授權給測試使用者

[[email protected] ~]# mysqladmin -uroot password lzh123!
[[email protected] ~]# mysql -uroot -plzh123!
MariaDB [(none)]> grant all privileges on *.* to [email protected]'%' identified by 'test123!';
MariaDB [(none)]> flush privileges;

③.寫傳送郵件的指令碼

[[email protected] ~]# vim /scripts/notify.sh
#!/bin/bash

VIP=$1
sendmail (){
        subject="${VIP} ip address drift"        content="`date +'%F-%H%M%S'`:`ip a | grep ens33 | grep inet | head -1 | awk -F '[ /]+' '{print $3}'` change to master"
        echo $conter | mail -s "$subject" [email protected]
}

sendmail
[[email protected] ~]# chmod +x /scripts/notify.sh 
[[email protected] ~]# yum -y install mailx

④.修改keepalived配置檔案

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {

   router_id lb_02
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.11.200
    }
    notify_master "/scripts/notify.sh 172.16.11.200"
}

virtual_server 172.16.11.200 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 172.16.11.19 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.11.21 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}


測試

在master和backup都開啟mariadb和keepalived的時候,可以看到資料庫訪問的是172.16.11.19上面的資料庫

[[email protected] ~]# mysql -utest -ptest123! -h172.16.11.200
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

然後在master上面關閉mariadb,可以看到keepalived被指令碼關閉,虛擬ip漂移到backup上面,然後資料庫訪問的是172.16.11.21上面的資料庫,併發送郵件提示

  • 172.16.11.19
[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: VRRP_Instance(VI_1) Sending/qu...0
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: Sending gratuitous ARP on eth0...0
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: Sending gratuitous ARP on eth0...0
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: Sending gratuitous ARP on eth0...0
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: Sending gratuitous ARP on eth0...0
Nov 01 19:17:08 lizihan Keepalived[13032]: Stopping
Nov 01 19:17:08 lizihan systemd[1]: Stopping LVS and VRRP High Availability M.....
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: VRRP_Instance(VI_1) sent 0 pri...y
Nov 01 19:17:08 lizihan Keepalived_vrrp[13034]: VRRP_Instance(VI_1) removing p....
Nov 01 19:17:09 lizihan systemd[1]: Stopped LVS and VRRP High Availability Mo...r.
Hint: Some lines were ellipsized, use -l to show in full.

  • 172.16.11.21
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:72:aa:10 brd ff:ff:ff:ff:ff:ff
    inet 172.16.11.21/24 brd 172.16.11.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 172.16.11.200/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::c6eb:d9f9:32e9:f3df/64 scope link 
       valid_lft forever preferred_lft forever

訪問結果

[[email protected] ~]# mysql -utest -ptest123! -h172.16.11.200
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cba                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)