redis应用2:主从同步、哨兵模式详解、高可用集群部署、集群数据迁移、槽位分配、集群扩缩容

一.详细总结Redis 主从同步过程

同步过程首次全量同步,后面增量同步,同步过程属于非阻塞过程,不影响写入数![在这里插入图片描述](https://img-blog.csdnimg.cn/f039ecc41df34b369fa4454e304fff78.png#pic_center)
据,从服务器只能读不能写。
1、从服务器连接到主服务器,发送sync命令
2、主服务器接收到命令后,会fork一个子进程在后台进行bgsave,将redis内存数据生成RDB快照文件并使用缓存区记录此后所有的写命令。
3、主服务器bgsave后,向所有从服务器发送快照文件,并在发送期间记录被执行的写命令。
4、从服务器接收到RDB快照文件后,丢弃自己的旧数据,载入新的快照。
5、主服务器快照发送完成后开始向从服务器发送缓冲区的写命令。
6、从服务器完成对快照的载入,开始接收命令请求,并执行来自主服务器缓冲区的写命令。
7、后期同步会先发送slave_repl_offset位置,只同步新增的数据,不再全量同步。

在这里插入图片描述

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

ip地址作用端口
172.18.10.141master6379
172.18.10.142slave6379
root@redis-2:/apps/redis# vim /apps/redis/etc/redis.conf 
replicaof 172.18.10.141 6379
masterauth q1w2e3r4ys@123
root@redis-2:/apps/redis# systemctl restart redis
# 查看slave状态
172.18.10.142:6379> info Replication
# Replication
role:slave
master_host:172.18.10.141
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:126
slave_repl_offset:126
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e163ebdfaee1c26d73653dc9bc5ea7f56ad2111b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126

# 查看master状态
172.18.10.141:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.18.10.142,port=6379,state=online,offset=84,lag=0
master_failover_state:no-failover
master_replid:e163ebdfaee1c26d73653dc9bc5ea7f56ad2111b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84

# 验证数据同步 ,172.18.10.141 master写入数据
root@redis-1:~# cat redis-client.sh 
#!/bin/bash
#Author: ZhangJie
NUM=`seq 1 1000`
for i in ${NUM};do
  redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 set key-${i} value-${i}
  echo "key-${i} value-${i} 写入完成"
done
echo "十万个key写入到Redis完成"
root@redis-1:~# sh redis-client.sh 

# slave验证

在这里插入图片描述

# 常见问题汇总
1、配置密码不对
2、redis版本不一致
3、开启了安全模式,没有设置bind地址或者密码
4、被防火墙、安全组限制
5、主从模式不能开启cluster模式

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

# slave配置
root@redis-2:~# vim /apps/redis/etc/redis.conf 
replicaof 172.18.10.141 6379
masterauth q1w2e3r4ys@123
root@redis-3:~# vim /apps/redis/etc/redis.conf 
replicaof 172.18.10.141 6379
masterauth q1w2e3r4ys@123
root@redis-1:~# systemctl restart redis
root@redis-2:~# systemctl restart redis
root@redis-3:~# systemctl restart redis
# 查看集群状态
# redis-1 master
172.18.10.141:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.18.10.143,port=6379,state=online,offset=224,lag=0
slave1:ip=172.18.10.142,port=6379,state=online,offset=224,lag=1
master_failover_state:no-failover
master_replid:19c97f82aa0edc86d29153c5d9e7bd02c74ff413
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:224
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:1
repl_backlog_histlen:224
# redis-2 slave
172.18.10.142:6379> info Replication
# Replication
role:slave
master_host:172.18.10.141
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:434
slave_repl_offset:434
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:19c97f82aa0edc86d29153c5d9e7bd02c74ff413
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:434
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:29
repl_backlog_histlen:406
# redis-3 slave
172.18.10.143:6379> info Replication
# Replication
role:slave
master_host:172.18.10.141
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:518
slave_repl_offset:518
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:19c97f82aa0edc86d29153c5d9e7bd02c74ff413
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:518
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:504

# 启动哨兵进程
# 配置哨兵配置文件
root@redis-1:/apps/redis# vim /apps/redis/etc/sentinel.conf 
protected-mode no # 关闭安全模式
port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
loglevel notice
logfile "sentinel_26379.log"
dir /apps/redis/data/sentinel
sentinel monitor mymaster 172.18.10.141 6379 2
sentinel auth-pass mymaster q1w2e3r4ys@123
sentinel down-after-milliseconds mymaster 10000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
# 分发配置文件给另外两个节点
root@redis-1:/apps/redis# scp etc/sentinel.conf root@172.18.10.142:/apps/redis/etc/
root@redis-1:/apps/redis# scp etc/sentinel.conf root@172.18.10.143:/apps/redis/etc/

# 启动哨兵
root@redis-1:/apps/redis# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf 
root@redis-2:/apps/redis# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf 
root@redis-3:/apps/redis# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf 

# 查看哨兵是否正常
root@redis-1:/apps/redis# redis-cli -h 172.18.10.141 -p 26379 -a q1w2e3r4ys@123
172.18.10.141: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
master0:name=mymaster,status=ok,address=172.18.10.141:6379,slaves=2,sentinels=3

# 检查日志是否正常监控master,确保没有报错
root@redis-1:/apps/redis# tail -f data/sentinel/sentinel_26379.log 
2060:X 13 Sep 2023 22:11:37.065 # +sdown sentinel 45f6b6b7cb7a46bfc2e53ac1fcb3bf0d580a8bd2 172.18.10.143 26379 @ mymaster 172.18.10.141 6379
2161:X 13 Sep 2023 22:28:13.937 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2162:X 13 Sep 2023 22:28:13.937 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2162:X 13 Sep 2023 22:28:13.937 * Redis version=7.2.0, bits=64, commit=00000000, modified=0, pid=2162, just started
2162:X 13 Sep 2023 22:28:13.937 * Configuration loaded
2162:X 13 Sep 2023 22:28:13.938 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2162:X 13 Sep 2023 22:28:13.938 * monotonic clock: POSIX clock_gettime
2162:X 13 Sep 2023 22:28:13.938 * Running mode=sentinel, port=26379.
2162:X 13 Sep 2023 22:28:13.939 * Sentinel ID is 0922c2c5252dc12147ecdec5e77207c4f28a374e
2162:X 13 Sep 2023 22:28:13.939 # +monitor master mymaster 172.18.10.141 6379 quorum 2

# 查看此时的哨兵配置文件
root@redis-1:/apps/redis# grep -Ev "^#|^$" /apps/redis/etc/sentinel.conf 
protected-mode no
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
loglevel notice
logfile "sentinel_26379.log"
dir "/apps/redis/data/sentinel"
sentinel monitor mymaster 172.18.10.141 6379 2
sentinel auth-pass mymaster q1w2e3r4ys@123
sentinel down-after-milliseconds mymaster 10000
acllog-max-len 128
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel announce-hostnames no
latency-tracking-info-percentiles 50 99 99.9
user default on nopass sanitize-payload ~* &* +@all
sentinel myid 0922c2c5252dc12147ecdec5e77207c4f28a374e
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
sentinel known-replica mymaster 172.18.10.142 6379
sentinel known-replica mymaster 172.18.10.143 6379
sentinel known-sentinel mymaster 172.18.10.142 26379 584afba5be893e3cf5818a3bd03fcb7489951763
sentinel known-sentinel mymaster 172.18.10.143 26379 45f6b6b7cb7a46bfc2e53ac1fcb3bf0d580a8bd2

# Redis Master故障转移,停止master
root@redis-1:/apps/redis# systemctl stop redis
# 检查另外两台slave的状态 ,此时172.18.10.142状态变为master
root@redis-2:/apps/redis# redis-cli -h 172.18.10.142 -a q1w2e3r4ys@123
172.18.10.142:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.18.10.143,port=6379,state=online,offset=291272,lag=0
master_failover_state:no-failover
master_replid:974652fb44c01cf36c97a307554d3630665ba13f
master_replid2:19c97f82aa0edc86d29153c5d9e7bd02c74ff413
master_repl_offset:291272
second_repl_offset:279181
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:29
repl_backlog_histlen:291244

root@redis-3:/apps/redis# redis-cli -h 172.18.10.143 -a q1w2e3r4ys@123
172.18.10.143:6379> info Replication
# Replication
role:slave
master_host:172.18.10.142
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:322065
slave_repl_offset:322065
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:974652fb44c01cf36c97a307554d3630665ba13f
master_replid2:19c97f82aa0edc86d29153c5d9e7bd02c74ff413
master_repl_offset:322065
second_repl_offset:279181
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:15
repl_backlog_histlen:322051

# 修复之前的 master,配置文件指向现在新的master
root@redis-1:/apps/redis# vim /apps/redis/etc/redis.conf 
replicaof 172.18.10.142 6379
masterauth q1w2e3r4ys@123

root@redis-1:/apps/redis# systemctl restart redis
# 查看状态,重启后重新加入主从
root@redis-1:/apps/redis# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123
172.18.10.141:6379> info Replication
# Replication
role:slave
master_host:172.18.10.142
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:412789
slave_repl_offset:412789
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:974652fb44c01cf36c97a307554d3630665ba13f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:412789
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:536870912
repl_backlog_first_byte_offset:408086
repl_backlog_histlen:4704

四.总结Redis sentinel的实现机制

	redis 哨兵是在主从加工的基础上实现的,为了实现master和slave进行自动切换和故障转移,实现无缝切换,还可以动态扩展redis服务器。在主从的基础上,配置哨兵配置文件,监控master的状态,可以在环境中运行多个哨兵,哨兵是一个分布式系统,使用流言协议(gossip protocols)获取master是否下线的消息,使用太安排协议来决定是否进行故障迁移,以及确定新的master。
	哨兵进程会想其他哨兵、master、slave定时发送消息,以确认对方的状态,如果未得到回应,暂时认为对方掉线,既主管认为的宕机,当其他哨兵群中的多数认为master下线是,则认为是客观宕机,此时会从剩下的slave从服务器中选择一台提升为master主服务器节点,然后自动修改相关配置,并开启故障转移。

总结:
	1、redis sentinel哨兵主要用来监控master状态
    2、当master不可用时实现自动故障转移
    3、通知app客户端连接新的master
    4、维护配置文件

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

# 打开集群节点配置
root@redis-2:~# vim /apps/redis/etc/redis.conf
cluster-enabled yes
cluster-config-file nodes-6379.conf
# 创建集群
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster create 172.18.10.141:6379 172.18.10.142:6379 172.18.10.143:6379 172.18.10.144:6379 172.18.10.145:6379 172.18.10.146:6379 --cluster-replicas 1
# 验证集群状态
172.18.10.141: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:1225
cluster_stats_messages_pong_sent:1199
cluster_stats_messages_sent:2424
cluster_stats_messages_ping_received:1199
cluster_stats_messages_pong_received:1225
cluster_stats_messages_received:2424
total_cluster_links_buffer_limit_exceeded:0
172.18.10.141:6379> cluster nodes
4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 0 1694794322234 2 connected 5461-10922
1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379@16379 slave 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 0 1694794323244 1 connected
fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379@16379 slave 4b0d5f291f6a93c7e0b751e4321bb3738e695009 0 1694794320217 2 connected
fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 0 1694794324253 3 connected 10923-16383
1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 0 1694794323000 1 connected 0-5460
bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379@16379 slave fb451eed765a8af9efec665776991b24b7321f22 0 1694794321225 3 connected

# 使用python写入数据
root@redis-7:~# apt install python3-pip
root@redis-7:~# mkdir ~/.pip
root@redis-7:~# vim ~/.pip/pip.conf
root@redis-7:~# pip3 install redis-py-cluster
root@redis-7:~# cat 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': '172.18.10.141', 'port': 6379},
        {'host': '172.18.10.142', 'port': 6379},
        {'host': '172.18.10.143', 'port': 6379},
        {'host': '172.18.10.144', 'port': 6379},
        {'host': '172.18.10.145', 'port': 6379},
        {'host': '172.18.10.146', 'port': 6379},
    ]
    try:
        conn = RedisCluster(startup_nodes=startup_nodes,decode_responses=True, password='q1w2e3r4ys@123')
    print('OK! 连接成功', 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()
root@redis-7:~# python3 redis-clister-client.py
OK! 连接成功 RedisCluster<172.18.10.141:6379, 172.18.10.142:6379,
172.18.10.143:6379, 172.18.10.144:6379, 172.18.10.145:6379, 172.18.10.146:6379>
value0
value1

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

# 安装redis-shake
root@redis-7:~# cd /usr/local/src/
root@redis-7:/usr/local/src# wget https://github.com/tair-opensource/RedisShake/releases/download/v4.0.0/redis-shake-linux-amd64.tar.gz
root@redis-7:/usr/local/src# tar xvf redis-shake-linux-amd64.tar.gz
./redis-shake
./shake.toml
root@redis-7:/usr/local/src# vim shake.toml
root@redis-7:~# cat /usr/local/src/shake.toml 
function = ""

# 读取数据的配置 源主机 单节点
[sync_reader]
cluster = false
address = "172.18.10.147: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"

# 配置写数据的主机,cluster模式 目的主机
[redis_writer]
cluster = true
address = "172.18.10.141: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@redis-7:~# cat redis-client.sh 
#!/bin/bash
#Author: ZhangJie
NUM=`seq 1 5000`
for i in ${NUM};do
  redis-cli -h 172.18.10.147 -a q1w2e3r4ys@123 set key-${i} value-${i}
  echo "key-${i} value-${i} 写入完成"
done
echo "十万个key写入到Redis完成"

root@redis-7:~# sh redis-client.sh 

# 执行数据同步,此过程会持续同步
root@redis-7:/usr/local/src# ./redis-shake shake.toml 
root@redis-7:/usr/local/src# ./redis-shake shake.toml 
2023-09-16 00:53:57 INF load config from file: shake.toml
2023-09-16 00:53:57 INF log_level: [info], log_file: [/usr/local/src/data/shake.log]
2023-09-16 00:53:57 INF changed work dir to [/usr/local/src/data]
2023-09-16 00:53:57 INF GOMAXPROCS defaults to the value of runtime.NumCPU [2]
2023-09-16 00:53:57 INF not set pprof port
2023-09-16 00:53:57 INF no function script
2023-09-16 00:53:57 INF create SyncStandaloneReader: 172.18.10.147:6379
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 0 1694796835000 10 connected 0-1365 6827-10922
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 0 1694796836000 11 connected 1366-2730 12288-16383
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 0 1694796835000 9 connected 2731-6826 10923-12287
2023-09-16 00:53:57 INF redisClusterWriter connected to redis cluster successful. addresses=[172.18.10.142:6379 172.18.10.143:6379 172.18.10.141:6379]
2023-09-16 00:53:57 INF create RedisClusterWriter: 172.18.10.141:6379
2023-09-16 00:53:57 INF not set status port
2023-09-16 00:53:57 INF start syncing...
2023-09-16 00:54:02 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[5000]
2023-09-16 00:54:07 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[0]
2023-09-16 00:54:17 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[0]

# 目的端redis验证
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.141 -p 6379 --no-auth-warning dbsize
(integer) 1653
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.142 -p 6379 --no-auth-warning dbsize
(integer) 1673
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.143 -p 6379 --no-auth-warning dbsize
(integer) 1675

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

# 数据库准备
root@redis-7:~/soft/docker# tar -xf runtime-docker24.0.2-containerd1.6.21-binary-install.tar.gz 
root@redis-7:~/soft/docker# bash runtime-install.sh containerd
root@redis-7:~/soft/docker# mkdir -p /data/mysql
root@redis-7:~/soft/docker# nerdctl run -d -p 3306:3306 --name mysql-container-test -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD="mypass@123" registry.cn-hangzhou.aliyuncs.com/zhangshijie/mysql:5.7.36
root@redis-7:~/soft/docker# apt install mysql-client-core-8.0
root@redis-7:~/soft/docker# mysql -u root -h 172.31.4.3 -pmypass@123
root@redis-7:~/soft/docker# mysql -u root -h 172.18.10.147 -pmypass@123
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 8
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_store;
Query OK, 1 row affected (0.00 sec)

mysql> use test_store;
Database changed
mysql> create table products(product_id BIGINT PRIMARY KEY AUTO_INCREMENT,product_name VARCHAR(50),price DOUBLE) Engine = InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO products(product_name, price) VALUES ('Virtual Private Servers','5.00');
Query OK, 1 row affected (0.01 sec)

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

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

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

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

mysql> select * from products;
+------------+-------------------------+-------+
| 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)

mysql> quit;
Bye

# web服务器准备
root@redis-7:~/soft/docker# apt-get install software-properties-common
root@redis-7:~/soft/docker# add-apt-repository ppa:ondrej/php
root@redis-7:~/soft/docker# apt-get update
root@redis-7:~/soft/docker# apt install apache2 php-fpm libapache2-mod-php php7.4-mysql php7.4-redis

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

$redis = new Redis();
$redis->connect('172.18.10.147', 6379);
$redis->auth('q1w2e3r4ys@123');
$key = 'PRODUCTS';
if (!$redis->get($key)) {
$source = '未命中缓存,数据来源MySQL Server';
$database_name = 'test_store';
$database_user = 'root';
$database_password = 'mypass@123';
$mysql_host = '172.17.10.147';
$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 products";
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$products[] = $row;
}
$redis->set($key, serialize($products));
$redis->expire($key, 60);
} else {
$source = '成功命中缓存,数据来源 Redis Server';
$products = unserialize($redis->get($key));
}
echo $source . ': <br>';
print_r($products);
# 页面访问测试 http://172.18.10.147/redis-mysql.php

八.对Redis cluster进行集群扩容、并为新的master分配槽位

# 添加节点到集群汇总,默认为master
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster add-node 172.18.10.147:6379 172.18.10.141:6379

# 查看集群节点信息
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster info 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1550
cluster_stats_messages_pong_sent:1522
cluster_stats_messages_sent:3072
cluster_stats_messages_ping_received:1521
cluster_stats_messages_pong_received:1550
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:3072
total_cluster_links_buffer_limit_exceeded:0
# 节点信息
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 0 1694794654518 2 connected 5461-10922
1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379@16379 slave 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 0 1694794653511 1 connected
f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379@16379 master - 0 1694794651490 0 connected
fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379@16379 slave 4b0d5f291f6a93c7e0b751e4321bb3738e695009 0 1694794650476 2 connected
fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 0 1694794651000 3 connected 10923-16383
1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 0 1694794652000 1 connected 0-5460
bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379@16379 slave fb451eed765a8af9efec665776991b24b7321f22 0 1694794653000 3 connected

# 添加节点172.18.10.148:6379 并设置为172.18.10.147:6379的slave
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster add-node 172.18.10.148:6379 172.18.10.141:6379 --cluster-slave --cluster-master-id f2f96b952bf55548e5523589d8041ec6122f8905

# 查看集群节点
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 0 1694795120332 2 connected 5461-10922
1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379@16379 slave 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 0 1694795120000 1 connected
f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379@16379 master - 0 1694795122352 0 connected
607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379@16379 slave f2f96b952bf55548e5523589d8041ec6122f8905 0 1694795121343 0 connected
fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379@16379 slave 4b0d5f291f6a93c7e0b751e4321bb3738e695009 0 1694795120000 2 connected
fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 0 1694795121000 3 connected 10923-16383
1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 0 1694795119000 1 connected 0-5460
bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379@16379 slave fb451eed765a8af9efec665776991b24b7321f22 0 1694795120000 3 connected

# 重新分配槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster reshard 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.18.10.141:6379)
M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379
   slots: (0 slots) slave
   replicates 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
   slots: (0 slots) master
   1 additional replica(s)
S: 607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379
   slots: (0 slots) slave
   replicates f2f96b952bf55548e5523589d8041ec6122f8905
S: fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379
   slots: (0 slots) slave
   replicates 4b0d5f291f6a93c7e0b751e4321bb3738e695009
M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379
   slots: (0 slots) slave
   replicates fb451eed765a8af9efec665776991b24b7321f22
[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? f2f96b952bf55548e5523589d8041ec6122f8905  接收槽位的节点
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是自动再所有的redis node上划分,如果redie cluster删除主机可以使用此方式将主机上的槽位全部移动到别的redis主机上
Ready to move 4096 slots.
  Source nodes:
    M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
       slots: (0 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 5461 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5462 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5463 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5464 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5465 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5466 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5467 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    Moving slot 5468 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
    ...
    Moving slot 12287 from fb451eed765a8af9efec665776991b24b7321f22
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 172.18.10.142:6379 to 172.18.10.147:6379: 
Moving slot 5462 from 172.18.10.142:6379 to 172.18.10.147:6379: 
Moving slot 5463 from 172.18.10.142:6379 to 172.18.10.147:6379: 
Moving slot 5464 from 172.18.10.142:6379 to 172.18.10.147:6379: 
Moving slot 5465 from 172.18.10.142:6379 to 172.18.10.147:6379: 
Moving slot 5466 from 172.18.10.142:6379 to 172.18.10.147:6379: 
...

# 验证集群槽位状态
root@redis-1:~# redis-cli -a q1w2e3r4ys@123  --cluster info  172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) -> 0 keys | 4096 slots | 1 slaves.
172.18.10.142:6379 (4b0d5f29...) -> 0 keys | 4096 slots | 1 slaves.
172.18.10.147:6379 (f2f96b95...) -> 1 keys | 4096 slots | 1 slaves.
172.18.10.143:6379 (fb451eed...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.


九.对Redis cluster进行集群缩容、并提前迁移走之前的数据

# 迁移槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster reshard 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.18.10.141:6379)
M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379
   slots: (0 slots) slave
   replicates 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379
   slots: (0 slots) slave
   replicates f2f96b952bf55548e5523589d8041ec6122f8905
S: fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379
   slots: (0 slots) slave
   replicates 4b0d5f291f6a93c7e0b751e4321bb3738e695009
M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379
   slots: (0 slots) slave
   replicates fb451eed765a8af9efec665776991b24b7321f22
[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
# 接收的节点,先迁移到172.18.10.141上
What is the receiving node ID? 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
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: f2f96b952bf55548e5523589d8041ec6122f8905
Source node #2: done
# done 完成

# 检查槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster info 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) -> 1 keys | 8192 slots | 3 slaves.
172.18.10.142:6379 (4b0d5f29...) -> 0 keys | 4096 slots | 1 slaves.
172.18.10.143:6379 (fb451eed...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.

# 从集群中删除指定节点
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster del-node 172.18.10.141:6379 f2f96b952bf55548e5523589d8041ec6122f8905
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster del-node 172.18.10.141:6379 607a1c0cf8e469be07df402aac9eb31f525b348f

# 重新平衡槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster rebalance 172.18.10.141:6379
>>> Performing Cluster Check (using node 172.18.10.141: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 172.18.10.141:6379 to 172.18.10.142:6379
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 172.18.10.141:6379 to 172.18.10.143:6379
#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

# 查看槽位分配
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster info 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) -> 0 keys | 5461 slots | 1 slaves.
172.18.10.142:6379 (4b0d5f29...) -> 1 keys | 5462 slots | 1 slaves.
172.18.10.143:6379 (fb451eed...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值