Rockylinux 9.2部署redis 7.0.11的哨兵模式

Rockylinux 9.2部署redis 7.0.11的哨兵模式

1. redis三種模式說明

1.1 單機模式

單機模式是Redis最簡單的部署形式,僅包含一個Redis實例。

這種模式易于設(shè)置和管理,適用于開發(fā)、測試和小型生產(chǎn)環(huán)境。

單機模式的缺點是,它無法提供高可用性和水平擴展性。如果Redis實例出現(xiàn)故障,可能導(dǎo)致服務(wù)中斷。

1.2 哨兵模式

哨兵模式是基于主從復(fù)制架構(gòu)的,包含一個主節(jié)點和多個從節(jié)點。主節(jié)點負責(zé)讀寫操作,從節(jié)點負責(zé)復(fù)制主節(jié)點的數(shù)據(jù)。

Redis哨兵實例負責(zé)監(jiān)控主從節(jié)點的健康狀況。當主節(jié)點出現(xiàn)故障時,哨兵會自動將一個從節(jié)點提升為新的主節(jié)點,從而實現(xiàn)自動故障轉(zhuǎn)移和高可用性。

哨兵模式提供了讀取操作的水平擴展性,可以將讀請求分發(fā)到從節(jié)點以減輕主節(jié)點的負擔(dān)。但是,它不支持寫操作的水平擴展。

哨兵模式比單機模式更復(fù)雜,需要配置和管理主從復(fù)制和哨兵實例。

作為開發(fā)人員需要知道的是,與單機模式不同,使用哨兵模式時要修改少量代碼。

1.3 集群模式

集群模式是Redis的分布式部署方案,數(shù)據(jù)分布在多個主節(jié)點上,每個主節(jié)點都有一個或多個從節(jié)點。

集群模式提供了高可用性、讀寫操作的水平擴展性和數(shù)據(jù)分片功能。Redis集群通過鍵空間分區(qū)將數(shù)據(jù)分布在多個主節(jié)點上,每個主節(jié)點負責(zé)一部分鍵空間。

當某個主節(jié)點出現(xiàn)故障時,其對應(yīng)的從節(jié)點會被提升為新的主節(jié)點,實現(xiàn)故障轉(zhuǎn)移。

集群模式相對于單機模式和哨兵模式更復(fù)雜,需要配置和管理多個主從節(jié)點和分片策略。

作為開發(fā)人員需要知道的是,與單機模式、哨兵模式不同,使用集群模式時要修改少量代碼。

在實際使用中,集群模式也較少使用,主要還是哨兵模式;集群模式設(shè)備成本、維護成本、開發(fā)成本都比較高;

2. 環(huán)境說明

序號 操作系統(tǒng) IP 主機名 備注
1 Rockylinux 9.2 192.168.3.51 web-c1.tiga.cc 業(yè)務(wù)服務(wù)器C1
2 Rockylinux 9.2 192.168.3.52 web-c2.tiga.cc 業(yè)務(wù)服務(wù)器C2
3 Rockylinux 9.2 192.168.3.53 redis-01.tiga.cc redis主節(jié)點
4 Rockylinux 9.2 192.168.3.54 redis-02.tiga.cc redis從節(jié)點
    +----+         +----+
    | M1 |----+----| R1 |
    | S1 |    |    | S2 |
    +----+    |    +----+
              |
        +-----+-----+
        |           |
        |           |
    +----+        +----+
    | C1 |        | C2 |
    | S3 |        | S4 |
    +----+        +----+

    Configuration: quorum = 3

在實際生產(chǎn)環(huán)境中,大部分中小企業(yè)為了節(jié)省成本,通常不會有很多服務(wù)器專門用來跑redis哨兵或集群;

本文redis一主一從4哨兵,其中2個哨兵進程運行在業(yè)務(wù)(web)服務(wù)器上;

3. 部署redis哨兵

3.1 通過源碼編譯安裝redis

echo "vm.overcommit_memory=1" >> /etc/sysctl.conf 
echo "net.core.somaxconn = 1000" >> /etc/sysctl.conf 
sysctl -p 

mkdir /var/log/redis
mkdir /etc/redis


wget https://github.com/redis/redis/archive/refs/tags/7.0.11.tar.gz

tar -xf 7.0.11.tar.gz
cd redis-7.0.11
make
make install

3.2 修改redis server主節(jié)點配置文件

編輯文件/etc/redis/redis-server.conf

bind 0.0.0.0
port 6379
masterauth tigacc
requirepass tigacc
tcp-backlog 800
maxclients 10000
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
stop-writes-on-bgsave-error yes
save ""
rdbcompression no
dbfilename dump.rdb
dir "/tmp"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 1gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
appendonly no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

3.3 修改redis server從節(jié)點配置文件

# 與主節(jié)點配置文件基本相同
# 只需增加一行指定主節(jié)點IP和端口
slaveof 192.168.3.53 6379

3.4 修改哨兵配置文件

編輯文件/etc/redis/redis-sentinel.conf

protected-mode no
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/redis-sentinel.log"
dir "/tmp"
acllog-max-len 128

# 注意,只有redis 6.2及以上版本才支持填寫主機名;低于6.2的只能填寫IP
sentinel monitor mymaster 192.168.3.53 6379 3
sentinel down-after-milliseconds mymaster 10000


acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster tigacc
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0

3.5 啟動redis與redis哨兵

redis-server /etc/redis/redis-server.conf

redis-sentinel /etc/redis/redis-sentinel.conf

4. 驗證

4.1 向sentinel查詢master的狀態(tài)

redis-cli -p 26379 SENTINEL MASTER mymaster

輸出

 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.3.53"
 5) "port"
 6) "6379"
 7) "runid"
 8) "1ff06f8e4f53f933b69bff997efd15a6fe89e7a0"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "762"
19) "last-ping-reply"
20) "762"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "5192"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "15371"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "1"
33) "num-other-sentinels"
34) "1"
35) "quorum"
36) "3"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

4.2 獲取當前master地址

redis-cli -p 26379  SENTINEL get-master-addr-by-name mymaster

輸出

1) "192.168.3.53"
2) "6379"

4.3 測試故障轉(zhuǎn)移

模擬redis故障

# shutdown -h now

等待一會,獲取master地址

redis-cli -p 26379  SENTINEL get-master-addr-by-name mymaster

輸出

1) "192.168.3.54"
2) "6379"
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容