配置Keepalived實現MySQL高可用
阿新 • • 發佈:2018-11-07
配置環境
- 已關閉防火牆、selinux;
- 伺服器的IP地址及角色如下表:
IP地址 | 伺服器/角色 | 安裝服務 |
---|---|---|
192.168.91.131 | master | Keepalived、MariaDB-server、MariaDB |
192.168.91.132 | backup | Keepalived、MariDB-server、MariaDB |
192.168.91.133 | 測試連線mysql | MariaDB |
192.168.91.254(VIP) | master、backup | ----------------------------------------- |
配置步驟
- 在master和backup兩主機上分別配置網路源、安裝epel-release,並安裝Keepalived和MySQL
[[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
[[email protected] ~]# yum -y install epel-release
[[email protected] ~]# yum -y install keepalived mariadb mariadb-server
- 在master主機上修改/etc/keepalived/keepalived.conf配置檔案
[[email protected] ~]# cp -a /etc/keepalived/keepalived.conf{,.bak}
[ [email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 001
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.131 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.132 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
- 在backup主機上修改/etc/keepalived/keepalived.conf配置檔案
[[email protected] ~]# cp -a /etc/keepalived/keepalived.conf{,.bak}
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 002
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.131 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.132 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
- 在master和backup主機上分別啟動keepalived服務
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ps aux | grep keepalived
root 12929 0.0 0.1 118624 1364 ? Ss 04:26 0:00 /usr/sbin/keepalived -D
root 12930 0.0 0.2 118740 2532 ? S 04:26 0:00 /usr/sbin/keepalived -D
root 12931 0.0 0.1 118624 1832 ? S 04:26 0:00 /usr/sbin/keepalived -D
- 檢視VIP在那臺主機上
## 在master主機上檢視 ##
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:34:95:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.131/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
## 在backup上檢視 ##
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c7:d2:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.132/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
- 在master和backup主機上分別啟動mysqld服務
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[[email protected] ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
- 在master和backup主機上分別設定mysql資料庫密碼
[[email protected] ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): ##按回車
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: ##輸入密碼
Re-enter new password: ##確認密碼
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y ##移除匿名使用者
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n ##不禁止root遠端登入
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y ##移除測試資料庫
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y ##重新載入特權表
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
注:也可以使用mysqladmin設定mysql密碼
[[email protected] ~]# mysqladmin -uroot password 123456
- 在master和backup主機上分別登入mysql,給虛擬IP進行授權
[[email protected] ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on keepalived.* to [email protected] identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 在master主機上建立keepalived資料庫,並建立master表
MariaDB [(none)]> create database keepalived;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use keepalived;
Database changed
MariaDB [keepalived]> create table master(id int null,name char null);
Query OK, 0 rows affected (0.00 sec)
- 在backup主機上建立keepalived資料庫,並建立backup表
MariaDB [(none)]> create database keepalived;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use keepalived;
Database changed
MariaDB [keepalived]> create table backup(id int null,name char null);
Query OK, 0 rows affected (0.00 sec)
- 在192.168.91.133主機上進行測試連線
[[email protected] ~]# mysql -ukeepalived -p123456 -h192.168.91.254
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
- 在master上編寫指令碼來監控mysql資料庫
[[email protected] ~]# mkdir /etc/keepalived/scripts
[[email protected] ~]# vim /etc/keepalived/scripts/check_m.sh
#!/bin/bash
pidof mysqld
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
- 在master上修改/etc/keepalived/keepalived.conf配置檔案,並重啟keepalived服務
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id 001
}
vrrp_script mysql_check {
script "/etc/keepalived/scripts/check_m.sh"
interval 1
weight -20
}
......
virtual_ipaddress {
192.168.91.254
}
track_script {
mysql_check
}
......
[[email protected] ~]# chmod +x /etc/keepalived/scripts/check_m.sh
[[email protected] ~]# systemctl restart keepalived
- 檢查監控指令碼是否生效
[[email protected] ~]# ps aux | grep -v 'grep' | grep keepalived
root 13364 0.0 0.1 118624 1368 ? Ss 06:36 0:00 /usr/sbin/keepalived -D
root 13365 0.0 0.2 118740 2532 ? S 06:36 0:00 /usr/sbin/keepalived -D
root 13366 0.0 0.2 120720 2456 ? S 06:36 0:00 /usr/sbin/keepalived -D
[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# ps aux | grep -v 'grep' | grep keepalived
- 看看master和backup的VIP地址
## master主機上無VIP地址 ##
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:34:95:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.131/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
## backup主機上有VIP地址 ##
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c7:d2:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.132/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
- 在192.168.91.133主機上登入mysql資料庫
[[email protected] ~]# mysql -ukeepalived -p123456 -h192.168.91.254
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 82
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye