keepalived高可用搭建
阿新 • • 發佈:2018-11-13
- [1. keepalived 簡介 ##](#1-keepalived-簡介-)
- [1. keepalived](#1-keepalived)
- [2. 安裝keepalived](#2-安裝keepalived)
- [2. keepalived 高可用配置 ##](#2-keepalived-高可用配置-)
- [1. 安裝keepalived](#1-安裝keepalived)
- [2. 配置keepalived以實現虛擬IP繫結](#2-配置keepalived以實現虛擬ip繫結)
1. keepalived 簡介
1. keepalived
Keepalived的作用是檢測伺服器的狀態,如果有一臺web伺服器宕機,或工作出現故障,Keepalived將檢測到,並將有故障的伺服器從系統中剔除,同時使用其他伺服器代替該伺服器的工作,當伺服器工作正常後Keepalived自動將伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工干涉,需要人工做的只是修復故障的伺服器。
2. 安裝keepalived
Centos7 預設的keepalived版本有問題,直接原始碼編譯安裝
這裡下載的keepadlived版本為2.0.7
首先安裝依賴檔案(兩臺機器)
# yum install -y gcc gcc-c++ openssl-devel
然後執行編譯安裝(兩臺機器)
# tar xvf keepalived-2.0.7.tar
# cd keepalived-2.0.7
# ./configure
# make
# make install
複製配置檔案(兩臺機器)
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
2. keepalived 高可用配置
這裡的兩臺伺服器使用keepalived執行虛擬IP的動態切換,具體要求如下:
1. 安裝keepalived
首先兩臺伺服器均要按照上一節描述的方法來進行Keepalived的安裝
2. 配置keepalived以實現虛擬IP繫結
修改配置檔案:/etc/keepalived/keepalived.conf(主)
1 ! Configuration File for keepalived
2
3 global_defs {
4 router_id master
5 }
6
7 vrrp_instance VI_1 {
8 state MASTER
9 interface em2
10 virtual_router_id 52
11 priority 100
12 advert_int 1
13 authentication {
14 auth_type PASS
15 auth_pass 1111
16 }
17 virtual_ipaddress {
18 172.16.0.8/24
19 }
20 }
21
22 virtual_server 192.168.200.100 443 {
注: 檔案其他部分不動
修改配置檔案:/etc/keepalived/keepalived.conf(備)
1 ! Configuration File for keepalived
2
3 global_defs {
4 router_id backup
5 }
6
7 vrrp_instance VI_1 {
8 state BACKUP
9 interface em2
10 virtual_router_id 52
11 priority 90
12 advert_int 1
13 authentication {
14 auth_type PASS
15 auth_pass 1111
16 }
17 virtual_ipaddress {
18 172.16.0.8/24
19 }
20 }
21
22 virtual_server 192.168.200.100 443 {
注: 檔案其他部分不動
兩機器都設定服務啟動,並通過ps檢視程序是否執行
# systemctl start keepalived
# systemctl enable keepalived
然後通過 ip a 來檢視虛擬IP是否繫結成功
# ip a