Docker極簡入門:使用Docker-Compose 搭建redis叢集

AD_milk發表於2022-04-27

為了構建一個叢集,我們首先要讓 redis 啟用叢集模式
一個簡單的配置檔案如下redis.conf

# redis.conf file
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

因為是在單機上演示,所以我們得修改 port 防止衝突
比如我們要建立3主5副的叢集,那麼這八個 redis 例項啟動的時候就應該用不同的埠(7000,7001...7007),這樣能防止衝突。

.
├── 7000
│   └── redis.conf
├── 7001
│   └── redis.conf
├── 7002
│   └── redis.conf
├── 7003
│   └── redis.conf
├── 7004
│   └── redis.conf
├── 7005
│   └── redis.conf
├── 7006
│   └── redis.conf
├── 7007
│   └── redis.conf

./7000/redis.conf 中的 port 設定成7000,依此類推./7007/redis.conf中的 port 應該設定成7007

萬事俱備,只需執行redis-server ./redis.conf命令即可,當然你需要依次啟動八個 redis server

for i in `seq 7000 7007`;
do
	exec redis-server ./${i}/redis.conf &
done

redis-cli --cluster create\
127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003\
127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 127.0.0.1:7007\
--cluster-replicas 1

這樣整個叢集就搭建好了,你可以選擇執行在 port 7000-7007中的任意一個 servr 登入,然後去執行一些你喜歡的命令驗證一下[狗頭]

使用Docker-Compose構建叢集

現在讓我們來明確一下流程,首先我們需要啟動八個 redis server 例項,然後執行 redis-cli --cluster create 命令建立叢集,這意味著我們需要建立一個 docker network 使得叢集成員能夠互相通訊

建立一個 network 也很簡單

networks:
  redis-network:
    name: redis-network
    ## 使用網橋
    driver: bridge
    ipam:
      driver: default
      ## 指定網段,需要注意不能與已有的 docker network 相沖突
      config:
        - subnet: 172.18.0.0/24

還記得之前命令列是如何啟動叢集的嗎?最後需要執行命令redis-cli --cluster create ,整個叢集才算成功建立,那在 yml 檔案中該怎麼寫呢?
我們可以藉助 depends_on來告訴 docker 當前的 service 依賴其他的 service
具體的細節請看完整的yml檔案

## redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes-redis.conf
cluster-node-timeout 5000
appendonly yes
appendfilename "appendonly-redis.aof"
version: "3.7"

networks:
  redis-network:
    name: redis-network
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/24

services:
  redis-cluster:
    image: redis:6-alpine
    command: 'redis-cli --cluster create 
    172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 
    172.18.0.15:6379 172.18.0.16:6379 172.18.0.17:6379 172.18.0.18:6379
    --cluster-yes --cluster-replicas 1'
    networks:
      - redis-network
    depends_on:
      - redis1
      - redis2
      - redis3
      - redis4
      - redis5
      - redis6
      - redis7
      - redis8

  redis1:
    image: redis:6-alpine
    container_name: redis1
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.11
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis1:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf

  redis2:
    image: redis:6-alpine
    container_name: redis2
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.12
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis2:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf

  redis3:
    image: redis:6-alpine
    container_name: redis3
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.13
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis3:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf

  redis4:
    image: redis:6-alpine
    container_name: redis4
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.14
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis4:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf
      
  redis5:
    image: redis:6-alpine
    container_name: redis5
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.15
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis5:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf
      
  redis6:
    image: redis:6-alpine
    container_name: redis6
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.16
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis6:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf    
        
  redis7:
    image: redis:6-alpine
    container_name: redis7
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.17
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis7:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf
      
  redis8:
    image: redis:6-alpine
    container_name: redis8
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.18
    expose:
      - 6379
    volumes:
      - $PWD/docker-data/redis8:/data
      - $PWD/docker-config/redis.conf:/usr/local/etc/redis/redis.conf

最後不要忘記了最基本的命令

sudo docker-compose up # 在當前命令下執行,啟動叢集
sudo docker-compose down # 關閉叢集的同時會將 docker-network也一併刪除

如何與叢集通訊

# 登入任意一個節點執行命令即可,叢集會根據key的雜湊值,將你的命令重定位
sudo docker exec -it redis5 redis-cli -p 6379
set foo bar
sudo docker exec -it redis1 redis-cli -p 6379
get foo
# output bar

參考

reids官網 course

相關文章