您好,登錄后才能下訂單哦!
本篇內容介紹了“Docker-Compose如何搭建Redis集群”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
3主+3從
由于僅用于測試,故我這里只用1臺服務器進行模擬
redis列表
在server上創建一個目錄用于存放redis集群部署文件。這里我放的路徑為/root/redis-cluster
在/opt/docker/redis-cluster目錄下創建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夾
mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6} #創建持久化目錄 mkdir -p /opt/docker/redis-cluster/redis-1/data mkdir -p /opt/docker/redis-cluster/redis-2/data mkdir -p /opt/docker/redis-cluster/redis-3/data mkdir -p /opt/docker/redis-cluster/redis-4/data mkdir -p /opt/docker/redis-cluster/redis-5/data mkdir -p /opt/docker/redis-cluster/redis-6/data
cluster-enabled yes # 開啟集群 cluster-config-file nodes.conf # 集群配置文件 cluster-node-timeout 5000 # 集群節點多少時間未響應視為該節點丟失 appendonly yes port 6379 # redis監聽端口 masterauth passwd123 #設置master節點密碼 requirepass passwd123 #設置密碼
注意:port值不能都為6379,根據上面redis列表設置的端口號,依次給redis-1 ~ redis-6設置6379~6384端口號
這里直接使用daocloud提供的redis鏡像地址daocloud.io/library/redis:6.0.4
在/root/redis-cluster文件夾下創建docker-compose.yml文件。
docker-compose.yml文件內容如下:
version: '3.1' services: # redis1配置 redis1: image: daocloud.io/library/redis:6.0.4 container_name: redis-1 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-1/data:/data - /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis2配置 redis2: image: daocloud.io/library/redis:6.0.4 container_name: redis-2 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-2/data:/data - /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis3配置 redis3: image: daocloud.io/library/redis:6.0.4 container_name: redis-3 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-3/data:/data - /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis4配置 redis4: image: daocloud.io/library/redis:6.0.4 container_name: redis-4 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-4/data:/data - /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis5配置 redis5: image: daocloud.io/library/redis:6.0.4 container_name: redis-5 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-5/data:/data - /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis6配置 redis6: image: daocloud.io/library/redis:6.0.4 container_name: redis-6 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-6/data:/data - /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
啟動容器,執行命令:
#啟動容器 docker-compose -f xxx.yaml up -d
隨便找一個容器進入,這里我選擇redis-1進入。 在進入容器后,輸入如下命令開啟集群:
redis-cli --cluster create 192.168.66.180:6379 \ 192.168.66.180:6380 \ 192.168.66.180:6381 \ 192.168.66.180:6382 \ 192.168.66.180:6383 \ 192.168.66.180:6384 \ --cluster-replicas 1
出現下圖即為成功。
使用redis-cli命令,連接集群中任意節點。(隨便找一臺能ping通集群所在IP的電腦,這里我用的本機進行測試)
redis-cli -c -h 192.168.66.180 -p 6379 192.168.66.180:6379> cluster info
顯示為如下圖,即為集群健康狀態
cluster nodes
注意看圖中的slave,master,myself等關鍵字。
192.168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380>
注意:這里根據切片自動切換到了該數據分片所在的節點上,所以下面可以看到連接的節點變為了192.168.1.4:6380
get test
168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380> get test "hello world" 192.168.66.180:6380>
這里因為就是在該數據所在分片上進行的查詢,所以連接節點沒有改變。
#為redis.conf文件添加如下配置。這里設置密碼為123456 masterauth 123456 requirepass 123456
#為docker-compose.yml中每個容器添加如下配置: environment: - REDISCLI_AUTH=123456
redis-cli -c -h 192.168.66.180 -p 6379 -a 123456
“Docker-Compose如何搭建Redis集群”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。