redis作业

redis作业

1.部署一个redis 给开发使用

redis源码包官方下载:http://download.redis.io/releases/


------------------------------------------------------------------------------------

                                      yum安装redis
------------------------------------------------------------------------------------
#centos7---安装redis
[root@centos7 ~]#yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: 
Available Packages
Name       : redis
Arch       : x86_64
Version     : 3.2.12
Release     : 2.el7
Size       : 544 k
Repo       : epel/7/x86_64
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a 
data
           : structure server since keys can contain strings, hashes, lists, 
sets and
           : sorted sets.
           : 
           : You can run atomic operations on these types, like appending to a 
string;
           : incrementing the value in a hash; pushing to a list; computing set
           : intersection, union and difference; or getting the member with 
highest
           : ranking in a sorted set.
           : 
           : In order to achieve its outstanding performance, Redis works with 
an
           : in-memory dataset. Depending on your use case, you can persist it 
either
           : by dumping the dataset to disk every once in a while, or by 
appending
           : each command to a log.
           : 
           : Redis also supports trivial-to-setup master-slave replication, with 
very
           : fast non-blocking first synchronization, auto-reconnection on net 
split
           : and so forth.
           : 
           : Other features include Transactions, Pub/Sub, Lua scripting, Keys 
with a
           : limited time-to-live, and configuration settings to make Redis 
behave like
           : a cache.
           : 
           : You can use Redis from most programming languages also.
           






#centos8---安装redis
[root@centos7 ~]#dnf -y install redis
[root@centos7 ~]#systemctl enable --now redis
[root@centos7 ~]#ss -tnl
State       Recv-Q       Send-Q       Local Address:Port           Peer 
Address:Port 
LISTEN       0             128                 0.0.0.0:22                   
0.0.0.0:*     
LISTEN       0             128               127.0.0.1:6379                 
0.0.0.0:*     
LISTEN       0             128                   [::]:22                     
[::]:*     
[root@centos7 ~]#pstree -p|grep redis
           |-redis-server(3383)-+-{redis-server}(3384)
           |                   |-{redis-server}(3385)
           |                    -{redis-server}(3386)
[root@centos7 ~]#redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info 
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:8c0bf22bfba82c8f
redis_mode:standalone
os:Linux 4.18.0-147.el8.x86_64 x86_64









------------------------------------------------------------------------------------

                                      编译安装redis
------------------------------------------------------------------------------------





#编译安装--redis
一.源码编译
#安装依赖包
[root@centos8~]#yum -y install gcc make jemalloc-devel
#如果支持systemd需要安装下面包
[root@centos8~]#yum -y install gcc jemalloc-devel systemd-devel
[root@ubuntu2004 ~]#apt -y install make gcc libjemalloc-dev libsystemd-dev
#下载源码
[root@centos7 ~]#wget http://download.redis.io/releases/redis-6.2.4.tar.gz
[root@centos7 ~]#tar xvf redis-6.2.4.tar.gz
#编译安装
[root@centos7 ~]#cd redis-6.2.4/
[root@centos8 redis-6.2.4]#make -j 2 PREFIX=/apps/redis install #指定redis安装目录
#如果支持systemd,需要执行下面
[root@centos8 redis-6.2.4]#make -j 2 USE_SYSTEMD=yes PREFIX=/apps/redis install
#配置环境变量
[root@centos7 ~]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
[root@centos7 ~]#. /etc/profile.d/redis.sh
#目录结构
[root@centos7 ~]#tree /apps/redis/
/apps/redis/
└── bin
   ├── redis-benchmark
   ├── redis-check-aof
   ├── redis-check-rdb
   ├── redis-cli
   ├── redis-sentinel -> redis-server
   └── redis-server
1 directory, 6 files

#准备相关目录和配置文件
[root@centos7 ~]#mkdir /apps/redis/{etc,log,data,run} #创建配置文件、日志、数据等目录
[root@centos8 redis-6.2.4]#cp redis.conf /apps/redis/etc/


二.创建redis用户和设置数据目录权限
[root@centos7 ~]#useradd -r -s /sbin/nologin redis
#设置目录权限
[root@centos7 ~]#chown -R redis.redis /apps/redis/    #redis.redis 表示将所有者(Owner)改为用户 redis,并将组(Group)也改为 redis。




三.创建redis服务service文件
#可以复制CentOS8利用yum安装Redis生成的redis.service文件,进行修改
[root@centos7 ~]#scp 10.0.1.172:/lib/systemd/system/redis.service 
/lib/systemd/system/
[root@centos7 ~]#vim /lib/systemd/system/redis.service
[root@centos7 ~]#cat /lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify #如果支持systemd可以启用此行
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000   #指定此值才支持更大的maxclients值
[Install]
WantedBy=multi-user.target



#启动redis
[root@centos7 ~]#redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:673d8c0ee1a8872
redis_mode:standalone
os:Linux 3.10.0-1062.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:1669
run_id:5e0420e92e35ad1d740e9431bc655bfd0044a5d1
tcp_port:6379
uptime_in_seconds:140
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:4807524
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis.conf
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# Memory
used_memory:575792
used_memory_human:562.30K
used_memory_rss:3506176
used_memory_rss_human:3.34M
used_memory_peak:575792
used_memory_peak_human:562.30K
used_memory_peak_perc:100.18%
used_memory_overhead:562590
used_memory_startup:512896
used_memory_dataset:13202
used_memory_dataset_perc:20.99%
allocator_allocated:1201392
allocator_active:1531904
allocator_resident:8310784
total_system_memory:1019645952
total_system_memory_human:972.41M
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.28
allocator_frag_bytes:330512
allocator_rss_ratio:5.43
allocator_rss_bytes:6778880
rss_overhead_ratio:0.42
rss_overhead_bytes:-4804608
mem_fragmentation_ratio:6.57
mem_fragmentation_bytes:2972384
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1581865688
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
# Stats
total_connections_received:1
total_commands_processed:2
instantaneous_ops_per_sec:0
total_net_input_bytes:45
total_net_output_bytes:11475
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:0
master_replid:f7228f0b6203183004fae8db00568f9f73422dc4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.132821
used_cpu_user:0.124317
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
127.0.0.1:6379> exit
[root@centos7 ~]#





#默认redis启用配置文件
[root@redis etc]# grep -Ev "^#|^$" /apps/redis/etc/redis.conf 
bind 127.0.0.1 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes









------------------------------------------------------------------------------------

                                 shell安装redis
------------------------------------------------------------------------------------


#!/bin/bash
#
#
#Author:         nwq
#QQ:             3078499367
#Date:           2024-07-22
#FileName:       install_redis.sh
#URL:             http://www.sansi.fun
#Description:     The test script
#Copyright (C):   2024 All rights reserved
#
REDIS_VERSION=redis-6.2.5
PASSWORD=123456
INSTALL_DIR=/apps/redis
CPUS=lscpu |awk '/^CPU\(s\)/{print $2}'
. /etc/os-release
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $" OK "    
    elif [ $2 = "failure" -o $2 = "1" ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
          echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}
prepare(){
    if [ $ID = "centos" ];then
       yum  -y install gcc make jemalloc-devel systemd-devel
    else
   apt update 
   apt -y install  gcc make libjemalloc-dev libsystemd-dev
    fi
    if [ $? -eq 0 ];then
       color "安装软件包成功"  0
    else
       color "安装软件包失败,请检查网络配置" 1
        exit
    fi
}
install() {   
    if [ ! -f ${REDIS_VERSION}.tar.gz ];then
        wget http://download.redis.io/releases/${REDIS_VERSION}.tar.gz || { 
color "Redis 源码下载失败" 1 ; exit; }
    fi
   tar xf ${REDIS_VERSION}.tar.gz
    cd ${REDIS_VERSION}
    make -j $CUPS USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} install && color "Redis 
编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }
    ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/
    
    mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
  
    cp redis.conf  ${INSTALL_DIR}/etc/
    sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/'  -e "/# requirepass/a requirepass 
$PASSWORD"  -e "/^dir .*/c dir ${INSTALL_DIR}/data/"  -e "/logfile .*/c logfile 
${INSTALL_DIR}/log/redis-6379.log"  -e  "/^pidfile .*/c pidfile 
${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
    if id redis &> /dev/null ;then 
         color "Redis 用户已存在" 1
    else
         useradd -r -s /sbin/nologin redis
         color "Redis 用户创建成功" 0
    fi
    chown -R redis.redis ${INSTALL_DIR}
    cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
   sysctl -p
     if [ $ID = "centos" ];then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> 
/etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
       /etc/rc.d/rc.local 
    else
        echo -e '#!/bin/bash\necho never > 
/sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /etc/rc.local
       /etc/rc.local
    fi
cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --
supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
EOF
     systemctl daemon-reload 
     systemctl enable --now redis &> /dev/null 
     if [ $? -eq 0 ];then
         color "Redis 服务启动成功,Redis信息如下:"  0
     else
         color "Redis 启动失败" 1
         exit
     fi
     sleep 2
     redis-cli -a $PASSWORD INFO Server 2> /dev/null
}
prepare 
install
2.redis配置参数 都代表啥意思 写到这
Redis配置文件中的部分关键参数及其含义包括:
-------------------------------------------------------------------------------------
                                          必须熟悉
--------------------------------------------------------------------------------------
bind: 指定Redis监听的IP地址,默认是监听所有接口。
port: Redis服务运行的端口号,默认是6379。
daemonize: 是否以后台守护进程方式运行Redis,默认为否,生产环境一般设为yes。
pidfile: Redis守护进程的PID文件路径。
timeout: 客户端闲置多长时间后关闭连接,默认0表示永不超时。
save: Redis持久化策略,定义在什么条件下执行RDB快照。
appendonly: 是否开启AOF持久化,默认关闭。
appendfilename: AOF文件名。
databases: 设置数据库的数量,默认16个。
maxmemory: Redis最大内存限制,达到此限制后将依据内存策略进行处理。
requirepass: 设置Redis服务器的密码。





-------------------------------------------------------------------------------------
                                          详细版(详细版做了解)
--------------------------------------------------------------------------------------

bind 0.0.0.0 #指定监听地址,支持用空格隔开的多个监听IP
protected-mode yes #redis3.2之后加入的新特性,在没有设置bind IP和密码的时候,redis只允许访
问127.0.0.1:6379,可以远程连接,但当访问将提示警告信息并拒绝远程访问
port 6379 #监听端口,默认6379/tcp
tcp-backlog 511 #三次握手的时候server端收到client ack确认号之后的队列值,即全连接队列长度
timeout 0 #客户端和Redis服务端的连接超时时间,默认是0,表示永不超时
tcp-keepalive 300 #tcp 会话保持时间300s
daemonize no #默认no,即直接运行redis-server程序时,不作为守护进程运行,而是以前台方式运行,如果想在后台运行需改成yes,当redis作为守护进程运行的时候,它会写一个 pid 到/var/run/redis.pid 文件
supervised no #和OS相关参数,可设置通过upstart和systemd管理Redis守护进程,centos7后都使用systemd
pidfile /var/run/redis_6379.pid #pid文件路径,可以修改为/apps/redis/run/redis_6379.pid
loglevel notice #日志级别
logfile "/path/redis.log" #日志路径,示例:logfile "/apps/redis/log/redis_6379.log"
databases 16 #设置数据库数量,默认:0-15,共16个库
always-show-logo yes #在启动redis 时是否显示或在日志中记录记录redis的logo
save 900 1 #在900秒内有1个key内容发生更改,就执行快照机制
save 300 10 #在300秒内有10个key内容发生更改,就执行快照机制
save 60 10000  #60秒内如果有10000个key以上的变化,就自动快照备份
stop-writes-on-bgsave-error yes #默认为yes时,可能会因空间满等原因快照无法保存出错时,会禁止redis写入操作,生产建议为no
 #此项只针对配置文件中的自动save有效
rdbcompression yes #持久化到RDB文件时,是否压缩,"yes"为压缩,"no"则反之
rdbchecksum yes #是否对备份文件开启RC64校验,默认是开启
dbfilename dump.rdb #快照文件名
dir ./ #快照文件保存路径,示例:dir "/apps/redis/data"
#主从复制相关
# replicaof <masterip> <masterport> #指定复制的master主机地址和端口,5.0版之前的指令为
slaveof 
# masterauth <master-password> #指定复制的master主机的密码
replica-serve-stale-data yes #当从库同主库失去连接或者复制正在进行,从机库有两种运行方式:
1、设置为yes(默认设置),从库会继续响应客户端的读请求,此为建议值
2、设置为no,除去特定命令外的任何请求都会返回一个错误"SYNC with master in progress"。

replica-read-only yes #是否设置从库只读,建议值为yes,否则主库同步从库时可能会覆盖数据,造成数据丢失
repl-diskless-sync no #是否使用socket方式复制数据(无盘同步),新slave第一次连接master时需要做数据的全量同步,redis server就要从内存dump出新的RDB文件,然后从master传到slave,有两种方式把RDB文件传输给客户端:
1、基于硬盘(disk-backed):为no时,master创建一个新进程dump生成RDB磁盘文件,RDB完成之后由
父进程(即主进程)将RDB文件发送给slaves,此为默认值
2、基于socket(diskless):master创建一个新进程直接dump RDB至slave的网络socket,不经过主
进程和硬盘
#推荐使用基于硬盘(为no),是因为RDB文件创建后,可以同时传输给更多的slave,但是基于socket(为yes), 新slave连接到master之后得逐个同步数据。只有当磁盘I/O较慢且网络较快时,可用diskless(yes),否则一般建议使用磁盘(no)

repl-diskless-sync-delay 5 #diskless时复制的服务器等待的延迟时间,设置0为关闭,在延迟时间内到达的客户端,会一起通过diskless方式同步数据,但是一旦复制开始,master节点不会再接收新slave的复制请求,直到下一次同步开始才再接收新请求。即无法为延迟时间后到达的新副本提供服务,新副本将排队等待下一次RDB传输,因此服务器会等待一段时间才能让更多副本到达。推荐值:30-60

repl-ping-replica-period 10 #slave根据master指定的时间进行周期性的PING master,用于监测master状态,默认10s
repl-timeout 60 #复制连接的超时时间,需要大于repl-ping-slave-period,否则会经常报超时
repl-disable-tcp-nodelay no #是否在slave套接字发送SYNC之后禁用 TCP_NODELAY,如果选择"yes",Redis将合并多个报文为一个大的报文,从而使用更少数量的包向slaves发送数据,但是将使数据传输到slave上有延迟,Linux内核的默认配置会达到40毫秒,如果 "no" ,数据传输到slave的延迟将会减少,但要使用更多的带宽

repl-backlog-size 512mb #复制缓冲区内存大小,当slave断开连接一段时间后,该缓冲区会累积复制副本数据,因此当slave 重新连接时,通常不需要完全重新同步,只需传递在副本中的断开连接后没有同步的部分数据即可。只有在至少有一个slave连接之后才分配此内存空间,建议建立主从时此值要调大一些或在低峰期配置,否则会导致同步到slave失败

repl-backlog-ttl 3600 #多长时间内master没有slave连接,就清空backlog缓冲区
replica-priority 100 #当master不可用,哨兵Sentinel会根据slave的优先级选举一个master,此值最低的slave会优先当选master,而配置成0,永远不会被选举,一般多个slave都设为一样的值,让其自动选择
#min-replicas-to-write 3 #至少有3个可连接的slave,mater才接受写操作
#min-replicas-max-lag 10 #和上面至少3个slave的ping延迟不能超过10秒,否则master也将停止写操作

requirepass foobared #设置redis连接密码,之后需要AUTH pass,如果有特殊符号,用" "引起来,生产建议设置
rename-command #重命名一些高危命令,示例:rename-command FLUSHALL "" 禁用命令
   #示例: rename-command del magedu
   
maxclients 10000 #Redis最大连接客户端
maxmemory <bytes> #redis使用的最大内存,单位为bytes字节,0为不限制,建议设为物理内存一半,8G内存的计算方式8(G)*1024(MB)1024(KB)*1024(Kbyte),需要注意的是缓冲区是不计算在maxmemory内,生产中如果不设置此项,可能会导致OOM
#maxmemory-policy noeviction 此为默认值
# MAXMEMORY POLICY:当达到最大内存时,Redis 将如何选择要删除的内容。您可以从以下行为中选择一种:
#
# volatile-lru -> Evict 使用近似 LRU,只有设置了过期时间的键。
# allkeys-lru -> 使用近似 LRU 驱逐任何键。
# volatile-lfu -> 使用近似 LFU 驱逐,只有设置了过期时间的键。
# allkeys-lfu -> 使用近似 LFU 驱逐任何键。
# volatile-random -> 删除设置了过期时间的随机密钥。
# allkeys-random -> 删除一个随机密钥,任何密钥。
# volatile-ttl -> 删除过期时间最近的key(次TTL)
# noeviction -> 不要驱逐任何东西,只是在写操作时返回一个错误。
#
# LRU 表示最近最少使用
# LFU 表示最不常用
#
# LRU、LFU 和 volatile-ttl 都是使用近似随机算法实现的。
#
# 注意:使用上述任何一种策略,当没有合适的键用于驱逐时,Redis 将在需要更多内存的写操作时返回错误。这些通常是创建新密钥、添加数据或修改现有密钥的命令。一些示例是:SET、INCR、HSET、LPUSH、SUNIONSTORE、SORT(由于 STORE 参数)和 EXEC(如果事务包括任何需要内存的命令)。
#MAXMEMORY POLICY:当达到最大内存时,Redis 将如何选择要删除的内容。可以从下面行为中进行选择:
# volatile-lru -> 在具有过期集的键中使用近似 LRU 驱逐。
# allkeys-lru -> 使用近似 LRU 驱逐任何键。
# volatile-lfu -> 在具有过期集的键中使用近似 LFU 驱逐。
# allkeys-lfu -> 使用近似 LFU 驱逐任何键。
# volatile-random -> 从具有过期设置的密钥中删除一个随机密钥。
# allkeys-random -> 删除一个随机密钥,任何密钥。
# volatile-ttl -> 删除过期时间最近的key(次TTL)
# noeviction -> 不要驱逐任何东西,只是在写操作时返回一个错误。
#
# LRU 表示最近最少使用
# LFU 表示最不常用
#
# LRU、LFU 和 volatile-ttl 均使用近似实现随机算法。
#
# 注意:使用上述任何一种策略,Redis 都会在写入时返回错误操作,当没有合适的键用于驱逐时。

appendonly no #是否开启AOF日志记录,默认redis使用的是rdb方式持久化,这种方式在许多应用中已经足够用了,但是redis如果中途宕机,会导致可能有几分钟的数据丢失(取决于dump数据的间隔时间),根据save来策略进行持久化,Append Only File是另一种持久化方式,可以提供更好的持久化特性,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里,先忽略RDB文件。默认不启用此功能

appendfilename "appendonly.aof" #文本文件AOF的文件名,存放在dir指令指定的目录中

appendfsync everysec #aof持久化策略的配置
#no表示由操作系统保证数据同步到磁盘,Linux的默认fsync策略是30秒,最多会丢失30s的数据
#always表示每次写入都执行fsync,以保证数据同步到磁盘,安全性高,性能较差
#everysec表示每秒执行一次fsync,可能会导致丢失这1s数据,此为默认值,也生产建议值
#同时在执行bgrewriteaof操作和主进程写aof文件的操作,两者都会操作磁盘,而bgrewriteaof往往会涉及大量磁盘操作,这样就会造成主进程在写aof文件的时候出现阻塞的情形,以下参数实现控制

no-appendfsync-on-rewrite no #在aof rewrite期间,是否对aof新记录的append暂缓使用文件同步策略,主要考虑磁盘IO开支和请求阻塞时间。
#默认为no,表示"不暂缓",新的aof记录仍然会被立即同步到磁盘,是最安全的方式,不会丢失数据,但是要忍受阻塞的问题
#为yes,相当于将appendfsync设置为no,这说明并没有执行磁盘操作,只是写入了缓冲区,因此这样并不会造成阻塞(因为没有竞争磁盘),但是如果这个时候redis挂掉,就会丢失数据。丢失多少数据呢?Linux的默认fsync策略是30秒,最多会丢失30s的数据,但由于yes性能较好而且会避免出现阻塞因此比较推荐
#rewrite 即对aof文件进行整理,将空闲空间回收,从而可以减少恢复数据时间

auto-aof-rewrite-percentage 100 #当Aof log增长超过指定百分比例时,重写AOF文件,设置为0表示不自动重写Aof日志,重写是为了使aof体积保持最小,但是还可以确保保存最完整的数据

auto-aof-rewrite-min-size 64mb #触发aof rewrite的最小文件大小
aof-load-truncated yes #是否加载由于某些原因导致的末尾异常的AOF文件(主进程被kill/断电等),建议yes
aof-use-rdb-preamble no #redis4.0新增RDB-AOF混合持久化格式,在开启了这个功能之后,AOF重写产生的文件将同时包含RDB格式的内容和AOF格式的内容,其中RDB格式的内容用于记录已有的数据,而AOF格式的内容则用于记录最近发生了变化的数据,这样Redis就可以同时兼有RDB持久化和AOF持久化的优点(既能够快速地生成重写文件,也能够在出现问题时,快速地载入数据),默认为no,即不启用此功能

lua-time-limit 5000 #lua脚本的最大执行时间,单位为毫秒
cluster-enabled yes #是否开启集群模式,默认不开启,即单机模式
cluster-config-file nodes-6379.conf #由node节点自动生成的集群配置文件名称
cluster-node-timeout 15000 #集群中node节点连接超时时间,单位ms,超过此时间,会踢出集群
cluster-replica-validity-factor 10 #单位为次,在执行故障转移的时候可能有些节点和master断开一段时间导致数据比较旧,这些节点就不适用于选举为master,超过这个时间的就不会被进行故障转移,不能当选master,计算公式:(node-timeout * replica-validity-factor) + repl-pingreplica-period

cluster-migration-barrier 1 #集群迁移屏障,一个主节点至少拥有1个正常工作的从节点,即如果主节点的slave节点故障后会将多余的从节点分配到当前主节点成为其新的从节点。

cluster-require-full-coverage yes #集群请求槽位全部覆盖,如果一个主库宕机且没有备库就会出现集群槽位不全,那么yes时redis集群槽位验证不全,就不再对外提供服务(对key赋值时,会出现CLUSTERDOWN The cluster is down的提示,cluster_state:fail,但ping 仍PONG),而no则可以继续使用,但是会出现查询数据查不到的情况(因为有数据丢失)。生产建议为no

cluster-replica-no-failover no #如果为yes,此选项阻止在主服务器发生故障时尝试对其主服务器进行故障转移。 但是,主服务器仍然可以执行手动强制故障转移,一般为no
#Slow log 是 Redis 用来记录超过指定执行时间的日志系统,执行时间不包括与客户端交谈,发送回复等I/O操作,而是实际执行命令所需的时间(在该阶段线程被阻塞并且不能同时为其它请求提供服务),由于slow log 保存在内存里面,读写速度非常快,因此可放心地使用,不必担心因为开启 slow log 而影响Redis 的速度

slowlog-log-slower-than 10000 #以微秒为单位的慢日志记录,为负数会禁用慢日志,为0会记录每个命令操作。默认值为10ms,一般一条命令执行都在微秒级,生产建议设为1ms-10ms之间

slowlog-max-len 128 #最多记录多少条慢日志的保存队列长度,达到此长度后,记录新命令会将最旧的命令从命令队列中删除,以此滚动删除,即,先进先出,队列固定长度,默认128,值偏小,生产建议设为1000以上
3.配置一个redis哨兵 和集群
环境:虚拟机
redis-master-sentinel:10.0.1.172
redis-slave1-sentinel:10.0.1.173
redis-slave2-sentinel:10.0.1.174

注意: 
1.master 的配置文件中masterauth 和slave 都必须相同
所有主从节点的 redis.conf 中关健配置
2.主从复制redis版本必须一致
3.哨兵配置redis版本必须一致(版本2.8以上才可)



----------------------------------------------------------------
                         第一步-主从复制
----------------------------------------------------------------
准备主从环境配置
#在所有主从节点执行
[root@centos7 ~]#dnf -y install redis
[root@centos7 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
#或者非交互执行
[root@centos7 ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 123456/' -e 's/^# requirepass .*/requirepass 123456/' /etc/redis.conf
#在所有从节点执行
[root@centos7 ~]#echo "replicaof 10.0.1.172 6379" >> /etc/redis.conf
#在所有主从节点执行
[root@centos7 ~]#systemctl enable --now redis

#master 服务器状态
[root@redis-master ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not 
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.1.183,port=6379,state=online,offset=112,lag=1
slave1:ip=10.0.1.180,port=6379,state=online,offset=112,lag=0
master_replid:8fdca730a2ae48fb9c8b7e739dcd2efcc76794f3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112
127.0.0.1:6379>



#配置 slave1
[root@redis-slave1 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> REPLICAOF 10.0.1.172 6379
OK
127.0.0.1:6379> CONFIG SET masterauth "123456"
OK
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:10.0.1.172
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:140
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8fdca730a2ae48fb9c8b7e739dcd2efcc76794f3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:99
repl_backlog_histlen:42



#配置slave2
[root@redis-slave2 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> REPLICAOF 10.0.1.172 6379
OK
127.0.0.1:6379> CONFIG SET masterauth "123456"
OK
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:10.0.1.172
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:182
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8fdca730a2ae48fb9c8b7e739dcd2efcc76794f3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:182
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:168
127.0.0.1:6379>

----------------------------------------------------------------
                         第二步-(配置)哨兵
----------------------------------------------------------------

 #编辑哨兵配置
sentinel配置
Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在26379/tcp端口.
哨兵服务可以和Redis服务器分开部署在不同主机,但为了节约成本一般会部署在一起
所有redis节点使用相同的以下示例的配置文件

#如果是编译安装,在源码目录有sentinel.conf,复制到安装目录即可,
如:/apps/redis/etc/sentinel.conf
[root@centos7 ~]#cp redis-6.2.5/sentinel.conf /apps/redis/etc/sentinel.conf
[root@centos7 ~]#chown redis.redis /apps/redis/etc/sentinel.conf 
[root@centos7 ~]#vim /etc/redis-sentinel.conf 
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "sentinel_26379.log"
dir "/tmp"  #工作目录
sentinel monitor mymaster 10.0.1.172 6379 2
#mymaster是集群的名称,此行指定当前mymaster集群中master服务器的地址和端口
#2为法定人数限制(quorum),即有几个sentinel认为master down了就进行故障转移,一般此值是所有sentinel节点(一般总数是>=3的 奇数,如:3,5,7等)的一半以上的整数值,比如,总数是3,即3/2=1.5,取整为2,是master的ODOWN客观下线的依据
sentinel auth-pass mymaster 123456
#mymaster集群中master的密码,注意此行要在上面行的下面
sentinel down-after-milliseconds mymaster 30000
#判断mymaster集群中所有节点的主观下线(SDOWN)的时间,单位:毫秒,建议3000
sentinel parallel-syncs mymaster 1
#发生故障转移后,可以同时向新master同步数据的slave的数量,数字越小总同步时间越长,但可以减轻新
master的负载压力
sentinel failover-timeout mymaster 180000
#所有slaves指向新的master所需的超时时间,单位:毫秒
sentinel deny-scripts-reconfig yes #禁止修改脚本
logfile /var/log/redis/sentinel.log




#三个哨兵服务器的配置都如下
[root@redis-master ~]#grep -vE "^#|^$" /etc/redis-sentinel.conf 
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 10.0.1.172 6379 2        #修改此行
sentinel auth-pass mymaster 123456               #增加此行
sentinel down-after-milliseconds mymaster 3000   #修改此行
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#注意此行自动生成必须唯一,一般不需要修改,如果相同则修改此值需重启redis和sentinel服务
sentinel myid 50547f34ed71fd48c197924969937e738a39975b  
.....
# Generated by CONFIG REWRITE
protected-mode no
supervised systemd
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 10.0.1.183 6379
sentinel current-epoch 0
[root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave1:/etc/
[root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave2:/etc/

#启动哨兵服务
tips:将所有哨兵服务器都启动起来
#确保每个哨兵主机myid不同,如果相同,必须手动修改为不同的值
[root@redis-slave1 ~]#vim /etc/redis-sentinel.conf
sentinel myid 50547f34ed71fd48c197924969937e738a39975c 
[root@redis-slave2 ~]#vim /etc/redis-sentinel.conf
sentinel myid 50547f34ed71fd48c197924969937e738a39975d 
[root@redis-master ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave1 ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave2 ~]#systemctl enable --now redis-sentinel.service



#如果是编译安装,在所有哨兵服务器执行下面操作启动哨兵
[root@redis-master ~]##vim /apps/redis/etc/sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
Logfile "sentinel_26379.log"
dir "/apps/redis/data"
sentinel monitor mymaster 10.0.1.172 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
[root@redis-master ~]#/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf


#如果是编译安装,可以在所有节点生成新的service文件
[root@redis-master ~]#vim /lib/systemd/system/redis-sentinel.service
[Unit]
Description=Redis Sentinel
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target


#拷贝哨兵启动文件到其他的机器上
[root@redis ~]# scp -r /lib/systemd/system/redis-sentinel.service root@10.0.1.173:/lib/systemd/system/redis-sentinel.service
[root@redis ~]# scp -r /lib/systemd/system/redis-sentinel.service root@10.0.1.174:/lib/systemd/system/redis-sentinel.service

#注意所有节点的目录权限,否则无法启动服务(如果失败复制过去的哨兵配置文件,权限还是root,所以要改一下所有者和所属组)
[root@redis-master ~]#chown -R redis.redis /apps/redis/

#启动所有机器哨兵服务
[root@redis ~]# systemctl start redis-sentinel



#验证哨兵服务
[root@redis-master ~]#ss -ntl
State   Recv-Q Send-Q Local Address:Port Peer Address:Port        
LISTEN  0       128          0.0.0.0:22         0.0.0.0:*           
LISTEN  0       128          0.0.0.0:26379      0.0.0.0:*           
LISTEN  0       128          0.0.0.0:6379       0.0.0.0:*           
LISTEN  0       128             [::]:22           [::]:*           
LISTEN  0       128             [::]:26379         [::]:*           
LISTEN  0       128             [::]:6379         [::]:*


# 查看哨兵日志
master的哨兵日志
[root@redis ~]# tail -f /apps/redis/log/sentinel.log 
2470:X 19 Apr 2024 20:48:55.358 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
2470:X 19 Apr 2024 20:48:55.358 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
2470:X 19 Apr 2024 20:48:55.358 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
2470:X 19 Apr 2024 20:48:55.358 * monotonic clock: POSIX clock_gettime
2470:X 19 Apr 2024 20:48:55.358 * Running mode=sentinel, port=26379.
2470:X 19 Apr 2024 20:48:55.358 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2470:X 19 Apr 2024 20:48:55.358 # Sentinel ID is 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363
2470:X 19 Apr 2024 20:48:55.358 # +monitor master mymaster 10.0.1.172 6379 quorum 2
2470:X 19 Apr 2024 20:48:55.358 # systemd supervision error: NOTIFY_SOCKET not found!
2470:X 19 Apr 2024 20:48:55.358 # systemd supervision error: NOTIFY_SOCKET not found!




#slave1 的哨兵日志
[root@redis-slave1 ~]#tail -f /var/log/redis/sentinel.log 
[root@redis etc]# tail -f /apps/redis/log/sentinel.log 
13975:X 19 Apr 2024 20:47:52.998 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
13975:X 19 Apr 2024 20:47:52.998 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
13975:X 19 Apr 2024 20:47:52.998 * monotonic clock: POSIX clock_gettime
13975:X 19 Apr 2024 20:47:52.998 * Running mode=sentinel, port=26379.
13975:X 19 Apr 2024 20:47:52.998 # Sentinel ID is 1835822e3453202cdcd2d18af249329d61d05144
13975:X 19 Apr 2024 20:47:52.998 # +monitor master mymaster 10.0.1.172 6379 quorum 2
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:48:20.481 # +sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 20:48:55.838 # -sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379


#slave2的哨兵日志
[root@redis ~]# tail -f /apps/redis/log/sentinel.log 
44731:X 19 Apr 2024 20:46:57.702 * monotonic clock: POSIX clock_gettime
44731:X 19 Apr 2024 20:46:57.703 * Running mode=sentinel, port=26379.
44731:X 19 Apr 2024 20:46:57.703 # Sentinel ID is 65a4b5100456085e7730c16324e7585383e60570
44731:X 19 Apr 2024 20:46:57.703 # +monitor master mymaster 10.0.1.172 6379 quorum 2
44731:X 19 Apr 2024 20:46:57.703 # systemd supervision error: NOTIFY_SOCKET not found!
44731:X 19 Apr 2024 20:46:57.703 # systemd supervision error: NOTIFY_SOCKET not found!
44731:X 19 Apr 2024 20:47:00.720 # +sdown sentinel 1835822e3453202cdcd2d18af249329d61d05144 10.0.1.173 26379 @ mymaster 10.0.1.172 6379
44731:X 19 Apr 2024 20:47:53.997 # -sdown sentinel 1835822e3453202cdcd2d18af249329d61d05144 10.0.1.173 26379 @ mymaster 10.0.1.172 6379
44731:X 19 Apr 2024 20:48:20.565 # +sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
44731:X 19 Apr 2024 20:48:55.969 # -sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379




#当前sentinel状态
在sentinel状态中尤其是最后一行,涉及到masterIP是多少,有几个slave,有几个sentinels,必须是符
合全部服务器数量

#master的哨兵状态
[root@redis-master ~]#redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.1.172:6379,slaves=2,sentinels=3
 #两个slave,三个sentinel服务器,如果sentinels值不符合,检查myid可能冲突



#slave1的哨兵状态
[root@redis-slave1 ~]#redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.1.172:6379,slaves=2,sentinels=3



#slave2的哨兵状态
[root@redis-slave2 ~]#redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.1.172:6379,slaves=2,sentinels=3




----------------------------------------------------------------

                      第三步-测试哨兵(故障转移)

----------------------------------------------------------------



#停止Master 实现故障转移
停止 Master 节点
[root@redis-master ~]#killall redis-server

#查看各节点上哨兵信息:
[root@redis-master ~]#redis-cli -a 123456 -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.1.173:6379,slaves=2,sentinels=3



#故障转移时master--sentinel的信息:
[root@redis ~]# tail -f /apps/redis/log/sentinel.log 
2470:X 19 Apr 2024 20:48:55.358 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
2470:X 19 Apr 2024 20:48:55.358 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
2470:X 19 Apr 2024 20:48:55.358 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
2470:X 19 Apr 2024 20:48:55.358 * monotonic clock: POSIX clock_gettime
2470:X 19 Apr 2024 20:48:55.358 * Running mode=sentinel, port=26379.
2470:X 19 Apr 2024 20:48:55.358 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2470:X 19 Apr 2024 20:48:55.358 # Sentinel ID is 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363
2470:X 19 Apr 2024 20:48:55.358 # +monitor master mymaster 10.0.1.172 6379 quorum 2
2470:X 19 Apr 2024 20:48:55.358 # systemd supervision error: NOTIFY_SOCKET not found!
2470:X 19 Apr 2024 20:48:55.358 # systemd supervision error: NOTIFY_SOCKET not found!
2470:X 19 Apr 2024 21:04:13.132 # +sdown master mymaster 10.0.1.172 6379
2470:X 19 Apr 2024 21:04:13.198 # +new-epoch 1
2470:X 19 Apr 2024 21:04:13.199 # +vote-for-leader 1835822e3453202cdcd2d18af249329d61d05144 1
2470:X 19 Apr 2024 21:04:14.218 # +odown master mymaster 10.0.1.172 6379 #quorum 3/2
2470:X 19 Apr 2024 21:04:14.218 # Next failover delay: I will not start a failover before Fri Apr 19 21:10:14 2024
2470:X 19 Apr 2024 21:04:14.341 # +config-update-from sentinel 1835822e3453202cdcd2d18af249329d61d05144 10.0.1.173 26379 @ mymaster 10.0.1.172 6379
2470:X 19 Apr 2024 21:04:14.341 # +switch-master mymaster 10.0.1.172 6379 10.0.1.173 6379
2470:X 19 Apr 2024 21:04:14.341 * +slave slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.173 6379
2470:X 19 Apr 2024 21:04:14.341 * +slave slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379
2470:X 19 Apr 2024 21:04:17.404 # +sdown slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379



#故障转移时slave1--sentinel信息
[root@redis etc]# tail -f /apps/redis/log/sentinel.log 
13975:X 19 Apr 2024 20:47:52.998 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
13975:X 19 Apr 2024 20:47:52.998 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
13975:X 19 Apr 2024 20:47:52.998 * monotonic clock: POSIX clock_gettime
13975:X 19 Apr 2024 20:47:52.998 * Running mode=sentinel, port=26379.
13975:X 19 Apr 2024 20:47:52.998 # Sentinel ID is 1835822e3453202cdcd2d18af249329d61d05144
13975:X 19 Apr 2024 20:47:52.998 # +monitor master mymaster 10.0.1.172 6379 quorum 2
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:48:20.481 # +sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 20:48:55.838 # -sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.162 # +sdown master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.215 # +odown master mymaster 10.0.1.172 6379 #quorum 2/2
13975:X 19 Apr 2024 21:04:13.215 # +new-epoch 1
13975:X 19 Apr 2024 21:04:13.215 # +try-failover master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.217 # +vote-for-leader 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.220 # 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 voted for 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.222 # 65a4b5100456085e7730c16324e7585383e60570 voted for 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.301 # +elected-leader master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.301 # +failover-state-select-slave master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.373 # +selected-slave slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.373 * +failover-state-send-slaveof-noone slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.435 * +failover-state-wait-promotion slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.307 # +promoted-slave slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.307 # +failover-state-reconf-slaves master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.361 * +slave-reconf-sent slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.329 * +slave-reconf-inprog slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.329 * +slave-reconf-done slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # -odown master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # +failover-end master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # +switch-master mymaster 10.0.1.172 6379 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:15.396 * +slave slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:15.397 * +slave slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:18.454 # +sdown slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379


#故障转移时slave1主从复制的信息
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.1.174,port=6379,state=online,offset=411182,lag=0
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:5a7545360639c3093dfa212492edd75ad51eec4c
master_repl_offset:411182
second_repl_offset:320330
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:411182





#故障转移时slave2--sentinel的信息:
[root@redis etc]# tail -f /apps/redis/log/sentinel.log 
13975:X 19 Apr 2024 20:47:52.998 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
13975:X 19 Apr 2024 20:47:52.998 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
13975:X 19 Apr 2024 20:47:52.998 * monotonic clock: POSIX clock_gettime
13975:X 19 Apr 2024 20:47:52.998 * Running mode=sentinel, port=26379.
13975:X 19 Apr 2024 20:47:52.998 # Sentinel ID is 1835822e3453202cdcd2d18af249329d61d05144
13975:X 19 Apr 2024 20:47:52.998 # +monitor master mymaster 10.0.1.172 6379 quorum 2
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:47:52.998 # systemd supervision error: NOTIFY_SOCKET not found!
13975:X 19 Apr 2024 20:48:20.481 # +sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 20:48:55.838 # -sdown sentinel 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 10.0.1.172 26379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.162 # +sdown master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.215 # +odown master mymaster 10.0.1.172 6379 #quorum 2/2
13975:X 19 Apr 2024 21:04:13.215 # +new-epoch 1
13975:X 19 Apr 2024 21:04:13.215 # +try-failover master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.217 # +vote-for-leader 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.220 # 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363 voted for 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.222 # 65a4b5100456085e7730c16324e7585383e60570 voted for 1835822e3453202cdcd2d18af249329d61d05144 1
13975:X 19 Apr 2024 21:04:13.301 # +elected-leader master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.301 # +failover-state-select-slave master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.373 # +selected-slave slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.373 * +failover-state-send-slaveof-noone slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:13.435 * +failover-state-wait-promotion slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.307 # +promoted-slave slave 10.0.1.173:6379 10.0.1.173 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.307 # +failover-state-reconf-slaves master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:14.361 * +slave-reconf-sent slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.329 * +slave-reconf-inprog slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.329 * +slave-reconf-done slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # -odown master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # +failover-end master mymaster 10.0.1.172 6379
13975:X 19 Apr 2024 21:04:15.396 # +switch-master mymaster 10.0.1.172 6379 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:15.396 * +slave slave 10.0.1.174:6379 10.0.1.174 6379 @ mymaster 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:15.397 * +slave slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379
13975:X 19 Apr 2024 21:04:18.454 # +sdown slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379

#slave2主从复制状态信息
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.1.173
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:414450
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:1
slave0:ip=10.0.1.175,port=6379,state=online,offset=414450,lag=1
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:5a7545360639c3093dfa212492edd75ad51eec4c
master_repl_offset:414450
second_repl_offset:320330
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:414436








#验证故障转移
故障转移后redis.conf中的replicaof行的master IP会被修改

[root@redis ~]# grep "^[a-Z]" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile "/apps/redis/run/redis_6379.pid"
loglevel notice
logfile "/apps/redis/log/redis-6379.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/apps/redis/data"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
replicaof 10.0.1.173 6379
supervised systemd
save 3600 1
save 300 100
save 60 10000
user default on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all



哨兵配置文件的sentinel monitor IP 同样也会被修改
[root@redis ~]# grep "^[a-Z]" /apps/redis/etc/sentinel.conf 
bind 0.0.0.0
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/apps/redis/log/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 10.0.1.173 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
acllog-max-len 128
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel announce-hostnames no
protected-mode no
user default on nopass sanitize-payload ~* &* +@all
sentinel myid 65a4b5100456085e7730c16324e7585383e60570
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
sentinel current-epoch 1
sentinel known-replica mymaster 10.0.1.172 6379
sentinel known-replica mymaster 10.0.1.174 6379
sentinel known-sentinel mymaster 10.0.1.173 26379 1835822e3453202cdcd2d18af249329d61d05144
sentinel known-sentinel mymaster 10.0.1.172 26379 9de5f4c4cb3ce568b5a497e3fe71569fa8c7a363
supervised systemd
maxclients 4064



#验证redis各节点状态
新的master状态
[root@redis-slave1 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.1.174,port=6379,state=online,offset=411182,lag=0
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:5a7545360639c3093dfa212492edd75ad51eec4c
master_repl_offset:411182
second_repl_offset:320330
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:411182



#另一个slave指向新的master
[root@redis-slave2 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:10.0.1.173
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:502546
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:1
slave0:ip=10.0.1.175,port=6379,state=online,offset=502141,lag=1
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:5a7545360639c3093dfa212492edd75ad51eec4c
master_repl_offset:502546
second_repl_offset:320330
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:502532



#原master重新加入redis集群
[root@redis-master ~]#cat /etc/redis.conf 
#sentinel会自动修改下面行指向新的master
replicaof 10.0.1.180 6379      

#在原master上观察状态
[root@redis-master ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
# Replication
role:slave
master_host:10.0.1.173
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:560485
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560485
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:543732
repl_backlog_histlen:16754

[root@redis-master ~]#redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.1.173:6379,slaves=2,sentinels=3



#观察新master上状态和日志
[root@redis-slave1 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.1.174,port=6379,state=online,offset=598537,lag=1
slave1:ip=10.0.1.172,port=6379,state=online,offset=598537,lag=1
master_failover_state:no-failover
master_replid:0c26a7e53423197551314589988db39b5cd4df51
master_replid2:5a7545360639c3093dfa212492edd75ad51eec4c
master_repl_offset:598821
second_repl_offset:320330
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:598821


[root@redis-slave1 ~]#tail -f /apps/redis/etc/sentinel.log 
13975:X 19 Apr 2024 21:22:49.348 * +convert-to-slave slave 10.0.1.172:6379 10.0.1.172 6379 @ mymaster 10.0.1.173 6379



---------------------------------------------------------------

                          第四步-哨兵运维工具
---------------------------------------------------------------
 sentinel运维
手动让主节点下线
127.0.0.1:26379> sentinel failover <masterName>

案例--手动故障转移
[root@centos7 ~]#vim /etc/redis.conf
replica-priority 10 #指定优先级,值越小sentinel会优先将之选为新的master,默为值为100
[root@centos7 ~]#systemctl restart redis
#或者动态修改
[root@centos7 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
127.0.0.1:6379> CONFIG GET replica-priority
1) "replica-priority"
2) "100"
127.0.0.1:6379> CONFIG SET replica-priority 99
OK
127.0.0.1:6379> CONFIG GET replica-priority
1) "replica-priority"
2) "99"
[root@centos7 ~]#redis-cli   -p 26379
127.0.0.1:26379> sentinel failover mymaster 
OK




















----------------------------------------------------------------
                   
                   第五步-redis集群(高可用-负载均衡)

----------------------------------------------------------------



#步骤
集群节点
10.0.1.172
10.0.1.173(10.0.1.180)
10.0.1.174(10.0.1.183)


10.0.1.175
10.0.1.176(10.0.1.177)
10.0.1.178(10.0.1.179)




预留服务器扩展使用
10.0.1.181
10.0.1.182


#部署方式
redis cluster 有多种部署方法
1.原生命令安装
理解Redis Cluster架构
生产环境不使用

2.官方工具安装
高效、准确
生产环境可以使用


3.自主研发
可以实现可视化的自动化部署



----------------------------------------------------------------

                第一步-部署redis(shell脚本)

----------------------------------------------------------------
#每个机器上面跑一遍



#!/bin/bash
#
#********************************************************************
#Author:          nwq
#QQ:              3078499367
#Date:            2024-07-22
#FileName:       install_redis.sh
#URL:             http://www.sansi.fun
#Description:     The test script
#Copyright (C):   2024 All rights reserved
#********************************************************************
REDIS_VERSION=redis-6.2.5
#REDIS_VERSION=redis-4.0.14
PASSWORD=123456
INSTALL_DIR=/apps/redis
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}


prepare(){
    if [ $ID = "centos" -o $ID = "rocky" ];then
        yum  -y install gcc make jemalloc-devel systemd-devel
    else
	    apt update 
	    apt -y install  gcc make libjemalloc-dev libsystemd-dev
    fi
    if [ $? -eq 0 ];then
        color "安装软件包成功"  0
    else
        color "安装软件包失败,请检查网络配置" 1
        exit
    fi
}
install() {   
    if [ ! -f ${REDIS_VERSION}.tar.gz ];then
        wget http://download.redis.io/releases/${REDIS_VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
    fi
    tar xf ${REDIS_VERSION}.tar.gz -C /usr/local/src
    cd /usr/local/src/${REDIS_VERSION}
    make -j $CUPS USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }

    ln -s ${INSTALL_DIR}/bin/redis-*  /usr/bin/
    
    mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
  
    cp redis.conf  ${INSTALL_DIR}/etc/

    sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/'  -e "/# requirepass/a requirepass $PASSWORD"  -e "/^dir .*/c dir ${INSTALL_DIR}/data/"  -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log"  -e  "/^pidfile .*/c  pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf


    if id redis &> /dev/null ;then 
         color "Redis 用户已存在" 1 
    else
         useradd -r -s /sbin/nologin redis
         color "Redis 用户创建成功" 0
    fi

    chown -R redis.redis ${INSTALL_DIR}

    cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
    sysctl -p 
    if [ $ID = "centos" -o $ID = "rocky" ];then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
        /etc/rc.d/rc.local 
    else 
        echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /etc/rc.local
        /etc/rc.local
    fi


cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target

EOF
     systemctl daemon-reload 
     systemctl enable --now  redis &> /dev/null 
     if [ $? -eq 0 ];then
         color "Redis 服务启动成功,Redis信息如下:"  0 
     else
         color "Redis 启动失败" 1 
         exit
     fi
     sleep 2
     redis-cli -a $PASSWORD INFO Server 2> /dev/null
}

prepare 
install 





#脚本安装信息
服务器信息:
- Redis版本:6.2.5
- Git仓库SHA1校验和:00000000(表示未从Git仓库构建)
- Git仓库是否包含未提交更改:0(表示没有未提交的修改)
- Redis构建标识符:612914b0e47e2735
- Redis运行模式:单机模式(standalone)
- 操作系统:Linux 3.10.0-1160.114.2.el7.x86_64(基于x86_64架构)
- 架构位数:64位
- 多路复用API:epoll
- 原子变量API:原子内置(atomic-builtin)
- GCC编译器版本:4.8.5
- 进程ID:14028
- 进程监督程序:systemd
- Redis运行实例ID:991dc40e620a6b8ea30a36088da4ee1d63dcdc1b
- TCP端口:6379
- 服务器当前时间(微秒):1713261706931676
- 已运行时间(秒):497秒
- 已运行天数:0天
- Hz频率(事件轮询频率):10
- 配置Hz频率:10
- 最近最少使用算法计数器(LRU Clock):1985674
- 可执行文件路径:/apps/redis/bin/redis-server
- 配置文件路径:/apps/redis/etc/redis.conf
- I/O线程活跃数量:0(表示当前无活跃的I/O线程)








----------------------------------------------------------------


                第二步--启用 redis cluster 配置

----------------------------------------------------------------

#所有机器执行以下配置
yum -y install redis

#每个节点修改redis配置,必须开启cluster功能的参数
命令内修改----#手动修改配置文件
[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不可用


命令行外修改---#或者执行下面命令,批量修改
[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf

#如果是编译安装可以执行下面操作
[root@redis-node1 ~]#sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf

[root@redis-node1 ~]#systemctl enable --now redis


#查看状态(是否有cluster--有代表成功开启)
[root@redis ~]# ps aux|grep redis
redis      1092  0.1  0.4 198112  4116 ?        Ssl  23:53   0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root       1104  0.0  0.0 112812   980 pts/0    R+   23:55   0:00 grep --color=auto redi



#查看端口是否有16379(正常情况下是根据你的redis端口再增加10000)
[root@redis ~]# ss -tulpn
Netid State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
udp   UNCONN     0      0              *:68                         *:*                   users:(("dhclient",pid=555,fd=6))
tcp   LISTEN     0      128            *:22                         *:*                   users:(("sshd",pid=843,fd=3))
tcp   LISTEN     0      100    127.0.0.1:25                         *:*                   users:(("master",pid=955,fd=13))
tcp   LISTEN     0      511            *:16379                      *:*                   users:(("redis-server",pid=1092,fd=9))
tcp   LISTEN     0      511            *:6379                       *:*                   users:(("redis-server",pid=1092,fd=6))
tcp   LISTEN     0      128         [::]:22                      [::]:*                   users:(("sshd",pid=843,fd=4))
tcp   LISTEN     0      100        [::1]:25                      [::]:*                   users:(("master",pid=955,fd=14))
tcp   LISTEN     0      511        [::1]:16379                   [::]:*                   users:(("redis-server",pid=1092,fd=10))
tcp   LISTEN     0      511        [::1]:6379                    [::]:*                   users:(("redis-server",pid=1092,fd=7))




----------------------------------------------------------------


                         第三步---创建集群

----------------------------------------------------------------
#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点
[root@redis-node1 ~]#redis-cli -a 123456 --cluster create \
    10.0.1.172:6379 \
    10.0.1.173:6379 \
    10.0.1.174:6379 \
    10.0.1.175:6379 \
    10.0.1.176:6379 \
    10.0.1.178:6379 \
    --cluster-replicas 1
    
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.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#观察以上结果,可以看到3组master/slave
Adding replica 10.0.1.176:6379 to 10.0.1.172:6379
Adding replica 10.0.1.178:6379 to 10.0.1.173:6379
Adding replica 10.0.1.175:6379 to 10.0.1.174:6379

#如果节点少于3个会出下面提示错误
[root@node1 ~]#redis-cli -a 123456 --cluster create 10.0.1.172:6379   
10.0.1.180:6379 
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 2 nodes and 0 replicas per node.
*** At least 3 nodes are required.




#此时再用这个命令就开始测试redis集群配置是否成功了
[root@redis ~]# redis-cli -a 123456 cluster help
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 1) CLUSTER <subcommand> [<arg> [value] [opt] ...]. Subcommands are:
 2) ADDSLOTS <slot> [<slot> ...]
 3)     Assign slots to current node.
 4) BUMPEPOCH
 5)     Advance the cluster config epoch.
 6) COUNT-FAILURE-REPORTS <node-id>
 7)     Return number of failure reports for <node-id>.
 8) COUNTKEYSINSLOT <slot>
 9)     Return the number of keys in <slot>.
10) DELSLOTS <slot> [<slot> ...]
11)     Delete slots information from current node.
12) FAILOVER [FORCE|TAKEOVER]
13)     Promote current replica node to being a master.
14) FORGET <node-id>
15)     Remove a node from the cluster.
16) GETKEYSINSLOT <slot> <count>
17)     Return key names stored by current node in a slot.
18) FLUSHSLOTS
19)     Delete current node own slots information.
20) INFO
21)     Return information about the cluster.
22) KEYSLOT <key>
23)     Return the hash slot for <key>.
24) MEET <ip> <port> [<bus-port>]
25)     Connect nodes into a working cluster.
26) MYID
27)     Return the node id.
28) NODES
29)     Return cluster configuration seen by node. Output format:
30)     <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ...
31) REPLICATE <node-id>
32)     Configure current node as replica to <node-id>.
33) RESET [HARD|SOFT]
34)     Reset current node (default: soft).
35) SET-CONFIG-EPOCH <epoch>
36)     Set config epoch of current node.
37) SETSLOT <slot> (IMPORTING|MIGRATING|STABLE|NODE <node-id>)
38)     Set slot state.
39) REPLICAS <node-id>
40)     Return <node-id> replicas.
41) SAVECONFIG
42)     Force saving cluster configuration on disk.
43) SLOTS
44)     Return information about slots range mappings. Each range is made of:
45)     start, end, master and replicas IP addresses, ports and ids
46) HELP
47)     Prints this help.




----------------------------------------------------------------


                       第四步---验证集群

----------------------------------------------------------------

#查看主从状态
[root@redis-node1 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.1.179,port=6379,state=online,offset=938,lag=0
master_failover_state:no-failover
master_replid:33a015f7b91a88a92ec92e2bd0aac11f5f019945
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:938
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:938

#查看指定master节点的slave节点信息
[root@redis ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713544396003 2 connected 5461-10922
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713544395000 3 connected 10923-16383
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713544396000 2 connected
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713544397010 3 connected
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713544394000 1 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713544394000 1 connected 0-5460


#以下命令查看指定master节点的slave节点信息,其中
#a177c5cbc2407ebb6230ea7e2a7de914bf8c2dab 为master节点的ID
[root@centos8 ~]#redis-cli -a 123456 cluster slaves 
a177c5cbc2407ebb6230ea7e2a7de914bf8c2dab
1) "cb20d58870fe05de8462787cf9947239f4bc5629 10.0.0.38:6379@16379 slave 
a177c5cbc2407ebb6230ea7e2a7de914bf8c2dab 0 1602571574844 10 connected"



#验证集群状态
[root@redis ~]# redis-cli -a 123456 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:6       #节点数
cluster_size:3              #集群数
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:392
cluster_stats_messages_pong_sent:411
cluster_stats_messages_sent:803
cluster_stats_messages_ping_received:411
cluster_stats_messages_pong_received:390
cluster_stats_messages_received:801












#查看任意节点的集群状态
[root@redis ~]# redis-cli -a 123456 --cluster info 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.1.172:6379 (3f269538...) -> 0 keys | 5461 slots | 1 slaves.
10.0.1.180:6379 (3006b8fa...) -> 0 keys | 5462 slots | 1 slaves.
10.0.1.183:6379 (a072f6f8...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.






#查看对应关系
[root@redis ~]# redis-cli -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713603065000 3 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713603063000 2 connected 5461-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713603064457 2 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713603066471 3 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713603065464 1 connected









----------------------------------------------------------------

                  第五步---测试集群写入数据


----------------------------------------------------------------


#redis cluster 写入key
#经过算法计算,当前key的槽位需要写入指定的node
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.1.172 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
(error) MOVED 9189 10.0.1.172:6379    #槽位不在当前node所以无法写入


#指定槽位对应node可写入
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.1.172 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
OK
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.1.172 GET key1
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
"values1"



#可开启集群模式,无论你在那台机器上执行,都会写入到对应的机器上去
[root@redis ~]# redis-cli -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set nwq 774
-> Redirected to slot [16146] located at 10.0.1.183:6379
OK





# redis cluster 计算key所属的slot
[root@redis ~]# redis-cli -a 123456 -h 10.0.1.172 --no-auth-warning cluster nodes 
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713604133565 3 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713604130547 2 connected 5461-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713604132000 2 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713604132561 3 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713604134571 1 connected


#计算得到hello对应的slot
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning cluster 
keyslot hello
(integer) 866
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning set hello 
magedu
OK
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning cluster 
keyslot name 
(integer) 5798
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning set name 
wang
(error) MOVED 5798 10.0.1.172:6379
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning set name 
wang
OK
[root@centos8 ~]#redis-cli -h 10.0.1.172 -a 123456 --no-auth-warning get name
"wang"


#使用选项-c 以集群模式连接
[root@centos8 ~]#redis-cli -c -h 10.0.1.172 -a 123456 --no-auth-warning 
10.0.1.183:6379> cluster keyslot nwq
(integer) 16146
10.0.1.183:6379> set linux love
OK
10.0.1.183:6379> get linux
"love"
10.0.1.183:6379> exit

[root@centos8 ~]#redis-cli -h 10.0.0.28 -a 123456 --no-auth-warning get linux
"love"




总结:这里就已经打好集群了,三主 三从   主负责写  从负责读




















---------------------------------------------------------------------------------------


                                第六步--测试集群(故障转移)

---------------------------------------------------------------------------------------
[root@redis ~]# cat /apps/redis/data/nodes-6379.conf 
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713543647145 2 connected 5461-10922
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713543646000 3 connected 10923-16383
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713543648152 2 connected
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713543649160 3 connected
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713543646138 1 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713543648000 1 connected 0-5460
vars currentEpoch 6 lastVoteEpoch 0




#断掉10.0.1.180主节点redis服务
[root@redis ~]# systemctl stop redis.service


#新集群日志状态
[root@redis ~]# cat /apps/redis/data/nodes-6379.conf 
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713604789000 3 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master,fail - 1713604774712 1713604769669 2 disconnected
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 master - 0 1713604789873 7 connected 5461-10922
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713604790883 3 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713604788863 1 connected
vars currentEpoch 7 lastVoteEpoch 7


#新的状态主从状态
[root@redis ~]# redis-cli -a 123456 --cluster info 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 10.0.1.180:6379: Connection refused
10.0.1.172:6379 (3f269538...) -> 1 keys | 5461 slots | 1 slaves.
10.0.1.179:6379 (db69383e...) -> 1 keys | 5462 slots | 0 slaves.
10.0.1.183:6379 (a072f6f8...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.



#各集群状态
[root@redis ~]# redis-cli -a 123456 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:6
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2499
cluster_stats_messages_pong_sent:2559
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:5059
cluster_stats_messages_ping_received:2559
cluster_stats_messages_pong_received:2497
cluster_stats_messages_fail_received:1
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:5058


#恢复10.0.1.180redis状态--会自动变为从节点
[root@redis ~]# systemctl start redis.service 
[root@redis ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.1.179
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:3249
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4e8970f78800d1f836ee8c6b381c0549e5c047dc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3249
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3208
repl_backlog_histlen:42


#此时各节点对应关系
10.0.1.175:6379 -> 10.0.1.183:6379
10.0.1.180:6379 -> 10.0.1.179:6379
10.0.1.177:6379 -> 10.0.1.172:6379




#如何恢复原来的10.0.1.180的集群对应关系
#在10.0.1.180对应的机器上-10.0.1.179 上执行shutdown
[root@redis ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> SHUTDOWN

#此时在重启10.0.1.179的redis服务
[root@redis ~]# systemctl restart redis.service 
[root@redis ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.1.180
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:3697
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:321281d7da2cb1399f2de778d7bd8ab1149c813f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3697
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3684
repl_backlog_histlen:14





#python脚本实现RedisCluster集群写入
[root@redis-node1 ~]#yum -y install python3
[root@redis-node1 ~]#pip3 install redis-py-cluster
[root@redis-node1 ~]#vim redis_cluster_test.py
[root@redis-node1 ~]#cat ./redis_cluster_test.py
#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
   {"host":"10.0.0.7", "port":6379},
   {"host":"10.0.0.7", "port":6380},
   {"host":"10.0.0.17", "port":6379},
   {"host":"10.0.0.17", "port":6380},
   {"host":"10.0.0.27", "port":6379},
   {"host":"10.0.0.27", "port":6380}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', 
decode_responses=True)
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)))
[root@redis-node1 ~]#chmod +x redis_cluster_test.py
[root@redis-node1 ~]#./redis_cluster_test.py
......
key9998: value9998
key9999: value9999


#验证脚本写入的状态
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.7 DBSIZE
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
(integer) 3331
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.17 DBSIZE
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
(integer) 3340
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.27 DBSIZE
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
(integer) 3329
[root@redis-node1 ~]#redis-cli -a 123456 GET key1
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
(error) MOVED 9189 10.0.0.17:6379
[root@redis-node1 ~]#redis-cli -a 123456 GET key2
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
"value2"
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.17 GET key1
Warning: Using a password with '-a' option on the command line interface may not 
be safe.
"value1"
[root@redis-node1 ~]#redis-trib.rb info 10.0.0.7:6379
10.0.0.7:6379 (739cb4c9...) -> 3331 keys | 5461 slots | 1 slaves.
10.0.0.27:6379 (a01fd3d8...) -> 3329 keys | 5461 slots | 1 slaves.
10.0.0.17:6379 (dddabb4e...) -> 3340 keys | 5462 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
[root@redis-node1 ~]#






总结:集群中从节点不提供读写,只是提供一个存放redis键值对的位置

















---------------------------------------------------------------------------------------

                                 第七步--集群扩容


---------------------------------------------------------------------------------------

新增机器:
10.0.1.181
10.0.1.182


#扩容适用场景:
当前客户量激增,现有的Redis cluster架构已经无法满足越来越高的并发访问请求,为解决此问题,新购
置两台服务器,要求将其动态添加到现有集群,但不能影响业务的正常访问。
注意: 生产环境一般建议master节点为奇数个,比如:3,5,7,以防止脑裂现象


#步骤

#添加节点准备
增加Redis 新节点,需要与之前的Redis node版本和配置一致,然后分别再启动两台Redis node,应为
一主一从。

#配置node7节点
[root@redis-node7 ~]#dnf -y install redis
[root@redis-node7 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf

#编译安装执行下面操作
[root@redis-node7 ~]#sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf;systemctl restart redis
[root@redis-node7 ~]#systemctl enable --now redis

#配置node8节点
[root@redis-node8 ~]#dnf -y install redis
[root@redis-node8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf


#编译安装执行下面操作
[root@redis-node8 ~]#sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf;systemctl restart redis
[root@redis-node8 ~]#systemctl enable --now redis



#添加新的master节点到集群
使用以下命令添加新节点,要添加的新redis节点IP和端口添加到的已有的集群中任意节点的IP:端口
add-node new_host:new_port existing_host:existing_port [--slave --master-id
<arg>]
#说明:
new_host:new_port #指定新添加的主机的IP和端口
existing_host:existing_port #指定已有的集群中任意节点的IP和端口


#Redis 3/4 版本的添加命令:
#把新的Redis 节点10.0.0.37添加到当前Redis集群当中。
[root@redis-node1 ~]#redis-trib.rb add-node 10.0.0.37:6379 10.0.0.7:6379
[root@redis-node1 ~]#redis-trib.rb info 10.0.0.7:6379
10.0.0.7:6379 (29a83275...) -> 3331 keys | 5461 slots | 1 slaves.
10.0.0.37:6379 (12ca273a...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.27:6379 (90b20613...) -> 3329 keys | 5461 slots | 1 slaves.
10.0.0.17:6379 (fb34c3a7...) -> 3340 keys | 5462 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.



#Redis 5 以上版本的添加命令:
#将一台新的主机10.0.1.181加入集群,以下示例中10.0.0.58可以是任意存在的集群节点
[root@redis ~]# redis-cli -a 123456 --cluster add-node 10.0.1.181:6379 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 10.0.1.181:6379 to cluster 10.0.1.172:6379
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[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.1.181:6379 to make it join the cluster.
[OK] New node added correctly.

#观察到该节点已经加入成功,但此节点上没有slot位,也无从节点,而且新的节点是master
[root@redis ~]# redis-cli -a 123456 --cluster info 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.1.172:6379 (3f269538...) -> 1 keys | 5461 slots | 1 slaves.
10.0.1.181:6379 (44461edd...) -> 0 keys | 0 slots | 0 slaves.
10.0.1.180:6379 (3006b8fa...) -> 1 keys | 5462 slots | 1 slaves.
10.0.1.183:6379 (a072f6f8...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.


[root@redis ~]# redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.1.172:6379 (3f269538...) -> 1 keys | 5461 slots | 1 slaves.
10.0.1.181:6379 (44461edd...) -> 0 keys | 0 slots | 0 slaves.
10.0.1.180:6379 (3006b8fa...) -> 1 keys | 5462 slots | 1 slaves.
10.0.1.183:6379 (a072f6f8...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
M: 44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379
   slots: (0 slots) master
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


[root@redis ~]# cat /apps/redis/data/nodes-6379.conf 
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713607060000 3 connected
44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379@16379 master - 0 1713607061018 0 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713607058000 8 connected 5461-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713607059808 8 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713607060814 3 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713607058800 1 connected
vars currentEpoch 8 lastVoteEpoch 8

#和上面显示结果一样
[root@redis ~]# redis-cli -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713607259120 3 connected
44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379@16379 master - 0 1713607256100 0 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713607260127 8 connected 5461-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713607258000 8 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713607257107 3 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713607258114 1 connected


#查看集群状态
[root@redis ~]# redis-cli -a 123456 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:8
cluster_my_epoch:1
cluster_stats_messages_ping_sent:4761
cluster_stats_messages_pong_sent:4897
cluster_stats_messages_auth-ack_sent:2
cluster_stats_messages_sent:9660
cluster_stats_messages_ping_received:4896
cluster_stats_messages_pong_received:4759
cluster_stats_messages_meet_received:1
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_received:9660




#在新的master上重新分配槽位
新的node节点加到集群之后,默认是master节点,但是没有slots,需要重新分配,否则没有槽位将无法访问
注意: 重新分配槽位需要清空数据,所以需要先备份数据,扩展后再恢复数据


#Redis 3/4 版本命令:
[root@redis-node1 ~]# redis-trib.rb check 10.0.0.67:6379 #当前状态
[root@redis-node1 ~]# redis-trib.rb reshard <任意节点>:6379 #重新分片
[root@redis-node1 ~]# redis-trib.rb fix 10.0.0.67:6379 #如果迁移失败使用此命令修复集群


#Redis 5以上版本命令:
[root@redis-node1 ~]#redis-cli -a 123456 --cluster reshard <当前任意集群节点>: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.1.181:6379)
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.1.181:6379
   slots: (0 slots) master
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.1.172:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
M: f67f1c02c742cd48d3f48d8c362f9f1b9aa31549 10.0.1.182:6379
   slots: (0 slots) master
S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
   slots: (0 slots) slave
   replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
[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? d6e2eca6b338b717923f64866bd31d42e52edc98 #新的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选择划分,如果是从redis cluster删除某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机
......
Do you want to proceed with the proposed reshard plan (yes/no)?  yes #确认分配
......
Moving slot 12280 from 10.0.0.28:6379 to 10.0.1.181:6379: .
Moving slot 12281 from 10.0.0.28:6379 to 10.0.1.181:6379: .
Moving slot 12282 from 10.0.0.28:6379 to 10.0.1.181:6379: 
Moving slot 12283 from 10.0.0.28:6379 to 10.0.1.181:6379: ..
Moving slot 12284 from 10.0.0.28:6379 to 10.0.1.181:6379: 
Moving slot 12285 from 10.0.0.28:6379 to 10.0.1.181:6379: .
Moving slot 12286 from 10.0.0.28:6379 to 10.0.1.181:6379: 
Moving slot 12287 from 10.0.0.28:6379 to 10.0.1.181:6379: ..
[root@redis-node1 ~]#
#确定slot分配成功
[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.1.172:6379 (cb028b83...) -> 5019 keys | 4096 slots | 1 slaves.
10.0.1.181:6379 (d6e2eca6...) -> 4948 keys | 4096 slots | 0 slaves.
10.0.1.180:6379 (d04e524d...) -> 5033 keys | 4096 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 5000 keys | 4096 slots | 1 slaves.
[OK] 20000 keys in 5 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.1.172:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.1.181:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master #可看到4096个slots
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
   slots: (0 slots) slave
   replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.



# 为新的master指定新的slave节点
当前Redis集群中新的master节点存单点问题,还需要给其添加一个对应slave节点,实现高可用功能
有两种方式:
1.在新加节点到集群时,直接将之设置为slave
#Redis 3/4 添加命令:
redis-trib.rb   add-node --slave --master-id 
750cab050bc81f2655ed53900fd43d2e64423333 10.0.0.77:6379 <任意集群节点>:6379


#Redis 5 以上版本添加命令:
redis-cli -a 123456 --cluster add-node 10.0.1.182:6379 <任意集群节点>:6379 --
cluster-slave --cluster-master-id d6e2eca6b338b717923f64866bd31d42e52edc98


范例:
#查看当前状态
[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.1.172:6379 (cb028b83...) -> 5019 keys | 4096 slots | 1 slaves.
10.0.1.181:6379 (d6e2eca6...) -> 4948 keys | 4096 slots | 0 slaves.
10.0.1.180:6379 (d04e524d...) -> 5033 keys | 4096 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 5000 keys | 4096 slots | 1 slaves.
[OK] 20000 keys in 4 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.1.172:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.1.181:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
   slots: (0 slots) slave
   replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#直接加为slave节点
[root@redis-node1 ~]#redis-cli -a 123456 --cluster add-node 10.0.1.182:6379 10.0.1.172:6379 --cluster-slave --cluster-master-id 44461eddf80d1f5c45eb82d15c6f157fca0890c3

#验证是否成功
[root@redis ~]# redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.1.172:6379 (3f269538...) -> 1 keys | 4096 slots | 1 slaves.
10.0.1.181:6379 (44461edd...) -> 0 keys | 4096 slots | 1 slaves.
10.0.1.180:6379 (3006b8fa...) -> 1 keys | 4096 slots | 1 slaves.
10.0.1.183:6379 (a072f6f8...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
M: 44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
S: 5d509fabf27efeb4c504aa51f0d865a22701c0db 10.0.1.182:6379
   slots: (0 slots) slave
   replicates 44461eddf80d1f5c45eb82d15c6f157fca0890c3
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@centos8 ~]#redis-cli -a 123456 -h 10.0.1.172 --no-auth-warning cluster info
[root@redis ~]# redis-cli -a 123456 -h 10.0.1.172 --no-auth-warning cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:8    #八个节点
cluster_size:4           #四组主从
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:6174
cluster_stats_messages_pong_sent:6404
cluster_stats_messages_auth-ack_sent:2
cluster_stats_messages_sent:12580
cluster_stats_messages_ping_received:6402
cluster_stats_messages_pong_received:10268
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_received:16676

tips:正常这时候这里扩容就结束了




2.先将新节点加入集群,再修改为slave
为新的master添加slave节点
#Redis 3/4 版本命令:
[root@redis-node1 ~]#redis-trib.rb add-node 10.0.1.182:6379 10.0.1.172:6379


#Redis 5 以上版本命令:
#把10.0.1.182:6379添加到集群中:
[root@redis-node1 ~]#redis-cli -a 123456 --cluster add-node 10.0.1.182:6379 
10.0.1.172:6379


#更改新节点更改状态为slave:
需要手动将其指定为某个master的slave,否则其默认角色为master。
[root@redis-node1 ~]#redis-cli -h 10.0.1.182 -p 6379 -a 123456 #登录到新添加节点
10.0.1.182:6380> CLUSTER NODES #查看当前集群节点,找到目标master 的ID
10.0.1.182:6380> CLUSTER REPLICATE 886338acd50c3015be68a760502b239f4509881c #将其设
置slave,命令格式为cluster replicate MASTERID
10.0.1.182:6380> CLUSTER NODES #再次查看集群节点状态,验证节点是否已经更改为指定master 的
slave























---------------------------------------------------------------------------------------

                                         第八步--集群缩减



---------------------------------------------------------------------------------------

缩容适用场景:
随着业务萎缩用户量下降明显,和领导商量决定将现有Redis集群的8台主机中下线两台主机挪做它用,缩容后性能仍能满足当前业务需求

删除节点过程:
扩容时是先添加node到集群,然后再分配槽位,而缩容时的操作相反,是先将被要删除的node上的槽位迁移到集群中的其他node上,然后 才能再将其从集群中删除,如果一个node上的槽位没有被完全迁移空,删除该node时也会提示有数据出错导致无法删除。


#步骤

删除二个节点:10.0.1.181 10.0.1.182




#迁移要删除的master节点上面的槽位到其它master
注意: 被迁移Redis master源服务器必须保证没有数据,否则迁移报错并会被强制中断。
Redis 3/4 版本命令
[root@redis-node1 ~]# redis-trib.rb reshard 10.0.1.172:6379
[root@redis-node1 ~]# redis-trib.rb fix 10.0.1.172:6379 #如果迁移失败使用此命令修复集群


Redis 5版本以上命令
#查看当前状态
[root@redis ~]# redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.1.172:6379 (3f269538...) -> 1 keys | 4096 slots | 1 slaves.
10.0.1.181:6379 (44461edd...) -> 0 keys | 4096 slots | 1 slaves.
10.0.1.180:6379 (3006b8fa...) -> 1 keys | 4096 slots | 1 slaves.
10.0.1.183:6379 (a072f6f8...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
M: 44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
S: 5d509fabf27efeb4c504aa51f0d865a22701c0db 10.0.1.182:6379
   slots: (0 slots) slave
   replicates 44461eddf80d1f5c45eb82d15c6f157fca0890c3
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


#查看删除节点的槽位范围
[root@redis ~]# cat /apps/redis/data/nodes-6379.conf 
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713608542000 3 connected
44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379@16379 master - 0 1713608542595 9 connected 0-1364 5461-6826 10923-12287          #要移除节点的槽位范围
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 1 connected 1365-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713608543602 8 connected 6827-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713608543000 8 connected
5d509fabf27efeb4c504aa51f0d865a22701c0db 10.0.1.182:6379@16379 slave 44461eddf80d1f5c45eb82d15c6f157fca0890c3 0 1713608544610 9 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713608542000 3 connected 12288-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713608538000 1 connected
vars currentEpoch 9 lastVoteEpoch 8



#连接到任意集群节点,#最后1365个slot从10.0.1.172移动到第一个master节点10.0.0.28上
[root@redis ~]# redis-cli -a 123456 --cluster reshard 10.0.1.172: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.1.172:6379)
M: 3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379
   slots: (0 slots) slave
   replicates a072f6f813c72801458d775a34af3649d25b06a2
M: 44461eddf80d1f5c45eb82d15c6f157fca0890c3 10.0.1.181:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379
   slots: (0 slots) slave
   replicates 3006b8fa7bb117d6e58625e04710f5727e465c43
S: 5d509fabf27efeb4c504aa51f0d865a22701c0db 10.0.1.182:6379
   slots: (0 slots) slave
   replicates 44461eddf80d1f5c45eb82d15c6f157fca0890c3
M: a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379
   slots: (0 slots) slave
   replicates 3f2695389ab756b43ab0b87d891becc14c8974ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1365   #共4096/3分别给其它三个master节点

What is the receiving node ID? 3f2695389ab756b43ab0b87d891becc14c8974ee  #正常你填写其他留下的master节点id都可以,填之前你和谁获取的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: 44461eddf80d1f5c45eb82d15c6f157fca0890c3        #填写移除的master节点id
Source node #2: done                                            #只有一个节点要删除,所以done执行

Ready to move 1356 slots.
 Source nodes:
   M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.1.172:6379
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
 Destination node:
   M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
 Resharding plan:
   Moving slot 1365 from cb028b83f9dc463d732f6e76ca6bbcd469d948a7
......
 Moving slot 2719 from cb028b83f9dc463d732f6e76ca6bbcd469d948a7
   Moving slot 2720 from cb028b83f9dc463d732f6e76ca6bbcd469d948a7
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确定
......
Moving slot 2718 from 10.0.1.172:6379 to 10.0.0.28:6379: ..
Moving slot 2719 from 10.0.1.172:6379 to 10.0.0.28:6379: .
Moving slot 2720 from 10.0.1.172:6379 to 10.0.0.28:6379: ..



#非交互式方式
#再将1365个slot从10.0.1.172移动到第二个master节点10.0.1.180上
[root@redis-node1 ~]#redis-cli -a 123456 --cluster reshard 10.0.1.172:6379 --cluster-slots 1365 --cluster-from cb028b83f9dc463d732f6e76ca6bbcd469d948a7 --cluster-to d04e524daec4d8e22bdada7f21a9487c2d3e1057 --cluster-yes

#最后的slot从10.0.1.172移动到第三个master节点10.0.0.68上
[root@redis-node1 ~]#redis-cli -a 123456 --cluster reshard 10.0.1.172:6379 --cluster-slots 1375 --cluster-from cb028b83f9dc463d732f6e76ca6bbcd469d948a7 --cluster-to d6e2eca6b338b717923f64866bd31d42e52edc98 --cluster-yes


#确认10.0.1.172的所有slot都移走了,上面的slave也自动删除,成为其它master的slave 
[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.1.172:6379
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.1.172:6379 (cb028b83...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.68:6379 (d6e2eca6...) -> 6631 keys | 5471 slots | 2 slaves.
10.0.1.180:6379 (d04e524d...) -> 6694 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 6675 keys | 5452 slots | 1 slaves.
[OK] 20000 keys in 4 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.1.172:6379
   slots: (0 slots) master
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.0.68:6379
   slots:[0-1364],[4086-6826],[10923-12287] (5471 slots) master
   2 additional replica(s)
S: 36840d7eea5835ba540d9b64ec018aa3f8de6747 10.0.0.78:6379
   slots: (0 slots) slave
   replicates d6e2eca6b338b717923f64866bd31d42e52edc98
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
   slots: (0 slots) slave
   replicates d6e2eca6b338b717923f64866bd31d42e52edc98
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[2721-4085],[6827-10922] (5461 slots) master
   1 additional replica(s)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[1365-2720],[12288-16383] (5452 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.

#原有的10.0.0.38自动成为10.0.0.68的slave
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.68 INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.78,port=6379,state=online,offset=129390,lag=0
slave1:ip=10.0.0.38,port=6379,state=online,offset=129390,lag=0
master_replid:43e3e107a0acb1fd5a97240fc4b2bd8fc85b113f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:129404
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:129404
[root@centos8 ~]#redis-cli -a 123456 -h 10.0.1.172 --no-auth-warning cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:8  #集群中8个节点
cluster_size:3         #少了一个主从的slot
cluster_current_epoch:16
cluster_my_epoch:13
cluster_stats_messages_ping_sent:3165
cluster_stats_messages_pong_sent:2489
cluster_stats_messages_fail_sent:6
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_update_sent:27
cluster_stats_messages_sent:5693
cluster_stats_messages_ping_received:2483
cluster_stats_messages_pong_received:2400
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_update_received:4
cluster_stats_messages_received:4894




#从集群中删除服务器
上面步骤完成后,槽位已经迁移走,但是节点仍然还属于集群成员,因此还需从集群删除该节点
注意: 删除服务器前,必须清除主机上面的槽位,否则会删除主机失败
#Redis 3/4命令:
[root@s~]#redis-trib.rb del-node <任意集群节点的IP>:6379 
dfffc371085859f2858730e1f350e9167e287073
#dfffc371085859f2858730e1f350e9167e287073 是删除节点的ID
>>> Removing node dfffc371085859f2858730e1f350e9167e287073 from cluster
192.168.7.102:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.



#Redis 5以上版本命令:
[root@redis-node1 ~]#redis-cli -a 123456 --cluster del-node <任意集群节点的IP>:6379 cb028b83f9dc463d732f6e76ca6bbcd469d948a7

[root@redis ~]# redis-cli -a 123456 --cluster del-node 10.0.1.172:6379 44461eddf80d1f5c45eb82d15c6f157fca0890c3
#44461eddf80d1f5c45eb82d15c6f157fca0890c3是删除节点的ID

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 44461eddf80d1f5c45eb82d15c6f157fca0890c3 from cluster 10.0.1.172:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.


#删除第二个节点
[root@redis ~]# redis-cli -a 123456 --cluster del-node 10.0.1.172:6379 5d509fabf27efeb4c504aa51f0d865a22701c0db
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 5d509fabf27efeb4c504aa51f0d865a22701c0db from cluster 10.0.1.172:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

[root@redis ~]# cat /apps/redis/data/nodes-6379.conf 
8178bf9cd79c67bb3a0a1d6d711bcdde06e9043d 10.0.1.175:6379@16379 slave a072f6f813c72801458d775a34af3649d25b06a2 0 1713610663620 12 connected
3f2695389ab756b43ab0b87d891becc14c8974ee 10.0.1.172:6379@16379 myself,master - 0 1713631507000 10 connected 0-5460
3006b8fa7bb117d6e58625e04710f5727e465c43 10.0.1.180:6379@16379 master - 0 1713610664627 11 connected 5461-10922
db69383e70918ceaa6459de71fc3c6a8c081a4bc 10.0.1.179:6379@16379 slave 3006b8fa7bb117d6e58625e04710f5727e465c43 0 1713610661606 11 connected
a072f6f813c72801458d775a34af3649d25b06a2 10.0.1.183:6379@16379 master - 0 1713610661505 12 connected 10923-16383
b0481c6ef1f2a04aafccd6a9c32d9e0d02f822dc 10.0.1.177:6379@16379 slave 3f2695389ab756b43ab0b87d891becc14c8974ee 0 1713610662613 10 connected
vars currentEpoch 12 lastVoteEpoch 8


#删除节点后,redis进程自动关闭
#删除节点信息
[root@redis-node1 ~]#rm -f /var/lib/redis/nodes-6379.conf



#删除多余的slave节点验证结果
#验证删除成功
[root@redis-node1 ~]#ss -ntl
State       Recv-Q       Send-Q   Local Address:Port     Peer Address:Port     
   
LISTEN       0             128            0.0.0.0:22             0.0.0.0:*       
    
LISTEN       0             128               [::]:22               [::]:*  

[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.1.172:6379 
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.0.68:6379 (d6e2eca6...) -> 6631 keys | 5471 slots | 2 slaves.
10.0.1.180:6379 (d04e524d...) -> 6694 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 6675 keys | 5452 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
   slots: (0 slots) slave
   replicates d6e2eca6b338b717923f64866bd31d42e52edc98
S: 36840d7eea5835ba540d9b64ec018aa3f8de6747 10.0.0.78:6379
   slots: (0 slots) slave
   replicates d6e2eca6b338b717923f64866bd31d42e52edc98
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.0.68:6379
   slots:[0-1364],[4086-6826],[10923-12287] (5471 slots) master
   2 additional replica(s)
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[2721-4085],[6827-10922] (5461 slots) master
   1 additional replica(s)
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[1365-2720],[12288-16383] (5452 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.


#删除多余的slave从节点
[root@redis-node1 ~]#redis-cli -a 123456 --cluster del-node 10.0.1.172:6379 
f9adcfb8f5a037b257af35fa548a26ffbadc852d
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
>>> Removing node f9adcfb8f5a037b257af35fa548a26ffbadc852d from cluster 
10.0.1.172:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

#删除集群文件
[root@redis-node4 ~]#rm -f /var/lib/redis/nodes-6379.conf 
[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.1.172:6379 
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.0.68:6379 (d6e2eca6...) -> 6631 keys | 5471 slots | 1 slaves.
10.0.1.180:6379 (d04e524d...) -> 6694 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 6675 keys | 5452 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.1.172:6379)
S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.1.172:6379
   slots: (0 slots) slave
   replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057
S: 36840d7eea5835ba540d9b64ec018aa3f8de6747 10.0.0.78:6379
   slots: (0 slots) slave
   replicates d6e2eca6b338b717923f64866bd31d42e52edc98
M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.0.68:6379
 slots:[0-1364],[4086-6826],[10923-12287] (5471 slots) master
   1 additional replica(s)
S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
   slots: (0 slots) slave
   replicates d34da8666a6f587283a1c2fca5d13691407f9462
M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.1.180:6379
   slots:[2721-4085],[6827-10922] (5461 slots) master
   1 additional replica(s)
M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
   slots:[1365-2720],[12288-16383] (5452 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.
[root@redis-node1 ~]#redis-cli -a 123456 --cluster info 10.0.1.172:6379 
Warning: Using a password with '-a' or '-u' option on the command line interface 
may not be safe.
10.0.0.68:6379 (d6e2eca6...) -> 6631 keys | 5471 slots | 1 slaves.
10.0.1.180:6379 (d04e524d...) -> 6694 keys | 5461 slots | 1 slaves.
10.0.0.28:6379 (d34da866...) -> 6675 keys | 5452 slots | 1 slaves.
[OK] 20000 keys in 3 masters.
1.22 keys per slot on average.

#查看集群信息
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.1.172 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:6  #只有6个节点
cluster_size:3
cluster_current_epoch:11
cluster_my_epoch:10
cluster_stats_messages_ping_sent:12147
cluster_stats_messages_pong_sent:12274
cluster_stats_messages_update_sent:14
cluster_stats_messages_sent:24435
cluster_stats_messages_ping_received:12271
cluster_stats_messages_pong_received:12147
cluster_stats_messages_meet_received:3
cluster_stats_messages_update_received:28
cluster_stats_messages_received:24449
4.整理常用redis命令
Redis是一个流行的开源、高性能、键值存储系统,支持多种数据结构。以下列举了50条Redis常用的命令(注:这里不包括全部Redis命令,仅展示一部分核心命令):

1. 键(Key)命令:
   - DEL key:删除指定的key。
   - EXISTS key:检查key是否存在。
   - GET key:获取key对应的值。
   - SET key value:设置key的值。
   - MGET key1 key2 ...:批量获取多个key的值。
   - KEYS pattern:查找所有符合给定模式的key。
   - RANDOMKEY:随机返回一个key。
   - RENAME oldkey newkey:重命名key。
   - TTL key:获取key的剩余生存时间。
   - PERSIST key:移除key的生存时间。

2. String(字符串)命令:
   - APPEND key value:追加值到key的末尾。
   - INCR key:递增数字类型的key值。
   - DECR key:递减数字类型的key值。
   - STRLEN key:返回key的值的长度。
   - SETEX key seconds value:设置带有过期时间的string值。

3. Hash(哈希)命令:
   - HSET key field value:向哈希表中设置字段field的值。
   - HGET key field:获取哈希表中field的值。
   - HGETALL key:获取哈希表中所有字段及其值。
   - HDEL key field1 field2 ...:删除哈希表中的一个或多个字段。

4. List(列表)命令:
   - LPUSH key value:将值插入列表头部。
   - RPUSH key value:将值插入列表尾部。
   - LPOP key:从列表头部移除并返回元素。
   - RPOP key:从列表尾部移除并返回元素。
   - LRANGE key start stop:获取列表指定范围内的元素。

5. Set(集合)命令:
   - SADD key member:将成员添加到集合中。
   - SMEMBERS key:返回集合中的所有成员。
   - SISMEMBER key member:判断成员是否存在于集合中。
   - SREM key member:从集合中移除指定成员。

6. Sorted Set(有序集合)命令:
   - ZADD key score member:向有序集合添加一个成员,按score排序。
   - ZRANGE key start stop [WITHSCORES]:返回有序集合中指定范围内的成员及其分数。
   - ZREM key member:从有序集合中删除指定成员。

7. 事务处理:
   - MULTI:开启事务。
   - EXEC:执行事务块内的命令序列。
   - DISCARD:取消事务,放弃执行事务块内的命令。

8. Pub/Sub(发布订阅):
   - PUBLISH channel message:向指定频道发布消息。
   - SUBSCRIBE channel [channel ...]:订阅指定频道接收消息。
   - UNSUBSCRIBE [channel [channel ...]]:退订指定频道。

9. 哨兵(Sentinel)相关命令:
   - SENTINEL MONITOR master-name ip port quorum:监控一个主节点。
   - SENTINEL FAILOVER master-name:触发主节点的故障转移。

10. 集群管理:
    - CLUSTER SLOTS:获取集群的slot分配情况。
    - CLUSTER MEET ip port:将一个节点加入到集群中。

11. 其他命令:
    - PING:测试服务器响应。
    - INFO:获取服务器信息。
    - CONFIG GET/SET parameter:获取或设置服务器配置。
    - SAVE:同步保存数据到硬盘。
    - BGSAVE:后台异步保存数据到硬盘。
    - LASTSAVE:返回上次成功保存到磁盘的时间。
    - FLUSHDB:清空当前数据库的所有key。
    - FLUSHALL:清空所有数据库的所有key。
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值