Redis5.0集群搭建

主从模式

redis安装好后(redis安装:https://blog.csdn.net/lazyRabbitLLL/article/details/84204496
在redis下新建目录,并复制配置文件

cd /usr/local/redis
mkdir -p cluster/7001
mkdir -p cluster/7002
cp redis.conf cluster/7001/
cp redis.conf cluster/7002/

Master配置

# 注释指定ip访问
# bind 127.0.0.1

# 开启后台运行
daemonize yes
# 端口
port 7001
# pid文件
pidfile /var/run/redis_7001.pid
# 我这里配置了密码,可以不配置
requirepass 123456

Slave配置

# 注释指定ip访问
# bind 127.0.0.1

# 开启后台运行
daemonize yes
# 端口
port 7002
# pid文件
pidfile /var/run/redis_7002.pid
# 我这里配置了密码,可以不配置
requirepass 123456
# 配置主节点
slaveof 127.0.0.1 6379
# 若主节点设置了密码,则需要配置
masterauth 123456
#如果slave 无法与master同步,设置成slave不可读,方便监控脚本发现问题。
 slave-serve-stale-data no

vim下全局替换命令

# 替换7001为7002
:%s/7001/7002/g

连接主节点

./bin/redis-cli -p 127.0.0.1 -p 7001
auth 123456
# 查看主从配置是否成功
info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7002,state=online,offset=1533,lag=1
master_replid:1871af846ca88645c83f8179a2bc88dae791562f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1533
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1533

哨兵模式

新建一个sentinel.conf文件

# 后台运行
daemonize yes
# 端口
port 7000
# 配置监视的进群的主节点ip和端口 1表示至少需要几个哨兵统一认定才可以做出判断
sentinel monitor master7001 127.0.0.1 7001 1
# 配置哨兵连接主节点的认证密码
sentinel auth-pass master7001 lazyrabbit
# 配置多少毫秒后没收到主节点的反馈,则主观认为主节点down了
sentinel down-after-milliseconds master7001 5000
# failover过期时间。当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败
ssentinel failover-timeout master7001 30000

启动哨兵

./bin/redis-sentinel cluster/sentinel.conf

连接主节点,执行shutdown关闭主节点,连接从节点查询状态,过一阵子后,执行info replication,发现成功切换为主节点。

集群模式

新建文件夹17001-17006,复制redis.conf到节点下面,作以下配置:

#配置yes开启redis-cluster
cluster-enabled yes
#配置节点之间超时时间
cluster-node-timeout 15000
#这个配置很重要,cluster开启必须重命名指定cluster-config-file,不能与别的节点相同,否则会启动失败,最好按主机+端口命名
cluster-config-file nodes-6379.conf
# 这里需要注释掉
# REPLIACOF hostname port 会将当前服务器转变为某一服务器的副本服务器
#replicaof 127.0.0.1 7002
# 我这里没有设置密码

启动节点,我这里写了一个脚本来批量启动

# 创建一个脚本文件
vim cluster-startup.sh
# 内容如下
#!/bin/sh
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17001/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17002/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17003/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17004/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17005/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster/17006/redis.conf
# 给cluster-startup.sh可执行权限
chmod a+x cluster-startup.sh
# 启动6个节点
./cluster-startup.sh

创建集群

./bin/redis-cli --cluster create 127.0.0.1:17001 127.0.0.1:17002 127.0.0.1:17003 127.0.0.1:17004 127.0.0.1:17005 127.0.0.1:17006 --cluster-replicas 1
# 这里是执行命令后的提示信息
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:17004 to 127.0.0.1:17001
Adding replica 127.0.0.1:17005 to 127.0.0.1:17002
Adding replica 127.0.0.1:17006 to 127.0.0.1:17003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 322d298a1c25d482a6631562b975277534afe106 127.0.0.1:17001
   slots:[0-5460] (5461 slots) master
M: 666e4a05219e27e6aaf2e0756a36a13833b953c6 127.0.0.1:17002
   slots:[5461-10922] (5462 slots) master
M: 2314d5e2900012938478f81a5d840e2edca0146d 127.0.0.1:17003
   slots:[10923-16383] (5461 slots) master
S: 4f8d9f08aa2eae4162e07408f9b901b5effdfd8b 127.0.0.1:17004
   replicates 666e4a05219e27e6aaf2e0756a36a13833b953c6
S: 958dee45fb1e9d024f448bcd3337eb90b82dffa3 127.0.0.1:17005
   replicates 2314d5e2900012938478f81a5d840e2edca0146d
S: cf2a91642d009e58ccfbb2c947a66e81341447a5 127.0.0.1:17006
   replicates 322d298a1c25d482a6631562b975277534afe106
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:17001)
M: 322d298a1c25d482a6631562b975277534afe106 127.0.0.1:17001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 666e4a05219e27e6aaf2e0756a36a13833b953c6 127.0.0.1:17002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 2314d5e2900012938478f81a5d840e2edca0146d 127.0.0.1:17003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: cf2a91642d009e58ccfbb2c947a66e81341447a5 127.0.0.1:17006
   slots: (0 slots) slave
   replicates 322d298a1c25d482a6631562b975277534afe106
S: 4f8d9f08aa2eae4162e07408f9b901b5effdfd8b 127.0.0.1:17004
   slots: (0 slots) slave
   replicates 666e4a05219e27e6aaf2e0756a36a13833b953c6
S: 958dee45fb1e9d024f448bcd3337eb90b82dffa3 127.0.0.1:17005
   slots: (0 slots) slave
   replicates 2314d5e2900012938478f81a5d840e2edca0146d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

连接17001节点

./bin.redis-cli -h 127.0.0.1 -p 17001 -c

进行测试

127.0.0.1:17001> get name 
-> Redirected to slot [5798] located at 127.0.0.1:17002
(nil)
127.0.0.1:17002> set name lazyrabbit
OK
127.0.0.1:17002> get name 
"lazyrabbit"

参考

http://www.cnblogs.com/ibethfy/p/9965902.html
https://blog.csdn.net/qq_20597727/article/details/83385737

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值