在一台linux服务器上使用Docker搭建一个cluster模式的redis集群。三个master节点,三个slave节点,六个节点因为在同一台服务器上,所以每个节点使用不同的端口,端口范围是6380到6385。
redis cluster集群具有如下几个特点:
可以选择指定版本的redis,本文为了方便演示,使用最新版本
docker pull redis
docker network create rediscluster
因为六个节点监听端口不同,所以配置文件也有区别,可以使用如下shell脚本生成对应配置文件(假如命名为createRedisConf.sh):
#!/bin/sh
for port in $(seq 6380 6385);
do
mkdir -p ~/redisCluster/node-${port}/conf
touch ~/redisCluster/node-${port}/conf/redis.conf
cat << EOF > ~/redisCluster/node-${port}/conf/redis.conf
#节点端口
port ${port}
#添加访问认证
requirepass luduoxin
#如果主节点开启了访问认证,从节点访问主节点需要认证
masterauth luduoxin
#保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问
protected-mode no
#bind 0.0.0.0
#是否以守护线程的方式启动(后台启动),默认 no
daemonize no
#是否开启 AOF 持久化模式,默认 no
Appendonly yes
#是否开启集群模式,默认 no
cluster-enabled yes
#集群节点信息文件
cluster-config-file nodes.conf
#群节点连接超时时间
cluster-node-timeout 5000
#集群节点 IP,我使用的服务的ip为172.16.3.110,替换为自己的服务器的即可
cluster-announce-ip 172.16.3.110
#集群节点映射端口
cluster-announce-port ${port}
#集群节点总线端口
cluster-announce-bus-port 1${port}
EOF
done
创建此文件后,先赋予执行权限,然后执行生成配置文件
$ chmod 755 ./createRedisConf.sh
$ sh ./createRedisConf.sh
可以使用如下shell脚本快速创建出来(createRedis.sh)
#!/bin/sh
## 首次创建并运行redis容器
if [ "$1" = "create" ]; then
for port in $(seq 6380 6385);
do
docker run -di --restart always --name redis-${port} --net rediscluster -p ${port}:${port} -p 1${port}:1${port} -v ~/redisCluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redisCluster/node-${port}/data:/data redis redis-server /usr/local/etc/redis/redis.conf
done
fi
##停止redis容器
if [ "$1" = "stop" ]; then
for port in $(seq 6380 6385);
do
docker stop redis-${port}
done
fi
##启动已有的redis容器
if [ "$1" = "start" ]; then
for port in $(seq 6380 6385);
do
docker start redis-${port}
done
fi
## 删除redis容器
if [ "$1" = "rm" ]; then
for port in $(seq 6380 6385);
do
docker rm redis-${port}
done
fi
创建此文件后,先赋予执行权限,然后执行创建出redis服务
$ chmod 755 ./createRedis.sh
$ sh ./createRedis.sh create
创建完成后,可以使用命令 docker ps -a 查看容器。
选择一个redis容器,例如选择redis-6380容器,进入容器
docker exec -it redis-6380 /bin/bash
在容器里面执行如下命令
redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110:6385 --cluster-replicas 1
然后出现如下提示,输入 yes 继续
Can I set the above configuration ? (type 'yes' to accept):
创建过程如下:
# redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110: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 172.16.3.110:6384 to 172.16.3.110:6380
Adding replica 172.16.3.110:6385 to 172.16.3.110:6381
Adding replica 172.16.3.110:6383 to 172.16.3.110:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
replicates 78891932599b7497c8dd921295ba19eb0f549285
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
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 172.16.3.110:6380)
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
slots: (0 slots) slave
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
slots: (0 slots) slave
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
slots: (0 slots) slave
replicates 78891932599b7497c8dd921295ba19eb0f549285
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到这里集群就创建成功了。
检查集群状态
redis-cli -a luduoxin --cluster check 172.16.3.110:6382
连接集群某个节点
$ redis-cli -c -a luduoxin -h 172.16.3.110 -p 6382
//查看集群信息
172.16.3.110:6381> cluster info
//查看集群结点信息
172.16.3.110:6381> cluster nodes
//查看集群的slot分配区间及对应的主从节点映射关系
172.16.3.110:6381> cluster slots
本文主要讲了如何在一台Linux服务器上使用docker搭建一个cluster模式的redis集群,这种方式搭建的集群主要用于测试用途,不建议在生产环境使用。