redis集群配置
1.一主三从三哨兵
第一步,下载安装redis
第二步,配置节点的redis.conf
1.主节点6379
protected-mode yes
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
logfile "/var/log/redis/redis_6379.log"
dir "/usr/local/redis-cluster/6379/data"
replica-read-only yes
requirepass "password"
masterauth "password"
maxmemory 200mb
2.从节点6380
protected-mode yes
port 6380
daemonize yes
pidfile "/var/run/redis_6380.pid"
logfile "/var/log/redis/redis_6380.log"
dir "/usr/local/redis-cluster/6380/data"
replicaof 127.0.0.1 6379
masterauth "password"
requirepass "password"
maxmemory 200mb
3.从节点6381,6382同理
4.哨兵节点1配置文件sentinel.conf
protected-mode no
daemonize yes
port 26379
dir "/usr/local/redis-cluster/6379/temp"
sentinel deny-scripts-reconfig yes
sentinel monitor yourname 127.0.0.1 6379 2
5.哨兵节点2
protected-mode no
daemonize yes
port 26380
dir "/usr/local/redis-cluster/6380/temp"
sentinel deny-scripts-reconfig yes
sentinel monitor yourname 127.0.0.1 6379 2
6.哨兵节点3
protected-mode no
daemonize yes
port 26381
dir "/usr/local/redis-cluster/6381/temp"
sentinel deny-scripts-reconfig yes
sentinel monitor yourname 127.0.0.1 6379 2
第三步,节点启动
redis-server /usr/local/redis-cluster/6379/redis.conf
redis-server /usr/local/redis-cluster/6380/redis.conf
redis-server /usr/local/redis-cluster/6381/redis.conf
redis-server /usr/local/redis-cluster/6382/redis.conf
redis-server /usr/local/redis-cluster/6380/sentinel.conf --sentinel
redis-server /usr/local/redis-cluster/6381/sentinel.conf --sentinel
redis-server /usr/local/redis-cluster/6379/sentinel.conf --sentinel
一个一个启动太繁琐了,于是乎写个简单的shell脚本
#!/bin/bash
echo "start or stop or watch"
read -p "please input:" input
if [ $input == "start" ];then
redis-server /usr/local/redis-cluster/6379/redis.conf
redis-server /usr/local/redis-cluster/6380/redis.conf
redis-server /usr/local/redis-cluster/6381/redis.conf
redis-server /usr/local/redis-cluster/6382/redis.conf
redis-server /usr/local/redis-cluster/6380/sentinel.conf --sentinel
redis-server /usr/local/redis-cluster/6381/sentinel.conf --sentinel
redis-server /usr/local/redis-cluster/6379/sentinel.conf --sentinel
elif [ $input == "watch" ];then
ps -ef | grep redis-server
elif [ $input == "stop" ];then
for i in `ps -ef | grep redis-server| awk '{print $2}'`
do
echo "$i 被kill掉"
kill -9 $i
re=`ps -ef | grep redis-server | awk '{print $2}' | wc -l`
echo "$re"
done
ps -ef | grep redis-server
fi
done
2.Redis群集数据分片
第一步,同上
第二步,节点配置文件
1.节点5001 redis.conf
protected-mode yes
port 5001
daemonize yes
pidfile "/var/run/redis_5001.pid"
logfile "/var/log/redis/redis_5001.log"
dir "/usr/local/redis-cluster1.5/5001/data"
maxmemory 200mb
cluster-enabled yes
cluster-config-file nodes-5001.conf
cluster-node-timeout 15000
2.节点5002 redis.conf
protected-mode yes
port 5002
daemonize yes
pidfile "/var/run/redis_5002.pid"
logfile "/var/log/redis/redis_5002.log"
dir "/usr/local/redis-cluster1.5/5002/data"
maxmemory 200mb
cluster-enabled yes
cluster-config-file nodes-5002.conf
cluster-node-timeout 15000
3.5003同理
第三步,握手过程
fancy@ubuntu:~$ redis-cli -p 5001
127.0.0.1:5001> CLUSTER MEET 127.0.0.1 5002
OK
127.0.0.1:5001> CLUSTER MEET 127.0.0.1 5003
OK
第四步,分配槽
redis-cli -p 5001 cluster addslots {0..5461}
redis-cli -p 5002 cluster addslots {5462..10922}
redis-cli -p 5003 cluster addslots {10923..16383}
done
3.三主三从集群搭建
两种方法
第一种:
使用redis-cli --cluster create命令 需要配置各个节点
第二种:
用redis为我们提供的bash脚本create-cluster创建,我们只需要改改这个bash脚本的几个值就可自定义启动的集群
PORT代表启动节点的端口-1,比如我要在7000端口,把他改成6999即可
NODES代表节点数量
简单看一下README就可小白创建or启动or停止集群。