一、操作步骤
下载软件包
redis 下载官网:http://download.redis.io/releases/ (建议下载redis-5.0.4 版本)
[注]:以下操作都是基于redis 的5.0 以上版本所写,低于5.0 以下版本不适用
创建test用户
1.先以root的用户登录服务器+
2.创建用户test命令:
--useradd test (创建用户命令)
--passwd test (设置用户密码命令)
安装redis
先采用root 用户安装redis 安装包,启动时再切换回 test
1.在linux 上创建一个目录存放redis 的tar 包r
cd /home/test
mkdir redis-5.0.4
2.将redis-5.0.4.tar.gz上传到/home/test/redis-5.0.4 目录
3.在/home/test/redis-5.0.4目录下进行解压
tar-xvf redis-5.0.4.tar.gzu
4.在/home/test/redis-5.0.4/redis-5.0.4/目录下安装
先 cd /home/test/redis-5.0.4/redis-5.0.4
然后 make &&make install 这一步确保不出现error;
如果出现错误,就采用:
make MALLOC=libc
make install
创建伪集群
1.4.1 创建并启动redis 节点
1. 这里以两台服务器部署三主三从的集群为例,先创建一个目录存放redis 集群节点配置文件,并在该文件夹下创建 8000~8005 总共 6 个文件夹(以其中一台服务器159.1.55.166为例)
mkdir /home/test/redis
cd /home/test/redis.mkdir 8000 8001 8002
2.将/home/test/redis-5.0.4/redis-5.0.4目录下的redis.conf 复制到各个节点目录下+
cd /home/test/redis-5.0.4/redis-5.0.4
cp redis.conf /home/test/redis/8000
cp redis,cont /home/test/redis/8001
cp redis,conf /home/test/redis/8002
3.修改各个端口号文件夹下 redis.conf配置文件(并针对配置文件中配置项
dir /home/test/redis/data/8000 手动创建对应的数据目录),以 8000 端口为例
先手动创建/home/test/redis/data/8000 目录:
mkdir /home/test/redis/data/8000
修改配置文件 redis.conf
protected-mode no
port 8000
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 5000
daemonize yes+
pidfile /var/run/redis_8000.pid
logfile "/home/test/redis/data/8000/8000.log”
dir /home/test/redis/data/8000.
bind 159.1.55.91 (绑定本机 IP)
[注]: 如果是多台机器部署集群,需要在每台服务器的/etc/localhost 下设贵集群中其他服务器的主机名绑定的IP(以便通过对方主机名能找到相应的机器)“
4.启动各个redis节点
cd /home/test/redis-5.0.4/redis-5.0.4/src
redis-server/home/test/redis/8000/redis.conf
redis-server/home/test/redis/8001/redis.conf
redis-server/home/test/redis/8002/redis.conf
5.查看集群各节点启动是否正常
ps -eflgrep redis
root 3778 1 0 14:20? 00:00:00 src/redis-server 127.0.0.1:8000[cluster]
root 3819 1 0 14:21? 00:00:00 src/redis-server 127.0.0.1:8001[cluster]
root 3827 1 0 14:21? 00:00:00 src/redis-server127.0.0.1:8002 [cluster
root 3858 1598 0 14:21? pts/1 00:00:00grep –color=auto redis
6、在另外一台服务器上重复上面的操作来创建 8003、8004、8005 集群的另外三个节点
1.4.2 创建并启动集群
1启动集群命令如下:
端口号与 1.3.1的步骤3 中每台服务器redis.conf 的配置的 bind 159.1.33.91 相关:
redis-cli --cluster create 159.1.55.166:8000159.1.55.166:8001 159.1.55.166:8002 159.1.55.167:8003 159.1.55.167:8004 159.1.55.167:8005--cluster-replicas 1
查看控制台打印信息:“
>>> Performing hash slotsallocation on 6 nodes....
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461-10922
Master[2] -> Slots 10923 – 16383
Adding replica 159.1.55.167:8004 to 159.1.55.166:8000
Adding replica 159.1.55.167:8005 to 159.1.55.166:8001
Adding replica 159.1.55.167:8003 to 159.1.55.166:8002
[WARNING]Some slaves are in the same hostas their master
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 159.1.55.166:8000
slots:[0-5460] (5461slots) master
M: 860d39006e21ba74052627e899f307d0cfbec0b5 159.1.55.166:8001
slots:[5461-10922] (5462slots) master
M: d5882b089f975713c94dcbc1537c11428d5a9e1a 159 1.55.166:8002
slots:[10923-16383] (5461slots) master
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 159.1.55.167:8003
replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
S: fd6db44a16e12d6dc00a70d02472d07a69917acd 159.1.55.167:8004
replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
S: 93de823d19aa2879c283e47e02a7b0a33f0e0a3 159.1.55.167:8005
replicates 860d39006e21ba74052627e899f307d0cfbec0b5
Can I set the above configuration? (typeyes' to accept): yes
Nodes configuration updated-
>>> Assign a different configepoch to each node+
>>> Sending CLUSTER MEET messagesto join the cluster-
Waiting for the duster to join*!
Performing Cluster Check (using node 159.1.55.166:8000)
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 159.1.55.166:8000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 159.1.55.167:8003
slots: (0 slots) slave
replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
M: d5882b089f975713c94dcbr1537c11428d5a9e1a 159.1.55.166:8002
slots:[10923-16383](5461 slots) master+
1 additional replica(s)
M: 860d39006e21ba74052627e899f307d0cfbec0b5159.1.55.166:8001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 93de823d19aa2879c283e47e02a7b0a33ff0e0a3 159.1.55.167:8005
slots: (0 slots) slave+
replicates 860d39006e21ba74052627e899f307d0cfbec0b5
S: fd6db44a16e12d6dc00a70d02472d07a69917acd159.1.55.167:8004
slots: (0 slots) slave
replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
[OK] All nodes agree about slotsconfiguration
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2检查集群状态
连接集群任意一个节点“
cd /home/test/redis-5.0.4/redis-5.0.4/src
>redis-cli -h 159.1.55.166-p 8000 -c
>159 1.55.166:8000>set str0 hello0
>ok
连接集群另外一个节点+
>redis-cli -h 159.1.55.167 -p 8004 -c
>159.1.55.167:8004>get str0
>Redirected to slot[5076] located at159.1.55.167:8004
>"hello0”
异常情况处理
修改redis 配置的时候,kill 掉所有的 redis节点,cluster 宕掉了,这个时候重新创建集群失败
(ERR] Node bigdata24:8000 is not empty.Either the node already knows other nodes (check with CLUSTER NODES) orcontains some key in database 0.-
需要删除掉redis根目录下 data 以及src 目录下的dump.rdb以及nodes-8003.conf-确认删除干净后一定要重启个redis节点