1. 概述
Redis官方对分布式的支持,基本分为两种:
主从模式
master-slave(replica),部署简单,可参考官方文档
Redis Slave Tree- 通过docker启动一个slave
docker run -d --rm \
--name myredis \
-v `pwd`/data:/data:rw \
-p 0.0.0.0:6379:6379 \
redis:5 \
redis-server --appendonly yes --maxclients 100000 --replicaof 10.176.154.39 6379
集群模式
从redis 3.0版本开始支持,并逐步完善,且是官方推荐的支持分布式横向scale
的solution。
但是,redis已经出到6.0了,此方案的生态并不完善,有待进一步发展,例如redis cluster proxy
、redis cluster client
、redis cluster monitor/dashboard
等。
所以,各家大厂纷纷自主开发了自己的redis相关项目,以满足自己的业务需求。
3主3从的Redis集群本文介绍如何搭建redis cluster,参考文章:
- Docker实战之Redis-Cluster集群
- 认识Redis集群——Redis Cluster
- cluster-tutorial
- cluster-tutorial,有些内容需要更新,可以重点参考上面英文版本介绍
2. 部署步骤
2.1. 集群设计与网络规划
计划在一台服务器上,通过docker创建一个3主3从
的集群,端口规划如下
Host IP | Host Port |
---|---|
10.176.154.39 | 7001 |
10.176.154.39 | 7002 |
10.176.154.39 | 7003 |
10.176.154.39 | 7004 |
10.176.154.39 | 7005 |
10.176.154.39 | 7006 |
2.2. 准备redis.conf文件
- redis-cluster.tmpl
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
cluster-announce-ip 10.176.154.39
- 创建cluster目录和配置文件
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
2.3. 准备docker-compose文件
- docker-compose.yml
version: '3'
services:
redis7001:
image: redis:5
container_name: redis7001
volumes:
- ./redis-cluster/7001/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7001/data:/data
ports:
- "0.0.0.0:7001:7001"
- "0.0.0.0:17001:17001"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
redis7002:
image: redis:5
container_name: redis7002
volumes:
- ./redis-cluster/7002/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7002/data:/data
ports:
- "0.0.0.0:7002:7002"
- "0.0.0.0:17002:17002"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
redis7003:
image: redis:5
container_name: redis7003
volumes:
- ./redis-cluster/7003/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7003/data:/data
ports:
- "0.0.0.0:7003:7003"
- "0.0.0.0:17003:17003"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
redis7004:
image: redis:5
container_name: redis7004
volumes:
- ./redis-cluster/7004/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7004/data:/data
ports:
- "0.0.0.0:7004:7004"
- "0.0.0.0:17004:17004"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
redis7005:
image: redis:5
container_name: redis7005
volumes:
- ./redis-cluster/7005/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7005/data:/data
ports:
- "0.0.0.0:7005:7005"
- "0.0.0.0:17005:17005"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
redis7006:
image: redis:5
container_name: redis7006
volumes:
- ./redis-cluster/7006/conf/redis.conf:/redis/redis.conf
- ./redis-cluster/7006/data:/data
ports:
- "0.0.0.0:7006:7006"
- "0.0.0.0:17006:17006"
command: redis-server /redis/redis.conf
environment:
- TZ=Asia/Shanghai
2.4. 启动redis集群
- 启动6个独立的redis实例
docker-compose up -d
- 使6个redis实例join在一起,组成redis集群
docker run -it redis:5 redis-cli --cluster create 10.176.154.39:7001 \
10.176.154.39:7002 10.176.154.39:7003 10.176.154.39:7004 10.176.154.39:7005 10.176.154.39:7006 \
--cluster-replicas 1
2.5. 最终的目录结构
$ tree .
.
├── docker-compose.yml
├── redis-cluster
│ ├── 7001
│ │ ├── conf
│ │ │ └── redis.conf
│ │ └── data
│ │ ├── appendonly.aof
│ │ ├── dump.rdb
│ │ └── nodes.conf
│ ├── 7002
│ │ ├── conf
│ │ │ └── redis.conf
│ │ └── data
│ │ ├── appendonly.aof
│ │ ├── dump.rdb
│ │ └── nodes.conf
│ ├── 7003
│ │ ├── conf
│ │ │ └── redis.conf
│ │ └── data
│ │ ├── appendonly.aof
│ │ ├── dump.rdb
│ │ └── nodes.conf
│ ├── 7004
│ │ ├── conf
│ │ │ └── redis.conf
│ │ └── data
│ │ ├── appendonly.aof
│ │ ├── dump.rdb
│ │ └── nodes.conf
│ ├── 7005
│ │ ├── conf
│ │ │ └── redis.conf
│ │ └── data
│ │ ├── appendonly.aof
│ │ ├── dump.rdb
│ │ └── nodes.conf
│ └── 7006
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
└── redis-cluster.tmpl
19 directories, 26 files
3. 测试
$ docker run -it redis:5 redis-cli -h 10.176.154.39 -p 7001 -c
10.176.154.39:7001> cluster help
1) CLUSTER <subcommand> arg arg ... arg. Subcommands are:
2) ADDSLOTS <slot> [slot ...] -- Assign slots to current node.
3) BUMPEPOCH -- Advance the cluster config epoch.
4) COUNT-failure-reports <node-id> -- Return number of failure reports for <node-id>.
5) COUNTKEYSINSLOT <slot> - Return the number of keys in <slot>.
6) DELSLOTS <slot> [slot ...] -- Delete slots information from current node.
7) FAILOVER [force|takeover] -- Promote current replica node to being a master.
8) FORGET <node-id> -- Remove a node from the cluster.
9) GETKEYSINSLOT <slot> <count> -- Return key names stored by current node in a slot.
10) FLUSHSLOTS -- Delete current node own slots information.
11) INFO - Return onformation about the cluster.
12) KEYSLOT <key> -- Return the hash slot for <key>.
13) MEET <ip> <port> [bus-port] -- Connect nodes into a working cluster.
14) MYID -- Return the node id.
15) NODES -- Return cluster configuration seen by node. Output format:
16) <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ... <slot>
17) REPLICATE <node-id> -- Configure current node as replica to <node-id>.
18) RESET [hard|soft] -- Reset current node (default: soft).
19) SET-config-epoch <epoch> - Set config epoch of current node.
20) SETSLOT <slot> (importing|migrating|stable|node <node-id>) -- Set slot state.
21) REPLICAS <node-id> -- Return <node-id> replicas.
22) SLOTS -- Return information about slots range mappings. Each range is made of:
23) start, end, master and replicas IP addresses, ports and ids
10.176.154.39:7001> cluster nodes
52983d5f4b97651e403a14dcfa86b595470c74bd 10.176.154.39:7003@17003 master - 0 1626401678000 3 connected 10923-16383
cde9bc3a404b58d75dd2015ffb0021641320fb6f 10.176.154.39:7006@17006 slave 13ef7bd1158d1c615dbbd01c7ab2a16b2622b72c 0 1626401677528 6 connected
f0e2c3b73d08ab6fce0cf3199c65d807725fd5d9 10.176.154.39:7004@17004 slave 52983d5f4b97651e403a14dcfa86b595470c74bd 0 1626401678000 4 connected
13ef7bd1158d1c615dbbd01c7ab2a16b2622b72c 10.176.154.39:7002@17002 master - 0 1626401678832 2 connected 5461-10922
9e813c95041532f4cb10b2eae05b815cec7d8b71 10.176.154.39:7005@17005 slave f20c32babff7c0cf02270d692867a2d85531ff20 0 1626401677528 5 connected
f20c32babff7c0cf02270d692867a2d85531ff20 10.176.154.39:7001@17001 myself,master - 0 1626401678000 1 connected 0-5460
10.176.154.39:7001> cluster slots
1) 1) (integer) 10923
2) (integer) 16383
3) 1) "10.176.154.39"
2) (integer) 7003
3) "52983d5f4b97651e403a14dcfa86b595470c74bd"
4) 1) "10.176.154.39"
2) (integer) 7004
3) "f0e2c3b73d08ab6fce0cf3199c65d807725fd5d9"
2) 1) (integer) 5461
2) (integer) 10922
3) 1) "10.176.154.39"
2) (integer) 7002
3) "13ef7bd1158d1c615dbbd01c7ab2a16b2622b72c"
4) 1) "10.176.154.39"
2) (integer) 7006
3) "cde9bc3a404b58d75dd2015ffb0021641320fb6f"
3) 1) (integer) 0
2) (integer) 5460
3) 1) "10.176.154.39"
2) (integer) 7001
3) "f20c32babff7c0cf02270d692867a2d85531ff20"
4) 1) "10.176.154.39"
2) (integer) 7005
3) "9e813c95041532f4cb10b2eae05b815cec7d8b71"
10.176.154.39:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:119301
cluster_stats_messages_pong_sent:118626
cluster_stats_messages_sent:237927
cluster_stats_messages_ping_received:118621
cluster_stats_messages_pong_received:119301
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:237927
10.176.154.39:7001> set hello world
OK
10.176.154.39:7001> get hello
"world"
10.176.154.39:7001> set hi friend
-> Redirected to slot [16140] located at 10.176.154.39:7003
OK
10.176.154.39:7003> get hi
"friend"
10.176.154.39:7003> set hurry up
-> Redirected to slot [4652] located at 10.176.154.39:7001
OK
10.176.154.39:7001> get hurry
"up"
4. 工具推荐
Redis Cluster的开发和运维工具比较欠缺,目前发现:
-
redis-py-cluster,支持redis-cluster的pip包,感觉还不太稳定(起码没有pyredis稳定)
https://github.com/Grokzen/redis-py-cluster -
redis proxy with haproxy
https://gist.github.com/tastyone/18171132216065440031 -
redis官方的redis cluster proxy,还不稳定
https://github.com/RedisLabs/redis-cluster-proxy -
网易开源的redis cluster proxy
https://github.com/netease-im/camellia -
redis web cli,不支持redis-cluster,可以单独连接某一个redis
https://hub.docker.com/r/marian/rebrow
网友评论