Docker搭建Redis集群

Docker搭建Redis集群

本篇博客主要参考了这篇文章,也有一些小变动,记录一下方便自己之后需要。

编辑配置

配置模板

redis-cluster.tmpl

# redis端口
port ${PORT}
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 192.xxx
# 集群节点端口 7001 - 7006
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
# 开启 appendonly 备份模式
appendonly yes
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb

生成配置脚本 redis-cluster-config.sh

for port in `seq 7001 7006`; do \
  mkdir -p ./redis-cluster/${port}/conf \
  && PORT=${port} envsubst < ./redis-cluster.tmpl > ./redis-cluster/${port}/conf/redis.conf \
  && mkdir -p ./redis-cluster/${port}/data; \
done

利用脚本批量生成配置

[hzh@localhost redis]$ chmod +x redis-cluster-config.sh
[hzh@localhost redis]$ ./redis-cluster-config.sh
[hzh@localhost redis]$ tree
.
├── redis-cluster
│   ├── 7001
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   ├── 7002
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   ├── 7003
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   ├── 7004
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   ├── 7005
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   └── 7006
│       ├── conf
│       │   └── redis.conf
│       └── data
├── redis-cluster-config.sh

生成容器

docker-compose 配置批量

docker-compose-redis-cluster.yml

version: '3.7'

services:
  redis7001:
    image: 'redis'
    container_name: redis7001
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7001/data:/data
    ports:
      - "7001:7001"
      - "17001:17001"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

  redis7002:
    image: 'redis'
    container_name: redis7002
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7002/data:/data
    ports:
      - "7002:7002"
      - "17002:17002"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

  redis7003:
    image: 'redis'
    container_name: redis7003
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7003/data:/data
    ports:
      - "7003:7003"
      - "17003:17003"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

  redis7004:
    image: 'redis'
    container_name: redis7004
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7004/data:/data
    ports:
      - "7004:7004"
      - "17004:17004"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

  redis7005:
    image: 'redis'
    container_name: redis7005
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7005/data:/data
    ports:
      - "7005:7005"
      - "17005:17005"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

  redis7006:
    image: 'redis'
    container_name: redis7006
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7006/data:/data
    ports:
      - "7006:7006"
      - "17006:17006"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai

执行生成命令

[hzh@localhost redis]$ docker-compose -f docker-compose-redis-cluster.yml up -d
bash: docker-compose: command not found...
[hzh@localhost redis]$ pip3 install --user docker-compose  # 安装docker-compose

再次执行

[hzh@localhost redis]$ docker-compose -f docker-compose-redis-cluster.yml up -d
Creating network "redis_default" with the default driver
Creating redis7003 ... done
Creating redis7001 ... done
Creating redis7004 ... done
Creating redis7002 ... done
Creating redis7006 ... done
Creating redis7005 ... done
[hzh@localhost redis]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS
                                              NAMES
8cab1789f565        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7005->7005/tcp, 6379/tcp, 0.0.0.0:17005->17005/tcp   redis7005
1743591a742f        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7006->7006/tcp, 6379/tcp, 0.0.0.0:17006->17006/tcp   redis7006
dd5c4405833f        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7003->7003/tcp, 6379/tcp, 0.0.0.0:17003->17003/tcp   redis7003
f3d813dfbfdd        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7001->7001/tcp, 6379/tcp, 0.0.0.0:17001->17001/tcp   redis7001
269f81bfcd6b        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7002->7002/tcp, 6379/tcp, 0.0.0.0:17002->17002/tcp   redis7002
9b978437186d        redis               "docker-entrypoint.s…"   24 seconds ago      Up 23 seconds       0.0.0.0:7004->7004/tcp, 6379/tcp, 0.0.0.0:17004->17004/tcp   redis7004

开启集群

docker exec -it redis7001 redis-cli -p 7001 --cluster create 59.78.194.153:7001 59.78.194.153:7002 59.78.194.153:7003 59.78.194.153:7004 59.78.194.153:7005 59.78.194.153:7006 --cluster-replicas 1

可能会报错,这里是因为端口没有开放。记得不仅要开放7001-7006端口,还需要开放17001-17006端口,不然开启集群时会卡在Waiting for the cluster to join 。因为1700X端口是集群通信端口。

[hzh@localhost redis]$ su
Password:
(base) [root@localhost redis]# firewall-cmd --zone=public --add-port=7001/tcp --permanent
success
......
(base) [root@localhost redis]# firewall-cmd --zone=public --add-port=7006/tcp --permanent
success
......
(base) [root@localhost redis]# firewall-cmd --zone=public --add-port=17006/tcp --permanent
success
(base) [root@localhost redis]# firewall-cmd --reload
success

再次执行

[hzh@localhost redis]$ docker exec -it redis7001 redis-cli -p 7001 --cluster create 59.78.194.153:7001 59.78.194.153:7002 59.78.194.153:7003 59.78.194.153:7004 59.78.194.153:7005 59.78.194.153:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 59.78.194.153:7005 to 59.78.194.153:7001
Adding replica 59.78.194.153:7006 to 59.78.194.153:7002
Adding replica 59.78.194.153:7004 to 59.78.194.153:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e2cd40d097ea688af18cd022883fa13a8afd5ef3 59.78.194.153:7001
   slots:[0-5460] (5461 slots) master
M: a698698fbe9ce85e7448bcb27ba5f19f0d175207 59.78.194.153:7002
   slots:[5461-10922] (5462 slots) master
M: 51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4 59.78.194.153:7003
   slots:[10923-16383] (5461 slots) master
S: 14f8fbba8b2e9623865dd9453f22b63bd914ef13 59.78.194.153:7004
   replicates a698698fbe9ce85e7448bcb27ba5f19f0d175207
S: 003c2b6cad73550648f93ba3257abcf085b347e0 59.78.194.153:7005
   replicates 51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4
S: 112ce7ce41c2930f6ce8507ffe3880e36e319cac 59.78.194.153:7006
   replicates e2cd40d097ea688af18cd022883fa13a8afd5ef3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 59.78.194.153:7001)
M: e2cd40d097ea688af18cd022883fa13a8afd5ef3 59.78.194.153:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a698698fbe9ce85e7448bcb27ba5f19f0d175207 59.78.194.153:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 14f8fbba8b2e9623865dd9453f22b63bd914ef13 59.78.194.153:7004
   slots: (0 slots) slave
   replicates a698698fbe9ce85e7448bcb27ba5f19f0d175207
S: 112ce7ce41c2930f6ce8507ffe3880e36e319cac 59.78.194.153:7006
   slots: (0 slots) slave
   replicates e2cd40d097ea688af18cd022883fa13a8afd5ef3
S: 003c2b6cad73550648f93ba3257abcf085b347e0 59.78.194.153:7005
   slots: (0 slots) slave
   replicates 51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4
M: 51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4 59.78.194.153:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

简单测试

> 59.78.194.153@7001 connected!
> set name admin
OK
> get name
admin
> 59.78.194.153@7002 connected!
> get name
admin

查看集群状态

> 59.78.194.153@7001 connected!
> cluster nodes
e2cd40d097ea688af18cd022883fa13a8afd5ef3 59.78.194.153:7001@17001 master - 0 1606736718359 1 connected 0-5460
14f8fbba8b2e9623865dd9453f22b63bd914ef13 59.78.194.153:7004@17004 slave a698698fbe9ce85e7448bcb27ba5f19f0d175207 0 1606736718000 2 connected
003c2b6cad73550648f93ba3257abcf085b347e0 59.78.194.153:7005@17005 slave 51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4 0 1606736718560 3 connected
112ce7ce41c2930f6ce8507ffe3880e36e319cac 59.78.194.153:7006@17006 slave e2cd40d097ea688af18cd022883fa13a8afd5ef3 0 1606736716854 1 connected
a698698fbe9ce85e7448bcb27ba5f19f0d175207 59.78.194.153:7002@17002 myself,master - 0 1606736717000 2 connected 5461-10922
51f55fa1094c0eed9a2ff8121a4923e9a86ff0d4 59.78.194.153:7003@17003 master - 0 1606736718560 3 connected 10923-16383

参考

https://segmentfault.com/a/1190000021882802

https://blog.csdn.net/XIANZHIXIANZHIXIAN/article/details/82392172

要使用 Docker 搭建 Redis 集群,你可以按照以下步骤进行操作: 1. 确保已经安装并配置好了 Docker。 2. 创建一个用于 Redis 集群的自定义网络: ```bash docker network create redis-net ``` 3. 创建 Redis 主节点的容器。假设我们需要创建3个主节点,分别使用端口 7001、7002 和 7003: ```bash docker run -d --name redis1 --network redis-net -p 7001:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes docker run -d --name redis2 --network redis-net -p 7002:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes docker run -d --name redis3 --network redis-net -p 7003:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes ``` 这将创建三个 Redis 主节点容器,并将端口映射到主机的 7001、7002 和 7003 端口。 4. 创建 Redis 集群: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster create <node-ip>:<node-port> <node-ip>:<node-port> <node-ip>:<node-port> ``` 将 `<node-ip>` 替换为你主机的 IP 地址, `<node-port>` 替换为容器中 Redis 主节点的端口(6379)。 例如: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster create 172.18.0.2:6379 172.18.0.3:6379 172.18.0.4:6379 ``` 这将创建一个 Redis 集群,并将三个主节点添加到集群中。 5. 检查集群状态: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster check <node-ip>:<node-port> ``` 例如: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster check 172.18.0.2:6379 ``` 这将检查集群的状态,并输出每个节点的信息。 这样,你就成功使用 Docker 搭建了一个 Redis 集群。请根据你的实际需求进行相应的配置和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值