redis作业
1.部署一个redis 给开发使用
redis源码包官方下载:http://download.redis.io/releases/
------------------------------------------------------------------------------------
yum安装redis
------------------------------------------------------------------------------------
[root@centos7 ~]
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.
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
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 ~]
|-redis-server(3383)-+-{redis-server}(3384)
| |-{redis-server}(3385)
| -{redis-server}(3386)
[root@centos7 ~]
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
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
------------------------------------------------------------------------------------
一.源码编译
[root@centos8~]
[root@centos8~]
[root@ubuntu2004 ~]
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
[root@centos8 redis-6.2.4]
[root@centos8 redis-6.2.4]
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
/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 ~]
[root@centos8 redis-6.2.4]
二.创建redis用户和设置数据目录权限
[root@centos7 ~]
[root@centos7 ~]
三.创建redis服务service文件
[root@centos7 ~]
/lib/systemd/system/
[root@centos7 ~]
[root@centos7 ~]
[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
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
[root@centos7 ~]
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
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
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
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
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
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
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
used_cpu_sys:0.132821
used_cpu_user:0.124317
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
cluster_enabled:0
127.0.0.1:6379> exit
[root@centos7 ~]
[root@redis etc]
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
------------------------------------------------------------------------------------
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
protected-mode yes
问127.0.0.1:6379,可以远程连接,但当访问将提示警告信息并拒绝远程访问
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/path/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slaveof
replica-serve-stale-data yes
1、设置为yes(默认设置),从库会继续响应客户端的读请求,此为建议值
2、设置为no,除去特定命令外的任何请求都会返回一个错误"SYNC with master in progress"。
replica-read-only yes
repl-diskless-sync no
1、基于硬盘(disk-backed):为no时,master创建一个新进程dump生成RDB磁盘文件,RDB完成之后由
父进程(即主进程)将RDB文件发送给slaves,此为默认值
2、基于socket(diskless):master创建一个新进程直接dump RDB至slave的网络socket,不经过主
进程和硬盘
repl-diskless-sync-delay 5
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 512mb
repl-backlog-ttl 3600
replica-priority 100
requirepass foobared
rename-command
maxclients 10000
maxmemory <bytes>
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 no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-replica-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
cluster-replica-no-failover no
slowlog-log-slower-than 10000
slowlog-max-len 128
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 ~]
[root@centos7 ~]
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
[root@redis-master ~]
Warning: Using a password with '-a' or '-u' option on the command line interface
may not
127.0.0.1:6379> INFO 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>
[root@redis-slave1 ~]
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
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
[root@redis-slave2 ~]
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
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节点使用相同的以下示例的配置文件
如:/apps/redis/etc/sentinel.conf
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
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
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
master的负载压力
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
logfile /var/log/redis/sentinel.log
[root@redis-master ~]
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
sentinel myid 50547f34ed71fd48c197924969937e738a39975b
.....
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 ~]
[root@redis-master ~]
tips:将所有哨兵服务器都启动起来
[root@redis-slave1 ~]
sentinel myid 50547f34ed71fd48c197924969937e738a39975c
[root@redis-slave2 ~]
sentinel myid 50547f34ed71fd48c197924969937e738a39975d
[root@redis-master ~]
[root@redis-slave1 ~]
[root@redis-slave2 ~]
[root@redis-master ~]
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 ~]
[root@redis-master ~]
[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 ~]
[root@redis ~]
[root@redis-master ~]
[root@redis ~]
[root@redis-master ~]
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 ~]
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
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
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
[root@redis-slave1 ~]
[root@redis etc]
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
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
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:48:20.481
13975:X 19 Apr 2024 20:48:55.838
[root@redis ~]
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
44731:X 19 Apr 2024 20:46:57.703
44731:X 19 Apr 2024 20:46:57.703
44731:X 19 Apr 2024 20:46:57.703
44731:X 19 Apr 2024 20:47:00.720
44731:X 19 Apr 2024 20:47:53.997
44731:X 19 Apr 2024 20:48:20.565
44731:X 19 Apr 2024 20:48:55.969
在sentinel状态中尤其是最后一行,涉及到masterIP是多少,有几个slave,有几个sentinels,必须是符
合全部服务器数量
[root@redis-master ~]
127.0.0.1:26379> info 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
[root@redis-slave1 ~]
127.0.0.1:26379> INFO 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
[root@redis-slave2 ~]
127.0.0.1:26379> INFO 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 节点
[root@redis-master ~]
[root@redis-master ~]
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_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
[root@redis ~]
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
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
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 20:48:55.358
2470:X 19 Apr 2024 21:04:13.132
2470:X 19 Apr 2024 21:04:13.198
2470:X 19 Apr 2024 21:04:13.199
2470:X 19 Apr 2024 21:04:14.218
2470:X 19 Apr 2024 21:04:14.218
2470:X 19 Apr 2024 21:04:14.341
2470:X 19 Apr 2024 21:04:14.341
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
[root@redis etc]
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
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
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:48:20.481
13975:X 19 Apr 2024 20:48:55.838
13975:X 19 Apr 2024 21:04:13.162
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.217
13975:X 19 Apr 2024 21:04:13.220
13975:X 19 Apr 2024 21:04:13.222
13975:X 19 Apr 2024 21:04:13.301
13975:X 19 Apr 2024 21:04:13.301
13975:X 19 Apr 2024 21:04:13.373
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
13975:X 19 Apr 2024 21:04:14.307
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
13975:X 19 Apr 2024 21:04:15.396
13975:X 19 Apr 2024 21:04:15.396
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
127.0.0.1:6379> info 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
[root@redis etc]
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
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
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:47:52.998
13975:X 19 Apr 2024 20:48:20.481
13975:X 19 Apr 2024 20:48:55.838
13975:X 19 Apr 2024 21:04:13.162
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.215
13975:X 19 Apr 2024 21:04:13.217
13975:X 19 Apr 2024 21:04:13.220
13975:X 19 Apr 2024 21:04:13.222
13975:X 19 Apr 2024 21:04:13.301
13975:X 19 Apr 2024 21:04:13.301
13975:X 19 Apr 2024 21:04:13.373
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
13975:X 19 Apr 2024 21:04:14.307
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
13975:X 19 Apr 2024 21:04:15.396
13975:X 19 Apr 2024 21:04:15.396
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
127.0.0.1:6379> info 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 ~]
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
哨兵配置文件的sentinel monitor IP 同样也会被修改
[root@redis ~]
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
新的master状态
[root@redis-slave1 ~]
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
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
[root@redis-slave2 ~]
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
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
[root@redis-master ~]
replicaof 10.0.1.180 6379
[root@redis-master ~]
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
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 ~]
127.0.0.1:26379> info 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
[root@redis-slave1 ~]
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
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 ~]
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 ~]
replica-priority 10
[root@centos7 ~]
[root@centos7 ~]
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 ~]
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脚本)
----------------------------------------------------------------
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" -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
命令内修改----
[root@redis-node1 ~]vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456
requirepass 123456
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-require-full-coverage no
命令行外修改---
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@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
[root@redis ~]
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))
----------------------------------------------------------------
第三步---创建集群
----------------------------------------------------------------
[root@redis-node1 ~]
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.
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
[root@node1 ~]
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.
[root@redis ~]
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 ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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
[root@redis ~]
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
[root@centos8 ~]
a177c5cbc2407ebb6230ea7e2a7de914bf8c2dab
1) "cb20d58870fe05de8462787cf9947239f4bc5629 10.0.0.38:6379@16379 slave
a177c5cbc2407ebb6230ea7e2a7de914bf8c2dab 0 1602571574844 10 connected"
[root@redis ~]
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 ~]
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 ~]
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
----------------------------------------------------------------
第五步---测试集群写入数据
----------------------------------------------------------------
[root@redis-node1 ~]
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
[root@redis-node1 ~]
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
OK
[root@redis-node1 ~]
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
"values1"
[root@redis ~]
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
[root@redis ~]
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
[root@centos8 ~]
keyslot hello
(integer) 866
[root@centos8 ~]
magedu
OK
[root@centos8 ~]
keyslot name
(integer) 5798
[root@centos8 ~]
wang
(error) MOVED 5798 10.0.1.172:6379
[root@centos8 ~]
wang
OK
[root@centos8 ~]
"wang"
[root@centos8 ~]
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 ~]
"love"
总结:这里就已经打好集群了,三主 三从 主负责写 从负责读
---------------------------------------------------------------------------------------
第六步--测试集群(故障转移)
---------------------------------------------------------------------------------------
[root@redis ~]
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
[root@redis ~]
[root@redis ~]
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 ~]
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 ~]
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
[root@redis ~]
[root@redis ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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
[root@redis ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> SHUTDOWN
[root@redis ~]
[root@redis ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
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 ~]
[root@redis-node1 ~]
......
key9998: value9998
key9999: value9999
[root@redis-node1 ~]
Warning: Using a password with '-a' option on the command line interface may not
be safe.
(integer) 3331
[root@redis-node1 ~]
Warning: Using a password with '-a' option on the command line interface may not
be safe.
(integer) 3340
[root@redis-node1 ~]
Warning: Using a password with '-a' option on the command line interface may not
be safe.
(integer) 3329
[root@redis-node1 ~]
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 ~]
Warning: Using a password with '-a' option on the command line interface may not
be safe.
"value2"
[root@redis-node1 ~]
Warning: Using a password with '-a' option on the command line interface may not
be safe.
"value1"
[root@redis-node1 ~]
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,应为
一主一从。
[root@redis-node7 ~]
[root@redis-node7 ~]
[root@redis-node7 ~]
[root@redis-node7 ~]
[root@redis-node8 ~]
[root@redis-node8 ~]
[root@redis-node8 ~]
[root@redis-node8 ~]
使用以下命令添加新节点,要添加的新redis节点IP和端口添加到的已有的集群中任意节点的IP:端口
add-node new_host:new_port existing_host:existing_port [--slave --master-id
<arg>]
new_host:new_port
existing_host:existing_port
[root@redis-node1 ~]
[root@redis-node1 ~]
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.
[root@redis ~]
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.
[root@redis ~]
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 ~]
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 ~]
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 ~]
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 ~]
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
新的node节点加到集群之后,默认是master节点,但是没有slots,需要重新分配,否则没有槽位将无法访问
注意: 重新分配槽位需要清空数据,所以需要先备份数据,扩展后再恢复数据
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
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
What is the receiving node ID? d6e2eca6b338b717923f64866bd31d42e52edc98
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
......
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 ~]
[root@redis-node1 ~]
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
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.
当前Redis集群中新的master节点存单点问题,还需要给其添加一个对应slave节点,实现高可用功能
有两种方式:
1.在新加节点到集群时,直接将之设置为slave
redis-trib.rb add-node --slave --master-id
750cab050bc81f2655ed53900fd43d2e64423333 10.0.0.77:6379 <任意集群节点>:6379
redis-cli -a 123456 --cluster add-node 10.0.1.182:6379 <任意集群节点>:6379 --
cluster-slave --cluster-master-id d6e2eca6b338b717923f64866bd31d42e52edc98
范例:
[root@redis-node1 ~]
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.
[root@redis-node1 ~]
[root@redis ~]
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 ~]
[root@redis ~]
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节点
[root@redis-node1 ~]
[root@redis-node1 ~]
10.0.1.172:6379
需要手动将其指定为某个master的slave,否则其默认角色为master。
[root@redis-node1 ~]
10.0.1.182:6380> CLUSTER NODES
10.0.1.182:6380> CLUSTER REPLICATE 886338acd50c3015be68a760502b239f4509881c
置slave,命令格式为cluster replicate MASTERID
10.0.1.182:6380> CLUSTER NODES
slave
---------------------------------------------------------------------------------------
第八步--集群缩减
---------------------------------------------------------------------------------------
缩容适用场景:
随着业务萎缩用户量下降明显,和领导商量决定将现有Redis集群的8台主机中下线两台主机挪做它用,缩容后性能仍能满足当前业务需求
删除节点过程:
扩容时是先添加node到集群,然后再分配槽位,而缩容时的操作相反,是先将被要删除的node上的槽位迁移到集群中的其他node上,然后 才能再将其从集群中删除,如果一个node上的槽位没有被完全迁移空,删除该node时也会提示有数据出错导致无法删除。
删除二个节点:10.0.1.181 10.0.1.182
注意: 被迁移Redis master源服务器必须保证没有数据,否则迁移报错并会被强制中断。
Redis 3/4 版本命令
[root@redis-node1 ~]
[root@redis-node1 ~]
Redis 5版本以上命令
[root@redis ~]
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 ~]
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
[root@redis ~]
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
What is the receiving node ID? 3f2695389ab756b43ab0b87d891becc14c8974ee
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
Source node
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: ..
[root@redis-node1 ~]
[root@redis-node1 ~]
[root@redis-node1 ~]
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.
[root@redis-node1 ~]
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
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 ~]
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:3
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
上面步骤完成后,槽位已经迁移走,但是节点仍然还属于集群成员,因此还需从集群删除该节点
注意: 删除服务器前,必须清除主机上面的槽位,否则会删除主机失败
[root@s~]
dfffc371085859f2858730e1f350e9167e287073
>>> Removing node dfffc371085859f2858730e1f350e9167e287073 from cluster
192.168.7.102:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@redis-node1 ~]
[root@redis ~]
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 ~]
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 ~]
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
[root@redis-node1 ~]
[root@redis-node1 ~]
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 ~]
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.
[root@redis-node1 ~]
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 ~]
[root@redis-node1 ~]
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 ~]
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 ~]
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: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。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南