redis主从复制与集群

http://www.redis.cn/topics/cluster-tutorial.html

http://doc.redisfans.com/topic/cluster-tutorial.html

下载

http://download.redis.io/releases/

wget http://download.redis.io/releases/redis-3.2.12.tar.gz

安装配置

yum install gcc gcc-c++ automake autoconf jemalloc jemalloc-devel hiredis-devel
tar xf redis-3.2.12.tar.gz -C /usr/local/
ln -s /usr/local/redis-3.2.12 /usr/local/redis
cd /usr/local/redis
make

cat /etc/profile.d/redis.sh

export PATH="/usr/local/redis/src:$PATH"

主从复制

创建工作目录

mkdir /data/638{0..2}

配置文件

cat >> /data/6380/redis.conf <<EOF
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dbfilename dump.rdb
dir /data/6380
requirepass 123
masterauth 123
EOF
cat >>   /data/6381/redis.conf <<EOF
port 6381
daemonize yes
pidfile /data/6381/redis.pid
loglevel notice
logfile "/data/6381/redis.log"
dbfilename dump.rdb
dir /data/6381
requirepass 123
masterauth 123
EOF
cat >>   /data/6382/redis.conf <<EOF
port 6382
daemonize yes
pidfile /data/6382/redis.pid
loglevel notice
logfile "/data/6382/redis.log"
dbfilename dump.rdb
dir /data/6382
requirepass 123
masterauth 123
EOF

启动

redis-server /data/6380/redis.conf
redis-server /data/6381/redis.conf
redis-server /data/6382/redis.conf

配置主从

从库执行

redis-cli -p 6381 -a 123 SLAVEOF 127.0.0.1 6380
redis-cli -p 6382 -a 123 SLAVEOF 127.0.0.1 6380

查询主从状态

redis-cli -p 6380 -a 123 info replication
redis-cli -p 6381 -a 123 info replication
redis-cli -p 6382 -a 123 info replication

主从切换

主库down之后,在一个从库执行

SLAVEOF no one

其它从库重新做主从

SLAVEOF 127.0.0.1 6381

slave-read-only 只读模式

masterauth 主服务器设置密码后需要填写密码

执行写操作所需的至少从服务器数量

min-slaves-to-write <number of slaves>

指定网络延迟的最大值

min-slaves-max-lag <number of seconds>

从服务器延迟不大于

CONFIG set slave-read-only yes

Config set masterauth root

INFO replication

SLAVEOF NO ONE 升级至MASTER 

安装集群插件

yum install ruby rubygems -y

使用国内源

gem sources -l
gem sources  --remove https://rubygems.org/
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources -a https://gems.ruby-china.com
gem install redis -v 3.3.3

集群节点准备

mkdir /data/700{0..5} -p

cat /data/7000/redis.conf

port 7000
daemonize yes
pidfile /data/7000/redis.pid
loglevel notice
logfile "/data/7000/redis.log"
dbfilename dump.rdb
dir /data/7000
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

cat /data/7001/redis.conf

port 7001
daemonize yes
pidfile /data/7001/redis.pid
loglevel notice
logfile "/data/7001/redis.log"
dbfilename dump.rdb
dir /data/7001
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

cat /data/7002/redis.conf

port 7002
daemonize yes
pidfile /data/7002/redis.pid
loglevel notice
logfile "/data/7002/redis.log"
dbfilename dump.rdb
dir /data/7002
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

cat /data/7003/redis.conf

port 7003
daemonize yes
pidfile /data/7003/redis.pid
loglevel notice
logfile "/data/7003/redis.log"
dbfilename dump.rdb
dir /data/7003
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

cat /data/7004/redis.conf

port 7004
daemonize yes
pidfile /data/7004/redis.pid
loglevel notice
logfile "/data/7004/redis.log"
dbfilename dump.rdb
dir /data/7004
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

cat /data/7005/redis.conf

port 7005
daemonize yes
pidfile /data/7005/redis.pid
loglevel notice
logfile "/data/7005/redis.log"
dbfilename dump.rdb
dir /data/7005
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123
masterauth 123

启动节点

redis-server /data/7000/redis.conf 
redis-server /data/7001/redis.conf 
redis-server /data/7002/redis.conf 
redis-server /data/7003/redis.conf 
redis-server /data/7004/redis.conf 
redis-server /data/7005/redis.conf

redis-trib.rb

如果redis设定了密码则修改此脚本

vim /usr/local/redis/src/redis-trib.rb

@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60)
改为
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60, :password => "你的密码")

将节点加入集群

redis-trib.rb create --replicas 1 127.0.0.1:7000 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

集群主节点状态

redis-cli -p 7000 cluster nodes | grep master

集群从节点状态

redis-cli -p 7000 cluster nodes | grep slave

集群状态查看

[root@bogon ~]# redis-cli -p 7000 cluster nodes |grep master
8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001 master - 0 1592272018900 2 connected 5461-10922
3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460   
042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002 master - 0 1592272017372 3 connected 10923-16383
700070017002
0-54605461-1092210923-16383
[root@bogon ~]# redis-cli -p 7000 cluster nodes |grep slave
ea1cb3d361091325c8c606c3f6ca9558a58e6062 127.0.0.1:7003 slave 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 0 1592272028581 4 connected
5376da53ee4e073feddb2a6e68446412f1fa1af8 127.0.0.1:7004 slave 8e8728fef3d0692b83cce98bbbaec607cc50861e 0 1592272028581 5 connected
2a3ec76fceed7f8f86a335097edaa0e116d5ef6f 127.0.0.1:7005 slave 042f7179f6fa957bba821022cfd8049aae913a5f 0 1592272028072 6 connected

增加新的节点

mkdir /data/7006
mkdir /data/7007

cat /data/7006/redis.conf

port 7006
daemonize yes
pidfile /data/7006/redis.pid
loglevel notice
logfile "/data/7006/redis.log"
dbfilename dump.rdb
dir /data/7006
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

cat /data/7007/redis.conf

port 7007
daemonize yes
pidfile /data/7007/redis.pid
loglevel notice
logfile "/data/7007/redis.log"
dbfilename dump.rdb
dir /data/7007
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
redis-server /data/7006/redis.conf 
redis-server /data/7007/redis.conf 

集群添加一个主节点

redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

说明:第一个参数是新节点的地址,第二个参数是任意一个已经存在的节点的IP和端口

数据分布状态

700070017002
0-54605461-1092210923-16383

16384/4 = 4096

转移slot(添加节点之后的重新分片)

redis-trib.rb reshard 127.0.0.1:7000

>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: ea1cb3d361091325c8c606c3f6ca9558a58e6062 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182
M: 8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7006
   slots: (0 slots) master
   0 additional replica(s)
S: 5376da53ee4e073feddb2a6e68446412f1fa1af8 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 8e8728fef3d0692b83cce98bbbaec607cc50861e
S: 2a3ec76fceed7f8f86a335097edaa0e116d5ef6f 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 042f7179f6fa957bba821022cfd8049aae913a5f
M: 042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002
   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.

# 每个节点最终分配多少个槽(计算方法:16384/master数量  这里具体是 16384/4 = 4096)
How many slots do you want to move (from 1 to 16384)? 4096

# 接受节点的ID
What is the receiving node ID? caa2a269eb7d2e356f4a708acc2f62bfc57b6952

# 哪些节点需要导出
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:all

重新分片之后的结果:

节点slot
70001365-5460
70016827-10922
700212288-16383
70060-1364 5461-6826 10923-12287

1365 1366 1365 (1365+1366+1365= 4096)

集群添加一个从节点

给127.0.0.1:7006 添加一个从节点(127.0.0.1:7007)

redis-cli -p 7000 cluster nodes |grep master
8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001 master - 0 1592279321660 2 connected 6827-10922
caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7006 master - 0 1592279321157 7 connected 0-1364 5461-6826 10923-12287
3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000 myself,master - 0 0 1 connected 1365-5460
042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002 master - 0 1592279320654 3 connected 12288-16383
redis-trib.rb add-node --slave --master-id caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7007 127.0.0.1:7000

说明:redis-trib.rb add-node --slave --master-id MASTERID 新节点的地址和端口 任意一个已经存在的master节点的IP和端口

集群删除节点

集群删除master节点

删除主节点之前先重新分片

redis-trib.rb reshard 127.0.0.1:7000

>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 5bffe2ad35291ae151097aae7467d4a6462c0149 127.0.0.1:7007
   slots: (0 slots) slave
   replicates caa2a269eb7d2e356f4a708acc2f62bfc57b6952
S: ea1cb3d361091325c8c606c3f6ca9558a58e6062 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182
M: 8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7006
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: 5376da53ee4e073feddb2a6e68446412f1fa1af8 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 8e8728fef3d0692b83cce98bbbaec607cc50861e
S: 2a3ec76fceed7f8f86a335097edaa0e116d5ef6f 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 042f7179f6fa957bba821022cfd8049aae913a5f
M: 042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002
   slots:12288-16383 (4096 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.

# 需要迁移多少个槽
How many slots do you want to move (from 1 to 16384)? 1365

# 接受节点ID是多少
What is the receiving node ID? 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182

# 哪些节点需要导出
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:caa2a269eb7d2e356f4a708acc2f62bfc57b6952
Source node #2:done

重复以上操作(详细如下),直到分配完毕!!!

redis-trib.rb reshard 127.0.0.1:7000

>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5bffe2ad35291ae151097aae7467d4a6462c0149 127.0.0.1:7007
   slots: (0 slots) slave
   replicates caa2a269eb7d2e356f4a708acc2f62bfc57b6952
S: ea1cb3d361091325c8c606c3f6ca9558a58e6062 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182
M: 8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7006
   slots:5461-6826,10923-12287 (2731 slots) master
   1 additional replica(s)
S: 5376da53ee4e073feddb2a6e68446412f1fa1af8 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 8e8728fef3d0692b83cce98bbbaec607cc50861e
S: 2a3ec76fceed7f8f86a335097edaa0e116d5ef6f 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 042f7179f6fa957bba821022cfd8049aae913a5f
M: 042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002
   slots:12288-16383 (4096 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.

# 需要迁移多少个槽
How many slots do you want to move (from 1 to 16384)? 1366

# 接受节点ID是多少
What is the receiving node ID? 8e8728fef3d0692b83cce98bbbaec607cc50861e

# 哪些节点需要导出
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:caa2a269eb7d2e356f4a708acc2f62bfc57b6952
Source node #2:done
redis-trib.rb reshard 127.0.0.1:7000

>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5bffe2ad35291ae151097aae7467d4a6462c0149 127.0.0.1:7007
   slots: (0 slots) slave
   replicates caa2a269eb7d2e356f4a708acc2f62bfc57b6952
S: ea1cb3d361091325c8c606c3f6ca9558a58e6062 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 3dcb5942fbc0c7d28a904f60c6ba5d663fcc2182
M: 8e8728fef3d0692b83cce98bbbaec607cc50861e 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: caa2a269eb7d2e356f4a708acc2f62bfc57b6952 127.0.0.1:7006
   slots:10923-12287 (1365 slots) master
   1 additional replica(s)
S: 5376da53ee4e073feddb2a6e68446412f1fa1af8 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 8e8728fef3d0692b83cce98bbbaec607cc50861e
S: 2a3ec76fceed7f8f86a335097edaa0e116d5ef6f 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 042f7179f6fa957bba821022cfd8049aae913a5f
M: 042f7179f6fa957bba821022cfd8049aae913a5f 127.0.0.1:7002
   slots:12288-16383 (4096 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.

# 需要迁移多少个槽
How many slots do you want to move (from 1 to 16384)? 1365

# 接受节点ID是多少
What is the receiving node ID? 042f7179f6fa957bba821022cfd8049aae913a5f

# 哪些节点需要导出
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:caa2a269eb7d2e356f4a708acc2f62bfc57b6952
Source node #2:done

重新分片后删除master节点

redis-trib.rb del-node 127.0.0.1:7000 caa2a269eb7d2e356f4a708acc2f62bfc57b6952

说明:第一个参数是任意一个master节点的地址, 第二个参数是你想要移除的节点地址

删除slave节点(直接删除)

redis-trib.rb del-node 127.0.0.1:7000 5bffe2ad35291ae151097aae7467d4a6462c0149
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值