redis sentinel &集群+haproxy+keepalived

1.redis部署

为什么使用nosql?

取连接数
[root@openvpn-server bin]# redis-cli -h 101.201.116.250 info clients |awk -F: ‘/connected_clients/{print $NF}’
3

debug populate 10000 #写入10000条数据

yum 安装dump.rdb 路径
/var/lib/redis/dump.rdb

redis-cli -a 123456 bgsave && pstree -p|grep redis
bgsave会开子进程备份

redis-cli -a 123456 info Persistence
rdb_bgsave_in_progress:1 #1代表在备份

save “” 表示禁用自动保存RDB

save 900 1
save 300 10
save 60 10000

rdb和aof
aof占用空间大

开启aof会导致数据丢失,因为重启服务会优先读取aof,在命令行修改127.0.0.1:6379> config set appendonly yes ,再改redis.conf配置文件

aof rewrite

BGREWRITEAOF 清理重复数据

aof

redis service文件解释

在Redis服务器的systemd服务文件中,以下是一些选项的解释:

- RuntimeDirectory=root:指定运行时目录的位置为根目录(/)。
- RuntimeDirectoryMode=2755:设置运行时目录的访问权限为2755。这表示目录的所有者和所属组具有读、写和执行权限,其他用户具有读和执行权限。
- UMask=007:设置进程的默认文件权限掩码为007。它确定当进程创建新文件时,默认权限的设置。
- PrivateTmp=yes:将Tmp设为私有模式。这意味着进程将在私有的临时目录中运行,其他进程无法访问该目录。
- LimitNOFILE=65535:设置进程可打开的文件描述符的最大数量为65535。
- PrivateDevices=yes:将设备设为私有模式。这意味着进程将在私有的/dev目录中运行,其他进程无法访问该目录。
- ProtectHome=yes:限制进程对用户家目录的访问权限,防止进程访问其他用户的个人文件。
- ReadOnlyDirectories=/:将根目录(/)设为只读模式。这意味着进程无法对根目录进行写操作,只能进行读取操作。

以下是对于在Redis服务器的systemd服务文件中出现的选项的解释:

- NoNewPrivileges=true:禁止进程在运行时获取新的特权。这意味着一旦进程以指定的权限启动,它将无法提升权限。

- CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE:限制进程可以获取的能力集。在此示例中,进程被限制为只能执行CAP_SETGID、CAP_SETUID和CAP_SYS_RESOURCE这些特定能力。这有助于降低系统面临的安全风险。

- MemoryDenyWriteExecute=true:限制进程对内存的访问权限。此选项将禁止进程写入可执行内存区域,并进一步增强了安全性。

- ProtectKernelModules=true:防止进程加载或卸载内核模块。这可防止恶意加载恶意内核模块,从而保护系统安全。

- ProtectKernelTunables=true:限制进程对内核可调整参数的访问。这有助于防止进程对系统进行不必要或不安全的更改。

- ProtectControlGroups=true:保护控制组层次结构免受进程的更改。控制组用于组织和管理进程,通过保护控制组层次结构,可防止进程滥用或更改控制组。

- RestrictRealtime=true:限制进程对实时资源的访问。实时资源是系统中有特定时间要求的关键任务所需的资源。限制对实时资源的访问可防止进程对系统性能产生不必要的影响。

- RestrictNamespaces=true:限制进程使用的命名空间。命名空间允许进程隔离资源,限制进程对其他命名空间的访问可增加系统的安全性。

- RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX:限制进程可以访问的网络地址族。在此示例中,进程仅能访问IPv4、IPv6和UNIX域套接字地址族。

这些选项是用于提高Redis服务器的安全性和隔离性,并确保进程对系统资源的访问受到限制。这些保护措施有助于提高系统的安全性并减少潜在的攻击面。

如果您有任何其他问题,请随时问我。

2.redis的数据类型和消息队列:

字符串 list set 有序集合 hash

SET hongbao 100 EX 10 #有效期10s

127.0.0.1:6379> ttl hongbao 查看有效期
(integer) 5

同时设置多个值
127.0.0.1:6379> mset a 1 b 2
OK
127.0.0.1:6379> mget a b

  1. “1”
  2. “2”

列表
left right
lpush 从左往右推 rpush 重右往左
127.0.0.1:6379> lpush stduents a
127.0.0.1:6379> lpush stduents b

127.0.0.1:6379> LINDEX stduents -1 #查看最后一个数据
rpush stduents c

LINDEX stduents -1
lpop 弹出数据
127.0.0.1:6379> LPOP stduents
“0”
127.0.0.1:6379> LINDEX stduents 0
“b”
del stduents 清除列表

集合

127.0.0.1:6379> sadd my_friend a b c
(integer) 3

127.0.0.1:6379> SMEMBERS my_friend

  1. “b”
  2. “c”
  3. “a”

有序集合
127.0.0.1:6379> zadd movie 90 naza 50 ll 100 yuguyao
127.0.0.1:6379> ZRANGE movie 0 -1 #从低到高排序

  1. “ll”
  2. “naza”
  3. “yuguyao”

127.0.0.1:6379> ZRANGE movie 0 -1 withscores

  1. “ll”
  2. “50”
  3. “naza”
  4. “90”
  5. “yuguyao”
  6. “100”

127.0.0.1:6379> ZREVRANGE movie 0 -1 withscores

  1. “yuguyao”
  2. “100”
  3. “naza”
  4. “90”
  5. “ll”
  6. “50”

127.0.0.1:6379> hset 9527 name luo age 22 sex man home shenze
(integer) 4
127.0.0.1:6379> HGETALL 9527

  1. “name”
  2. “luo”
  3. “age”
  4. “22”
  5. “sex”
  6. “man”
  7. “home”
  8. “shenze”

应用场景:
排行榜
共同好友
点赞

为什么快:基于内存,单线程 C开发

有序集合:排行榜
集合

消息队列的功能:

解耦
生产消费者:1对1
发布 订阅 :1对多

127.0.0.1:6379> lpush goods car
(integer) 1
127.0.0.1:6379> lpush goods book
(integer) 2
127.0.0.1:6379> lpush goods perfume
(integer) 3
127.0.0.1:6379> LRANGE goods 0 -1

  1. “perfume”
  2. “book”
  3. "car

127.0.0.1:6379> rpop goods # 消费
“car”
127.0.0.1:6379> LRANGE goods 0 -1

  1. “perfume”
  2. “book”

发布订

subscribe DM222 #先订阅
publish DM222 666 发送消息
在这里插入图片描述

3. redis集群和高可用

3.1 主从

myql主从:
二进制日志放到独立空间中,和数据库放在一起都丢了
log-bin=/path
grant replicatin slave on . to repluser@‘xxx’ by
从节点
change master to
三个线程

主:dump
slave: IO,接受日志,放到中继日志()
sql ,读取中继日志写入

redis主从:
主:
requirepass 123456
从:
replicaof 192.168.1.121 6379
masterauth 123456

从节点开启会进行同步数据,之前数据会丢失,确认数据是否需要

安装redis模块
pip3 install redis

单主节点故障,提升一个节点为新主,

master_link_status:down #说明连不了主节点

REPLICAOF no one #取消从节点
从节点换新主 REPLICAOF 192.168.1.122 6379

主从复制优化
redis 主从配置必须一样,不一样会导致复制失败
内存一样,密码一样,软件版本一样

哨兵模式

apt install redis-sentinel -y

bind 0.0.0.0
port 26379
daemonize yes
pidfile “/var/run/sentinel/redis-sentinel.pid”
logfile “/var/log/redis/redis-sentinel.log”
dir “/var/lib/redis”

sentinel myid d6fca1380ff4beccdcec9cd2210f39a506c22c7b #此行自动生成,每个哨兵节点不一样

sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.1.122 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 18000

sentinel auth-pass mymaster 123456

在这里插入图片描述

日志解释

164722:X 22 Jul 2023 15:15:36.113 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=164722, just started
表示Redis Sentinel进程刚刚启动。这行日志还包含了Redis版本号、位数、提交版本、是否有修改以及进程ID。
164722:X 22 Jul 2023 15:15:36.113 # Configuration loaded
表示Redis Sentinel成功加载了配置文件。
164736:X 22 Jul 2023 15:15:36.118 * Running mode=sentinel, port=26379.
表示Sentinel正在以Sentinel模式运行,并监听26379端口
164736:X 22 Jul 2023 15:15:36.119 # Sentinel ID is f92a54b40e92f8888d1342e9887fc0d3a131373b
表示Sentinel的唯一标识符是f92a54b40e92f8888d1342e9887fc0d3a131373b
164736:X 22 Jul 2023 15:15:36.119 # +monitor master mymaster 192.168.1.121 6379 quorum 2
表示Sentinel开始监视名为mymaster的主节点。192.168.1.121 是主节点的IP地址。6379 是主节点的端口号。quorum 2 表示故障切换所需的投票数为2
164736:X 22 Jul 2023 15:17:06.461 * +reboot master mymaster 192.168.1.121 6379
+reboot 表示主节点已重启。mymaster 是主节点的名称。192.168.1.121 是主节点的IP地址。6379 是主节点的端口号。
164736:X 22 Jul 2023 15:17:26.596 * +reboot master mymaster 192.168.1.121 6379
这行日志与上一条类似,表示主节点再次重启。
164736:X 22 Jul 2023 15:18:27.496 # +sdown master mymaster 192.168.1.121 6379
+sdown 表示主节点被标记为主观下线。mymaster 是主节点的名称。192.168.1.121 是主节点的IP地址。6379 是主节点的端口号。
164736:X 22 Jul 2023 15:18:27.545 # +new-epoch 1
+new-epoch 表示进入了新的纪元(epoch)。1 是新纪元的编号。

164736:X 22 Jul 2023 15:18:27.545 # +vote-for-leader 8e9a327ce012eeda9b24bf67ada4e650254af1f8 1
+vote-for-leader 表示该Sentinel节点投票支持指定的领导者。
8e9a327ce012eeda9b24bf67ada4e650254af1f8 是领导者的Sentinel ID。
1 是该Sentinel节点所选举的新领导者的纪元。

164736:X 22 Jul 2023 15:18:27.552 # +odown master mymaster 192.168.1.121 6379 #quorum 3/2
+odown 表示主节点(master)被判定为宕机(down),mymaster 是主节点的名称。192.168.1.121 是主节点的IP地址。6379 是主节点的端口号。#quorum 3/2 表示需要至少3个Sentinel节点中的2个进行投票认定主节点宕机。

164736:X 22 Jul 2023 15:18:27.552 # Next failover delay: I will not start a failover before Sat Jul 22 15:19:04 2023
表示下一次故障切换(failover)的延迟时间。在此日志中,Sentinel决定在2023年7月22日15:19:04之前不启动故障切换。
164736:X 22 Jul 2023 15:18:28.344 # +config-update-from sentinel 8e9a327ce012eeda9b24bf67ada4e650254af1f8 192.168.1.123 26379 @ mymaster 192.168.1.121 6379
+config-update-from 表示Sentinel接收到了来自另一个Sentinel节点的新配置更新。
sentinel 8e9a327ce012eeda9b24bf67ada4e650254af1f8 192.168.1.123 26379 是发送配置更新的Sentinel节点的信息。mymaster 是更新的主节点名称。192.168.1.121 是原来的主节点地址。6379 是原来的主节点端口。
164736:X 22 Jul 2023 15:18:28.344 # +switch-master mymaster 192.168.1.121 6379 192.168.1.122 6379
+switch-master 表示发生了主节点切换。mymaster 是切换后的新主节点名称。192.168.1.121 是切换前的主节点地址。6379 是切换前的主节点端口。192.168.1.122 是切换后的新主节点地址。6379 是切换后的新主节点端口。


164736:X 22 Jul 2023 15:18:28.344 * +slave slave 192.168.1.123:6379 192.168.1.123 6379 @ mymaster 192.168.1.122 6379
+slave 表示有一个新的从节点加入。slave 192.168.1.123:6379 是新加入的从节点的IP地址和端口号。192.168.1.123 6379 是从节点连接的主节点的IP地址和端口号。
mymaster 192.168.1.122 6379 是主节点的名称以及主节点的IP地址和端口号。

164736:X 22 Jul 2023 15:18:28.345 * +slave slave 192.168.1.121:6379 192.168.1.121 6379 @ mymaster 192.168.1.122 6379
这行日志与上一行类似,表示另一个新的从节点加入。

164736:X 22 Jul 2023 15:18:31.382 # +sdown slave 192.168.1.121:6379 192.168.1.121 6379 @ mymaster 192.168.1.122 6379
+sdown 表示一个从节点被标记为主观下线。
slave 192.168.1.121:6379 是下线的从节点的IP地址和端口号。
192.168.1.121 6379 是从节点连接的主节点的IP地址和端口号。
mymaster 192.168.1.122 6379 是主节点的名称以及主节点的IP地址和端口号。

replica-priority 50

replica-priority是Redis配置选项之一,用于设置从节点(replica)的优先级。当一个从节点重新连接到主节点时,主节点会检查从节点的优先级。优先级较高的从节点将有更高的可能性被选为新的主节点,当主节点发生故障或下线时。
在配置文件中,你可以为每个从节点设置一个优先级。默认情况下,所有从节点的优先级均为 100,表示没有优先级差异。通过设置 replica-priority 选项,你可以为某个特定的从节点设置一个正整数值,该值越高,优先级越高。
例如,replica-priority 50 将为该从节点设置优先级为 50。
请注意,设置优先级仅在进行主从切换时起作用。当所有的从节点优先级相同或没有设置优先级时,Redis将选择一个最适合的从节点作为新的主节点。优先级设置可以在一些特殊情况下,如恢复故障后的主节点时提供额外的灵活性。

sentinel failover mymaster
手动让主节点下线

使用python连接sentinel集群

apt install python3 python3-redis

搭建haproxy代理sentinel

vi /etc/haproxy/haproxy.cfg 
listen redis-sentinel
        bind 0.0.0.0:2222
        mode tcp
        server sentinel01 192.168.1.121:26379  check
        server sentinel02 192.168.1.122:26379  check
        server sentinel03 192.168.1.123:26379  check

在这里插入图片描述

redis cluster集群

当数据量过大,512G内存服务器怎么存1G数据?
进行分片

redis分片原理:
1.redis会有多组分片构成,本片笔记环境是3组
2.redis cluster 使用固定个数的slot储存数据,一共16384个slot
3.每组分片分得1/3 slot个数,
4.基于CRC16(key)%16384公式会得到一个槽位号,
5.计算得出槽位值,找到相对应的分片的主节点,储存到相应槽位上
6. 如果客户端当时连接的节点不是将来要储存的分片节点,分片集群会将客户端连接切换至真正储存节点进行数据储存;

[root@redis-node1 ~]vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后 redis 进程会有cluster标识
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态数据文件,记录主从关系
及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整
个cluster不可用

创建cluster集群

redis-cli  -a 123456 --cluster create 192.168.1.121:6379  192.168.1.122:6379 192.168.1.123:6379 192.168.1.125:6379 192.168.1.126:6379 192.168.1.127:6379   --cluster-replicas 1 

python连接cluster插入数据

#!/usr/bin/env python3
from rediscluster  import RedisCluster

if __name__ == '__main__':

    startup_nodes = [
        {"host":"192.168.1.121", "port":6379},
        {"host":"192.168.1.122", "port": 6379},
        {"host":"192.168.1.123", "port":6379},
        {"host":"192.168.1.125", "port": 6379},
        {"host":"192.168.1.126", "port":6379},
        {"host":"192.168.1.127", "port":6379}]
    try:
        redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True)
    except Exception as e:
        print(e)

    for i in range(0, 10000):
        redis_conn.set('key'+str(i),'value'+str(i))
        print('key'+str(i)+':',redis_conn.get('key'+str(i)))

集群扩容

查看集群状态
redis-cli -a 123456 --cluster check 192.168.1.122:6379.

添加192.168.1.128:6379 192.168.1.122:6379 为集群任一节点

root@redis01:~#  redis-cli -a 123456 --cluster add-node  192.168.1.128:6379 192.168.1.122:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.1.128:6379 to cluster 192.168.1.122:6379
>>> Performing Cluster Check (using node 192.168.1.122:6379)
M: 657fb8ef5dde62561326617e9bd4e673317ce94b 192.168.1.122:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 88ed6770429d22392352ef613ce6081b98edcc50 192.168.1.123:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 902b9c0fcfcceefae1ad8a7a599845898990e041 192.168.1.125:6379
   slots: (0 slots) slave
   replicates 88ed6770429d22392352ef613ce6081b98edcc50
M: 2edd069021df65b153bf0cd33f10fddb3aeff936 192.168.1.121:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5606e145ebb7238966781890654c6a1c30c1e71e 192.168.1.127:6379
   slots: (0 slots) slave
   replicates 657fb8ef5dde62561326617e9bd4e673317ce94b
S: f84d545049358b19d0e9f30271d44d9bcbc28f23 192.168.1.126:6379
   slots: (0 slots) slave
   replicates 2edd069021df65b153bf0cd33f10fddb3aeff936
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.128:6379 to make it join the cluster.
[OK] New node added correctly.

查看

root@redis01:~# redis-cli -a 123456 --cluster check 192.168.1.128:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.128:6379 (81524d0e...) -> 0 keys | 0 slots | 0 slaves.
192.168.1.123:6379 (88ed6770...) -> 3329 keys | 5461 slots | 1 slaves.
192.168.1.121:6379 (2edd0690...) -> 3331 keys | 5461 slots | 1 slaves.
192.168.1.122:6379 (657fb8ef...) -> 3340 keys | 5462 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.128:6379)
M: 81524d0efb06ac6492bf8ffeca868184e48b17d5 192.168.1.128:6379
   slots: (0 slots) master
M: 88ed6770429d22392352ef613ce6081b98edcc50 192.168.1.123:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 902b9c0fcfcceefae1ad8a7a599845898990e041 192.168.1.125:6379
   slots: (0 slots) slave
   replicates 88ed6770429d22392352ef613ce6081b98edcc50
S: 5606e145ebb7238966781890654c6a1c30c1e71e 192.168.1.127:6379
   slots: (0 slots) slave
   replicates 657fb8ef5dde62561326617e9bd4e673317ce94b
M: 2edd069021df65b153bf0cd33f10fddb3aeff936 192.168.1.121:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 657fb8ef5dde62561326617e9bd4e673317ce94b 192.168.1.122:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f84d545049358b19d0e9f30271d44d9bcbc28f23 192.168.1.126:6379
   slots: (0 slots) slave
   replicates 2edd069021df65b153bf0cd33f10fddb3aeff936
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

给新master分配槽位

root@redis01:~# redis-cli  -a 123456 --cluster reshard 192.168.1.122:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.1.122:6379)
M: 657fb8ef5dde62561326617e9bd4e673317ce94b 192.168.1.122:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 88ed6770429d22392352ef613ce6081b98edcc50 192.168.1.123:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 902b9c0fcfcceefae1ad8a7a599845898990e041 192.168.1.125:6379
   slots: (0 slots) slave
   replicates 88ed6770429d22392352ef613ce6081b98edcc50
M: 2edd069021df65b153bf0cd33f10fddb3aeff936 192.168.1.121:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5606e145ebb7238966781890654c6a1c30c1e71e 192.168.1.127:6379
   slots: (0 slots) slave
   replicates 657fb8ef5dde62561326617e9bd4e673317ce94b
S: f84d545049358b19d0e9f30271d44d9bcbc28f23 192.168.1.126:6379
   slots: (0 slots) slave
   replicates 2edd069021df65b153bf0cd33f10fddb3aeff936
M: 81524d0efb06ac6492bf8ffeca868184e48b17d5 192.168.1.128:6379
   slots: (0 slots) master
[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   #新分配多少个槽位=16384/master个数
What is the receiving node ID? 81524d0efb06ac6492bf8ffeca868184e48b17d5   #新的
master的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是自动在所有的redis 
node选择划分

Ready to move 4096 slots.
  Source nodes:
    M: 657fb8ef5dde62561326617e9bd4e673317ce94b 192.168.1.122:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 88ed6770429d22392352ef613ce6081b98edcc50 192.168.1.123:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 2edd069021df65b153bf0cd33f10fddb3aeff936 192.168.1.121:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 81524d0efb06ac6492bf8ffeca868184e48b17d5 192.168.1.128:6379
       slots: (0 slots) master
  Resharding plan:
    Moving slot 5461 from 657fb8ef5dde62561326617e9bd4e673317ce94b
    Moving slot 5462 from 657fb8ef5dde62561326617e9bd4e673317ce94b.
    按yes确定

槽位分配成功,都是4096

root@redis01:~# redis-cli -a 123456 --cluster check 192.168.1.128:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.128:6379 (81524d0e...) -> 2474 keys | 4096 slots | 0 slaves.
192.168.1.123:6379 (88ed6770...) -> 2500 keys | 4096 slots | 1 slaves.
192.168.1.121:6379 (2edd0690...) -> 2511 keys | 4096 slots | 1 slaves.
192.168.1.122:6379 (657fb8ef...) -> 2515 keys | 4096 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.128:6379)
M: 81524d0efb06ac6492bf8ffeca868184e48b17d5 192.168.1.128:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 88ed6770429d22392352ef613ce6081b98edcc50 192.168.1.123:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 902b9c0fcfcceefae1ad8a7a599845898990e041 192.168.1.125:6379
   slots: (0 slots) slave
   replicates 88ed6770429d22392352ef613ce6081b98edcc50
S: 5606e145ebb7238966781890654c6a1c30c1e71e 192.168.1.127:6379
   slots: (0 slots) slave
   replicates 657fb8ef5dde62561326617e9bd4e673317ce94b
M: 2edd069021df65b153bf0cd33f10fddb3aeff936 192.168.1.121:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 657fb8ef5dde62561326617e9bd4e673317ce94b 192.168.1.122:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: f84d545049358b19d0e9f30271d44d9bcbc28f23 192.168.1.126:6379
   slots: (0 slots) slave
   replicates 2edd069021df65b153bf0cd33f10fddb3aeff936
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

在这里插入图片描述

#为新master添加加为slave节点
192.168.1.129:6379 是新slave

redis-cli -a 123456 --cluster add-node 192.168.1.129:6379 192.168.1.122:6379 --cluster-slave --cluster-master-id 81524d0efb06ac6492bf8ffeca868184e48b17d5

查看集群状态信息

redis-cli -a 123456 -h 192.168.1.122 --no-auth-warning cluster info
cluster_state: ok - 表示 Redis 集群的状态正常。
cluster_slots_assigned: 16384 - 表示集群中槽位的总数。
cluster_slots_ok: 16384 - 表示所有槽位都正常运行。
cluster_slots_pfail: 0 - 表示处于 PFAIL(疑似故障)状态的槽位的数量。
cluster_slots_fail: 0 - 表示失败的槽位的数量。
cluster_known_nodes: 8 - 表示已知的节点数量。
cluster_size: 4 - 表示集群中的节点数量。
cluster_current_epoch: 7 - 表示集群当前的纪元(epoch)。
cluster_my_epoch: 2 - 表示当前节点在集群中的纪元(epoch)。
cluster_stats_messages_ping_sent: 800 - 表示发送的 ping 消息数量。
cluster_stats_messages_pong_sent: 802 - 表示发送的 pong 消息数量。
cluster_stats_messages_meet_sent: 4 - 表示发送的 meet 消息数量。
cluster_stats_messages_update_sent: 21 - 表示发送的 update 消息数量。
cluster_stats_messages_sent: 1627 - 表示发送的总消息数量。
cluster_stats_messages_ping_received: 799 - 表示接收到的 ping 消息数量。
cluster_stats_messages_pong_received: 802 - 表示接收到的 pong 消息数量。
cluster_stats_messages_meet_received: 3 - 表示接收到的 meet 消息数量。
cluster_stats_messages_received: 1604 - 表示接收到的总消息数量。
这些指标可以提供有关 Redis 集群状态和性能的信息。

集群缩容

redis-cli -a 123456 --cluster reshard 192.168.1.122:6379

在这里插入图片描述

移动多少个槽位 1365

What is the receiving node ID? 2edd069021df65b153bf0cd33f10fddb3aeff936 接受槽位的id:
要移动的ID 填写要缩掉的id,然后done ,再yes

重复步骤

删除节点
192.168.1.122:6379 集群ip 49615809a2118d293bee38a2b3164cb32f4d20b7要删除的id

root@redis01:~# redis-cli -a 123456 --cluster del-node 192.168.1.122:6379  49615809a2118d293bee38a2b3164cb32f4d20b7
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 49615809a2118d293bee38a2b3164cb32f4d20b7 from cluster 192.168.1.122:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@redis01:~# redis-cli -a 123456 --cluster del-node 192.168.1.122:6379  81524d0efb06ac6492bf8ffeca868184e48b17d5

删除配置
rm -f /var/lib/redis/nodes-6379.conf

redis cluster +haproxy+keepalived 高可用

添加内核参数,当网卡没有192.168.1.188 ip时,haproxy 也能绑定运行
vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1

haproxy 配置
listen redis-cluster
	bind 192.168.1.188:2222
	mode tcp
	server cluster01 192.168.1.121:6379 check
	server cluster02 192.168.1.122:6379 check
	server cluster03 192.168.1.123:6379 check
	server cluster04 192.168.1.125:6379 check
	server cluster05 192.168.1.126:6379 check
	server cluster06 192.168.1.127:6379 check

keepalived 1,keepalived2
当haproxy1 服务挂掉是,keepalived自动减少30优先级,vip 切换到haproxy2,haproxy2提供服务

root@redis01:/etc/keepalived# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id  kv1   #keepalived2 为kv2
}
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy.sh"
   interval 1
   weight -30 
   fall 3 
   rise 5 
   timeout 2

}

vrrp_instance VI_1 {
    state MASTER     #keepalive2 为BACKUP
    interface eth0
    virtual_router_id 100    #keepalived2 为80 
    #nopreempt
    priority 100
    advert_int 1
    virtual_ipaddress {
        192.168.1.188 dev eth0 label eth0:1
    }
    track_interface {
	eth0
    }
    track_script {
 	check_haproxy
	}
    
}


redis cluster 使用vip正常登录
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值