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),需要確認 輸入 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 compose 搭建 fastDFS文件服務器
這篇文章主要介紹了如何使用 docker compose 搭建 fastDFS文件服務器,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10
docker --link容器互聯(lián)的實現(xiàn)
–link可以通過容器名互相通信,容器間共享環(huán)境變量,本文主要介紹了docker --link容器互聯(lián)的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
docker-compose部署etcd集群的實現(xiàn)步驟
本文主要介紹了docker-compose部署etcd集群的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Docker網(wǎng)絡之單host網(wǎng)絡及使用案例
本文重點給大家講解Docker單主機網(wǎng)絡的相關(guān)知識及使用案例,重點是使用案例,感興趣的朋友一起看看吧2017-08-08

