Redis Cluster集群部署

Redis Cluster是Redis官方提供的Redis集群功能。

下载镜像

docker pull redis:6.2.4

运行镜像

我们需要创建6个redis容器(redis集群,最少必须有6个节点,3主3从)

  • redis-node1:6379
  • redis-node2:6380
  • redis-node3:6381
  • redis-node4:6382
  • redis-node5:6383
  • redis-node6:6384

创建挂载目录

mkdir -p ~/Ethan/Docker/redis/node1/data
mkdir -p ~/Ethan/Docker/redis/node1/conf
mkdir -p ~/Ethan/Docker/redis/node2/data
mkdir -p ~/Ethan/Docker/redis/node2/conf
mkdir -p ~/Ethan/Docker/redis/node3/data
mkdir -p ~/Ethan/Docker/redis/node3/conf
mkdir -p ~/Ethan/Docker/redis/node4/data
mkdir -p ~/Ethan/Docker/redis/node4/conf
mkdir -p ~/Ethan/Docker/redis/node5/data
mkdir -p ~/Ethan/Docker/redis/node5/conf
mkdir -p ~/Ethan/Docker/redis/node6/data
mkdir -p ~/Ethan/Docker/redis/node6/conf

分别在每个conf目录下创建文件redis.conf

node1/conf/redis.conf :

#端口号,写文件夹对映的端口
Port 6379
#设置登录密码
requirepass 123456
#设置节点密码,集群必设
masterauth 123456
#开启aof存储
appendonly yes
# 关闭保护,外网可直接访问
protected-mode no
#开启集群
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
# 宿主机ip
cluster-announce-ip 10.252.187.163
# 集群节点映射端口
cluster-announce-port 6379
# 集群总线端口 port + 10000
cluster-announce-bus-port 16379

同样,node1 ~ 6端口号依次为: 6379,6380,6381,6382,6383,6384

创建虚拟网卡

创建虚拟网卡,主要是用于redis-cluster能于外界进行网络通信,一般常用桥接模式。

docker network create redis-net

编辑docker-compose.yml文件

version: "3.8"

services:
  redis1:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6379:6379"
      - "16379:16379"
    container_name: redis-node1
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node1/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node1/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis2:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6380:6380"
      - "16380:16380"
    container_name: redis-node2
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node2/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node2/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis3:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6381:6381"
      - "16381:16381"
    container_name: redis-node3
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node3/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node3/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis4:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6382:6382"
      - "16382:16382"
    container_name: redis-node4
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node4/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node4/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis5:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6383:6383"
      - "16383:16383"
    container_name: redis-node5
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node5/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node5/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis6:
    image: redis:6.2.4
    restart: "always"
    network_mode: "redis-net"
    ports:
      - "6384:6384"
      - "16384:16384"
    container_name: redis-node6
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - /Users/ethanxu/Ethan/Docker/redis/node6/data:/data
      - /Users/ethanxu/Ethan/Docker/redis/node6/conf/redis.conf:/usr/local/etc/redis/redis.conf

启动容器

docker-compose up -d

组建 Redis 集群

进入任意一个redis容器

docker exec -it redis-node1 /bin/bash

执行组件集群的命令

# cluster-replicas 1 表示每个主节点有一个从节点
redis-cli -a 123456 --cluster create 10.252.187.163:6379 10.252.187.163:6380 10.252.187.163:6381 10.252.187.163:6382 10.252.187.163:6383 10.252.187.163:6384 --cluster-replicas 1

执行结果

从上面的命令执行输出,可以清楚的看到redis对于哈希槽(slots)的分配。redis集群,固定共计 16384 个哈希槽,集群启动时,就会将这些哈希槽,分配给所有的主节点。从上图输出可知,三个主节点,各自负责了 [0, 5460], [5461, 10922], [10923, 16383]三段哈希槽。

输入“yes”确认

可以看到 “All 16384 slots covered”,只有主节点会分配哈希槽。

可以通过 redis-cli 查看一下集群节点信息

通过集群节点信息,我们也可以看到节点间的主从关系

master 10.252.187.163:6379 redis-node1
slave  10.252.187.163:6382 redis-node4

master 10.252.187.163:6380 redis-node2
slave  10.252.187.163:6383 redis-node5

master 10.252.187.163:6381 redis-node3
slave  10.252.187.163:6384 redis-node6

至此,我们的redis cluster集群就已经部署完成啦~

附录

为何是16384个哈希槽?

Redis Cluster “cluster nodes”命令

相关链接

1 评论
最新
最旧 最多投票
内联反馈
查看所有评论
滚动至顶部