共計(jì) 9655 個(gè)字符,預(yù)計(jì)需要花費(fèi) 25 分鐘才能閱讀完成。
這篇文章將為大家詳細(xì)講解有關(guān) Docker 如何搭建 Redis Cluster 集群環(huán)境,丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
使用 Docker 搭建 Redis Cluster,最重要的環(huán)節(jié)就是容器通信的問題,這一塊我們?cè)谥暗奈恼轮幸呀?jīng)給大家解決了《Docker 網(wǎng)絡(luò)模式詳解及容器間網(wǎng)絡(luò)通信》,本篇文章主要練習(xí)使用多個(gè)容器完成 Redis Cluster 集群環(huán)境的搭建,順便為學(xué)習(xí) Docker Compose 鋪鋪路。俗話說(shuō)沒有對(duì)比就沒有傷害,通過對(duì)比才能感受到 Docker Compose 的好處 ????。
關(guān)于 Redis Cluster 集群更多的內(nèi)容請(qǐng)閱讀《最通俗易懂的 Redis 架構(gòu)模式詳解》。
按照 Redis 官網(wǎng):https://redis.io/topics/cluster-tutorial 的提示,為了使 Docker 與 Redis Cluster 兼容,您需要使用 Docker 的 host 網(wǎng)絡(luò)模式。
host 網(wǎng)絡(luò)模式需要在創(chuàng)建容器時(shí)通過參數(shù) –net host 或者 –network host 指定,host 網(wǎng)絡(luò)模式可以讓容器共享宿主機(jī)網(wǎng)絡(luò)棧,容器將不會(huì)虛擬出自己的網(wǎng)卡,配置自己的 IP 等,而是使用宿主機(jī)的 IP 和端口。
環(huán)境
為了讓環(huán)境更加真實(shí),本文使用多機(jī)環(huán)境:
192.168.10.10
192.168.10.11
每臺(tái)機(jī)器所使用的基礎(chǔ)設(shè)施環(huán)境如下:
CentOS 7.8.2003
Docker version 19.03.12
搭建
整體搭建步驟主要分為以下幾步:
下載 Redis 鏡像(其實(shí)這步可以省略,因?yàn)閯?chuàng)建容器時(shí),如果本地鏡像不存在,就會(huì)去遠(yuǎn)程拉取);
編寫 Redis 配置文件;
創(chuàng)建 Redis 容器;
創(chuàng)建 Redis Cluster 集群。
編寫 Redis 配置文件創(chuàng)建目錄及文件
分別在 192.168.10.10 和 192.168.10.11 兩臺(tái)機(jī)器上執(zhí)行以下操作。
# 創(chuàng)建目錄
mkdir -p /usr/local/docker-redis/redis-cluster
# 切換至指定目錄
cd /usr/local/docker-redis/redis-cluster/
# 編寫 redis-cluster.tmpl 文件
vi redis-cluster.tmpl
編寫配置文件
192.168.10.10 機(jī)器的 redis-cluster.tmpl 文件內(nèi)容如下:
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.10
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
192.168.10.11 機(jī)器的 redis-cluster.tmpl 文件內(nèi)容如下:
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.11
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
port:節(jié)點(diǎn)端口;
requirepass:添加訪問認(rèn)證;
masterauth:如果主節(jié)點(diǎn)開啟了訪問認(rèn)證,從節(jié)點(diǎn)訪問主節(jié)點(diǎn)需要認(rèn)證;
protected-mode:保護(hù)模式,默認(rèn)值 yes,即開啟。開啟保護(hù)模式以后,需配置 bind ip 或者設(shè)置訪問密碼;關(guān)閉保護(hù)模式,外部網(wǎng)絡(luò)可以直接訪問;
daemonize:是否以守護(hù)線程的方式啟動(dòng)(后臺(tái)啟動(dòng)),默認(rèn) no;
appendonly:是否開啟 AOF 持久化模式,默認(rèn) no;
cluster-enabled:是否開啟集群模式,默認(rèn) no;
cluster-config-file:集群節(jié)點(diǎn)信息文件;
cluster-node-timeout:集群節(jié)點(diǎn)連接超時(shí)時(shí)間;
cluster-announce-ip:集群節(jié)點(diǎn) IP,填寫宿主機(jī)的 IP;
cluster-announce-port:集群節(jié)點(diǎn)映射端口;
cluster-announce-bus-port:集群節(jié)點(diǎn)總線端口。
每個(gè) Redis 集群節(jié)點(diǎn)都需要打開兩個(gè) TCP 連接。一個(gè)用于為客戶端提供服務(wù)的正常 Redis TCP 端口,例如 6379。還有一個(gè)基于 6379 端口加 10000 的端口,比如 16379。
第二個(gè)端口用于集群總線,這是一個(gè)使用二進(jìn)制協(xié)議的節(jié)點(diǎn)到節(jié)點(diǎn)通信通道。節(jié)點(diǎn)使用集群總線進(jìn)行故障檢測(cè)、配置更新、故障轉(zhuǎn)移授權(quán)等等??蛻舳擞肋h(yuǎn)不要嘗試與集群總線端口通信,與正常的 Redis 命令端口通信即可,但是請(qǐng)確保防火墻中的這兩個(gè)端口都已經(jīng)打開,否則 Redis 集群節(jié)點(diǎn)將無(wú)法通信。
在 192.168.10.10 機(jī)器的 redis-cluster 目錄下執(zhí)行以下命令:
for port in `seq 6371 6373`; do \
mkdir -p ${port}/conf \
PORT=${port} envsubst redis-cluster.tmpl ${port}/conf/redis.conf \
mkdir -p ${port}/data;\
done
在 192.168.10.11 機(jī)器的 redis-cluster 目錄下執(zhí)行以下命令:
for port in `seq 6374 6376`; do \
mkdir -p ${port}/conf \
PORT=${port} envsubst redis-cluster.tmpl ${port}/conf/redis.conf \
mkdir -p ${port}/data;\
done
上面兩段 shell for 語(yǔ)句,意思就是循環(huán)創(chuàng)建 6371 ~ 6376 相關(guān)的目錄及文件。
在 192.168.10.10 機(jī)器執(zhí)行查看命令結(jié)果如下,如果沒有 tree 命令先安裝 yum install -y tree。
在 192.168.10.11 機(jī)器執(zhí)行查看命令結(jié)果如下。
以下內(nèi)容為每個(gè)節(jié)點(diǎn)的配置文件詳細(xì)信息。
============================== 192.168.10.10 ==============================
[root@localhost redis-cluster]# cat /usr/local/docker-redis/redis-cluster/637{1..3}/conf/redis.conf
port 6371
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.10
cluster-announce-port 6371
cluster-announce-bus-port 16371
port 6372
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.10
cluster-announce-port 6372
cluster-announce-bus-port 16372
port 6373
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.10
cluster-announce-port 6373
cluster-announce-bus-port 16373
============================== 192.168.10.10 ==============================
============================== 192.168.10.11 ==============================
[root@localhost redis-cluster]# cat /usr/local/docker-redis/redis-cluster/637{4..6}/conf/redis.conf
port 6374
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.11
cluster-announce-port 6374
cluster-announce-bus-port 16374
port 6375
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.11
cluster-announce-port 6375
cluster-announce-bus-port 16375
port 6376
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.10.11
cluster-announce-port 6376
cluster-announce-bus-port 16376
============================== 192.168.10.11 ==============================
創(chuàng)建 Redis 容器創(chuàng)建容器
將宿主機(jī)的 6371 ~ 6376 之間的端口與 6 個(gè) Redis 容器映射,并將宿主機(jī)的目錄與容器內(nèi)的目錄進(jìn)行映射(目錄掛載)。記得指定網(wǎng)絡(luò)模式,使用 host 網(wǎng)絡(luò)模式。
在 192.168.10.10 機(jī)器執(zhí)行以下命令:
for port in $(seq 6371 6373); do \
docker run -di --restart always --name redis-${port} --net host \
-v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/docker-redis/redis-cluster/${port}/data:/data \
redis redis-server /usr/local/etc/redis/redis.conf; \
done
在 192.168.10.11 機(jī)器執(zhí)行以下命令:
for port in $(seq 6374 6376); do \
docker run -di --restart always --name redis-${port} --net host \
-v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/docker-redis/redis-cluster/${port}/data:/data \
redis redis-server /usr/local/etc/redis/redis.conf; \
done
在 192.168.10.10 機(jī)器執(zhí)行 docker ps -n 3 查看容器是否創(chuàng)建成功。
在 192.168.10.11 機(jī)器執(zhí)行 docker ps -n 3 查看容器是否創(chuàng)建成功。
創(chuàng)建 Redis Cluster 集群
隨便進(jìn)入一個(gè)容器節(jié)點(diǎn),并進(jìn)入 /usr/local/bin/ 目錄:
# 進(jìn)入容器
docker exec -it redis-6371 bash
# 切換至指定目錄
cd /usr/local/bin/
接下來(lái)我們就可以通過以下命令實(shí)現(xiàn) Redis Cluster 集群的創(chuàng)建。
redis-cli -a 1234 --cluster create 192.168.10.10:6371 192.168.10.10:6372 192.168.10.10:6373 192.168.10.11:6374 192.168.10.11:6375 192.168.10.11:6376 --cluster-replicas 1
出現(xiàn)選擇提示信息,輸入 yes,結(jié)果如下所示:
集群創(chuàng)建成功如下:
以下內(nèi)容是創(chuàng)建集群時(shí)返回的詳細(xì)信息,也就是上兩幅圖中的所有內(nèi)容。
root@localhost:/usr/local/bin#redis-cli -a 1234 --cluster create 192.168.10.10:6371 192.168.10.10:6372 192.168.10.10:6373 192.168.10.11:6374 192.168.10.11:6375 192.168.10.11:6376 --cluster-replicas 1
Warning: Using a password with -a or -u option on the command line interface may not be safe.
Performing hash slots allocation on 6 nodes...
Master[0] - Slots 0 - 5460
Master[1] - Slots 5461 - 10922
Master[2] - Slots 10923 - 16383
Adding replica 192.168.10.11:6376 to 192.168.10.10:6371
Adding replica 192.168.10.10:6373 to 192.168.10.11:6374
Adding replica 192.168.10.11:6375 to 192.168.10.10:6372
M: 299cf79ddafc83dced27f628f1f82dac483fbc4e 192.168.10.10:6371
slots:[0-5460] (5461 slots) master
M: ac805b90b6e20e26dc4268454bb2855beea6cc19 192.168.10.10:6372
slots:[10923-16383] (5461 slots) master
S: db35494fcc5db0c88d27da7885c817e6cdcc9373 192.168.10.10:6373
replicates 7013270480d37eeab79b9cd0272e934d4548136a
M: 7013270480d37eeab79b9cd0272e934d4548136a 192.168.10.11:6374
slots:[5461-10922] (5462 slots) master
S: 8435e1b0d51f2690c5f94f9a5682a4ac34e94326 192.168.10.11:6375
replicates ac805b90b6e20e26dc4268454bb2855beea6cc19
S: 7b13c16fa6fe8e13cdc0b4846b87edffed55c62e 192.168.10.11:6376
replicates 299cf79ddafc83dced27f628f1f82dac483fbc4e
Can I set the above configuration? (type yes to accept): yes
Nodes configuration updated
Assign a different config epoch to each node
Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
Performing Cluster Check (using node 192.168.10.10:6371)
M: 299cf79ddafc83dced27f628f1f82dac483fbc4e 192.168.10.10:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 8435e1b0d51f2690c5f94f9a5682a4ac34e94326 192.168.10.11:6375
slots: (0 slots) slave
replicates ac805b90b6e20e26dc4268454bb2855beea6cc19
S: db35494fcc5db0c88d27da7885c817e6cdcc9373 192.168.10.10:6373
slots: (0 slots) slave
replicates 7013270480d37eeab79b9cd0272e934d4548136a
S: 7b13c16fa6fe8e13cdc0b4846b87edffed55c62e 192.168.10.11:6376
slots: (0 slots) slave
replicates 299cf79ddafc83dced27f628f1f82dac483fbc4e
M: 7013270480d37eeab79b9cd0272e934d4548136a 192.168.10.11:6374
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: ac805b90b6e20e26dc4268454bb2855beea6cc19 192.168.10.10:6372
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
至此一個(gè)高可用的 Redis Cluster 集群搭建完成,如下圖所示,該集群中包含 6 個(gè) Redis 節(jié)點(diǎn),3 主 3 從。三個(gè)主節(jié)點(diǎn)會(huì)分配槽,處理客戶端的命令請(qǐng)求,而從節(jié)點(diǎn)可用在主節(jié)點(diǎn)故障后,頂替主節(jié)點(diǎn)。
查看集群狀態(tài)
我們先進(jìn)入容器,然后通過一些集群常用的命令查看一下集群的狀態(tài)。
# 進(jìn)入容器
docker exec -it redis-6371 bash
# 切換至指定目錄
cd /usr/local/bin/
檢查集群狀態(tài)
redis-cli -a 1234 --cluster check 192.168.10.11:6375
查看集群信息和節(jié)點(diǎn)信息
# 連接至集群某個(gè)節(jié)點(diǎn)
redis-cli -c -a 1234 -h 192.168.10.11 -p 6376
# 查看集群信息
cluster info
# 查看集群結(jié)點(diǎn)信息
cluster nodes
SET/GET
在 6371 節(jié)點(diǎn)中執(zhí)行寫入和讀取,命令如下:
# 進(jìn)入容器并連接至集群某個(gè)節(jié)點(diǎn)
docker exec -it redis-6371 /usr/local/bin/redis-cli -c -a 1234 -h 192.168.10.10 -p 6371
# 寫入數(shù)據(jù)
set name mrhelloworld
set aaa 111
set bbb 222
# 讀取數(shù)據(jù)
get name
get aaa
get bbb
別著急,讓我來(lái)解釋一下上圖中的操作過程:
首先進(jìn)入容器并連接至集群某個(gè)節(jié)點(diǎn);
然后執(zhí)行第一個(gè) set 命令 set name mrhelloworld,name 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值為 [5798]。當(dāng)前集群環(huán)境的槽分配情況為:[0-5460] 6371 節(jié)點(diǎn),[5461-10922] 6374 節(jié)點(diǎn),[10923-16383] 6372 節(jié)點(diǎn),所以該鍵的存儲(chǔ)就被分配到了 6374 節(jié)點(diǎn)上;
再來(lái)看第二個(gè) set 命令 set aaa,這里大家可能會(huì)有一些疑問,為什么看不到 aaa 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值?因?yàn)閯偛胖囟ㄏ蛑?6374 節(jié)點(diǎn)插入了數(shù)據(jù),此時(shí)如果還有數(shù)據(jù)插入,正好鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值也還在該節(jié)點(diǎn)的范圍內(nèi),那么直接插入數(shù)據(jù)即可;
接著是第三個(gè) set 命令 set bbb,bbb 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值為 [5287],所以該鍵的存儲(chǔ)就被分配到了 6371 節(jié)點(diǎn)上;
然后是讀取操作,第四個(gè)命令 get name,name 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值為 [5798],被重定向至 6374 節(jié)點(diǎn)讀?。?/p>
第五個(gè)命令 get aaa,aaa 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值也在 6374 節(jié)點(diǎn),直接讀取;
第六個(gè)命令 get bbb,bbb 鍵根據(jù)哈希函數(shù)運(yùn)算以后得到的值為 [5287],被重定向至 6371 節(jié)點(diǎn)讀取。
通過以上操作我們得知 name 鍵的存儲(chǔ)被分配到了 6374 節(jié)點(diǎn),如果直接連接 6374 節(jié)點(diǎn)并獲取該值會(huì)怎么樣?沒錯(cuò),不需要重定向節(jié)點(diǎn),因?yàn)閿?shù)據(jù)就在該節(jié)點(diǎn),所以直接讀取返回。
客戶端連接
最后來(lái)一波客戶端連接操作,隨便哪個(gè)節(jié)點(diǎn),看看可否通過外部訪問 Redis Cluster 集群。
關(guān)于“Docker 如何搭建 Redis Cluster 集群環(huán)境”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。