RedisCluster集群方案是Redis官方推荐的一种集群方案,他没有中心节点。客户端与redis节点直连,不需要中间代理层,RedisCluster集群方案可以将数据分片存储。为避免其中一个节点宕机丢失分片数据,我们需要设置冗余节点。
-
架构设计:使用3个节点作为主节点,3个节点作为从节点(冗余节点)
2.1 拉取redis 镜像
docker pull redis
3 创建私有网络
3.1 创建私有网络
创建docker容器的私有网络,方便建立集群使用,网段为172.15.0.0
网络名字命名为redis_net1
.(ps: 如果提醒网段被占用,请自行改为其他网段,如: 172.16.0.01
172.17.0.0
...)
docker network create --subnet=172.15.0.0/16 redis_net1
3.2 查看网络
docker network inspect redis_net1
4 在宿主机中创建目录
4.1 创建目录 /bizwork/redis-cluster
之后会在 /bizwork/redis-cluster
目录下存放配置文件以及数据
mkdir -p /bizwork/redis-cluster
4.2 准备好配置文件
在/bizwork/redis-cluster
目录下 创建文件redis-cluster.tmpl
vim /bizwork/redis-cluster/redis-cluster.tmpl
将以下内容拷贝到redis-cluster.tmpl
文件中,我们后续会用到
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip xx.xx.xx.xx #你的公网IP或者内网IP
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
bind 0.0.0.0
在宿主机/bizwork/redis-cluster
批量创建 8000~8005 目录,用来作为容器映射目录
for port in `seq 8000 8005`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
批量创建容器
for port in `seq 8000 8005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /bizwork/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /bizwork/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis_net1 \
--sysctl net.core.somaxconn=1024 redis \
redis-server /usr/local/etc/redis/redis.conf;
done
查看各容器的网段
docker network inspect redis_net1
输出结果:
[
{
"Name": "redis_net1",
"Id": "01cf151982fdcfa6d59a1c0cf1f82e8778ea5caf55e4ef1101501485b402a5f2",
"Created": "2020-12-31T15:17:28.223939985+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.15.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2c908a8d54f9701858f774f0532304687b88b8327cac2e6b607cdf56652e3e58": {
"Name": "redis-8002",
"EndpointID": "c3866ce9080d1c446c2861280a15b89e53fef524dcba05623bdf9bc5cec74355",
"MacAddress": "02:42:ac:0f:00:04",
"IPv4Address": "172.15.0.4/16",
"IPv6Address": ""
},
"9e73858fd0c192d5be38a5ee9a79650f109f3b669bbcb0e8fb7f3f5105931f85": {
"Name": "redis-8001",
"EndpointID": "ef52a366f26a45a06e2da4b4201955308df7379631c9d5dff35691df1cb66fe0",
"MacAddress": "02:42:ac:0f:00:03",
"IPv4Address": "172.15.0.3/16",
"IPv6Address": ""
},
"aef661f9c8e16b490624b1fd4ed7883659addf7b1aacb13ef9c3b4737bb44a5c": {
"Name": "redis-8005",
"EndpointID": "c6e74a5a3de2da545d826097c44c686821c65f01164d35986629b9a1d0838cec",
"MacAddress": "02:42:ac:0f:00:07",
"IPv4Address": "172.15.0.7/16",
"IPv6Address": ""
},
"c5320fc978908440ab52f05f821162c38baaebde4a72abce5e66d9827f7fc010": {
"Name": "redis-8000",
"EndpointID": "9b73ee6eb6dbd63722b0275471fde1cae2bd5907c60179fcbc25522ba0e84eb5",
"MacAddress": "02:42:ac:0f:00:02",
"IPv4Address": "172.15.0.2/16",
"IPv6Address": ""
},
"c8ad19e2bd4bfe21968b0105d972ab0140707383a8e33a554c5b205df28953bd": {
"Name": "redis-8003",
"EndpointID": "4f84d1ff1cef06b267d97fd1904e11836333a7e6e51c687d849b8767dd1be7fe",
"MacAddress": "02:42:ac:0f:00:05",
"IPv4Address": "172.15.0.5/16",
"IPv6Address": ""
},
"e002e46453501bdf3d7e9cd7d214db63fa981ab053cd5e5bdd10b9fb566839ac": {
"Name": "redis-8004",
"EndpointID": "cb36c1d2d8902e9d1c04599e86caf51a4d705960be7ac6e8a7b4b26a1175fbe5",
"MacAddress": "02:42:ac:0f:00:06",
"IPv4Address": "172.15.0.6/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
这里我们可看出:
- 容器 redis-8000 网段为 172.15.0.2
- 容器 redis-8001 网段为 172.15.0.3
- 容器 redis-8002 网段为 172.15.0.4
- 容器 redis-8003 网段为 172.15.0.5
- 容器 redis-8004 网段为 172.15.0.6
- 容器 redis-8005 网段为 172.15.0.7
5. 创建集群
任意进入一个容器,我这里进入redis-8000
容器来执行命令创建集群
docker exec -it redis-8000 bash
使用redis-cli --cluster create
命令创建集群(如果设置了密码,需要输入密码参数)具体如下:
redis-cli -a 1234 --cluster create 172.15.0.2:8000 172.15.0.3:8001 172.15.0.4:8002 172.15.0.5:8003 172.15.0.6:8004 172.15.0.7:8005 --cluster-replicas 1
常见问题:
基于自己搭建redis cluster踩过的坑,希望能节约大家时间。
1、访问出现 Redis: ERR unknown command 'CLUSTER'
项目配置好集群后,访问报错ERR unknown command 'CLUSTER'
,原因是redis cluster
服集群没有搭建成功,我再次检查了一下,未搭建成功的原因是我的redis-8000
容器的配置文件的cluster-config-file node_8000.conf
与其他容器中的cluster-config-file node_8000.conf
相同
2、Redis无法使用其他库
Redis Cluster 集群方案只支持一个数据库(db 0)
3、SpringBoot 配置集群无法启动
2021-01-02 21:33:39.990 ERROR 10096 --- [isson-netty-2-7] o.r.cluster.ClusterConnectionManager : Can't connect to master: redis://172.21.0.12:8000 with slot ranges: [[0-5460]]
2021-01-02 21:33:39.991 ERROR 10096 --- [isson-netty-2-8] o.r.cluster.ClusterConnectionManager : Can't connect to master: redis://172.21.0.12:8002 with slot ranges: [[10923-16383]]
2021-01-02 21:33:39.990 ERROR 10096 --- [isson-netty-2-9] o.r.cluster.ClusterConnectionManager : Can't connect to master: redis://172.21.0.12:8001 with slot ranges: [[5461-10922]]