1. 程式人生 > >centos7下redis哨兵叢集配置

centos7下redis哨兵叢集配置

redis作為一個高效能記憶體資料庫,也常用於系統的快取資料庫,與memcache類似,再生產環境中,當然需要做高可用的結構,即主從複製,替換等功能,可以實現主資料庫掛掉,從庫自動補上,不影響正常使用。

redis的主從,哨兵配置也非常簡單,一主N從,N哨兵都可以。具體的配置方法下面記錄一下:
預設環境: 一主,2叢,3哨兵

此處以1臺機器,3個redis,3哨兵舉例

1.建立redis-ms目錄,裡面依次新建redis-7021目錄,redis-7022目錄,redis-7023目錄。redis-7021為master節點,redis-7022和redis7023為slave節點。

2.主節點配置,進入redis-7021目錄。

  配置redis.conf

# 複製redis-4.0.10下的redis.conf到redis-7021下
cp /usr/local/java/redis-4.0.10/redis.conf /usr/local/java/redis-ms/redis-7021/

# 調整配置如下:
port 7021
# bind 127.0.0.1 (註釋掉這個配置項,允許任意使用者連線)
protected-mode no (關閉保護程序)
daemonize yes (開啟守護程序)
timeout 300 (客戶端限制300秒後關閉連線,0則為不啟動此功能)

  配置sentinel.conf  哨兵檔案

# 複製redis-4.0.10下的sentinel.conf到redis-7021下
cp /usr/local/java/redis-4.0.10/sentinel.conf /usr/local/java/redis-ms/redis-7021/

# 調整配置如下:
### 哨兵配置 ###
#master節點
port 26381

#設定matser主機的IP和埠
#後面的2,表示有兩臺或以上哨兵認定master掛掉了
#則可認為master掛掉,進行master的切換,這裡設定三臺哨兵
sentinel monitor mymaster 192.168.2.2 7021 2

#master或slave多長時間(預設30秒)不能使用後標記為s_down狀態。
sentinel down-after-milliseconds mymaster 30000

#若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。 
sentinel failover-timeout mymaster 900000

#選項指定了在執行故障轉移時,最多可以有多少個從伺服器同時對新的主伺服器進行同步
#這個數字越小,完成故障轉移所需的時間就越長。
sentinel parallel-syncs mymaster 1

#指定master主機的密碼,如果你的redis叢集有密碼
#sentinel auth-pass mymaster 123456 

#新新增的配置,啟用守護程序
daemonize yes 
#關閉保護模式
protected-mode no 
#新新增的,只當日誌檔案位置
logfile "/usr/local/java/redis-ms/redis-7021/sentinel_26381.log" 
#設定pid檔案路徑  
pidfile "/usr/local/java/redis-ms/redis-7021/sentinel_26381.pid"
#工作路徑,不用改  
dir /tmp

3.從節點配置

 3.1進入redis-7022目錄

  配置redis.conf

# 複製redis-4.0.10下的redis.conf到redis-7022下
cp /usr/local/java/redis-4.0.10/redis.conf /usr/local/java/redis-ms/redis-7022/

# 調整配置如下:
port 7022
# bind 127.0.0.1 (註釋掉這個配置項,允許任意使用者連線)
protected-mode no (關閉保護程序)
daemonize yes (開啟守護程序)
timeout 300 (客戶端限制300秒後關閉連線,0則為不啟動此功能)
slaveof 192.168.2.2 7021  (指定master節點)

  配置sentinel.conf  哨兵檔案

# 複製redis-4.0.10下的sentinel.conf到redis-7022下
cp /usr/local/java/redis-4.0.10/sentinel.conf /usr/local/java/redis-ms/redis-7022/

# 調整配置如下:
### 哨兵配置 ###
#master節點
port 26382

#設定matser主機的IP和埠
#後面的2,表示有兩臺或以上哨兵認定master掛掉了
#則可認為master掛掉,進行master的切換,這裡設定三臺哨兵
sentinel monitor mymaster 192.168.2.2 7021 2

#master或slave多長時間(預設30秒)不能使用後標記為s_down狀態。
sentinel down-after-milliseconds mymaster 30000

#若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。 
sentinel failover-timeout mymaster 900000

#選項指定了在執行故障轉移時,最多可以有多少個從伺服器同時對新的主伺服器進行同步
#這個數字越小,完成故障轉移所需的時間就越長。
sentinel parallel-syncs mymaster 1

#指定master主機的密碼,如果你的redis叢集有密碼
#sentinel auth-pass mymaster 123456 

#新新增的配置,啟用守護程序
daemonize yes 
#關閉保護模式
protected-mode no 
#新新增的,只當日誌檔案位置
logfile "/usr/local/java/redis-ms/redis-7022/sentinel_26382.log" 
#設定pid檔案路徑  
pidfile "/usr/local/java/redis-ms/redis-7022/sentinel_26382.pid"
#工作路徑,不用改  
dir /tmp

 3.2進入redis-7023目錄

  配置redis.conf

# 複製redis-4.0.10下的redis.conf到redis-7023下
cp /usr/local/java/redis-4.0.10/redis.conf /usr/local/java/redis-ms/redis-7023/

# 調整配置如下:
port 7023
# bind 127.0.0.1 (註釋掉這個配置項,允許任意使用者連線)
protected-mode no (關閉保護程序)
daemonize yes (開啟守護程序)
timeout 300 (客戶端限制300秒後關閉連線,0則為不啟動此功能)
slaveof 192.168.2.2 7021  (指定master節點)

  配置sentinel.conf  哨兵檔案

# 複製redis-4.0.10下的sentinel.conf到redis-7023下
cp /usr/local/java/redis-4.0.10/sentinel.conf /usr/local/java/redis-ms/redis-7023/

# 調整配置如下:
### 哨兵配置 ###
#master節點
port 26383

#設定matser主機的IP和埠
#後面的2,表示有兩臺或以上哨兵認定master掛掉了
#則可認為master掛掉,進行master的切換,這裡設定三臺哨兵
sentinel monitor mymaster 192.168.2.2 7021 2

#master或slave多長時間(預設30秒)不能使用後標記為s_down狀態。
sentinel down-after-milliseconds mymaster 30000

#若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。 
sentinel failover-timeout mymaster 900000

#選項指定了在執行故障轉移時,最多可以有多少個從伺服器同時對新的主伺服器進行同步
#這個數字越小,完成故障轉移所需的時間就越長。
sentinel parallel-syncs mymaster 1

#指定master主機的密碼,如果你的redis叢集有密碼
#sentinel auth-pass mymaster 123456 

#新新增的配置,啟用守護程序
daemonize yes 
#關閉保護模式
protected-mode no 
#新新增的,只當日誌檔案位置
logfile "/usr/local/java/redis-ms/redis-7023/sentinel_26383.log" 
#設定pid檔案路徑  
pidfile "/usr/local/java/redis-ms/redis-7023/sentinel_26383.pid"
#工作路徑,不用改  
dir /tmp

4.建立啟動配置檔案(一次性啟動redis及哨兵)

touch redis-ms-start-all.sh

編輯內容如下:
cd /usr/local/java/redis-ms/redis-7021
/usr/local/java/redis-4.0.10/src/redis-server redis.conf
/usr/local/java/redis-4.0.10/src/redis-sentinel sentinel.conf
cd ..
cd /usr/local/java/redis-ms/redis-7022
/usr/local/java/redis-4.0.10/src/redis-server redis.conf
/usr/local/java/redis-4.0.10/src/redis-sentinel sentinel.conf
cd ..
cd /usr/local/java/redis-ms/redis-7023
/usr/local/java/redis-4.0.10/src/redis-server redis.conf
/usr/local/java/redis-4.0.10/src/redis-sentinel sentinel.conf
cd ..

 啟動redis服務及哨兵

 /usr/local/java/redis-ms/redis-ms-start-all.sh

5.測試redis服務

## 檢視master節點服務, 7021節點為主節點
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 7021
192.168.2.2:7021> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.2.2,port=7022,state=online,offset=26495,lag=1
slave1:ip=192.168.2.2,port=7023,state=online,offset=26509,lag=1
master_replid:418d4b1ad90eb2bc6ffd523ca88d03655716e804
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26509
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26509
192.168.2.2:7021> exit



## 檢視slave節點服務,7022節點為從節點
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 7022
192.168.2.2:7022> info replication
# Replication
role:slave
master_host:192.168.2.2
master_port:7021
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:67889
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:418d4b1ad90eb2bc6ffd523ca88d03655716e804
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:67889
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:67889
192.168.2.2:7022> exit

## 檢視slave節點服務,7023節點為從節點
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 7023
192.168.2.2:7023> info replication
# Replication
role:slave
master_host:192.168.2.2
master_port:7021
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:77398
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:418d4b1ad90eb2bc6ffd523ca88d03655716e804
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:77398
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:77398
192.168.2.2:7023> exit

6.檢視sentinel的狀態

## 檢視哨兵節點:26381
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 26381
192.168.2.2:26381> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.2.2:7021,slaves=2,sentinels=3
192.168.2.2:26381> exit

## 檢視哨兵節點:26382
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 26382
192.168.2.2:26382> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.2.2:7021,slaves=2,sentinels=3
192.168.2.2:26382> exit

## 檢視哨兵節點:26383
[[email protected] src]# ./redis-cli -h 192.168.2.2 -p 26383
192.168.2.2:26383> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.2.2:7021,slaves=2,sentinels=3
192.168.2.2:26383> exit

7.配置防火牆埠

[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=7021/tcp
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=7022/tcp
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=7023/tcp
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=26381/tcp
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=26382/tcp
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --add-port=26383/tcp
success
[[email protected] redis-7021]# firewall-cmd --reload
success
[[email protected] redis-7021]# firewall-cmd --permanent --zone=public --list-ports
8080/tcp 27017/tcp 6379/tcp 7001/tcp 7002/tcp 7003/tcp 7004/tcp 7005/tcp 7006/tcp 3306/tcp 11211/tcp 3690/tcp 80/tcp 2181/tcp 8081/tcp 8082/tcp 8083/tcp 7021/tcp 7022/tcp 7023/tcp 26381/tcp 26382/tcp 26383/tcp

至此,哨兵叢集配置成功。