keepalived的安裝部署及配置檔案詳解
一、實驗環境
[[email protected] ~]# uname -r3.10.0-862.el7.x86_64
[[email protected]~]# cat /etc/redhat-release
CentOS Linux release7.5.1804(Core)
keepalived安裝版本:
keepalived-2.0.20.tar.gz
二、keepalived的安裝
yum install -y keepalived
注意:keepalived與3.10的核心有相容性問題,不建議源碼安裝
三、keepalived.conf詳解
vim /etc/keepalived/keepalived.conf
#全局定義塊
global_defs {
notification_email { #指定keepalived在發生切換時需要發送email到的對象,一行一個;
[email protected]
}
notification_email_from [email protected] #指定發件人
smtp_server mail.jfedu.net #指定smtp伺服器位址
smtp_connect_timeout3#指定smtp連接配接逾時時間
router_id LVS_DEVEL #運作keepalived機器的辨別,使用hostname
}
#監控Nginx程序
vrrp_script chk_nginx {
script"/data/script/nginx.sh"#監控服務腳本,腳本x執行權限;
interval2#檢測時間間隔(執行腳本間隔)
weight2 #腳本條件成立,優先級+2("-"為減)
}
#VRRP執行個體定義塊
vrrp_sync_group VG_1{ #監控多個網段的執行個體
group {
VI_1 #執行個體名
VI_2
}
notify_master/data/sh/nginx.sh#指定當切換到master時,執行的腳本(常用與挂載檔案系統)
notify_backup/data/sh/nginx.sh#指定當切換到backup時,執行的腳本(常用與挂載檔案系統)
notify/data/sh/nginx.sh#發生任何切換,均執行的腳本
smtp_alert #使用global_defs中提供的郵件位址和smtp伺服器發送郵件通知(不常用);
}
vrrp_instance VI_1 {
inode1和inode2yum install -y nginx
inode1echo "www.inode1.com" > /usr/share/nginx/html/index.html
[[email protected]~]# curl 192.168.32.101www.inode1.com
inode2echo "www.inode2.com" > /usr/share/nginx/html/index.html
[[email protected]~]# curl 192.168.32.102www.inode2.com
}
四、實戰案例
1、部署keepalived+nginx高可用
實驗環境
keepalived:
inode1:192.168.32.101master
inode2:192.168.32.102backup
nginx:
inode1:192.168.32.101-----www.inode3.com
inode2:192.168.32.102-----www.inode4.com
VIP位址:192.168.32.222
nginx部署
inode1和inode2yum install -y nginx
inode1echo "www.inode1.com" > /usr/share/nginx/html/index.html
[[email protected]~]# curl 192.168.32.101www.inode1.com
inode2echo "www.inode2.com" > /usr/share/nginx/html/index.html
[[email protected]~]# curl 192.168.32.102www.inode2.com
keepalived部署
inode1和inode2
yum install -y keepalived
inode1 master的keepalived.conf檔案
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id LVS_1
}
vrrp_script chk_nginx {
script"/server/sh/nginx_status.sh"interval2weight2}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id51priority100advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_nginx
}
}
inode2 backup的keepalived.conf檔案
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id LVS_1
}
vrrp_script chk_nginx {
script"/server/sh/nginx_status.sh"interval2weight2}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id51priority90advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_nginx
}
}
nginx_status.sh
#!/bin/bashif [ $(pidof nginx|wc -l) -eq 0 ];thensystemctl stop keepalived.servicefichomd o+x /server/sh/nginx_status.sh
啟動keepalived
[[email protected] sh]# systemctl start keepalived
[[email protected]]# ps -ef |grepkeepalived
root12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived
[[email protected]]# systemctl start keepalived
[[email protected]]# ps -ef |grepkeepalived
root12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D
root12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived
檢視VIP位址
[[email protected] sh]# ip add list|grep 192.168.32.222
inet 192.168.32.222/32 scope global eth0
通路192.168.32.222的頁面
[[email protected] sh]# curl 192.168.32.222
www.inode1.com
關閉inode1上的nginx
[[email protected] sh]# nginx -s stop
[[email protected]]# ip add list|grep 192.168.32.222[[email protected]]# ps -ef |grepkeepalived
root12688 2016 0 06:33 pts/0 00:00:00 grep --color=auto keepalived
再次通路192.168.32.222的頁面
[[email protected] sh]# curl 192.168.32.222
www.inode2.com
頁面内容為inode2的内容
在inode2上檢視VIP
[[email protected] sh]# ip addr list|grep 192.168.32.222
inet 192.168.32.222/32 scope global eth0
VIP位址已經漂移到了inode2上
重新開機inode1的nginx和keepalived
[[email protected] sh]# nginx
[[email protected]]# systemctl start keepalived
[roo[email protected]]# ip addr |grep 192.168.32.222inet192.168.32.222/32 scope global eth0
可以看下inode1上nginx和keepalived啟動後,VIP有回到了inode1上,原因為,inode1上的keepalived的優先級高于inode2的優先級。
在一些情況下,由于業務的特殊需求,不要master搶占VIP。如下配置:
在inode1 master下配置
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id inode1
}
vrrp_script chk_nginx {
script"/server/sh/nginx_status.sh"interval2weight2}
vrrp_instance VI_1 {
state BACKUP #把state 該為BACKUP,因為不搶占隻在BACKUP下有效
nopreempt #不搶占
interface eth0
virtual_router_id51priority100advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_nginx
}
}
關閉inode1上的nginx
[[email protected] sh]# nginx -s stop
[[email protected]]# ip addr |grep 192.168.32.222[[email protected]]# curl 192.168.32.222www.inode2.com
#VIP已經漂移到了inode2上
inode1重新開機nginx和keepalived
[[email protected] sh]# nginx
[[email protected]]# systemctl start keepalived
[[email protected]]# curl 192.168.32.222www.inode2.com
[[email protected]]# ip addr |grep 192.168.32.222#可以看到inode1沒有搶占VIP
2、部署mysql主主+keepalived
實驗環境:
client:
inode3:192.168.32.103mysql:
inode1:192.168.32.101inode2:192.168.32.102keepalived:
inode1:192.168.32.101inode2:192.168.32.102VIP:192.168.32.222keepalived不搶占VIP
1、mysql部署
inode1和inode2
yum install -y mariadb mariadb-server mariadb-devel
2、mysql啟動和初始化
inode1和inode2
systemctl start mariadb
3、修改my.cnf,在[mysqld]子產品下添加log_bin和server_id兩項,并重新開機mariadb
inode1
[mysqld]
log_bin=inode1-bin
server_id=101
inode2
[mysqld]
log_bin=inode2-bin
server_id=102
inode1和inode2
systemctl restart mariadb
部署mysql主主
inode1和indoe2
配置遠端登陸賬戶和密碼
mysql-uroot -e "grant all on *.* to"root"@'192.168.32.%' identified by '123456';"配置主主
mysql-uroot -e "grant replication slave on *.* to"tongbu"@'192.168.32.%' identified by '123456';"
inode1主 inode2從
[[email protected] ~]# mysql -uroot -e "show master status;"
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| inode1-bin.000001 | 535 | | |
+-------------------+----------+--------------+------------------+[[email protected]]# mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.32.101',MASTER_USER='tongbu',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='inode1-bin.000001',MASTER_LOG_POS=535;"
inode2主 inode1從
[[email protected] sh]# mysql -uroot -e "show master status;"
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| inode2-bin.000001 | 535 | | |
+-------------------+----------+--------------+------------------+[[email protected]~]# mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.32.102',MASTER_USER='tongbu',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='inode2-bin.000001',MASTER_LOG_POS=535;"
啟動start slave inode1和inode2
mysql -uroot -e "start slave;"
檢視主主
[[email protected] ~]# mysql -uroot -e "show slave status\G;"|awk /Running/Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[[email protected]]# mysql -uroot -e "show slave status\G;"|awk /Running/Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4、keepalived部署
inode1和inode2
yum install -y keepalived
inode1 master的keepalived.conf檔案
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id inode1
}
vrrp_script chk_mysql {
script"/server/sh/mysql_status.sh"interval2weight2}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id51priority100advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_mysql
}
}
inode2 backup的keepalived.conf檔案
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id inode1
}
vrrp_script chk_mysql {
script"/server/sh/mysql_status.sh"interval2weight2}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id51priority90advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_mysql
}
}
mysql_status.sh
#!/bin/bash
NUM=$(ps -ef|grep mysql|grep -v grep|grep -v mysql_status.sh|wc -l)if [ $NUM -eq 0 ];thensystemctl stop keepalivedfichomd o+x /server/sh/mysql_status.sh
啟動keepalived
systemctl start keepalived
[[email protected]~]# systemctl start keepalived
[[email protected]~]# ps -ef |grepkeepalived
root13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D
root13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D
root13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D
root17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived
[[email protected]~]# systemctl start keepalived
[[email protected]~]# ps -ef |grepkeepalived
root13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D
root13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D
root13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D
root17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived
測試:
先檢視inode1和inode2上的資料庫
[[email protected] ~]# mysql -uroot -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+[[email protected]~]# mysql -uroot -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
在inode3上可以使用VIP登陸mysql,并建立ywx資料庫
[[email protected] ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "create database ywx charset=utf8;"
再次檢視inode1和inode2上的資料庫
[[email protected] ~]# mysql -uroot -e "show databases;"grepywx+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ywx |
+--------------------+[[email protected]~]# mysql -uroot -e "show databases;"grepywx+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ywx |
+--------------------+
檢視VIP位址
[[email protected] ~]# ip addr list|grep 192.168.32.222
inet 192.168.32.222/32 scope global eth0
#vip在inode2上
測試:
關閉indoe2上的資料庫,再次在inode3上使用VIP檢視資料ywx
[[email protected]]# ip addr list|grep 192.168.32.222inet192.168.32.222/32scope global eth0
[[email protected]]# systemctl stop mariadb
[[email protected]]# ip addr list|grep 192.168.32.222[[email protected]]#
[[email protected]]# ip addr list |grep 192.168.32.222inet192.168.32.222/32scope global eth0
[[email protected]~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ywx |
+--------------------+
inode3任然可以通路資料庫
3、使用keepalived+mysql主主,配置2個VIP
要求:
VIP1:192.168.32.222
inode1為master inode2為backup
VIP2: 192.168.32.223
inode2為master inode1為backup
keepalived.conf配置如下:
indoe1:
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id inode1
}
vrrp_script chk_mysql {
script"/server/sh/mysql_status.sh"interval2weight2}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id51priority100advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_mysql
}
}
vrrp_instance VI_3 {
state MASTER
interface eth0
virtual_router_id52priority90advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.223}
track_script {
chk_mysql
}
}
indoe1:
! Configuration File forkeepalived
global_defs {
notification_email {
[email protected]
}
[email protected]
smtp_server183.3.225.42#qq smtp_server ip
smtp_connect_timeout30router_id inode1
}
vrrp_script chk_mysql {
script"/server/sh/mysql_status.sh"interval2weight2}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id51priority90advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.222}
track_script {
chk_mysql
}
}
vrrp_instance VI_4 {
state MASTER
interface eth0
virtual_router_id52priority100advert_int1authentication {
auth_type PASS
auth_pass1111}
virtual_ipaddress {192.168.32.223}
track_script {
chk_mysql
}
}
檢視VIP分布
[[email protected] sh]# ip addr list |egrep "192.168.32.22[2|3]"inet192.168.32.222/32scope global eth0
[[email protected]]# ip addr list|egrep "192.168.32.22[2|3]"inet192.168.32.223/32 scope global eth0
在inode3上放為VIP1和VIP2
[[email protected] ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ywx |
+--------------------+[[email protected]~]# mysql -uroot -p123456 -h 192.168.32.223 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ywx |
+--------------------+
關閉inode2上的mysql,VIP2會漂移到inode1上
[[email protected] sh]# ip addr list|egrep "192.168.32.22[2|3]"inet192.168.32.223/32scope global eth0
[[email protected]]# systemctl stop mariadb
[[email protected]]# ip addr list|egrep "192.168.32.22[2|3]"[[email protected]]#
[[email protected]]# ip addr list |egrep "192.168.32.22[2|3]"inet192.168.32.222/32scope global eth0
inet192.168.32.223/32 scope global eth0