IP | master节点 | salve节点 |
---|---|---|
192.168.0.166 | redis-166:7001 |
redis-166:7002 |
192.168.0.167 | redis-167:7001 |
redis-167:7002 |
192.168.0.168 | redis-168:7001 |
redis-168:7002 |
[root@localhost tools]# vim /etc/hosts
192.168.0.166 redis-166
192.168.0.167 redis-167
192.168.0.168 redis-168
[root@localhost tools]# yum install -y gcc
[root@localhost tools]# tar -zxvf redis-5.0.5.tar.gz
[root@localhost tools]# cd redis-5.0.5/
[root@localhost redis-5.0.5]# make && make PREFIX=/usr/local/redis install
[root@localhost redis-5.0.5]# echo $?
0
[root@localhost tools]# vim /etc/profile
###### redis env ######
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
mkdir -p /usr/local/redis/redis_cluster/7001/conf/
mkdir -p /usr/local/redis/redis_cluster/7001/logs/
mkdir -p /usr/local/redis/redis_cluster/7001/data/
mkdir -p /usr/local/redis/redis_cluster/7002/conf/
mkdir -p /usr/local/redis/redis_cluster/7002/logs/
mkdir -p /usr/local/redis/redis_cluster/7002/data/
vim /usr/local/redis/redis_cluster/7001/conf/redis.conf
# 绑定服务器域名或IP地址
bind redis-166
# 设置端口,区分集群中Redis的实例
port 7001
# 后台运行
daemonize yes
# pid进程文件名,以端口号命名
pidfile /var/run/redis-7001.pid
# 日志文件名称,以端口号为目录来区分
logfile /usr/local/redis/redis_cluster/7001/logs/redis.log
# 数据文件存放地址,以端口号为目录名来区分
dir /usr/local/redis/redis_cluster/7001/data
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes_7001.conf
# 配置集群节点的超时时间
cluster-node-timeout 15000
# 启动AOF增量持久化策略
appendonly yes
# 发生改变,则记录日志
appendfsync always
vim /usr/local/redis/redis_cluster/7002/conf/redis.conf
# 绑定服务器域名或IP地址
bind redis-166
# 设置端口,区分集群中Redis的实例
port 7002
# 后台运行
daemonize yes
# pid进程文件名,以端口号命名
pidfile /var/run/redis-7002.pid
# 日志文件名称,以端口号为目录来区分
logfile /usr/local/redis/redis_cluster/7002/logs/redis.log
# 数据文件存放地址,以端口号为目录名来区分
dir /usr/local/redis/redis_cluster/7002/data
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes_7002.conf
# 配置集群节点的超时时间
cluster-node-timeout 15000
# 启动AOF增量持久化策略
appendonly yes
# 发生改变,则记录日志
appendfsync always
redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6460 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7001 [cluster]
root 6471 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7002 [cluster]
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6532 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7001 [cluster]
root 6548 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7002 [cluster]
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6486 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7001 [cluster]
root 6498 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7002 [cluster]
注:这里必须使用IP,如果使用像redis-166这样的域名,则会报错,具体原因我也不清楚
)redis-cli --cluster create 192.168.0.166:7001 192.168.0.167:7001 192.168.0.168:7001 192.168.0.166:7002 192.168.0.167:7002 192.168.0.168:7002 --cluster-replicas 1
[root@localhost redis-5.0.5]# redis-cli --cluster check 192.168.0.167:7001
192.168.0.167:7001 (b909c05c...) -> 0 keys | 5462 slots | 1 slaves.
192.168.0.166:7001 (e136a43b...) -> 0 keys | 5461 slots | 1 slaves.
192.168.0.168:7001 (4bf0b7df...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.0.167:7001)
M: b909c05ca4c89695fee7b4799050312ed20c989e 192.168.0.167:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: e136a43b8dbfd7612f48fe2c17e33203d5329eed 192.168.0.166:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64cee 192.168.0.168:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 84ada651baf494cbcdbfe26232b0061146260a3d 192.168.0.167:7002
slots: (0 slots) slave
replicates e136a43b8dbfd7612f48fe2c17e33203d5329eed
S: 4b9a181365ba49dde3def4d9d562c5a4d4ef657e 192.168.0.166:7002
slots: (0 slots) slave
replicates 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64cee
S: 02c3fc75bc6e6d4e070563f2b42a41f2616f66a5 192.168.0.168:7002
slots: (0 slots) slave
replicates b909c05ca4c89695fee7b4799050312ed20c989e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7002
192.168.0.167:7002> set key001 helloRredis
-> Redirected to slot [274] located at 192.168.0.166:7001
OK
192.168.0.166:7001> get key001
"helloRredis"
192.168.0.166:7001>
再去167上连接7001节点和168上连接7001或者7002看是否可以查询到数据
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7001
192.168.0.167:7001> get key001
-> Redirected to slot [274] located at 192.168.0.166:7001
"helloRredis"
192.168.0.166:7001>
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.168 -p 7002
192.168.0.168:7002> get key001
-> Redirected to slot [274] located at 192.168.0.166:7001
"helloRredis"
192.168.0.166:7001>
[root@localhost redis-5.0.5]# vim /etc/rc.local
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
[root@localhost redis-5.0.5]# chmod +x /etc/rc.d/rc.local