1. 程式人生 > 實用技巧 >四、keepalived的安裝部署及配置檔案詳解

四、keepalived的安裝部署及配置檔案詳解

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 

二、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  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


}

四、實戰案例

1、部署keepalived+nginx高可用

實驗環境

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

VIP地址已經漂移到了inode2上

重啟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。如下配置:

在inode1 master下配置

! 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

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從

[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任然可以訪問資料庫

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 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