ninth work

Redis主从同步过程

Redis主从同步可以让从服务器从主服务器备份数据,主从复制分为全量同步和增量同步,⾸次同步是全量同步,⽽且从服务器还可以有从服务器,redis 的主从同步是⾮阻塞的
1)从服务器连接主服务器,发送SYNC命令(2.8版本之前是PSYNC);
2) 主服务器接收到SYNC命名后,会fork⼀个⼦进程在后台执⾏bgsave命令,将Redis内存数据⽣成RDB快照⽂件并使⽤缓冲区记录此后执⾏的所有写命令;
3) 主服务器BGSAVE执⾏完后,向所有从服务器发送快照⽂件,并在发送期间继续记录被执⾏的写命令;
4) 从服务器收到快照⽂件后丢弃所有旧数据,载⼊收到的快照;
5) 主服务器快照发送完毕后开始向从服务器发送缓冲区中的写命令;
6) 从服务器完成对快照的载⼊,开始接收命令请求,并执⾏来⾃主服务器缓冲区的写命令;
7) 后期同步会先发送⾃⼰slave_repl_offset位置,只同步新增加的数据,不再全量同步。

基于配置文件实现Redis的主从模式

vim /apps/redis/etc/redis.conf
 replicaof 10.0.0.160 6379
# master ip及端口
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
 masterauth q1w2e3r4ys@123
#master 密码

systemctl restart redis

redis主从配置常⻅问题汇总

即配置的master密码不对,导致验证不通过⽽⽆法建⽴主从同步关系。
Redis版本不⼀致,不同的redis 版本之间存在兼容性问题,因此各master和slave之间必须保持版本⼀致。
在开启了安全模式情况下,没有设置bind地址或者密码被防⽕墙、安全组等限制访问
主从模式不能开启cluster 模式

基于Redis sentinel实现Redis 服务的高可用

配置redis一主两从(哨兵)

配置10.0.0.161 10.0.0.162为redis从服务器;10.0.0.160为redis主服务器

#编辑从服务器redis.conf
vim /apps/redis/etc/redis.conf
 replicaof 10.0.0.160 6379
# master ip及端口
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.

masterauth q1w2e3r4ys@123
#master 密码

systemctl restart redis.service

#查看主服务器redis状态
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.161,port=6379,state=online,offset=2757,lag=0
slave1:ip=10.0.0.162,port=6379,state=online,offset=2757,lag=1
master_failover_state:no-failover
master_replid:436b98ee8efdfb2a2d3eecfbbf148d5e2ce16110
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2757
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2757

#查看从服务器redis状态
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.160
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_read_repl_offset:2365
slave_repl_offset:2365
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:436b98ee8efdfb2a2d3eecfbbf148d5e2ce16110
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2365
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2351

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.160
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:2813
slave_repl_offset:2813
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:436b98ee8efdfb2a2d3eecfbbf148d5e2ce16110
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2813
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:2730
repl_backlog_histlen:84

3个服务器创建哨兵配置文件

#哨兵配置文件所在位置;源码目录sentinel.conf
root@ubuntu:~/redis-7.2.0# ll /root/redis-7.2.0/sentinel.conf 
-rw-rw-r-- 1 root root 14700 Aug 15 09:38 /root/redis-7.2.0/sentinel.conf
#创建哨兵工作目录
mkdir -p /apps/sentinel
#拷贝配置文件
cp /root/redis-7.2.0/sentinel.conf /apps/sentinel/
#编辑配置文件
vim /apps/sentinel/sentinel.conf
daemonize yes
pidfile /apps/sentinel/redis-sentinel.pid
logfile "/apps/sentinel/redis-sentinel.log"
dir /apps/sentinel  
sentinel monitor mymaster 10.0.0.160 6379 2  #redis主服务器的地址和端口;2是一共3个服务器,大于一半就是2;5个就是3
sentinel auth-pass mymaster q1w2e3r4ys@123  #redis主服务器redis密码

启动哨兵

#启动各个服务器的sentinel进程;三个服务器启动进程要快一点。
redis-sentinel /apps/sentinel/sentinel.conf

查看哨兵状态

#进入哨兵,查看状态
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.160 -p 26379
10.0.0.160:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
#与哨兵相关的信息,尤其是最后⼀⾏,涉及到master IP是多少、有⼏个slave、有⼏个sentinels、必须是符合全部服务器数量的
master0:name=mymaster,status=ok,address=10.0.0.160:6379,slaves=2,sentinels=3

验证数据

#进入主服务器;查看,添加新key
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.160 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning 
10.0.0.160:6379> keys *
1) "rt"
2) "mm"
3) "lian"
4) "name"
10.0.0.160:6379> set ces ces
OK
10.0.0.160:6379> keys *
1) "rt"
2) "lian"
3) "mm"
4) "name"
5) "ces"
10.0.0.160:6379> get ces
"ces"

#进入从服务器验证key
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.161:6379> keys *
1) "lian"
2) "ces"
3) "mm"
4) "name"
5) "rt"
10.0.0.161:6379> get ces
"ces"

root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.162 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.162:6379> keys *
1) "lian"
2) "name"
3) "ces"
4) "mm"
5) "rt"
10.0.0.162:6379> get ces
"ces"

测试master故障转移
注意:当发生故障转移时,哨兵回去修改redis.conf和sentinel.conf 文件

#关闭master redis服务
#查看哨兵日志
tail -f -n 200 /apps/sentinel/redis-sentinel.log
31201:X 12 Sep 2023 07:47:56.789 # +sdown master mymaster 10.0.0.160 6379 #哨兵主观认为10.0.0.160主服务器down了
31201:X 12 Sep 2023 07:47:56.962 * Sentinel new configuration saved on disk
31201:X 12 Sep 2023 07:47:56.963 # +new-epoch 1 #开启新纪元
31201:X 12 Sep 2023 07:47:56.967 * Sentinel new configuration saved on disk 修改配置文件
31201:X 12 Sep 2023 07:47:56.967 # +vote-for-leader 7730428b3f5168a7776b8446a32d601a27e00135 1
31201:X 12 Sep 2023 07:47:57.634 # +config-update-from sentinel 7730428b3f5168a7776b8446a32d601a27e00135 10.0.0.162 26379 @ mymaster 10.0.0.160 6379
31201:X 12 Sep 2023 07:47:57.634 # +switch-master mymaster 10.0.0.160 6379 10.0.0.162 6379 #10.0.0.162替换为master
31201:X 12 Sep 2023 07:47:57.635 * +slave slave 10.0.0.161:6379 10.0.0.161 6379 @ mymaster 10.0.0.162 6379
31201:X 12 Sep 2023 07:47:57.635 * +slave slave 10.0.0.160:6379 10.0.0.160 6379 @ mymaster 10.0.0.162 6379
31201:X 12 Sep 2023 07:47:57.639 * Sentinel new configuration saved on disk
31201:X 12 Sep 2023 07:48:27.695 # +sdown slave 10.0.0.160:6379 10.0.0.160 6379 @ mymaster 10.0.0.162 6379

#查看主状态
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.162 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.162:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.161,port=6379,state=online,offset=427540,lag=1
master_failover_state:no-failover
master_replid:36b28ce119cd65421da2f1c32db61a2e85dc838f
master_replid2:250725b94d090d872139954e4ccfa4898a58696b
master_repl_offset:427540
second_repl_offset:324069
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:427526

#查看从状态
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.161:6379> info replication
# Replication
role:slave
master_host:10.0.0.162
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:437330
slave_repl_offset:437330
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:36b28ce119cd65421da2f1c32db61a2e85dc838f
master_replid2:250725b94d090d872139954e4ccfa4898a58696b
master_repl_offset:437330
second_repl_offset:324069
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:437316

#验证数据同步
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.162 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.162:6379> keys * 
1) "lian"
2) "name"
3) "ces"
4) "mm"
5) "rt"
10.0.0.162:6379> set ooo pppp
OK
10.0.0.162:6379> get ooo
"pppp"

root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.161:6379> keys * 
1) "lian"
2) "ces"
3) "ooo"
4) "mm"
5) "name"
6) "rt"
10.0.0.161:6379> get ooo
"pppp"

验证恢复故障服务器

#编辑redis.conf
vim /apps/redis/etc/redis.conf
replicaof 10.0.0.162 6379
masterauth q1w2e3r4ys@123
#启动
systemctl start redis.service

#查看主服务器状态
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.162 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.162:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.161,port=6379,state=online,offset=544507,lag=1
slave1:ip=10.0.0.160,port=6379,state=online,offset=544507,lag=0
master_failover_state:no-failover
master_replid:36b28ce119cd65421da2f1c32db61a2e85dc838f
master_replid2:250725b94d090d872139954e4ccfa4898a58696b
master_repl_offset:544642
second_repl_offset:324069
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:544628

#查看从服务器状态
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.160 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.160:6379> info replication
# Replication
role:slave
master_host:10.0.0.162
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:552123
slave_repl_offset:552123
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:36b28ce119cd65421da2f1c32db61a2e85dc838f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:552123
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:537274
repl_backlog_histlen:14850

root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning

10.0.0.161:6379> info replication
# Replication
role:slave
master_host:10.0.0.162
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:559590
slave_repl_offset:559590
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:36b28ce119cd65421da2f1c32db61a2e85dc838f
master_replid2:250725b94d090d872139954e4ccfa4898a58696b
master_repl_offset:559590
second_repl_offset:324069
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:559576

#验证数据
#主
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.162 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.162:6379> keys *
1) "lian"
2) "name"
3) "ces"
4) "ooo"
5) "mm"
6) "rt"
10.0.0.162:6379> set tian xia
OK
10.0.0.162:6379> get tian
"xia"

#从
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.160 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.160:6379> keys * 
1) "lian"
2) "ces"
3) "mm"
4) "name"
5) "tian"
6) "rt"
7) "ooo"
10.0.0.160:6379> get tian
"xia"

#从
root@ubuntu:~/redis-7.2.0# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123 --no-auth-warning
10.0.0.161:6379> keys *
1) "lian"
2) "ces"
3) "ooo"
4) "mm"
5) "name"
6) "rt"
7) "tian"
10.0.0.161:6379> get tian
"xia"

Redis sentinel的实现机制

哨兵(Sentinel) 是⼀个分布式系统,可以在⼀个Redis 环境中运⾏多个哨兵(sentinel) 进程,这些进程使⽤流⾔协议(gossip protocols)来接收关于Master主服务器是否下线的信息,并使⽤投票协议(Agreement Protocols)来决定是否执⾏⾃动故障迁移,以及选择哪个Slave作为新的Master。每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)、Master、Slave定时发送消息,以确认对⽅是否”活”着,如果发现对⽅在指定配置时间(可配置的)内未得到回应,则暂时认为对⽅已掉线,也就是所谓的”主观认为宕机” ,主观是每个成员都具有的独⾃的⽽且可能相同也可能不同的意识,英⽂名称:Subjective Down,简称SDOWN。有主观宕机,肯定就有客观宕机。当“哨兵群”中的多数Sentinel进程在对Master主服务器做出 SDOWN 的判断,并且通过SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判断,这种⽅式就是“客观宕机”,客观是不依赖于某种意识⽽已经实际存在的⼀切事物,英⽂名称是:Objectively Down, 简称 ODOWN。通过⼀定的vote算法,从剩下的slave从服务器节点中,选⼀台提升为Master服务器节点,然后⾃动修改相关配置,并开启故障转移(failover)

实现Redis Cluster高可用集群,并验证基于python向Redis Cluster写入数据

集群环境准备

⽣产环境:
⽣产环境建议直接6台服务器:
10.0.0.160
10.0.0.161
10.0.0.162
10.0.0.163
10.0.0.164
10.0.0.165
10.0.0.166 #预留服务器演示更换服务器
10.0.0.167 #预留服务器演示更换服务器

redis版本3和redis版本4集群创建

cp /root/redis-4.0.4/src/redis-trib.rb /usr/bin/
yum install ruby  rubygems  -y
[root@s1 src]# gem install redis
Fetching: redis-4.1.2.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.3.0.
#解决ruby版本较低问题:
[root@s1 src]# yum remove ruby rubygems  -y
[root@s1 src]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
[root@s1 src]# tar xf ruby-2.5.5.tar.gz
[root@s1 src]# cd ruby-2.5.5
[root@s1 ruby-2.5.5]# ./configure
[root@s1 ruby-2.5.5]# make -j 2
[root@s1 ruby-2.5.5]# make install
[root@s1 ruby-2.5.5]# gem  install redis #https://rubygems.org/gems/redis, # gem install -l redis-3.3.0.gem

#创建集群
redis-trib.rb create --replicas 1 10.0.0.160 6379 10.0.0.161 6379 10.0.0.162 6379 10.0.0.163 6379 10.0.0.164 6379 10.0.0.165 6379

redis版本5及以上创建集群

直接使⽤redis-cli创建和管理redis cluster集群
如果有之前的操作导致Redis 集群创建报错,则执⾏清空数据和集群命令:
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> cluster reset
OK

redis-cli --cluster命令使用简介

root@ubuntu:~# redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN  #创建集群并执⾏节点数
                 --cluster-replicas <arg>  #指定master的副本数量;如果6个服务器, --cluster-replicas 1就表示3个master3个从
  check          <host:port> or <host> <port> - separated by either colon or space #任何一个节点ip及端口
                 --cluster-search-multiple-owners  #检查是否有槽位同时被分配给了多个节点
  info           <host:port> or <host> <port> - separated by either colon or space #查看集群主机信息
  fix            <host:port> or <host> <port> - separated by either colon or space #修复集群;创建集群过程中失败了,
  需要重新创建,这时候就需要fix。一般不使用这个命令
                 --cluster-search-multiple-owners #修复槽位被同时分配过多个节点的问题
                 --cluster-fix-with-unreachable-masters #修复不可达的主机
  reshard        <host:port> or <host> <port> - separated by either colon or space #⼿动热迁移集群指定主机的slots
  数据到新的⽬的 主机(⽤于特定主机维护、下线、更换主机);一般用交互式的方式,不用下面参数
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      <host:port> or <host> <port> - separated by either colon or space#如果各master的slot槽位差异很⼤
  (No rebalancing needed! All nodes are within the 2.00%threshold),可以使⽤rebalance实现⾃动平衡集群中各主机的slot数量;
  一般用交互式的方式,不用下面参数
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port #添加节点到集群
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id   #从集群删除主机
  call           host:port command arg arg .. arg #在集群上的所有节点上执⾏命令
                 --cluster-only-masters #在集群上的所有master上执⾏命令
                 --cluster-only-replicas #在集群上的所有slave上执⾏命令
  set-timeout    host:port milliseconds #设置节点的超时时间
  import         host:port   #将外部redis服务器的数据导⼊到当前集群
                 --cluster-from <arg>
                 --cluster-from-user <arg>
                 --cluster-from-pass <arg>
                 --cluster-from-askpass
                 --cluster-copy
                 --cluster-replace
  backup         host:port backup_directory #备份集群数据
  help           

For check, fix, reshard, del-node, set-timeout, info, rebalance, call, import, backup you can specify the host and port of any working node in the cluster.
#check, fix, reshard, del-node, set-timeout, info, rebalance, call, import, backup,可在任意⼀台节点执⾏
Cluster Manager Options:
  --cluster-yes  Automatic yes to cluster commands prompts #交互式的时候⾃动应答 yes;一般不加

创建集群

#编辑各个节点配置文件
vim /apps/redis/etc/redis.conf
masterauth q1w2e3r4ys@123
cluster-enabled yes


cluster-config-file nodes-6379.conf
#重启各个节点redis
systemctl restart redis.service
#查看各个节点集群功能是否开启
127.0.0.1:6379> info cluster
# Cluster
cluster_enabled:1

#创建集群;在其中任何一个节点执行
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster create 10.0.0.160:6379 10.0.0.161:6379 10.0.0.162:6379 10.0.0.163:6379 10.0.0.164:6379 10.0.0.165:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460 #⾃动为master分配槽位
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.164:6379 to 10.0.0.160:6379
#⾃动将10.0.0.164:6379添加为10.0.0.160:6379的副本
Adding replica 10.0.0.165:6379 to 10.0.0.161:6379
Adding replica 10.0.0.163:6379 to 10.0.0.162:6379
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
#带M的为master,并随机产⽣节点ID
   slots:[0-5460] (5461 slots) master  #当前master的槽位起始和结束位
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   slots:[10923-16383] (5461 slots) master
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
#带S的为slave,并随机产⽣节点ID
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d #slave没有分配槽位,以master为准
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
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  #向节点发送CLUSTER MEET消息以加⼊集群
Waiting for the cluster to join #开始等待节点集群,

>>> Performing Cluster Check (using node 10.0.0.160:6379) #执⾏集群状态检查,进⾏集群验证
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379 #节点信息
   slots:[0-5460] (5461 slots) master  #已分配的槽位
   1 additional replica(s)  #已分配的slave
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   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个槽位都已经覆盖完成

验证集群状态

root@ubuntu:~# redis-cli -h 10.0.0.160 -p 6379 -a q1w2e3r4ys@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.164,port=6379,state=online,offset=336,lag=0
master_failover_state:no-failover
master_replid:2f209c2dbab0aa14a151775182d23ada328d7fbb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336

#集群信息
10.0.0.160:6379> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1948
cluster_stats_messages_pong_sent:2036
cluster_stats_messages_sent:3984
cluster_stats_messages_ping_received:2031
cluster_stats_messages_pong_received:1948
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:3984
total_cluster_links_buffer_limit_exceeded:0

#集群节点信息
10.0.0.160:6379> CLUSTER NODES
a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694578761341 2 connected 5461-10922
9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694578760334 1 connected
4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379@16379 slave 38765927cee3046ba82331c6e2d016a80e899b8d 0 1694578759000 3 connected
661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379@16379 slave a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 0 1694578761000 2 connected
38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694578759325 3 connected 10923-16383
0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694578759000 1 connected 0-5460

#检查节点状态
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster check 10.0.0.160 6379 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379 (0b22b562...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.161:6379 (a494315d...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)

#集群信息
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster info 10.0.0.160 6379 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379 (0b22b562...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.161:6379 (a494315d...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

验证数据

root@ubuntu:~# redis-cli -h 10.0.0.160 -p 6379 -a q1w2e3r4ys@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379> set key1 asd   ##经过算法计算,当前key的槽位需要写⼊指定的槽位、⽽槽位有属于特定的master
(error) MOVED 9189 10.0.0.161:6379 #如果槽位不在当前node所以⽆法写⼊
10.0.0.160:6379> 

root@ubuntu:~# redis-cli -h 10.0.0.161 -p 6379 -a q1w2e3r4ys@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.161:6379> set key1 adsds   #写入成功;写入的数据的槽位在10.0.0.161上,所有这个key只在10.0.0.161和他的slave上有
OK

python 客户端写⼊数据

https://redis.io/docs/clients/

#安装python3-pip
apt install python3-pip -y
root@ubuntu:~# pip3 --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
root@ubuntu:~# mkdir ~/.pip
#将源指向国内
root@ubuntu:~# vim ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
#安装redis集群管理模块
root@ubuntu:~# pip3 install redis-py-cluster

#编辑Python脚本;在Python里面文件名不能和模块名一样
root@ubuntu:~# vim redis-cluster-client.py
#!/usr/bin/env python
#coding:utf-8
#Author:Zhang ShiJie
#python 2.7/3.8
#pip install redis-py-cluster
import sys
from rediscluster import RedisCluster
def init_redis():
    startup_nodes = [
        {'host': '10.0.0.160', 'port': 6379},
        {'host': '10.0.0.161', 'port': 6379},
        {'host': '10.0.0.162', 'port': 6379},
        {'host': '10.0.0.163', 'port': 6379},
        {'host': '10.0.0.164', 'port': 6379},
        {'host': '10.0.0.165', 'port': 6379},
    ]
    try:
       conn = RedisCluster(startup_nodes=startup_nodes,
 # 有密码要加上密码
                           decode_responses=True, password='q1w2e3r4ys@123')
       print('OK! 连接成功!!!!!1', conn)
       #conn.set("key-cluster","value-cluster")
       for i in range(100):
           conn.set("key%s" % i, "value%s" % i)
           # time.sleep(0.1)
           data = conn.get("key%s" % i)
           print(data)
   #return conn
    except Exception as e:
        print("connect error ", str(e))
        sys.exit(1)
init_redis()

#执行脚本写入数据
python3 redis-cluster-client.py

#验证key
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster info 10.0.0.160 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379 (0b22b562...) -> 32 keys | 5461 slots | 1 slaves.
10.0.0.161:6379 (a494315d...) -> 32 keys | 5462 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 36 keys | 5461 slots | 1 slaves.

#查看各个节点有多少个key
#--no-auth-warning不提示密码写出在命令行的警告
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster call 10.0.0.160:6379 dbsize
>>> Calling dbsize
10.0.0.160:6379: 32
10.0.0.161:6379: 32
10.0.0.164:6379: 32
10.0.0.163:6379: 36
10.0.0.165:6379: 32
10.0.0.162:6379: 36
#查看各个master节点有多少个key
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster call 10.0.0.160:6379 dbsize --cluster-only-masters
>>> Calling dbsize
10.0.0.160:6379: 32
10.0.0.161:6379: 32
10.0.0.162:6379: 36

Redis cluster横向扩容(添加服务器)

增加Redis node节点,需要与之前的Redis node版本相同、配置⼀致,然后分别启动两台Redis node(⼀主⼀从)。
主要过程:
添加master
添加slave
重新分配槽位
验证

添加master和slave

#将10.0.0.166:6379添加到已有集群10.0.0.160中
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster add-node 10.0.0.166:6379 10.0.0.160:6379 
>>> Adding node 10.0.0.166:6379 to cluster 10.0.0.160:6379
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   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.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 10.0.0.166:6379 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 10.0.0.166:6379
>>> Send CLUSTER MEET to node 10.0.0.166:6379 to make it join the cluster.
[OK] New node added correctly.

#查看当前集群状态;添加进来默认为master,没有槽位和slave
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster info 10.0.0.160:6379
10.0.0.160:6379 (0b22b562...) -> 6669 keys | 5461 slots | 1 slaves.
10.0.0.166:6379 (c06fdea8...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.161:6379 (a494315d...) -> 6663 keys | 5462 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 6668 keys | 5461 slots | 1 slaves.
[OK] 20000 keys in 4 masters.
1.22 keys per slot on average.

#查看节点信息,获取10.0.0.166的master ID
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning cluster nodes
c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379@16379 master - 0 1694587579000 0 connected  #166的master ID
a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694587579918 2 connected 5461-10922
9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694587578505 1 connected
4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379@16379 slave 38765927cee3046ba82331c6e2d016a80e899b8d 0 1694587578000 3 connected
661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379@16379 slave a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 0 1694587578000 2 connected
38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694587578909 3 connected 10923-16383
0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694587576000 1 connected 0-5460


#添加10.0.0.167:6379并且设置它为10.0.0.166:6379的slave
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster add-node 10.0.0.167:6379 10.0.0.160:6379 --cluster-slave --cluster-master-id c06fdea8399863f96f096a69f6001d8ad6d33274
>>> Adding node 10.0.0.167:6379 to cluster 10.0.0.160:6379
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379
   slots: (0 slots) master
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   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.
>>> Send CLUSTER MEET to node 10.0.0.167:6379 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 10.0.0.166:6379.
[OK] New node added correctly.


#验证是否设置成功
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning cluster nodes
c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379@16379 master - 0 1694588008000 0 connected
a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694588007000 2 connected 5461-10922
9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694588009544 1 connected
4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379@16379 slave 38765927cee3046ba82331c6e2d016a80e899b8d 0 1694588009000 3 connected
661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379@16379 slave a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 0 1694588009000 2 connected
38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694588008000 3 connected 10923-16383
#已经设置成功
8d5e232a4dce5bedd9b0129026ad9c5a05e06adb 10.0.0.167:6379@16379 slave c06fdea8399863f96f096a69f6001d8ad6d33274 0 1694588010554 0 connected
0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694588006000 1 connected 0-5460

重新分配槽位

#交互式的方式重新分配槽位
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster reshard 10.0.0.160:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379
   slots: (0 slots) master
   1 additional replica(s)
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 8d5e232a4dce5bedd9b0129026ad9c5a05e06adb 10.0.0.167:6379
   slots: (0 slots) slave
   replicates c06fdea8399863f96f096a69f6001d8ad6d33274
[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)? 4096 ##要分配多少个槽位;4个master,16384除以4
What is the receiving node ID? c06fdea8399863f96f096a69f6001d8ad6d33274 #目的服务器id
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
all #从哪些源服务器分配,即将哪些源主机的槽位分配给⽬的主机,all是⾃动在所有的redisnode选择划分,
#如果是从redis cluster删除主机可以使⽤此⽅式将主机上的槽位全部移动到别的redis主机
Ready to move 4096 slots.
  Source nodes:
    M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379
       slots: (0 slots) master
       1 additional replica(s)


#查看
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster info 10.0.0.160:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.160:6379 (0b22b562...) -> 5008 keys | 4096 slots | 1 slaves.
10.0.0.166:6379 (c06fdea8...) -> 4998 keys | 4096 slots | 1 slaves.
10.0.0.161:6379 (a494315d...) -> 4996 keys | 4096 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 4998 keys | 4096 slots | 1 slaves.
[OK] 20000 keys in 4 masters.
1.22 keys per slot on average.

root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster call 10.0.0.160:6379 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Calling dbsize
10.0.0.160:6379: 5008
10.0.0.166:6379: 4998
10.0.0.161:6379: 4996
10.0.0.164:6379: 5008
10.0.0.163:6379: 4998
10.0.0.165:6379: 4996
10.0.0.162:6379: 4998
10.0.0.167:6379: 4998
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --cluster call 10.0.0.160:6379 dbsize --cluster-only-masters
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Calling dbsize
10.0.0.160:6379: 5008
10.0.0.166:6379: 4998
10.0.0.161:6379: 4996
10.0.0.162:6379: 4998

Redis 集群缩容(删除服务器)

都是去掉一主一从

迁移master 的槽位到其他master

将指定 主机的数据迁移之后再从集群删除

#交互式的方式,迁移master 的槽位到其他master
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster reshard 10.0.0.160:6379
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379 #目的master
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379 #源master
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 8d5e232a4dce5bedd9b0129026ad9c5a05e06adb 10.0.0.167:6379
   slots: (0 slots) slave
   replicates c06fdea8399863f96f096a69f6001d8ad6d33274
[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)? 4096
What is the receiving node ID? 0b22b562e7ca9b0d35056a6a4850766ab6315569 #目的master
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: c06fdea8399863f96f096a69f6001d8ad6d33274  #源master
Source node #2: done #结束

#查看,master的槽位清理后会变成了slave
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster check 10.0.0.160:6379
10.0.0.160:6379 (0b22b562...) -> 10006 keys | 8192 slots | 3 slaves.
10.0.0.161:6379 (a494315d...) -> 4996 keys | 4096 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 4998 keys | 4096 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.160:6379)
M: 0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   3 additional replica(s)
S: c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
M: a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
S: 4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379
   slots: (0 slots) slave
   replicates 38765927cee3046ba82331c6e2d016a80e899b8d
S: 661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379
   slots: (0 slots) slave
   replicates a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604
M: 38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 8d5e232a4dce5bedd9b0129026ad9c5a05e06adb 10.0.0.167:6379
   slots: (0 slots) slave
   replicates 0b22b562e7ca9b0d35056a6a4850766ab6315569
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

删除节点

#查看集群节点信息
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 cluster nodes
#需要删除的节点
c06fdea8399863f96f096a69f6001d8ad6d33274 10.0.0.166:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694589973352 9 connected
a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694589975370 2 connected 6827-10922
9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694589976000 9 connected
4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379@16379 slave 38765927cee3046ba82331c6e2d016a80e899b8d 0 1694589976379 3 connected
661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379@16379 slave a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 0 1694589974360 2 connected
38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694589974000 3 connected 12288-16383
#需要删除的节点
8d5e232a4dce5bedd9b0129026ad9c5a05e06adb 10.0.0.167:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694589973000 9 connected
0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694589973000 9 connected 0-6826 10923-12287


#删除10.0.0.166
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster del-node 10.0.0.160:6379 c06fdea8399863f96f096a69f6001d8ad6d33274
>>> Removing node c06fdea8399863f96f096a69f6001d8ad6d33274 from cluster 10.0.0.160:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

#删除10.0.0.107
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster del-node 10.0.0.160:6379 8d5e232a4dce5bedd9b0129026ad9c5a05e06adb
>>> Removing node 8d5e232a4dce5bedd9b0129026ad9c5a05e06adb from cluster 10.0.0.160:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

#验证
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 cluster nodes
a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694590164000 2 connected 6827-10922
9bda23ffbce8f2e5a0df0f841f83e0c6d6a9e7a5 10.0.0.164:6379@16379 slave 0b22b562e7ca9b0d35056a6a4850766ab6315569 0 1694590164091 9 connected
4d62c6408cb2d7d43c38e48383a6362d5a03fb78 10.0.0.163:6379@16379 slave 38765927cee3046ba82331c6e2d016a80e899b8d 0 1694590163000 3 connected
661bf755198a6681ba3c3fc37051db347efd5809 10.0.0.165:6379@16379 slave a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 0 1694590165100 2 connected
38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694590166107 3 connected 12288-16383
0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694590164000 9 connected 0-6826 10923-12287

root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster info 10.0.0.160:6379
10.0.0.160:6379 (0b22b562...) -> 10006 keys | 8192 slots | 1 slaves. #槽位太多
10.0.0.161:6379 (a494315d...) -> 4996 keys | 4096 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 4998 keys | 4096 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.

重新平衡槽位

#平衡槽位
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster rebalance 10.0.0.160:6379
>>> Performing Cluster Check (using node 10.0.0.160:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3.00
Moving 1366 slots from 10.0.0.160:6379 to 10.0.0.161:6379
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 10.0.0.160:6379 to 10.0.0.162:6379
#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

#验证
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 --cluster info 10.0.0.160:6379
10.0.0.160:6379 (0b22b562...) -> 6672 keys | 5461 slots | 1 slaves.
10.0.0.161:6379 (a494315d...) -> 6659 keys | 5462 slots | 1 slaves.
10.0.0.162:6379 (38765927...) -> 6669 keys | 5461 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.

基于redis-shake实现不同Redis环境的数据迁移

使用RedisShake
https://github.com/tair-opensource/RedisShake
功能简介:
rdb_reader: ⽀持读取解析本地rdb⽂件,向⽬的redis/redis cluster恢复数据
sync_reader: ⽀持对源redis和⽬的redis进⾏数据在线远程同步,需要⽀持psync(redis 2.8开始⽀持),psync具备了数据全量重同步和增量重同步模式;可以用于在运行中的集群
scan_reader:⽀持基于dump从源主机导出key,然后在⽬的主机使⽤RESTORE导⼊key,通过SCAN命令遍历源端数据库中的所有 Key,不推荐。

准备一个单机redis带数据

#编写数据写入脚本
vim redis-client.sh
#!/bin/bash
#Author: ZhangJie
NUM=`seq 1 10000`
for i in ${NUM};do
 redis-cli -h 127.0.0.1 --no-auth-warning -a q1w2e3r4ys@123 set key-${i} value-${i}
 echo "key-${i} value-${i} 写完成"
done
echo "万个key写⼊到Redis完成"

#执行脚本
bash redis-client.sh
#查看是否写入成功
root@ubuntu:~# redis-cli --no-auth-warning -a q1w2e3r4ys@123 dbsize 
(integer) 10000

部署redis-shake

https://github.com/tair-opensource/RedisShake #github地址
https://tair-opensource.github.io/RedisShake/zh/reader/scan_reader.html #redis-shake中文文档
https://github.com/tair-opensource/RedisShake/blob/v4/shake.toml #配置文件示例

#可以直接下载二进制文件来使用
root@ubuntu:~# mkdir /apps/redis-shake
root@ubuntu:~# cd /apps/redis-shake
wget https://github.com/tair-opensource/RedisShake/releases/download/v4.0.0/redis-shake-linux-amd64.tar.gz
tar xvf redis-shake-linux-amd64.tar.gz 

#编辑配置文件
vim shake.toml
function = ""


[sync_reader]  #源主机
cluster = false  #是集群就写true
address = "10.0.0.167:6379" #源主机地址及端口
username = ""              # keep empty if not using ACL
password = "q1w2e3r4ys@123"              # keep empty if no authentication is required #密码
tls = false

# [scan_reader]
# cluster = false
# address = "127.0.0.1:6379"
# username = ""              # keep empty if not using ACL
# password = ""              # keep empty if no authentication is required
# tls = false

# [rdb_reader]
# filepath = "/tmp/dump.rdb"

[redis_writer] #目的主机
cluster = true #是集群就写true
address = "10.0.0.160:6379" #是集群就写其中一个节点的地址及端口就行
username = ""              # keep empty if not using ACL
password = "q1w2e3r4ys@123"              # keep empty if no authentication is required
tls = false


[advanced]
dir = "data"
ncpu = 0        # runtime.GOMAXPROCS, 0 means use runtime.NumCPU() cpu cores
pprof_port = 0  # pprof port, 0 means disable
status_port = 0 # status port, 0 means disable

# log
log_file = "shake.log"
log_level = "info"     # debug, info or warn
log_interval = 5       # in seconds

# redis-shake gets key and value from rdb file, and uses RESTORE command to
# create the key in target redis. Redis RESTORE will return a "Target key name
# is busy" error when key already exists. You can use this configuration item
# to change the default behavior of restore:
# panic:   redis-shake will stop when meet "Target key name is busy" error.
# rewrite: redis-shake will replace the key with new value.
# ignore:  redis-shake will skip restore the key when meet "Target key name is busy" error.
rdb_restore_command_behavior = "panic" # panic, rewrite or skip

# redis-shake uses pipeline to improve sending performance.
# This item limits the maximum number of commands in a pipeline.
pipeline_count_limit = 1024

# Client query buffers accumulate new commands. They are limited to a fixed
# amount by default. This amount is normally 1gb.
target_redis_client_max_querybuf_len = 1024_000_000

# In the Redis protocol, bulk requests, that are, elements representing single
# strings, are normally limited to 512 mb.
target_redis_proto_max_bulk_len = 512_000_000

# If the source is Elasticache or MemoryDB, you can set this item.
aws_psync = "" # example: aws_psync = "10.0.0.1:6379@nmfu2sl5osync,10.0.0.1:6379@xhma21xfkssync"

执行同步验证

root@ubuntu:/apps/redis-shake# /apps/redis-shake/redis-shake /apps/redis-shake/shake.toml 
2023-09-13 06:18:27 INF load config from file: /apps/redis-shake/shake.toml
2023-09-13 06:18:27 INF log_level: [info], log_file: [/apps/redis-shake/data/shake.log]
2023-09-13 06:18:27 INF changed work dir to [/apps/redis-shake/data]
2023-09-13 06:18:27 INF GOMAXPROCS defaults to the value of runtime.NumCPU [2]
2023-09-13 06:18:27 INF not set pprof port
2023-09-13 06:18:27 INF no function script
2023-09-13 06:18:27 INF create SyncStandaloneReader: 10.0.0.167:6379
2023-09-13 06:18:27 INF redisClusterWriter load cluster nodes. line=a494315dfff618c9f7bfebcc3e6cfd3a1ddf3604 10.0.0.161:6379@16379 master - 0 1694585905829 2 connected 5461-10922
2023-09-13 06:18:27 INF redisClusterWriter load cluster nodes. line=38765927cee3046ba82331c6e2d016a80e899b8d 10.0.0.162:6379@16379 master - 0 1694585904000 3 connected 10923-16383
2023-09-13 06:18:27 INF redisClusterWriter load cluster nodes. line=0b22b562e7ca9b0d35056a6a4850766ab6315569 10.0.0.160:6379@16379 myself,master - 0 1694585905000 1 connected 0-5460
2023-09-13 06:18:27 INF redisClusterWriter connected to redis cluster successful. addresses=[10.0.0.161:6379 10.0.0.162:6379 10.0.0.160:6379]
2023-09-13 06:18:27 INF create RedisClusterWriter: 10.0.0.160:6379
2023-09-13 06:18:27 INF not set status port
2023-09-13 06:18:27 INF start syncing...  #同步完成;后面的就相当于增量同步了
2023-09-13 06:18:32 INF read_count=[10000], read_ops=[0.00], write_count=[10000], write_ops=[0.00], syncing aof, diff=[10448]
2023-09-13 06:18:37 INF read_count=[10000], read_ops=[0.00], write_count=[10000], write_ops=[0.00], syncing aof, diff=[0]

#验证同步
root@ubuntu:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster call  10.0.0.160:6379 dbsize
>>> Calling dbsize
10.0.0.160:6379: 3330
10.0.0.161:6379: 3333
10.0.0.164:6379: 3330
10.0.0.163:6379: 3337
10.0.0.165:6379: 3333
10.0.0.162:6379: 3337

基于mysql和Redis,实现PHP页面的数据缓存效果

部署msyql

[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# ll
total 177552
-rw-r--r--. 1 root root   3381269 Aug 15 17:41 redis-7.2.0.tar.gz
-rw-r--r--. 1 root root 178429485 Jul  4 10:36 runtime-docker24.0.2-containerd1.6.21-binary-install.tar.gz

#部署docker
tar xvf runtime-docker24.0.2-containerd1.6.21-binary-install.tar.gz
bash runtime-install.sh docker

#创建mysql数据目录
mkdir -p /data/mysql
#部署mysql容器
docker run -it -d -p 3306:3306 -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD='123456' --name mysql-container registry.cn-hangzhou.aliyuncs.com/zhangshijie/mysql:5.7.36
#安装mysql客户端
yum install mysql -y

#登录mysql
[root@localhost src]# mysql -u root -h 10.0.0.130 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

#初始化数据
mysql> create database test; #创建数据库
Query OK, 1 row affected (0.00 sec)

mysql> use test; #切换数据库
Database changed
mysql> create table produtcs (product_id BIGINT PRIMARY KEY AUTO_INCREMENT,product_name VARCHAR(50),price DOUBLE) Engine = InnoDB;
Query OK, 0 rows affected (0.00 sec)  #创建表


mysql> INSERT INTO produtcs(product_name, price) VALUES ('Virtual Private Servers','5.00'); #插入数据
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO produtcs(product_name, price) VALUES ('Managed Databases', '15.00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO produtcs(product_name, price) VALUES ('Block Storage', '10.00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO produtcs(product_name, price) VALUES ('Managed Kubernetes','60.00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO produtcs(product_name, price) VALUES ('Load Balancer', '10.00');
Query OK, 1 row affected (0.00 sec)

mysql> select * from produtcs; #验证表数据
+------------+-------------------------+-------+
| product_id | product_name            | price |
+------------+-------------------------+-------+
|          1 | Virtual Private Servers |     5 |
|          2 | Managed Databases       |    15 |
|          3 | Block Storage           |    10 |
|          4 | Managed Kubernetes      |    60 |
|          5 | Load Balancer           |    10 |
+------------+-------------------------+-------+
5 rows in set (0.00 sec)

安装redis

 2088 -g 2088 redis -s /sbin/nologin
#给目录赋权
chown redis.redis -R /apps/redis/

#编辑redis配置文件,修改如下
vim /apps/redis/etc/redis.conf 
bind 0.0.0.0
daemonize yes
pidfile /apps/redis/run/redis_6379.pid
logfile "/apps/redis/logs/redis_6379.log"
save 5 1
stop-writes-on-bgsave-error no
dir /apps/redis/data
requirepass 123456

#启动redis
redis-server /apps/redis/etc/redis.conf

web服务器准备

#查看系统
root@ubuntu2004:~# lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:    20.04
Codename:    focal

#安装应用
apt update &&  apt install apache2 php-fpm libapache2-mod-php php7.4-mysql php7.4-redis -y

#编辑PHP测试页面
cd /var/www/html
vim index.php
<?php
phpinfo();
?>

访问php测试页面
在这里插入图片描述

测试redis数据访问

root@ubuntu2004:/var/www/html# pwd
/var/www/html

#编辑测试页面
root@ubuntu2004:/var/www/html# cat redis.php 
<?php
$redis = new Redis();
$redis->connect('10.0.0.130', 6379); //连接Redis
$redis->auth('123456'); //密码验证
$redis->select(0);//选择数据库,int类型,一般是0-16,选一个
$redis->set( "testKey" , "Hello Redis"); //设置测试key
echo $redis->get("testKey");//输出value
echo $redis->get("key1");//输出value
?>

访问redis.php页面
在这里插入图片描述
查看redis里面数据;成功写入redis,并读取数据

[root@localhost redis]# redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys * 
1) "testKey"

127.0.0.1:6379> get testKey
"Hello Redis"

测试redis+mysql数据访问

#编辑redis+mysql的测试页面
root@ubuntu2004:/var/www/html# pwd
/var/www/html

root@ubuntu2004:/var/www/html# cat redis-mysql.php 
<?php

$redis = new Redis();
$redis->connect('10.0.0.130', 6379);
$redis->auth('123456');

$key = 'PRODUCTS';

if (!$redis->get($key)) {
    $source = '未命中缓存:数据来源MySQL Server';
    $database_name     = 'test';
    $database_user     = 'root';
    $database_password = '123456';
    $mysql_host        = '10.0.0.130';

    $pdo = new PDO('mysql:host=' . $mysql_host . '; dbname=' . $database_name, $database_user, $database_password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql  = "SELECT * FROM produtcs";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       $products[] = $row;
    }

    $redis->set($key, serialize($products));
    $redis->expire($key, 120);

} else {
     $source = '成功命中缓存:数据来源 Redis Server';
     $products = unserialize($redis->get($key));

}

echo $source . ': <br>';
print_r($products); 

第一次访问测试页面;显示数据来源mysql,因为redis里面没有数据
在这里插入图片描述
第二次访问,数据就是读取的redis里面缓存的mysql的数据了
在这里插入图片描述
查看redis中的数据

127.0.0.1:6379> keys * 
1) "PRODUCTS"
2) "testKey"
127.0.0.1:6379> get PRODUCTS #数据缓存成功
"a:5:{i:0;a:3:{s:10:\"product_id\";s:1:\"1\";s:12:\"product_name\";s:23:\"Virtual Private Servers\";s:5:\"price\";s:1:\"5\";}i:1;a:3:{s:10:\"product_id\";s:1:\"2\";s:12:\"product_name\";s:17:\"Managed Databases\";s:5:\"price\";s:2:\"15\";}i:2;a:3:{s:10:\"product_id\";s:1:\"3\";s:12:\"product_name\";s:13:\"Block Storage\";s:5:\"price\";s:2:\"10\";}i:3;a:3:{s:10:\"product_id\";s:1:\"4\";s:12:\"product_name\";s:18:\"Managed Kubernetes\";s:5:\"price\";s:2:\"60\";}i:4;a:3:{s:10:\"product_id\";s:1:\"5\";s:12:\"product_name\";s:13:\"Load Balancer\";s:5:\"price\";s:2:\"10\";}}"
127.0.0.1:6379> get PRODUCTS #数据缓存过期
(nil)
127.0.0.1:6379> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值