redis cluster 6.2集群
redis最新版本:redis-6.2.1.tar.gz
安装的版本是redis-6.0.3
采用的主机:
djz-server-001 192.168.2.163 7001,7002,Admin@135
djz-server-002 192.168.2.164 7001,7002,Admin@135
djz-server-003 192.168.2.165 7001,7002,Admin@135
$ sudo mkdir -p /app/{server,soft}
$ sudo yum -y install centos-release-scl
$ sudo yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
# scl enable devtoolset-9 bash
# cd /app/soft
# wget http://download.redis.io/releases/redis-6.0.3.tar.gz
# tar zxf redis-6.0.3.tar.gz
# cd redis-6.0.3
# make
# make install PREFIX=/app/server/redis-cluster
$ cd /app/server/redis-cluster
$ sudo cp bin/redis-server .
$ sudo cp bin/redis-cli .
$ sudo mkdir -p /app/var/logs/redis
$ sudo mkdir -p /app/server/redis-cluster/run
$ sudo vim redis7001.conf
protected-mode yes
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /app/server/redis-cluster/run/redis_7001.pid
loglevel notice
logfile "/app/var/logs/redis/redis7001.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump7001.rdb
dir /app/server/redis-cluster/
masterauth Admin@135
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass Admin@135
maxclients 30000
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 5000
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-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
sudo vim /etc/init.d/redis7001
#!/bin/bash
#
# redis startup script for the redis7001 server
#
#
# chkconfig: 345 80 20
# description: start the redis7001 deamon
#
# Source function library
#. /etc/rc.d/init.d/functions
app_user=dzfp
export app_user
prog=redis7001
RDPATH_HOME=/app/server/redis-cluster
export RDPATH_HOME
CONF_FILE=redis7001.conf
export CONF_FILE
NODE_FILE=nodes_7001.conf
export NODE_FILE
REDIS_AUTH="Admin@135"
export REDIS_AUTH
P_PORT=7001
export P_PORT
if [ "$(whoami)" != "root" ] && [ "$(whoami)" != "${app_user}" ]; then
echo "please run this script as root or ${app_user} ."
exit 1
fi
case "$1" in
start)
echo "Starting " ${prog} "..."
cd ${RDPATH_HOME}
if [[ "$(whoami)" = "root" ]]; then
if [ -f ${NODE_FILE} ];then
CLUSTER_MYSELF=`su - ${app_user} -c "cat ${RDPATH_HOME}/${NODE_FILE} |grep myself"`
su - ${app_user} -c "echo ${CLUSTER_MYSELF} > ${RDPATH_HOME}/${NODE_FILE}"
fi
su - ${app_user} -c "${RDPATH_HOME}/redis-server ${RDPATH_HOME}/${CONF_FILE}"
elif [[ "$(whoami)" = "${app_user}" ]]; then
if [ -f ${NODE_FILE} ];then
CLUSTER_MYSELF=`cat ${RDPATH_HOME}/${NODE_FILE} |grep myself`
echo ${CLUSTER_MYSELF} > ${RDPATH_HOME}/${NODE_FILE}
fi
${RDPATH_HOME}/redis-server ${RDPATH_HOME}/${CONF_FILE}
fi
;;
stop)
echo "Stopping " ${prog} "..."
cd ${RDPATH_HOME}
if [[ "$(whoami)" = "root" ]]; then
su - ${app_user} -c "${RDPATH_HOME}/redis-cli -a ${REDIS_AUTH} -p ${P_PORT} shutdown"
elif [[ "$(whoami)" = "${app_user}" ]]; then
${RDPATH_HOME}/redis-cli -a ${REDIS_AUTH} -p ${P_PORT} shutdown
fi
;;
restart)
echo "Stopping " ${prog} "..."
cd ${RDPATH_HOME}
if [[ "$(whoami)" = "root" ]]; then
su - ${app_user} -c "${RDPATH_HOME}/redis-cli -a ${REDIS_AUTH} -p ${P_PORT} shutdown"
elif [[ "$(whoami)" = "${app_user}" ]]; then
${RDPATH_HOME}/redis-cli -a ${REDIS_AUTH} -p ${P_PORT} shutdown
fi
sleep 2
echo
echo "Starting " ${prog} "..."
cd ${RDPATH_HOME}
if [[ "$(whoami)" = "root" ]]; then
su - ${app_user} -c "${RDPATH_HOME}/redis-server ${RDPATH_HOME}/${CONF_FILE}"
elif [[ "$(whoami)" = "${app_user}" ]]; then
${RDPATH_HOME}/redis-server ${RDPATH_HOME}/${CONF_FILE}
fi
;;
*)
echo "Usage: $prog {start|stop|restart}"
;;
esac
exit 0
# chown -R dzfp.dzfp /app/server/redis-cluster /app/var/logs/redis/
# chmod 755 /etc/init.d/redis7001
# chkconfig --add redis7001
# /etc/init.d/redis7001 start
# ps -ef|grep redis
# 根据redis7001配置redis7002服务
# cd /app/server/redis-cluster
# cp -a redis7001.conf redis7002.conf
# cp -a /etc/init.d/redis7001 /etc/init.d/redis7002
# sed -i 's/7001/7002/g' redis7002.conf
# sed -i 's/7001/7002/g' /etc/init.d/redis7002
# 服务启动
# chkconfig --add redis7002
# /etc/init.d/redis7002 start
其他主机上也做上述同样的操作
$ cd /app/server/redis-cluster
# 在一台主机上执行 (ip换成实际主机ip)
$ ./redis-cli -a Admin@135 --cluster create 192.168.2.163:7001 192.168.2.164:7001 192.168.2.165:7001 192.168.2.163:7002 192.168.2.164:7002 192.168.2.165:7002 --cluster-replicas 1
$ cd /app/server/redis-cluster
$ ./redis-cli -c -h 192.168.2.163 -p 7001 -a "Admin@135"
192.168.2.163:7001> cluster info
192.168.2.163:7001> cluster nodes
192.168.2.163:7001> set name andy
-> Redirected to slot [5798] located at 192.168.2.164:7001
OK
192.168.2.163:7001>
在其他的节点验证是否成功
$ ./redis-cli -c -h 192.168.2.164 -p 7001 -a "Admin@135"
192.168.2.164:7001> get name
"andy"
192.168.2.164:7001>
$ ./redis-cli -c -h 192.168.2.165 -p 7001 -a "Admin@135"
192.168.2.165:7001> get name
-> Redirected to slot [5798] located at 192.168.2.164:7001
"andy"
192.168.2.164:7001>