首頁 >資料庫 >Redis >Docker-Compose如何搭建Redis集群

Docker-Compose如何搭建Redis集群

PHPz
PHPz轉載
2023-05-30 16:46:221824瀏覽

1. 叢集配置

  • 3主3從

  • 由於只用於測試,故我這裡只用1台伺服器進行模擬

redis列表

Docker-Compose如何搭建Redis集群

2.寫redis.conf

在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

3. 在每個redis-*資料夾下建立redis.conf文件,並寫入以下內容:

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埠號

4.編寫docker-compose.yml檔

這裡直接使用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

Docker-Compose如何搭建Redis集群

5.開啟叢集

隨便找一個容器進入,這裡我選擇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

Docker-Compose如何搭建Redis集群

出現下圖即為成功。

Docker-Compose如何搭建Redis集群

6.測試

使用redis-cli指令,連接叢集中任意節點。我隨便在一台能夠ping通集群所在IP的電腦上進行了測試,這裡我使用了本機

6.1 查看節點屬性

redis-cli -c -h 192.168.66.180 -p 6379
192.168.66.180:6379> cluster info

顯示為如下圖,即為集群健康狀態

Docker-Compose如何搭建Redis集群

6.2 檢視節點資訊

cluster nodes

Docker-Compose如何搭建Redis集群

#注意看圖中的slave,master,myself等關鍵字。

Docker-Compose如何搭建Redis集群

6.3 插入一個值

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>

Docker-Compose如何搭建Redis集群

#注意:這裡根據切片自動切換到了該資料分片所在的節點上,所以下面可以看到連接的節點變成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>

這裡因為就是在該資料所在分片上進行的查詢,所以連接節點沒有改變。

7.新增密碼:

7.1 編輯redis.conf檔

#为redis.conf文件添加如下配置。这里设置密码为123456
masterauth 123456
requirepass 123456

7.2 編輯docker-compose.yml檔案

#为docker-compose.yml中每个容器添加如下配置:
environment:
  - REDISCLI_AUTH=123456

7.3 連線叢集

redis-cli -c -h 192.168.66.180 -p 6379 -a 123456

以上是Docker-Compose如何搭建Redis集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除