mysql高可用衡搭建(Keepalived)

HelloWorld-Q發表於2020-12-08

2.1 Keepalived介紹

Keepalived的作用是檢測伺服器的狀態,如果有一臺伺服器當機,或工作出現故障,Keepalived將檢測到,並將有故障的伺服器從系統中剔除,同時使用其它伺服器代替該伺服器的工作,當伺服器工作 正常後Keepalived自動將伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工干涉,需要人工做的只是修復故障的伺服器。

192.168.8.110 (主)
192.168.8.111(從)

Keepalived安裝

  1. 安裝Keepalived需要的擴充套件
[root@localhost keepalived]# yum install gcc gcc-c++ openssl openssl-devel
[root@localhost home]# wget -q https://www.keepalived.org/software/keepalived-1.2.18.tar.gz
  1. 解壓Keepalived並安裝
[root@localhost home]# tar -zxvf keepalived-1.2.18.tar.gz
[root@localhost home]# cd keepalived-1.2.18
[root@localhost keepalived-1.2.18]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.2.18]# make && make install
  1. 將 keepalived 安裝成 Linux 系統服務

因為沒有使用 keepalived 的預設路徑安裝(預設是/usr/local) ,安裝完成之後,需要做一些工作 複製預設配置檔案到預設路徑

[root@localhost keepalived-1.2.18]# mkdir /etc/keepalived
[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

複製 keepalived 服務指令碼到預設的地址

[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.2.18]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.2.18]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

設定 keepalived 服務開機啟動

[root@localhost keepalived-1.2.18]# chkconfig keepalived on

3. mysql高可用搭建

3.1 Keepalived配置

keepalived主機配置

! Configuration File for keepalived
global_defs {
    router_id LVS_MASTER
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/haproxy_check.sh" ## 檢測 haproxy 狀態的指令碼路徑
    interval 2 ## 檢測時間間隔
    weight 2 ## 如果條件成立,權重+2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 79
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.29.101
    }
    track_script {
        chk_haproxy
    }
}
# 寫VIP virtual_server,只配置本地機器
virtual_server 192.168.8.101 3307 {# 定義虛擬伺服器,地址與上面的virtual_ipaddress相同
    delay_loop 3 # 健康檢查時間間隔,3秒
    lb_algo rr # 負載均衡排程演算法:rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind DR # 負載均衡轉發規則:NAT|DR|TUN
    # persistence_timeout 5 # 會話保持時間5秒,動態服務建議開啟
    protocol TCP # 轉發協議protocol,一般有tcp和udp兩種
    real_server 192.168.8.110 3307 {
        weight 1 # 權重越大負載分越大,0表示失效
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

Keepalived從機配置

! Configuration File for keepalived
global_defs {
    router_id LVS_MASTER
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/haproxy_check.sh" ## 檢測 haproxy 狀態的指令碼路徑
    interval 2 ## 檢測時間間隔
    weight 2 ## 如果條件成立,權重+2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 79
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.8.101
    }
    track_script {
        chk_haproxy
    }
}
# 寫VIP virtual_server,只配置本地機器
virtual_server 192.168.8.101 3307 {# 定義虛擬伺服器,地址與上面的virtual_ipaddress相同
    delay_loop 3 # 健康檢查時間間隔,3秒
    lb_algo rr # 負載均衡排程演算法:rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind DR # 負載均衡轉發規則:NAT|DR|TUN
    # persistence_timeout 5 # 會話保持時間5秒,動態服務建議開啟
    protocol TCP # 轉發協議protocol,一般有tcp和udp兩種
    real_server 192.168.8.111 3307 {
        weight 1 # 權重越大負載分越大,0表示失效
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

看網路卡192.168.8.110(多了一塊網路卡) 192.168.8.110 (沒有增加網路卡)

mysql高可用衡搭建(Keepalived)

測試192.168.8.110 (主停掉keepalived)
看192.168.8.111 的網路卡會新增192.168.8.101 這就是漂移

mysql高可用衡搭建(Keepalived)

本地測試(同樣輪訓)
D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.101 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>mysql -h192.168.8.101 -uqiao -proot -P3307  -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+

D:\phpstudy_pro\Extensions\MySQL5.7.26\bin>
本作品採用《CC 協議》,轉載必須註明作者和本文連結
有夢想的人睡不著,沒有夢想的人睡不醒。

相關文章