#!/bin/sh
####create cluster dir
dir=/data/redis-cluster
if [ ! -e  "${dir}" ];then
     mkdir -p $dir &&\
     mkdir /data/redis-cluster/{logs,nodes,pids,redis_6379,redis_6380,redis_6381} -p
fi
###create 6379 config
cd $dir/redis_6379 &&
cat >redis.conf<<EOF
include /data/redis-cluster/redis-common.conf
pidfile /data/redis-cluster/pids/redis_6379.pid
bind 0.0.0.0
port 6379 
logfile /data/redis-cluster/logs/redis_6379.log
dir /data/redis-cluster/redis_6379
cluster-config-file /data/redis-cluster/nodes/6379_nodes.conf     
EOF
###create 6380 config
cd $dir/redis_6380 &&
cat > redis.conf << EOF
#vim redis.conf
include /data/redis-cluster/redis-common.conf
pidfile /data/redis-cluster/pids/redis_6380.pid
bind 0.0.0.0
port 6380 
logfile /data/redis-cluster/logs/redis_6380.log
dir /data/redis-cluster/redis_6380
cluster-config-file /data/redis-cluster/nodes/6380_nodes.conf
EOF
###create 6381 config
cd $dir/redis_6381 &&
cat > redis.conf << EOF
include /data/redis-cluster/redis-common.conf
pidfile /data/redis-cluster/pids/redis_6381.pid
bind 0.0.0.0
port 6381 
logfile /data/redis-cluster/logs/redis_6381.log
dir /data/redis-cluster/redis_6381
cluster-config-file /data/redis-cluster/nodes/6381_nodes.conf
EOF
###create redis-common.conf
cd $dir
cat > redis-common.conf << EOF
daemonize yes
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
databases 1
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 8gb
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
cluster-enabled yes
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
###start redis
for n in 6379 6380 6381;
    do redis-server /data/redis-cluster/redis_"$n"/redis.conf
    sleep 2 
done
### check redis status
if [ $? -eq 0 ];then
   echo  "redis master create cuccess " 
else
  echo "redis master create false"
fi
