redis docker 集群搭建
1. 安装镜像
docker pull redis:latest
2. 创建conf模板和脚本
# 创建模板目录
mkdir /data/redis_conf
# 创建实际映射目录
mkdir /data/redis_data
# 在/data/redis_conf创建conf模板
touch redis_cluster.conf.template
# 编写redis_cluster.conf.template文件
vi redis_cluster.conf.template
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.112.136
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
cluster-announce-port:此端口为redis提供服务端口,用于应用客户端连接
cluster-announce-bus-port:此端口用于redis集群进行故障检测、配置更新、故障转移授权和内部通讯使用,不用于应用客户端连接使用
cluster-enabled:是否启动集群,选值:yes 、no
cluster-config-file 配置文件.conf :指定节点信息,自动生成
cluster-node-timeout 毫秒值: 配置节点连接超时时间
appendonly:是否开启持久化,选值:yes、no
也可使用官方conf进行修改
redis中文官方网站:http://www.redis.cn/download.html
根据此文件按如上修改即可
3. 创建6个redis实例的数据目录,配置文件目录和配置文件脚本
for port in `seq 6380 6385`
do
mkdir -p /data/redis_data/${port}/conf \
&& PORT=${port} envsubst < /data/redis_conf/redis_cluster.conf.template > /data/redis_data/${port}/conf/redis.conf \
&& mkdir -p /data/redis_data/${port}/data;\
done
# 查看创建目录
tree /data/redis_data
redis_data/
|-- 6380
| |-- conf
| | `-- redis.conf
| `-- data
|-- 6381
| |-- conf
| | `-- redis.conf
| `-- data
|-- 6382
| |-- conf
| | `-- redis.conf
| `-- data
|-- 6383
| |-- conf
| | `-- redis.conf
| `-- data
|-- 6384
| |-- conf
| | `-- redis.conf
| `-- data
`-- 6385
|-- conf
| `-- redis.conf
`-- data
18 directories, 6 files
4. 批量创建容器脚本
for port in $(seq 6380 6385); do \
docker run -d -it -p ${port}:${port} -p 1${port}:1${port} --restart always --name redis-${port} \
-v /data/redis_data/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /data/redis_data/${port}/data:/data \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
redis:latest redis-server /usr/local/etc/redis/redis.conf; \
done
5. 查看容器运行状态
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
006b561fde27 redis:latest "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6385->6385/tcp, 6379/tcp, 0.0.0.0:16385->16385/tcp redis-6385
583d5ee68dbe redis:latest "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6384->6384/tcp, 6379/tcp, 0.0.0.0:16384->16384/tcp redis-6384
f958ac78f9e4 redis:latest "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6383->6383/tcp, 6379/tcp, 0.0.0.0:16383->16383/tcp redis-6383
65fa57516a49 redis:latest "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6382->6382/tcp, 6379/tcp, 0.0.0.0:16382->16382/tcp redis-6382
e5d85bb5fca2 redis:latest "docker-entrypoint.s…" 36 minutes ago Up 36 minutes 0.0.0.0:6381->6381/tcp, 6379/tcp, 0.0.0.0:16381->16381/tcp redis-6381
ac5eb8a0d40d redis:latest "docker-entrypoint.s…" 43 minutes ago Up 43 minutes 0.0.0.0:6380->6380/tcp, 6379/tcp, 0.0.0.0:16380->16380/tcp redis-6380
6个redis容器创建好之后,选择其中的一个容器进入,进行redis-cluster集群创建
docker exec -it redis-6380 /bin/bash
# 执行组件集群的命令
# redis-cli -a wangl --cluster create 192.168.1.5:6380 192.168.1.5:6381 192.168.1.5:6382 192.168.1.5:6383 192.168.1.5:6384 192.168.1.5:6385 --cluster-replicas 1
root@ac5eb8a0d40d:/data# redis-cli -a 1234 --cluster create 192.168.1.5:6380 192.168.1.5:6381 192.168.1.5:6382 192.168.1.5:6383 192.168.1.5:6384 192.168.1.5:6385 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.5:6384 to 192.168.1.5:6380
Adding replica 192.168.1.5:6385 to 192.168.1.5:6381
Adding replica 192.168.1.5:6383 to 192.168.1.5:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c 192.168.1.5:6380
slots:[0-5460] (5461 slots) master
M: 53ae5eb45c6e975844be09e4265974d7c5dd5897 192.168.1.5:6381
slots:[5461-10922] (5462 slots) master
M: ea8d4b455c69084b84c80177f42b327af37988e0 192.168.1.5:6382
slots:[10923-16383] (5461 slots) master
S: 652c6abcbfe3229b5bf42b6e8879d7543744c2aa 192.168.1.5:6383
replicates 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c
S: aef42b280ab14f22d426e954d8b1b81dae0e9913 192.168.1.5:6384
replicates 53ae5eb45c6e975844be09e4265974d7c5dd5897
S: 7a3ed1d545c7b27ad5e1fe9984119a6ebd1da440 192.168.1.5:6385
replicates ea8d4b455c69084b84c80177f42b327af37988e0
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 192.168.1.5:6380)
M: 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c 192.168.1.5:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: aef42b280ab14f22d426e954d8b1b81dae0e9913 192.168.1.5:6384
slots: (0 slots) slave
replicates 53ae5eb45c6e975844be09e4265974d7c5dd5897
S: 7a3ed1d545c7b27ad5e1fe9984119a6ebd1da440 192.168.1.5:6385
slots: (0 slots) slave
replicates ea8d4b455c69084b84c80177f42b327af37988e0
M: ea8d4b455c69084b84c80177f42b327af37988e0 192.168.1.5:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 652c6abcbfe3229b5bf42b6e8879d7543744c2aa 192.168.1.5:6383
slots: (0 slots) slave
replicates 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c
M: 53ae5eb45c6e975844be09e4265974d7c5dd5897 192.168.1.5:6381
slots:[5461-10922] (5462 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.
– cluster-replicas 1,参数后面的数字表示的是主从比例,比如这里的 1 表示的是主从比例是 1:1
如果 --cluster-replicas 2 那么主从比例就是 1:2,也就是 1 个主节点对于应 2 个从节点。
即:3(master) + 6(slave) = 9个 Redis 实例。
6. 查看集群状态
root@ac5eb8a0d40d:/data# redis-cli -a 1234 --cluster check 192.168.1.5:6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.5:6380 (9053ff2d...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.5:6382 (ea8d4b45...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.5:6381 (53ae5eb4...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.5:6380)
M: 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c 192.168.1.5:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: aef42b280ab14f22d426e954d8b1b81dae0e9913 192.168.1.5:6384
slots: (0 slots) slave
replicates 53ae5eb45c6e975844be09e4265974d7c5dd5897
S: 7a3ed1d545c7b27ad5e1fe9984119a6ebd1da440 192.168.1.5:6385
slots: (0 slots) slave
replicates ea8d4b455c69084b84c80177f42b327af37988e0
M: ea8d4b455c69084b84c80177f42b327af37988e0 192.168.1.5:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 652c6abcbfe3229b5bf42b6e8879d7543744c2aa 192.168.1.5:6383
slots: (0 slots) slave
replicates 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c
M: 53ae5eb45c6e975844be09e4265974d7c5dd5897 192.168.1.5:6381
slots:[5461-10922] (5462 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.
7. 检查redis集群节点信息
root@ac5eb8a0d40d:/data# redis-cli -c -a wangl -h 192.168.1.5 -p 6381
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.5:6381> cluster nodes
7a3ed1d545c7b27ad5e1fe9984119a6ebd1da440 192.168.1.5:6385@16385 slave ea8d4b455c69084b84c80177f42b327af37988e0 0 1681459069195 3 connected
53ae5eb45c6e975844be09e4265974d7c5dd5897 192.168.1.5:6381@16381 myself,master - 0 1681459068000 2 connected 5461-10922
aef42b280ab14f22d426e954d8b1b81dae0e9913 192.168.1.5:6384@16384 slave 53ae5eb45c6e975844be09e4265974d7c5dd5897 0 1681459070104 2 connected
ea8d4b455c69084b84c80177f42b327af37988e0 192.168.1.5:6382@16382 master - 0 1681459069095 3 connected 10923-16383
652c6abcbfe3229b5bf42b6e8879d7543744c2aa 192.168.1.5:6383@16383 slave 9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c 0 1681459070508 1 connected
9053ff2d691ac1e11dceeb6e11b1d0f950e26a8c 192.168.1.5:6380@16380 master - 0 1681459069000 1 connected 0-5460
192.168.1.5:6381>
8. 测试
# 在 6385 端口下添加数据
root@ac5eb8a0d40d:/data# redis-cli -c -a wangl -h 192.168.1.5 -p 6385
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.5:6385> set testkey testvalue
-> Redirected to slot [4757] located at 192.168.1.5:6380
OK
192.168.1.5:6380> get testkey
"testvalue"
192.168.1.5:6380> exit
# 在 6382 端口下查询数据
root@ac5eb8a0d40d:/data# redis-cli -c -a wangl -h 192.168.1.5 -p 6382
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.5:6382> get testkey
-> Redirected to slot [4757] located at 192.168.1.5:6380
"testvalue"