docker-compose安裝redis集群教程
docker-compose安裝redis集群
1、安裝docker-compose命令
此處略過
2、編寫docker-compose.yml文件
version: '2.2'
services:
redis-node1:
image: redis:5.0
## --cluster-announce-ip 你的公網(wǎng)IP 這部分參數(shù)
command: redis-server --port 7000 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7000:7000"
- "17000:17000"
volumes:
- ./data/node1:/data
networks:
- redis-cluster
redis-node2:
image: redis:5.0
command: redis-server --port 7001 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7001:7001"
- "17001:17001"
volumes:
- ./data/node2:/data
networks:
- redis-cluster
redis-node3:
image: redis:5.0
command: redis-server --port 7002 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7002:7002"
- "17002:17002"
volumes:
- ./data/node3:/data
networks:
- redis-cluster
redis-node4:
image: redis:5.0
command: redis-server --port 7003 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7003:7003"
- "17003:17003"
volumes:
- ./data/node4:/data
networks:
- redis-cluster
redis-node5:
image: redis:5.0
command: redis-server --port 7004 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7004:7004"
- "17004:17004"
volumes:
- ./data/node5:/data
networks:
- redis-cluster
redis-node6:
image: redis:5.0
command: redis-server --port 7005 --requirepass lingco --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10
ports:
- "7005:7005"
- "17005:17005"
volumes:
- ./data/node6:/data
networks:
- redis-cluster
redis-init:
image: redis:5.0
command: >
sh -c "redis-server --port 7000 --cluster-enabled yes --cluster-config-file /data/nodes.conf --appendonly yes --bind $$(hostname -i)
&& sleep 5
&& redis-cli --cluster create redis-node1:7000 redis-node2:7001 redis-node3:7002 redis-node4:7003 redis-node5:7004 redis-node6:7005 --cluster-replicas 1"
depends_on:
- redis-node1
networks:
- redis-cluster
redis-node7:
image: redis:5.0
command: redis-server --port 7006 --appendonly yes --bind 0.0.0.0 --cluster-announce-ip 192.168.1.10 --cluster-announce-port 7006 --cluster-announce-bus-port 17006
ports:
- "7006:7006"
- "17006:17006"
volumes:
- ./data/node7:/data
networks:
redis-cluster:
備注:端口7000-7005為集群,密碼統(tǒng)一為 lingco,7006為單節(jié)點無密碼;在同路徑下創(chuàng)建 volumes配置的文件夾。
3、執(zhí)行docker compose命令
在docker-compose.yml目錄中執(zhí)行命令 docker-compose up -d,如果需要在其它目錄執(zhí)行,則需要指定配置文件yml的路徑。
4、查看執(zhí)行結(jié)果

5、創(chuàng)建集群
如果docker-compose.yml中command未執(zhí)行成功或是集群未創(chuàng)建,則需要手動執(zhí)行,如下:
- 步驟1、通過命令找到redis集群中的一臺,執(zhí)行:docker ps -a 命令,找到docker容器id
- 步驟2、執(zhí)行bash命令:docker exec -it 2b93cf1f4c4f bash,進入控制臺
- 步驟3、執(zhí)行創(chuàng)建集群:
redis-cli --cluster create 192.168.1.10:7000 192.168.1.10:7001 192.168.1.10:7002 192.168.1.10:7003 192.168.1.10:7004 192.168.1.10:7005 --cluster-replicas 1 -a lingco
備注:如果集群設(shè)置有密碼,則需要 -a 密碼(lingco),需要確認(rèn) 輸入 yes

集群創(chuàng)建成功,可以通過redis管理工具連接

6、問題排查
采用docker compose安裝redis集群,端口正常啟動,無法連接


測試連接可以使用,使用庫號無法連接,出現(xiàn)如上問題可能是 集群未創(chuàng)建成功,需要手動執(zhí)行創(chuàng)建集群
通過上面 5 中的 步驟1,步驟2,步驟3,來執(zhí)行。
如果執(zhí)行 “步驟3” 時出現(xiàn)提示:
“[ERR] Node xxx is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.”
則需要清空集群中所有節(jié)點的數(shù)據(jù) data,重新執(zhí)行命令進行集群創(chuàng)建。

如果執(zhí)行 “步驟3” 時出現(xiàn)提示:
“[ERR] Node 192.168.1.10:7001 NOAUTH Authentication required.”
則需要在命令中增加 -a參數(shù)來指定密碼,
如:
redis-cli --cluster create 192.168.1.10:7000 192.168.1.10:7001 192.168.1.10:7002 192.168.1.10:7003 192.168.1.10:7004 192.168.1.10:7005 --cluster-replicas 1 -a lingco
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Docker 容器文件系統(tǒng)詳細(xì)介紹(圖文)
這篇文章主要介紹了Docker 容器文件系統(tǒng)詳細(xì)介紹(圖文)的相關(guān)資料,這里對Docker 容器文件系統(tǒng)進行了具體的分析詳解,需要的朋友可以參考下2016-12-12
Docker部署Web在線版的PPT應(yīng)用程序PPTist
PPTist作為一款開源的Web應(yīng)用程序,無需安裝、跨平臺且支持實時協(xié)作的幻燈片制作,本文介紹了如何使用Docker部署Web在線版的PPTist,包括本地環(huán)境規(guī)劃、檢查、下載PPTist鏡像、部署PPTist應(yīng)用以及訪問PPTist首頁的步驟2025-02-02
Docker部署Nginx設(shè)置環(huán)境變量的實現(xiàn)步驟
本文主要介紹了Docker部署Nginx設(shè)置環(huán)境變量的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
詳解docker國內(nèi)鏡像拉取和鏡像加速registry-mirrors配置修改
由于國內(nèi)訪問直接訪問Docker hub網(wǎng)速比較慢,拉取鏡像的時間就會比較長。一般我們會使用鏡像加速或者直接從國內(nèi)的一些平臺鏡像倉庫上拉取2017-05-05

