基於DOCKER安裝Redis Sentinel 集羣導致的NAT網絡問題解決方法

基於DOCKER安裝Redis Sentinel 集羣導致的NAT網絡問題解決方法

在之前的文章

https://blog.csdn.net/yingziisme/article/details/83034298

記錄瞭如何基於Docker搭建Redis Sentinel 集羣

那個時候留下了一個網絡的問題

在服務器使用docker部署的,但是本地遠程的時候,sentinel集羣返回的地址是docker內部配置的地址,如何指定返回對外的ip

剛開始使用sentinel announce-ip這個配置,後來發現這個配置是用來聲明sentinel的ip,並不會改變返回給客戶端的redis的ip

查閱了redis官方配置文檔之後找到了一個配置,附上官方說明和配置項

# A Redis master is able to list the address and port of the attached
# slaves in different ways. For example the "INFO replication" section
# offers this information, which is used, among other tools, by
# Redis Sentinel in order to discover slave instances.
# Another place where this info is available is in the output of the
# "ROLE" command of a master.
#
# The listed IP and address normally reported by a slave is obtained
# in the following way:
#
#   IP: The address is auto detected by checking the peer address
#   of the socket used by the slave to connect with the master.
#
#   Port: The port is communicated by the slave during the replication
#   handshake, and is normally the port that the slave is using to
#   list for connections.
#
# However when port forwarding or Network Address Translation (NAT) is
# used, the slave may be actually reachable via different IP and port
# pairs. The following two options can be used by a slave in order to
# report to its master a specific set of IP and port, so that both INFO
# and ROLE will report those values.
#
# There is no need to use both the options if you need to override just
# the port or the IP address.
#
# slave-announce-ip 5.5.5.5
# slave-announce-port 1234

以及附上正確網絡連接的配置文件供參考

redis主服務的配置文件


# Redis默認不是以守護進程的方式運行,可以通過該配置項修改,使用yes啓用守護進程
# 啓用守護進程後,Redis會把pid寫到一個pidfile中,在/var/run/redis.pid
daemonize no

# 當Redis以守護進程方式運行時,Redis默認會把pid寫入/var/run/redis.pid文件,可以通過pidfile指定
pidfile /var/run/redis.pid

# 指定Redis監聽端口,默認端口爲6379
# 如果指定0端口,表示Redis不監聽TCP連接
port 6001

# 對外聲明的ip
slave-announce-ip 3x.1xx.1x.1xx


# 當客戶端閒置多長時間後關閉連接,如果指定爲0,表示關閉該功能
timeout 0

# 指定日誌記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認爲verbose
loglevel verbose

# 日誌記錄方式,默認爲標準輸出,如果配置爲redis爲守護進程方式運行,而這裏又配置爲標準輸出,則日誌將會發送給/dev/null
logfile stdout

# 設置數據庫的數量,默認數據庫爲0,可以使用select <dbid>命令在連接上指定數據庫id
# dbid是從0到‘databases’-1的數目
databases 16

################################ SNAPSHOTTING  #################################
# 指定在多長時間內,有多少次更新操作,就將數據同步到數據文件,可以多個條件配合
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   滿足以下條件將會同步數據:
#   900秒(15分鐘)內有1個更改
#   300秒(5分鐘)內有10個更改
#   60秒內有10000個更改
#   Note: 可以把所有“save”行註釋掉,這樣就取消同步操作了

save 900 1
save 300 10
save 60 10000

# 指定存儲至本地數據庫時是否壓縮數據,默認爲yes,Redis採用LZF壓縮,如果爲了節省CPU時間,可以關閉該選項,但會導致數據庫文件變的巨大
rdbcompression yes

# 指定本地數據庫文件名,默認值爲dump.rdb
dbfilename dump.rdb

# 工作目錄.
# 指定本地數據庫存放目錄,文件名由上一個dbfilename配置項指定
# 
# Also the Append Only File will be created inside this directory.
# 
# 注意,這裏只能指定一個目錄,不能指定文件名
dir ./

################################# REPLICATION #################################

# 主從複製。使用slaveof從 Redis服務器複製一個Redis實例。注意,該配置僅限於當前slave有效
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
# 設置當本機爲slav服務時,設置master服務的ip地址及端口,在Redis啓動時,它會自動從master進行數據同步
# slaveof <masterip> <masterport>


# 當master服務設置了密碼保護時,slav服務連接master的密碼
# 下文的“requirepass”配置項可以指定密碼
masterauth 123456
slave-serve-stale-data yes

# 設置Redis連接密碼,如果配置了連接密碼,客戶端在連接Redis時需要通過auth <password>命令提供密碼,默認關閉
requirepass 123456

# 設置同一時間最大客戶端連接數,默認無限制,Redis可以同時打開的客戶端連接數爲Redis進程可以打開的最大文件描述符數,
# 如果設置maxclients 0,表示不作限制。當客戶端連接數到達限制時,Redis會關閉新的連接並向客戶端返回max Number of clients reached錯誤信息
# maxclients 128

# errors for write operations, and this may even lead to DB inconsistency.
# 指定Redis最大內存限制,Redis在啓動時會把數據加載到內存中,達到最大內存後,Redis會先嚐試清除已到期或即將到期的Key,
# 當此方法處理後,仍然到達最大內存設置,將無法再進行寫入操作,但仍然可以進行讀取操作。
# Redis新的vm機制,會把Key存放內存,Value會存放在swap區
# maxmemory <bytes>


############################## APPEND ONLY MODE ###############################
# 指定是否在每次更新操作後進行日誌記錄,Redis在默認情況下是異步的把數據寫入磁盤,如果不開啓,可能會在斷電時導致一段時間內的數據丟失。
# 因爲redis本身同步數據文件是按上面save條件來同步的,所以有的數據會在一段時間內只存在於內存中。默認爲no

appendonly no

# 指定更新日誌條件,共有3個可選值:
# no:表示等操作系統進行數據緩存同步到磁盤(快)
# always:表示每次更新操作後手動調用fsync()將數據寫到磁盤(慢,安全)
# everysec:表示每秒同步一次(折衷,默認值)
appendfsync everysec
no-appendfsync-on-rewrite no


auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-max-len 1024

redis從服務的配置(另一個類似)

# Redis配置文件樣例

# Redis默認不是以守護進程的方式運行,可以通過該配置項修改,使用yes啓用守護進程
# 啓用守護進程後,Redis會把pid寫到一個pidfile中,在/var/run/redis.pid
daemonize no

# 當Redis以守護進程方式運行時,Redis默認會把pid寫入/var/run/redis.pid文件,可以通過pidfile指定
pidfile /var/run/redis.pid

# 指定Redis監聽端口,默認端口爲6379
# 如果指定0端口,表示Redis不監聽TCP連接
port 6002
slave-announce-ip 3x.1xx.1x.1xx

# 當客戶端閒置多長時間後關閉連接,如果指定爲0,表示關閉該功能
timeout 0

# 指定日誌記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認爲verbose
loglevel verbose

# 日誌記錄方式,默認爲標準輸出,如果配置爲redis爲守護進程方式運行,而這裏又配置爲標準輸出,則日誌將會發送給/dev/null
logfile stdout

# 設置數據庫的數量,默認數據庫爲0,可以使用select <dbid>命令在連接上指定數據庫id
# dbid是從0到‘databases’-1的數目
databases 16

################################ SNAPSHOTTING  #################################
# 指定在多長時間內,有多少次更新操作,就將數據同步到數據文件,可以多個條件配合
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   滿足以下條件將會同步數據:
#   900秒(15分鐘)內有1個更改
#   300秒(5分鐘)內有10個更改
#   60秒內有10000個更改
#   Note: 可以把所有“save”行註釋掉,這樣就取消同步操作了

save 900 1
save 300 10
save 60 10000

# 指定存儲至本地數據庫時是否壓縮數據,默認爲yes,Redis採用LZF壓縮,如果爲了節省CPU時間,可以關閉該選項,但會導致數據庫文件變的巨大
rdbcompression yes

# 指定本地數據庫文件名,默認值爲dump.rdb
dbfilename dump.rdb

# 工作目錄.
# 指定本地數據庫存放目錄,文件名由上一個dbfilename配置項指定
# 
# 注意,這裏只能指定一個目錄,不能指定文件名
dir ./

################################# REPLICATION #################################

# 主從複製。使用slaveof從 Redis服務器複製一個Redis實例。注意,該配置僅限於當前slave有效
# 設置當本機爲slav服務時,設置master服務的ip地址及端口,在Redis啓動時,它會自動從master進行數據同步
slaveof 192.168.100.11 6001


# 當master服務設置了密碼保護時,slav服務連接master的密碼
# 下文的“requirepass”配置項可以指定密碼
masterauth 123456
slave-serve-stale-data yes



# 設置Redis連接密碼,如果配置了連接密碼,客戶端在連接Redis時需要通過auth <password>命令提供密碼,默認關閉
requirepass 123456



# 指定是否在每次更新操作後進行日誌記錄,Redis在默認情況下是異步的把數據寫入磁盤,如果不開啓,可能會在斷電時導致一段時間內的數據丟失。
# 因爲redis本身同步數據文件是按上面save條件來同步的,所以有的數據會在一段時間內只存在於內存中。默認爲no
appendonly no


# 指定更新日誌條件,共有3個可選值:
# no:表示等操作系統進行數據緩存同步到磁盤(快)
# always:表示每次更新操作後手動調用fsync()將數據寫到磁盤(慢,安全)
# everysec:表示每秒同步一次(折衷,默認值)

appendfsync everysec
no-appendfsync-on-rewrite no


auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-max-len 1024

哨兵的配置(另外兩個配置除了端口都一樣)

port 7001

sentinel announce-ip 3x.1xx.1x.1xx

# redisgroup:自定義集羣名,如果需要監控多個redis集羣,只需要配置多次並定義不同的<master-name> <master-redis-ip>:主庫ip <master-redis-port>:主庫port <quorum>:最小投票數,由於有三臺redis-sentinel實例,所以可以設置成2
sentinel monitor redisgroup 3x.1xx.1x.1xx 6001 2

# 注:redis主從庫搭建的時候,要麼都不配置密碼(這樣下面這句也不需要配置),不然都需要設置成一樣的密碼
sentinel auth-pass redisgroup 123456


sentinel down-after-milliseconds redisgroup 1000
sentinel failover-timeout redisgroup 1000
# 添加後臺運行
daemonize no


本地啓動springboot的demo測試一下,使用WireShark抓包查看網絡通信信息,可以看到報文信息,並且訪問redis正常

*3
$8
SENTINEL
$23
get-master-addr-by-name
$10
redisgroup
*2
$13
3x.1xx.1x.1xx
$4
6001

停止redis主服務,可以看到報文信息發送新的服務地址,且訪問正常

*3
$8
SENTINEL
$23
get-master-addr-by-name
$10
redisgroup
*2
$13
3x.1xx.1x.1xx
$4
6002

我的博客即將同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=24putm5nxfs0g

歡迎關注微信交流
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章