搭建redis cluster集群服务
redis 5.0以下为ruby编写,运行命令时需要安装ruby,而5.0以上则为c编写,可直接安装后运行。因此本文使用redis5.0.5
1、编写配置文件
在 /home 下新建 redis-cluster 文件夹,然后创建 redis-cluster.tmpl 文件,文件内容如下:
port ${PORT} protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 10.141.177.28 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly no maxmemory 2gb maxmemory-policy allkeys-lru
2、生成工作目录,create_dir.sh
示例中生成7010-7015六个工作目录
for port in `seq 7010 7015`; do \ mkdir -p ./${port}/conf \ && touch ./${port}/conf/redis.conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; \ done
3、创建6个redis实例,start_redis.sh
for port in `seq 7010 7015`; do \ cd ./${port} redis-server conf/redis.conf & cd - done
4、创建cluster集群,start_cluster.sh
168.0.0.1应改成实际的host地址,这里创建了3master3slave的cluster集群,其中,master:7010,7011,7012,slave:7013,7014,7015
redis-cli --cluster create 168.0.0.1:7010 168.0.0.1:7011 168.0.0.1:7012 168.0.0.1:7013 168.0.0.1:7014 168.0.0.1:7015 --cluster-replicas 1
5、关闭,stop_redis.sh
ps aux |grep redis-server|grep -v grep |awk '{print $2}'|xargs kill -9
补充:
连接集群中某个redis实例
redis-cli -c -h 168.0.0.1 -p 7010
查看集群状态
redis-cli cluster nodes -p 7010
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=