Mysql+keepalived

需求说明

mysql+keepalived实现高可用

环境说明

ip 服务器类型
172.16.11.19 mysql(master)
172.16.11.21 mysql(backup)

①.关闭防火墙

[root@lizihan ~]# systemctl stop firewalld.service 

②.关闭selinux

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

③.配置网络源

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

操作步骤

  • 172.16.11.19

①.安装mariadb+keepalived

[root@lizihan ~]# yum -y install mariadb mariadb-server keepalived

②.给mysql设置密码并授权给测试用户

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

③.写控制keepalived的脚本

[root@lizihan ~]# mkdir /scripts/
[root@lizihan ~]# 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
[root@lizihan ~]# chmod +x /scripts/check_my.sh 

④.写发送邮件的脚本

[root@lizihan ~]# 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
[root@lizihan ~]# chmod +x /scripts/notify.sh 
[root@lizihan ~]# yum -y install mailx

⑤.修改keepalived配置文件

[root@lizihan ~]# 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
[root@lizihan ~]# yum -y install mariadb mariadb-server keepalived

②.给mysql设置密码并授权给测试用户

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

③.写发送邮件的脚本

[root@lizihan ~]# 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
[root@lizihan ~]# chmod +x /scripts/notify.sh 
[root@lizihan ~]# yum -y install mailx

④.修改keepalived配置文件

[root@lizihan ~]# 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上面的数据库

[root@lizihan ~]# 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
[root@lizihan ~]# systemctl stop mariadb
[root@lizihan ~]# 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
[root@lizihan ~]# 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

访问结果

[root@lizihan ~]# 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)

猜你喜欢

转载自blog.csdn.net/weixin_43154788/article/details/83625753
今日推荐