keepalived的安裝部署及配置文件詳解
一、實驗環境
[root@inode1 ~]# uname -r 3.10.0-862.el7.x86_64 [root@inode1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) keepalived安裝版本: keepalived-2.0.20.tar.gz
#全局定義塊 global_defs { notification_email { #指定keepalived在發生切換時需要發送email到的對象,一行一個; wgkgood@gmail.com } notification_email_from root@localhost #指定發件人 smtp_server mail.jfedu.net #指定smtp服務器地址 smtp_connect_timeout 3 #指定smtp連接超時時間 router_id LVS_DEVEL #運行keepalived機器的標識,使用hostname } #監控Nginx進程 vrrp_script chk_nginx { script "/data/script/nginx.sh" #監控服務腳本,腳本x執行權限; interval 2 #檢測時間間隔(執行腳本間隔) weight 2 #腳本條件成立,優先級+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和inode2 yum install -y nginx inode1 echo "www.inode1.com" > /usr/share/nginx/html/index.html [root@inode1 ~]# curl 192.168.32.101 www.inode1.com inode2 echo "www.inode2.com" > /usr/share/nginx/html/index.html [root@inode4 ~]# curl 192.168.32.102 www.inode2.com
}
keepalived: inode1:192.168.32.101 master inode2:192.168.32.102 backup nginx: inode1:192.168.32.101-----www.inode3.com inode2:192.168.32.102-----www.inode4.com VIP地址:192.168.32.222
nginx部署
inode1和inode2 yum install -y nginx inode1 echo "www.inode1.com" > /usr/share/nginx/html/index.html [root@inode1 ~]# curl 192.168.32.101 www.inode1.com inode2 echo "www.inode2.com" > /usr/share/nginx/html/index.html [root@inode4 ~]# curl 192.168.32.102 www.inode2.com
keepalived部署
inode1和inode2 yum install -y keepalived
inode1 master的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id LVS_1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } 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 { 192.168.32.222 } track_script { chk_nginx } }
inode2 backup的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id LVS_1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_nginx } }
nginx_status.sh
#!/bin/bash if [ $(pidof nginx|wc -l) -eq 0 ];then systemctl stop keepalived.service fi chomd o+x /server/sh/nginx_status.sh
啟動keepalived
[root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ps -ef |grep keepalived root 12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ps -ef |grep keepalived root 12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived
查看VIP地址
[root@inode1 sh]# ip add list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
訪問192.168.32.222的頁面
[root@inode1 sh]# curl 192.168.32.222 www.inode1.com
關閉inode1上的nginx
[root@inode1 sh]# nginx -s stop [root@inode1 sh]# ip add list|grep 192.168.32.222 [root@inode1 sh]# ps -ef |grep keepalived root 12688 2016 0 06:33 pts/0 00:00:00 grep --color=auto keepalived
再次訪問192.168.32.222的頁面
[root@inode1 sh]# curl 192.168.32.222 www.inode2.com 頁面內容為inode2的內容
在inode2上查看VIP
[root@inode2 sh]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
重啟inode1的nginx和keepalived
[root@inode1 sh]# nginx [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ip addr |grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
可以看下inode1上nginx和keepalived啟動后,VIP有回到了inode1上,原因為,inode1上的keepalived的優先級高於inode2的優先級。
在一些情況下,由於業務的特殊需求,不要master搶占VIP。如下配置:
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP #把state 該為BACKUP,因為不搶占只在BACKUP下有效 nopreempt #不搶占 interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_nginx } }
關閉inode1上的nginx
[root@inode1 sh]# nginx -s stop [root@inode1 sh]# ip addr |grep 192.168.32.222 [root@inode1 sh]# curl 192.168.32.222 www.inode2.com #VIP已經漂移到了inode2上
inode1重啟nginx和keepalived
[root@inode1 sh]# nginx [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# curl 192.168.32.222 www.inode2.com [root@inode1 sh]# ip addr |grep 192.168.32.222 #可以看到inode1沒有搶占VIP
2、部署mysql主主+keepalived
client: inode3:192.168.32.103 mysql: inode1:192.168.32.101 inode2:192.168.32.102 keepalived: inode1:192.168.32.101 inode2:192.168.32.102 VIP: 192.168.32.222 keepalived不搶占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
[mysqld] log_bin=inode1-bin server_id=101
inode2
[mysqld] log_bin=inode2-bin server_id=102
inode1和inode2
systemctl restart mariadb
部署mysql主主
配置遠程登陸賬戶和密碼 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從
[root@inode1 ~]# mysql -uroot -e "show master status;" +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | inode1-bin.000001 | 535 | | | +-------------------+----------+--------------+------------------+ [root@inode2 sh]# 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從
[root@inode2 sh]# mysql -uroot -e "show master status;" +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | inode2-bin.000001 | 535 | | | +-------------------+----------+--------------+------------------+ [root@inode1 ~]# 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;"
查看主主
[root@inode1 ~]# mysql -uroot -e "show slave status\G;"|awk /Running/ Slave_IO_Running: Yes Slave_SQL_Running: Yes [root@inode2 sh]# 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 for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } 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 { 192.168.32.222 } track_script { chk_mysql } }
inode2 backup的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } 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 ];then systemctl stop keepalived fi chomd o+x /server/sh/mysql_status.sh
啟動keepalived
systemctl start keepalived [root@inode1 ~]# systemctl start keepalived [root@inode1 ~]# ps -ef |grep keepalived root 13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D root 17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived [root@inode2 ~]# systemctl start keepalived [root@inode2 ~]# ps -ef |grep keepalived root 13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D root 17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived
先查看inode1和inode2上的數據庫
[root@inode1 ~]# mysql -uroot -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ [root@inode2 ~]# mysql -uroot -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+
在inode3上可以使用VIP登陸mysql,並創建ywx數據庫
[root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "create database ywx charset=utf8;"
再次查看inode1和inode2上的數據庫
[root@inode1 ~]# mysql -uroot -e "show databases;"grep ywx +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+ [root@inode2 ~]# mysql -uroot -e "show databases;"grep ywx +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
查看VIP地址
[root@inode2 ~]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 #vip在inode2上
測試:
關閉indoe2上的數據庫,再次在inode3上使用VIP查看數據ywx [root@inode2 sh]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 [root@inode2 sh]# systemctl stop mariadb [root@inode2 sh]# ip addr list|grep 192.168.32.222 [root@inode2 sh]# [root@inode1 sh]# ip addr list |grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 [root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
inode3任然可以訪問數據庫
要求:
VIP1:192.168.32.222 inode1為master inode2為backup VIP2: 192.168.32.223 inode2為master inode1為backup
indoe1:
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } 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 { 192.168.32.222 } track_script { chk_mysql } } vrrp_instance VI_3 { state MASTER interface eth0 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.223 } track_script { chk_mysql } }
indoe1:
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_mysql } } vrrp_instance VI_4 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.223 } track_script { chk_mysql } }
查看VIP分布
[root@inode1 sh]# ip addr list |egrep "192.168.32.22[2|3]" inet 192.168.32.222/32 scope global eth0 [root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" inet 192.168.32.223/32 scope global eth0
在inode3上放為VIP1和VIP2
[root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+ [root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.223 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
關閉inode2上的mysql,VIP2會漂移到inode1上
[root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" inet 192.168.32.223/32 scope global eth0 [root@inode2 sh]# systemctl stop mariadb [root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" [root@inode2 sh]# [root@inode1 sh]# ip addr list |egrep "192.168.32.22[2|3]" inet 192.168.32.222/32 scope global eth0 inet 192.168.32.223/32 scope global eth0