[Mac] 使用 redis-cli 連上 Redis Sentinel 管理的 Redis 端點

[Mac] 使用 redis-cli 連上 Redis Sentinel 管理的 Redis 端點

公司的專案用了 Redis Sentinel,它是一個 HA 的架構,

由一個哨兵 (sentinel) 端點來告訴連線的人,

應該要連到哪一個真正的 Redis 端點 (簡單理解是這樣)。

 

但要用 redis-cli 連線 Redis Sentinel 的話,要怎麼連呢?

下面是實驗的結果~

 

假設我們的 Redis Sentinel 是用 Helm 安裝,

架在 Kubernetes 裡的 test-app 命名空間 (namespace),

我們可以在同一個命名空間裡啟動一個 redis 的 pod,例如:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl -n test-app run -it --rm --image=redis redis-client -- /bin/bash
kubectl -n test-app run -it --rm --image=redis redis-client -- /bin/bash
kubectl -n test-app run -it --rm --image=redis redis-client -- /bin/bash

 

接著就可以在這個 pod 裡面,執行 redis-cli 指令,來連線到 Redis Sentinel。

下面的例子裡,我們假設 Redis Sentinel 已經設定成 TLS 模式,

所以要加上 –tls 選項來連線,並提供相關的 Root CA 憑證、server 憑證與私鑰:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
redis-cli -h test-redis.test-app.svc.cluster.local -p 26379 --tls --cacert ca.crt --cert server.crt --key server.key
redis-cli -h test-redis.test-app.svc.cluster.local -p 26379 --tls --cacert ca.crt --cert server.crt --key server.key
redis-cli -h test-redis.test-app.svc.cluster.local -p 26379 --tls --cacert ca.crt --cert server.crt --key server.key

 

成功連上之後,執行  

SENTINEL masters
SENTINEL masters 可以查看 master node 的相關資訊:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test-redis.test-app.svc.cluster.local:26379> SENTINEL masters
1) 1) "name"
2) "master"
3) "ip"
4) "10.0.156.138"
5) "port"
6) "6379"
7) "runid"
8) "e6025262090b263b3fafa42e650f520b1347d500"
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) "941"
19) "last-ping-reply"
20) "941"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "5558"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "165551809"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "5"
test-redis.test-app.svc.cluster.local:26379> SENTINEL masters 1) 1) "name" 2) "master" 3) "ip" 4) "10.0.156.138" 5) "port" 6) "6379" 7) "runid" 8) "e6025262090b263b3fafa42e650f520b1347d500" 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) "941" 19) "last-ping-reply" 20) "941" 21) "down-after-milliseconds" 22) "10000" 23) "info-refresh" 24) "5558" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "165551809" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "5"
test-redis.test-app.svc.cluster.local:26379> SENTINEL masters
1)  1) "name"
    2) "master"
    3) "ip"
    4) "10.0.156.138"
    5) "port"
    6) "6379"
    7) "runid"
    8) "e6025262090b263b3fafa42e650f520b1347d500"
    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) "941"
   19) "last-ping-reply"
   20) "941"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "5558"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "165551809"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "5"

 

或是執行  

SENTINEL get-master-addr-by-name master
SENTINEL get-master-addr-by-name master 取得 master node 的 IP 與 port:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test-redis.test-app.svc.cluster.local:26379> SENTINEL get-master-addr-by-name master
1) "10.0.156.138"
2) "6379"
test-redis.test-app.svc.cluster.local:26379> SENTINEL get-master-addr-by-name master 1) "10.0.156.138" 2) "6379"
test-redis.test-app.svc.cluster.local:26379> SENTINEL get-master-addr-by-name master
1) "10.0.156.138"
2) "6379"

 

取得 Redis master node 的 IP 和 port 後,

就可以使用 redis-cli 直接連線到 master node 了,記得這時候要給 Redis 密碼

(連 Redis Sentinel 時不需要給密碼):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
redis-cli -h 10.0.156.138 -p 6379 --tls --cacert ca.crt --cert server.crt --key server.key -a mypassword
redis-cli -h 10.0.156.138 -p 6379 --tls --cacert ca.crt --cert server.crt --key server.key -a mypassword
redis-cli -h 10.0.156.138 -p 6379 --tls --cacert ca.crt --cert server.crt --key server.key -a mypassword

 

連線成功後,可以執行 ping 測試連線有無正常,

也可以開始執行一般的 Redis 指令如 keys 囉:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
10.0.156.138:6379> ping
PONG
10.0.156.138:6379> keys *
(empty array)
10.0.156.138:6379> ping PONG 10.0.156.138:6379> keys * (empty array)
10.0.156.138:6379> ping
PONG

10.0.156.138:6379> keys *
(empty array)

 

參考資料:How to use redis-cli in redis-sentinel mode

(本頁面已被瀏覽過 540 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料