准备3台机器 192.168.1.27、192.168.1.28、192.168.1.29,centos7 操作系统
1、安装redis,安装目录/usr/local/redis
tar -zxvf redis-5.0.3.tar.gz
mv redis-5.0.3 /usr/local/redis
cd /usr/local/redis
make && make test && make install
安装后目录结构为
[root@sword-01 redis]# pwd
/usr/local/redis
[root@sword-01 redis]# ll
total 252
-rw-rw-r-- 1 root root 92434 Dec 12 2018 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 Dec 12 2018 BUGS
-rw-rw-r-- 1 root root 1894 Dec 12 2018 CONTRIBUTING
drwxr-xr-x 3 root root 86 Jun 13 13:51 copy
-rw-rw-r-- 1 root root 1487 Dec 12 2018 COPYING
drwxrwxr-x 6 root root 192 Jun 13 09:14 deps
-rw-rw-r-- 1 root root 11 Dec 12 2018 INSTALL
-rw-rw-r-- 1 root root 151 Dec 12 2018 Makefile
-rw-rw-r-- 1 root root 4223 Dec 12 2018 MANIFESTO
-rw-rw-r-- 1 root root 20555 Dec 12 2018 README.md
-rw-rw-r-- 1 root root 62155 Dec 12 2018 redis.conf
-rwxrwxr-x 1 root root 275 Dec 12 2018 runtest
-rwxrwxr-x 1 root root 280 Dec 12 2018 runtest-cluster
-rwxrwxr-x 1 root root 281 Dec 12 2018 runtest-sentinel
-rw-rw-r-- 1 root root 9710 Dec 12 2018 sentinel.conf
drwxr-xr-x 44 root root 4096 Jun 20 18:00 snapshotting
drwxrwxr-x 3 root root 8192 Jun 13 09:15 src
drwxrwxr-x 10 root root 167 Dec 12 2018 tests
drwxrwxr-x 8 root root 4096 Dec 12 2018 utils
安装6个实例的分别为
192.168.1.27:7001
192.168.1.27:7002
192.168.1.28:7003
192.168.1.28:7004
192.168.1.29:7005
192.168.1.29:7006
192.168.1.27:7001安装步骤如下
配置文件: 复制 /usr/local/redis/redis.conf 到 /etc/redis/7001.conf
mkdir /etc/redis #配置文件目录
cp /usr/local/redis/redis.conf /etc/redis/7001.conf
mkdir -p /etc/redis-cluster #集群配置目录
mkdir -p /var/log/redis #日志目录
mkdir -p /var/redis/7001 #工作目录
更改配置7001.conf
port 7001 #端口
cluster-enabled yes #启动集群
protected-mode no #保护模式
masterauth redis-master #集群认证
requirepass redis-pass #redis 密码
daemonize yes #后台启动
cluster-config-file /etc/redis-cluster/node-7001.conf #集群配置
cluster-node-timeout 15000 #超时时间
pidfile /var/run/redis_7001.pid #进程文件
dir /var/redis/7001 #工作目录
logfile /var/log/redis/7001.log #日志目录
bind 192.168.1.27 #绑定IP
appendonly yes #是否持久化
启动脚本
#启动脚本
cd /usr/local/redis/utils/
cp redis_init_script /etc/init.d/redis_7001
更改启动脚本redis_7001 端口 为7001
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=7001 #更改端口
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
剩下的5个实例同样的配置
启动实例
cd /etc/init.d/
./redis_7001 start
[root@sword-01 init.d]# ps -ef |grep redis
root 2061 1 0 18:01 ? 00:00:12 /usr/local/bin/redis-server 192.168.1.27:7001 [cluster]
root 2168 2111 0 19:15 pts/1 00:00:00 grep --color=auto redis
同样操作启动剩下5个实例
启动集群
/usr/local/redis/src/redis-cli --cluster create 192.168.1.27:7001 192.168.1.27:7002 192.168.1.28:7003 192.168.1.28:7004 192.168.1.29:7005 192.168.1.29:7006 --cluster-replicas 1 -a redis-pass
[root@sword-01 init.d]# /usr/local/redis/src/redis-cli --cluster create 192.168.1.27:7001 192.168.1.27:7002 192.168.1.28:7003 192.168.1.28:7004 192.168.1.29:7005 192.168.1.29:7006 --cluster-replicas 1 -a redis-pass
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 192.168.1.28:7004 to 192.168.1.27:7001
Adding replica 192.168.1.27:7002 to 192.168.1.28:7003
Adding replica 192.168.1.29:7006 to 192.168.1.29:7005
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: 121ba0dfba42436d0f54da329c54be441bf7023a 192.168.1.27:7001
slots:[0-5460] (5461 slots) master
S: 3aed761b317ece72eb37178b1d930395f94bf360 192.168.1.27:7002
replicates e26bef0a1bba9629cd681fd49a1ae77e3bccef26
M: dd9ef3065622a05602a3ae42b365ff2180ff3936 192.168.1.28:7003
slots:[5461-10922] (5462 slots) master
S: 2676b046e71ecaffff15d68ca1337e90d918bf39 192.168.1.28:7004
replicates 121ba0dfba42436d0f54da329c54be441bf7023a
M: e26bef0a1bba9629cd681fd49a1ae77e3bccef26 192.168.1.29:7005
slots:[10923-16383] (5461 slots) master
S: 5bff0d79a4f212d01120332af97129887e7e1218 192.168.1.29:7006
replicates dd9ef3065622a05602a3ae42b365ff2180ff3936
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 192.168.1.27:7001)
M: 121ba0dfba42436d0f54da329c54be441bf7023a 192.168.1.27:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 2676b046e71ecaffff15d68ca1337e90d918bf39 192.168.1.28:7004
slots: (0 slots) slave
replicates 121ba0dfba42436d0f54da329c54be441bf7023a
S: 5bff0d79a4f212d01120332af97129887e7e1218 192.168.1.29:7006
slots: (0 slots) slave
replicates dd9ef3065622a05602a3ae42b365ff2180ff3936
M: dd9ef3065622a05602a3ae42b365ff2180ff3936 192.168.1.28:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 3aed761b317ece72eb37178b1d930395f94bf360 192.168.1.27:7002
slots: (0 slots) slave
replicates e26bef0a1bba9629cd681fd49a1ae77e3bccef26
M: e26bef0a1bba9629cd681fd49a1ae77e3bccef26 192.168.1.29:7005
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证集群是否成功
登录任意一台实例
[root@sword-01 init.d]# redis-cli -h 192.168.1.27 -p 7001 -a redis-pass
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.27:7001> pping
(error) ERR unknown command `pping`, with args beginning with:
192.168.1.27:7001> ping
PONG
192.168.1.27:7001> set k1 v1
(error) MOVED 12706 192.168.1.29:7005
192.168.1.27:7001> set k1 v1
(error) MOVED 12706 192.168.1.29:7005
192.168.1.27:7001> set k1234123 v1