1.安装ruby与相关组件:
查看服务器是否安装ruby:
yum list | grep ruby
rpm -qa | grep ruby
若没有安装,则安装组件:
yum install ruby
安装gem相关:
yum install rubygems
gem install redis
2、下载
mkdir redis-cluster
$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
$ tar xzf redis-4.0.1.tar.gz
$ cd redis
$ make && make install
3、将 redis-trib.rb 复制到 /usr/local/bin 目录下
cd src
cp redis-trib.rb /usr/local/bin/
4、在redis同级新建目录
mkdir redis7001
复制src目录中redis-server redis-cli redis-benchmark到redis7001目录
复制redis.conf到redis7001中
5、修改redis.conf
port 7001 //端口7001,7002,7003
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7001.pid //pidfile文件对应7001,7002,7003,
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes-7001.conf //集群的配置 配置文件首次启动自动生成 7001,7002,7003
cluster-node-timeout 5000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
逐一复制新建7002-7006,修改配置
6、配置启动各个节点
编写启动脚本redis.sh
for port in $(seq 7001 7006) ;do /usr/local/redis-cluster/redis/src/redis-server /usr/local/redis-cluster/redis$port/redis.conf;done
编写stop脚本rediskill.sh
for port in $(seq 7001 7006) ;do /usr/local/redis-cluster/redis/src/redis-cli -h 127.0.0.1 -p $port shutdown;done
执行即可启动各个节点
7、创建集群
redis-trib.rb 是一个 ruby 脚本工具,用来建立和管理 redis 集群
由于redis-trib.rb 复制到 /usr/local/bin 可以直接在命令行中使用了。使用下面这个命令即可完成安装。
redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
这个命令在这里用于创建一个新的集群, 选项–replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是这个集群实例的地址列表,3个master3个slave redis-trib 会打印出一份预想中的配置给你看, 如果你觉得没问题的话, 就可以输入 yes , redis-trib 就会将这份配置应用到集群当中,让各个节点开始互相通讯
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: 7c735d9b65e18d784ae7eb3c897e9845562d4c4e 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 8d32fdf39eea5d4fd1dc91520d2584f4c8394897 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 33be8b61603fef126471b850b3e1387a49898df9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: 1319ecdc9d441f10ca4034d2ca39339e7d972154 127.0.0.1:7004
replicates 7c735d9b65e18d784ae7eb3c897e9845562d4c4e
S: 76dfb2c1850f0c1b16e13e956d2b80904d181d63 127.0.0.1:7005
replicates 8d32fdf39eea5d4fd1dc91520d2584f4c8394897
S: 41468246b5fd6e94cc01235baab78082798ceb22 127.0.0.1:7006
replicates 33be8b61603fef126471b850b3e1387a49898df9
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 127.0.0.1:7001)
M: 7c735d9b65e18d784ae7eb3c897e9845562d4c4e 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8d32fdf39eea5d4fd1dc91520d2584f4c8394897 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 1319ecdc9d441f10ca4034d2ca39339e7d972154 127.0.0.1:7004
slots: (0 slots) slave
replicates 7c735d9b65e18d784ae7eb3c897e9845562d4c4e
M: 33be8b61603fef126471b850b3e1387a49898df9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 41468246b5fd6e94cc01235baab78082798ceb22 127.0.0.1:7006
slots: (0 slots) slave
replicates 33be8b61603fef126471b850b3e1387a49898df9
S: 76dfb2c1850f0c1b16e13e956d2b80904d181d63 127.0.0.1:7005
slots: (0 slots) slave
replicates 8d32fdf39eea5d4fd1dc91520d2584f4c8394897
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
出现上面即为成功
进入redis7001
执行:
./redis-cli -c -p 7001则进入客户端.测试
若为ip则redis-cli -h 192.168.31.245 -c -p 7002 ,加参数 -C 可连接到集群,因为上面 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略。
127.0.0.1:7002> set name test
OK
127.0.0.1:7002> get name
"test"
127.0.0.1:7003> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"test"
8、设置密码
注意事项:
1.如果是使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码,集群构建完毕再通过config set + config rewrite命令逐个机器设置密码
2.如果对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题,可以模拟并观察日志
3.各个节点的密码都必须一致,否则Redirected就会失败
方法一:
[root@root redis7001]# vi redis.conf
masterauth <abc>
requirepass abc
方法二:
[root@root redis7001]# ./redis-cli -c -p 7001
127.0.0.1:7001> config set masterauth abc
OK
127.0.0.1:7001> config set requirepass abc
OK
127.0.0.1:7001> config rewrite
默认前面存在#号的,修改密码需要把#删除
修改完重启
9、Redis Cluster 再次安装出现的异常
今天补充一下一个异常,当你Redis Cluster 集群在集群一次,比如IP地址要变化或者什么的,你再次集群的时候你会发现一个异常:
Node 127.0.0.1:10001 is not empty. Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决之道:这个时候你需要把开始生成的nodes_*.conf 、appendonly.aof 、dump.rdb 文件删除,再次集群就可以了。
10、redis启动脚本单个文件
vi /etc/init.d/redis
#!/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
REDISPORT=7001
EXEC=/usr/java/redis-cluster/redis7001/redis-server
CLIEXEC=/usr/java/redis-cluster/redis7001/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
#设置为开机自启动服务器
chkconfig redisd on
#打开服务
service redisd start
#关闭服务
service redisd stop