Nosql数据库redis集群配置详解

一、Redis的安装 

环境介绍:

一主双从:10(redis-node1)主,20(redis-node2) 30(redis-node3)从——使用的是红帽9.1系统

源码安装redis

[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz——先将压缩包解压

[root@redis-node1 ~]# ls

redis-7.4.0 redis-7.4.0.tar.gz

[root@redis-node1 redis-7.4.0]# dnf install make gcc initscripts -y——安装编译工具

[root@redis-node1 redis-7.4.0]# make && make install——开始编译

启动Redis

[root@redis-node1 redis-7.4.0]# cd utils/

[root@redis-node1 utils]# vim install_server.sh ——将系统使用systemd的初始化方式注释

#_pid_1_exe="$(readlink -f /proc/1/exe)"

#if [ "${_pid_1_exe##*/}" = systemd ]

#then

#       echo "This systems seems to use systemd."

#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"

#       exit 1

#fi

[root@redis-node1 utils]# ./install_server.sh——编辑此文件后成功启动

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]——端口号

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]——配置文件

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]——日志文件

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]——数据文件

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]——命令文件

Selected config:

Port           : 6379

Config file    : /etc/redis/6379.conf

Log file       : /var/log/redis_6379.log

Data dir       : /var/lib/redis/6379

Executable     : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

配置redis

[root@redis-node1 utils]# vim /etc/redis/6379.conf

bind * -::*

protected-mode no

重启服务

[root@redis-node1 utils]# /etc/init.d/redis_6379 restart

Stopping ...

Redis stopped

Starting Redis server...

[root@redis-node1 utils]# netstat -antlpe | grep redis——查看端口是否启动

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      0          8          1587      49806/redis-server

tcp6       0      0 :::6379                 :::*                    LISTEN      0          8          1588      49806/redis-server

查看信息

[root@redis-node1 utils]# redis-cli

127.0.0.1:6379>info

[root@redis-node1 utils]# cd /usr/local/bin/

将编译好的软件文件和命令文件同步到20和30上

[root@redis-node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin

[root@redis-node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin

[root@redis-node1 bin]# cd /root

[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root

[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root

重复安装动作在20和30主机上

[root@redis-node2&3 ~]# cd redis-7.4.0/

[root@redis-node2&3 redis-7.4.0]# ls

00-RELEASENOTES     deps         MANIFESTO               runtest            SECURITY.md    TLS.md

BUGS                INSTALL      README.md               runtest-cluster    sentinel.conf  utils

CODE_OF_CONDUCT.md  LICENSE.txt  redis.conf              runtest-moduleapi  src

CONTRIBUTING.md     Makefile     REDISCONTRIBUTIONS.txt  runtest-sentinel   tests

[root@redis-node2&3 redis-7.4.0]# cd utils/

[root@redis-node2&3 utils]# ls

build-static-symbols.tcl    graphs                 reply_schema_linter.js

cluster_fail_time.tcl       hyperloglog            req-res-log-validator.py

corrupt_rdb.c               install_server.sh      req-res-validator

create-cluster              lru                    speed-regression.tcl

generate-command-code.py    redis-copy.rb          srandmember

generate-commands-json.py   redis_init_script      systemd-redis_multiple_servers@.service

generate-fmtargs.py         redis_init_script.tpl  systemd-redis_server.service

generate-module-api-doc.rb  redis-sha1.rb          tracking_collisions.c

gen-test-certs.sh           releasetools           whatisdoing.sh

[root@redis-node2&3 utils]# dnf install initscripts -y

[root@redis-node2&3 utils]# ./install_server.sh

[root@redis-node2&3 ~]# vim /etc/redis/6379.conf

bind * -::*

protected-mode no——关闭保护模式

 二、Redis的主从复制

2.1主从同步的原理

slave节点发送同步请求到master节点

slave节点通过master节点的认证开始进行同步

master节点会开启bgsave进程发送内存rbd到slave节点,在此过程中是异步操作,也就是说 master节点仍然可以进行写入动作

slave节点收到rdb后首先清空自己的所有数据

slave节点加载rdb并进行数据恢复

在master和slave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存(图上过程8)

然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到 slave中

2.2配置主从同步

Redis主从复制

两台slave上配置

[root@redis-node2&3 utils]# vim /etc/redis/6379.conf

replicaof 172.25.254.10 6379——添加主IP

[root@redis-node2&3 ~]# /etc/init.d/redis_6379 restart

Stopping ...

Redis stopped

Starting Redis server...

[root@redis-node1 utils]# redis-cli——可以在node1上查看到主从信息

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:2

slave0:ip=172.25.254.20,port=6379,state=online,offset=42,lag=0

slave1:ip=172.25.254.30,port=6379,state=wait_bgsave,offset=0,lag=0

master_failover_state:no-failover

master_replid:529a24e2de5ffc361cabcd76592ef3b021bd6d80

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:42

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:42

测试:在node1上添加数据可以在20和30上看到

[root@redis-node1 utils]# redis-cli

127.0.0.1:6379> set name jcl

OK

[root@redis-node2 ~]# redis-cli

127.0.0.1:6379> get name

"jcl"

[root@redis-node3 utils]# redis-cli

127.0.0.1:6379> get name

"jcl"

三、Redis的哨兵(高可用)

3.1Redis哨兵原理

Sentinel 进程是用于监控redis集群中Master主服务器工作的状态,在Master主服务器发生故障的时候, 可以实现Master和Slave服务器的切换,保证系统的高可用,生产环境也建议使用Redis的2.8版本的以后版本

每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)、Master、Slave定时发送消息,以确认对方是否”活” 着,如果发现对方在指定配置时间(此项可配置)内未得到回应,则暂时认为对方已离线,也就是所谓的” 主观认为宕机” (主观:是每个成员都具有的独自的而且可能相同也可能不同的意识),

当“哨兵群”中的多数Sentinel进程在对Master主服务器做出SDOWN 的 判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判 断,这种方式就是“客观宕机”(客观:是不依赖于某种意识而已经实际存在的一切事物)

通过一定的vote算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改 相关配置,并开启故障转移

Sentinel 机制可以解决master和slave角色的自动切换问题,但单个 Master 的性能瓶颈问题无法解决,类 似于MySQL中的MHA功能

sentinel中的三个定时任务:

      每10秒每个sentinel对master和slave执行info

      每2秒每个sentinel通过master节点的channel交换信息(pub/sub)

      每1秒每个sentinel对其他sentinel和redis执行pi

3.2哨兵实验过程

因为我们哨兵实现会改变配置文件,所以我们要提前将配置文件备份

编辑和备份配置文件

[root@redis-node1 ~]# cd redis-7.4.0/

[root@redis-node1 redis-7.4.0]# cp sentinel.conf  /etc/redis/

[root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf

92 sentinel monitor mymaster 172.25.254.10 6379 2——创建sentinel监控 masterIP 2表示必须得到两票

133 sentinel down-after-milliseconds mymaster 10000——master中断时长,表示10s连不上视为下线

将配置文件同步到20和30主机上

[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf

[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/sentinel.conf

备份配置文件

[root@redis-node1&2&3 redis-7.4.0]# cp /etc/redis/sentinel.conf /etc/redis/sentinel.conf.bak

在20和30上启动哨兵服务

[root@redis-node2 redis]# redis-sentinel sentinel.conf

60271:X 25 Aug 2024 23:02:40.539 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

60271:X 25 Aug 2024 23:02:40.539 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

60271:X 25 Aug 2024 23:02:40.539 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=60271, just started

60271:X 25 Aug 2024 23:02:40.539 * Configuration loaded

60271:X 25 Aug 2024 23:02:40.540 * Increased maximum number of open files to 10032 (it was originally set to 1024).

60271:X 25 Aug 2024 23:02:40.540 * monotonic clock: POSIX clock_gettime

                _._

           _.-``__ ''-._

      _.-``    `.  `_.  ''-._           Redis Community Edition

  .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 60271

  `-._    `-._  `-./  _.-'    _.-'

 |`-._`-._    `-.__.-'    _.-'_.-'|

 |    `-._`-._        _.-'_.-'    |           Redis - The Real-time Data Platform

  `-._    `-._`-.__.-'_.-'    _.-'

 |`-._`-._    `-.__.-'    _.-'_.-'|

 |    `-._`-._        _.-'_.-'    |

  `-._    `-._`-.__.-'_.-'    _.-'

      `-._    `-.__.-'    _.-'

          `-._        _.-'

              `-.__.-'

60271:X 25 Aug 2024 23:02:40.542 * Sentinel new configuration saved on disk

60271:X 25 Aug 2024 23:02:40.542 * Sentinel ID is 298458b72b6f29fc7cc2a7e074c99572871f66a3

60271:X 25 Aug 2024 23:02:40.542 # +monitor master mymaster 172.25.254.10 6379 quorum 2

60271:X 25 Aug 2024 23:02:40.544 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379

60271:X 25 Aug 2024 23:02:40.545 * Sentinel new configuration saved on disk

60271:X 25 Aug 2024 23:02:40.545 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379

60271:X 25 Aug 2024 23:02:40.546 * Sentinel new configuration saved on disk

测试:

关闭master10的redis模拟master下线

[root@redis-node1 redis]# redis-cli

127.0.0.1:6379> SHUTDOWN

not connected>

可以看到20和30上分别会显示

60271:X 25 Aug 2024 23:04:07.999 # +sdown master mymaster 172.25.254.10 6379

36897:X 25 Aug 2024 23:15:46.303 # -odown master mymaster 172.25.254.10 6379 ——表示两个识别到master挂掉的状态时自动成为客观事实

61855:X 25 Aug 2024 23:18:36.541 * +convert-to-slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.20 6379 ——表示10挂掉后20成为新master10恢复后成为20的slave

这时候再开一个20的新窗口,查看主从信息发现20只有30这一个slave

[root@redis-node2 redis]# redis-cli

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=172.25.254.30,port=6379,state=online,offset=64205,lag=0

master_failover_state:no-failover

master_replid:565c461a7c6b2d21d16bf2ffe97485d0e9512131

master_replid2:0d65657f58823cef4fbd11aed042e838fd67013f

master_repl_offset:64219

second_repl_offset:45967

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:64219

恢复10

[root@redis-node1 redis]# /etc/init.d/redis_6379 start

Starting Redis server...

20和30上会显示以下信息,表示10上线成为20的slave

61855:X 25 Aug 2024 23:27:54.804 # -sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.20 6379

再看20的主从信息

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:2

slave0:ip=172.25.254.30,port=6379,state=online,offset=72349,lag=1

slave1:ip=172.25.254.10,port=6379,state=online,offset=72349,lag=0

master_failover_state:no-failover

master_replid:565c461a7c6b2d21d16bf2ffe97485d0e9512131

master_replid2:0d65657f58823cef4fbd11aed042e838fd67013f

master_repl_offset:72349

second_repl_offset:45967

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:72349

如果想要sentinal哨兵在后台运行不在前台显示可以在sentinal配置文件中daemonize yes

[root@redis-node1 redis]# vim sentinel.conf

[root@redis-node1 redis]# redis-sentinel sentinel.conf

66626:X 25 Aug 2024 23:36:06.750 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see vm.max_map_count growing steadily when vm.overcommit_memory is 2 · Issue #1328 · jemalloc/jemalloc · GitHub. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

优化防止数据丢失

由于master出故障时数据可能还会一直写入导致数据丢失情况出现

在master上临时更改,表示当master有两个slave时才能写入数据

127.0.0.1:6379> CONFIG GET min-slaves-to-write

1) "min-slaves-to-write"

2) "0"

127.0.0.1:6379> CONFIG SET min-slaves-to-write 2

OK

127.0.0.1:6379> CONFIG GET min-slaves-to-write

1) "min-slaves-to-write"

2) "2"

永久更改需要编辑配置文件

[root@redis-node2 redis]# vim /etc/redis/6379.conf

min-slaves-to-write 2  添加参数

四、Redis cluster(无中心化设计)

4.1Redis Cluster工作原理

Redis Cluster特点:

1. 所有Redis节点使用(PING机制)互联
2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担 16384/N个槽位
5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使 用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点 上,从而有效解决单机瓶颈。

Redis cluster 主从架构:

Redis cluster的架构虽然解决了并发的问题,但是又引入了一个新的问题,每个Redis master的高可用 如何解决? 那就是对每个master 节点都实现主从复制,从而实现 redis 高可用性

Redis Cluster部署架构说明:

4.2创建redis cluster的前提

1.每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。

2.每个节点必须开启的参数

cluster-enabled yes        #必须开启集群状态,开启后redis进程会有cluster显示

cluster-config-file nodes-6380.conf        #此文件有redis cluster集群自动创建和维护,不需要任何手 动操作

3.所有redis服务器必须没有任何数据
4.先启动为单机redis且没有任何key value

4.3rpm包安装redis及集群配置 

三台10 20 30卸载源码安装的redis重新rpm安装

[root@redis-node3 redis]# ps aux | grep redis——查看redis进程

root       35174  0.4  0.6 348796 12196 ?        Ssl  22:43   0:17 /usr/local/bin/redis-server *:6379

root       38830  0.0  0.1 221664  2232 pts/2    S+   23:45   0:00 grep --color=auto redis

[root@redis-node3 redis]# killall -9 redis-server——将redis进程全部结束掉

[root@redis-node3 redis]# dnf install gcc -y——安装工具方便卸载编译安装的redis

[root@redis-node3 redis-7.4.0]# make uninstall——卸载

[root@redis-node3 redis-7.4.0]# yum install redis -y——yum源安装

[root@redis-node3 redis-7.4.0]# vim /etc/redis/redis.conf——编辑配置文件

486  masterauth "123456"——集群主从认证

903  requirepass "123456"——redis登录密码 redis-cli -a 命令连接数据库

1387  cluster-enabled yes——开启集群功能

1395  cluster-config-file nodes-6379.conf——指定集群配置文件

75 bind * -::*——设置为所有端口

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

Created symlink /etc/systemd/system/multi-user                                                   .target.wants/redis.service → /usr/lib/systemd                                                   /system/redis.service.

[root@redis-node1 ~]# which redis-cli——可以看到命令文件

/usr/bin/redis-cli

在新克隆出来的三台主机上安装redis

[root@redis-node110 &120&130~]# yum install redis -y

在node1上验证一下配置

[root@redis-node1 ~]# redis-cli

127.0.0.1:6379> keys *

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth 123456

OK

将配置文件拷贝到其他5台主机上

[root@redis-node1 ~]# for i in 20 30 110 120 130

> do

> scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf

> done

创建集群

[root@redis-node1 ~]# redis-cli --cluster create -a 123456 \

> 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \

> 172.25.254.110:6379 172.25.254.120:6379 172.25.254.130:6379 \

> --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Performing hash slots allocation on 6 nodes...

Master[0] -> Slots 0 - 5460

Master[1] -> Slots 5461 - 10922

Master[2] -> Slots 10923 - 16383

Adding replica 172.25.254.120:6379 to 172.25.254.10:6379

Adding replica 172.25.254.130:6379 to 172.25.254.20:6379

Adding replica 172.25.254.110:6379 to 172.25.254.30:6379

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join

>>> Performing Cluster Check (using node 172.25.254.10:6379)

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

查看集群信息

[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.20:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

172.25.254.20:6379 (c362d17f...) -> 1 keys | 5462 slots | 1 slaves.

172.25.254.10:6379 (94334ee4...) -> 0 keys | 5461 slots | 1 slaves.

172.25.254.30:6379 (157195f5...) -> 0 keys | 5461 slots | 1 slaves.

[OK] 1 keys in 3 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

测试:

[root@redis-node1 ~]# 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> set name jcl

(error) MOVED 5798 172.25.254.20:6379——可以看到它指引你去20master上写入数据

127.0.0.1:6379> exit

[root@redis-node2 ~]# 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> set name jcl

OK——20上成功写入

注:

如果redis-cli不能用该bash配置添加命令文件

[root@redis-node1 ~]# vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/bin

[root@redis-node2 ~]# source ~/.bash_profile

4.2集群扩容

安装redis软件

[root@redis-node40&140 ~]# dnf install redis -y

[root@redis-node40 &140~]# systemctl start redis

查看集群

[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.20:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

172.25.254.20:6379 (c362d17f...) -> 1 keys | 5462 slots | 1 slaves.

172.25.254.10:6379 (94334ee4...) -> 0 keys | 5461 slots | 1 slaves.

172.25.254.30:6379 (157195f5...) -> 0 keys | 5461 slots | 1 slaves.

[OK] 1 keys in 3 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

添加集群

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.40:6379 172.25.254.20:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Adding node 172.25.254.40:6379 to cluster 172.25.254.20:6379

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node 172.25.254.40:6379 to make it join the cluster.

[OK] New node added correctly.

给master40分配槽位

[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.40:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Performing Cluster Check (using node 172.25.254.40:6379)

M: 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936 172.25.254.40:6379

   slots: (0 slots) master

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

[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? 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936 ——40ID

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

给40添加slave

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.140:6379 172.25.254.20:6379 --cluster-slave --cluster-master-id 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936——这里的ID是40的

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Adding node 172.25.254.140:6379 to cluster 172.25.254.20:6379

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[6827-10922] (4096 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

M: 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936 172.25.254.40:6379

   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[1365-5460] (4096 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30: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.

>>> Send CLUSTER MEET to node 172.25.254.140:6379 to make it join the cluster.

Waiting for the cluster to join

>>> Configure node as replica of 172.25.254.40:6379.

[OK] New node added correctly.

查看集群可以看到四主四从

[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.20:6379                     Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

172.25.254.20:6379 (c362d17f...) -> 0 keys | 4096 slots | 1 slaves.

172.25.254.40:6379 (48f2f7aa...) -> 1 keys | 4096 slots | 1 slaves.

172.25.254.10:6379 (94334ee4...) -> 0 keys | 4096 slots | 1 slaves.

172.25.254.30:6379 (157195f5...) -> 0 keys | 4096 slots | 1 slaves.

[OK] 1 keys in 4 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[6827-10922] (4096 slots) master

   1 additional replica(s)

S: ae6378ba83e2d927f63a56d286dd9b41e6361bff 172.25.254.140:6379

   slots: (0 slots) slave

   replicates 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

M: 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936 172.25.254.40:6379

   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master

   1 additional replica(s)

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[1365-5460] (4096 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30: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.

4.3集群维护

删除集群

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.140:6379 ae6378ba83e2d927f63a56d286dd9b41e6361bff

将槽位分配给10

[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20:6379

How many slots do you want to move (from 1 to 16384)? 4096

What is the receiving node ID? 94334ee417e6536aaae592a95dfce014e60cf95f10ID

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: 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc193640ID

Source node #2: done

删除master

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.40:6379 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936

再查看集群

[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.20:6379                     Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

172.25.254.20:6379 (c362d17f...) -> 1 keys | 8192 slots | 1 slaves.

172.25.254.10:6379 (94334ee4...) -> 0 keys | 6144 slots | 1 slaves.

172.25.254.30:6379 (157195f5...) -> 0 keys | 2048 slots | 1 slaves.

[OK] 1 keys in 3 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 172.25.254.20:6379)

M: c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a 172.25.254.20:6379

   slots:[0-3412],[5461-10239] (8192 slots) master

   1 additional replica(s)

S: b33f211967dfea61399f275dc0c632cf28d18cc6 172.25.254.110:6379

   slots: (0 slots) slave

   replicates 157195f57cd549409676209e18b4881146aedf3e

S: db431728d7b1658ed50cbece7bb91287b80952ad 172.25.254.120:6379

   slots: (0 slots) slave

   replicates 94334ee417e6536aaae592a95dfce014e60cf95f

S: 3ad4a43dd7e33933862f28e7a78bacad02d8d50c 172.25.254.130:6379

   slots: (0 slots) slave

   replicates c362d17f7aaed4a7c1dbeb2eca262ba9dad4d80a

M: 94334ee417e6536aaae592a95dfce014e60cf95f 172.25.254.10:6379

   slots:[3413-5460],[10240-14335] (6144 slots) master

   1 additional replica(s)

M: 157195f57cd549409676209e18b4881146aedf3e 172.25.254.30:6379

   slots:[14336-16383] (2048 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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/409543.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端常见问题

一、<!DOCTYPE html>作用 <!DOCTYPE>是html文档的第一行&#xff0c;用于声明文档的类型&#xff0c;它会告诉浏览器要用哪一种HTML规范来渲染文档&#xff0c;确保正确渲染页面。 二、src与 href 的区别 &#xff08;一&#xff09;、请求资源类型的不同 在请…

女明星玩乙游,为何会推动国乙玩家“世纪大和解”?

“震惊&#xff01;叠姐和光姐竟然世纪大和解了。” 这件在国乙圈匪夷所思、想都不敢想的事&#xff0c;竟然在一位女明星的推动下发生了&#xff0c;也因此诞生了国乙圈的“8.22事件”。 事情的起因是女艺人乃万在社交平台上发布了乙游相关言论&#xff0c;引起了乙游玩家不…

Spring--三级缓存机制

一、什么是三级缓存 就是在Bean生成流程中保存Bean对象三种形态的三个Map集合&#xff0c;如下&#xff1a; // 一级缓存Map 存放完整的Bean&#xff08;流程跑完的&#xff09; private final Map<String, Object> singletonObjects new ConcurrentHashMap(256);// 二…

USB3.2 摘录(九)

系列文章目录 USB3.2 摘录&#xff08;一&#xff09; USB3.2 摘录&#xff08;二&#xff09; USB3.2 摘录&#xff08;三&#xff09; USB3.2 摘录&#xff08;四&#xff09; USB3.2 摘录&#xff08;五&#xff09; USB3.2 摘录&#xff08;六&#xff09; USB3.2 摘录&…

页面设计任务 商品详情页(带评论区)

目录 效果图&#xff1a; 任务描述 源码&#xff1a; 详细讲解&#xff1a; 1.产品信息部分 2.用户评论区域 效果图&#xff1a; 任务描述 页面结构: 页面应包括一个标题部分、一个产品展示区和一个客户评价区。使用图片展示产品&#xff0c;并添加描述。客户评价区展示一…

IP代理池学习记录

免责声明 本文仅供学习和研究目的使用。所提供的信息和技术仅限于合规和合法的使用场景。请读者在应用相关技术时遵守法律法规&#xff0c;尊重他人的数据隐私和网站使用条款。本文作者对因使用本文信息而产生的任何法律责任或损失不承担责任。 1、初识IP代理池 概述&#xff…

如何使用ssm实现基于SSM的社区物业管理系统的设计与实现+vue

TOC ssm223基于SSM的社区物业管理系统的设计与实现vue 绪论 1.1 研究背景 现在大家正处于互联网加的时代&#xff0c;这个时代它就是一个信息内容无比丰富&#xff0c;信息处理与管理变得越加高效的网络化的时代&#xff0c;这个时代让大家的生活不仅变得更加地便利化&…

Redis—缓存机制

Redis 缓存机制 1. 缓存三兄弟1.1 缓存击穿1.2 缓存穿透1.3 缓存雪崩 2. 布隆过滤器3. 缓存和数据库数据一致性3.1 缓存更新策略3.2 缓存不一致处理 4. 热点 key4.1 热点 key 处理4.2 热点 key 重建 5. 缓存预热 Redis&#xff0c;一个轻量级的开源内存数据结构存储系统&#x…

Redis计数器:数字的秘密

文章目录 Redis计数器incr 指令用户计数统计用户统计信息查询缓存一致性 小结 技术派项目源码地址 : Gitee :技术派 - https://gitee.com/itwanger/paicodingGithub :技术派 - https://github.com/itwanger/paicoding 用户的相关统计信息 文章数&#xff0c;文章总阅读数&am…

go设计模式——单例模式

概念 单例是一种创建型设计模式&#xff0c;它确保一个类在整个程序运行期间只有一个实例&#xff0c;并提供一个全局访问点来使用该实例。虽然单例模式在某些情况下非常有用&#xff0c;例如管理全局配置、日志记录或资源共享&#xff0c;但它也带来了与全局变量相似的问题。…

redis面试(二十三)写锁释放

先加了写锁&#xff0c;后面再次加写锁或者读锁 anyLock: { “mode”: “write”, “UUID_01:threadId_01:write”: 2, “UUID_01:threadId_01”: 1 } 写锁的释放lua脚本在这里 RedissonWriteLock.unlockInnerAsync() 比如说现在的参数是这 KEYS[1] anyLock KEYS[2] redi…

SQL手工注入漏洞测试(MongoDB数据库)靶场通关攻略

构造数据回显 });return ({title:1,content:2 成功回显1,2&#xff0c;接下来我们开始尝试查询数据库 });return({title:tojson(db),content:2 得到之后我们就可以继续查询他的表名了 });return({title:tojson(db.getCollectionNames()),content:2 最后我们就可以爆出他表里的数…

宝塔面板配置FTP服务并安装内网穿透实现无公网IP远程连接

文章目录 前言1. Linux安装Cpolar2. 创建FTP公网地址3. 宝塔FTP服务设置4. FTP服务远程连接小结 5. 固定FTP公网地址6. 固定FTP地址连接 前言 本文主要介绍宝塔FTP文件传输服务如何搭配内网穿透工具&#xff0c;实现随时随地远程连接局域网环境搭建的宝塔FTP文件服务并进行文件…

ssrf实现.SSH未创建写shell

一、介绍SSRF漏洞 SSRF (Server-Side Request Forgery,服务器端请求伪造)是一种由攻击者构造请求&#xff0c;由服务端发起请求的安全漏洞。一般情况下&#xff0c;SSRF攻击的目标是外网无法访问的内部系统(正因为请求是由服务端发起的&#xff0c;所以服务端能请求到与自身相…

C语言基础——函数详解

目录 函数的概述 1 函数的概念 2 函数的意义 函数的定义和使用 1 函数的定义 2 函数的调用 2.1 在同一文件中函数定义后函数调用 2.2 在同一文件中函数定义前函数调用 2.3 调用其它文件中定义的函数 2.3.1 在函数调用文件中进行声明 2.3.2 在头文件中进行函数的声明 函…

图片工具箱:一键批量加水印,守护创意,提升效率!

前言 你是否曾在处理海量图片时&#xff0c;被繁琐的步骤和漫长的等待时间折磨得苦不堪言&#xff1f;是否梦想过拥有一款神器&#xff0c;能让你的图片处理工作变得轻松愉快&#xff0c;从此告别加班的烦恼&#xff0c;迎接升职加薪的曙光&#xff1f;那么&#xff0c;让我向…

有限差分学习笔记

有限差分介绍 ​ 在数学中&#xff0c;有限差分法&#xff08;finite-difference methods&#xff0c;简称FDM&#xff09;&#xff0c;是一种微分方程数值方法&#xff0c;是通过有限差分来近似导数&#xff0c;从而寻求微分方程的近似解。 由泰勒展开式的推导 显式方…

Web应用加密数据传输方案

目录 概述 最初的方案 改进后的方案 秘钥的过期时间 概述 介于公司最近发布了一个面向C端用户的Web系统&#xff0c;为防止前端调用后端的API接口时&#xff0c;数据传输的内容轻易的被黑客获取&#xff0c;而设计的一个前后端数据加密传输方案 最初的方案 在最开始&#xf…

2 种方式申请免费 SSL 证书,阿里云 Certbot

如何使用免费的 SSL 证书&#xff0c;有时在项目中需要使用免费的 SSL 证书&#xff0c;Aliyun 提供免费证书&#xff0c;三个月有效期&#xff0c;可以直接在aliyun 申请&#xff0c;搜索 SSL 证书&#xff0c;选择测试证书。 Aliyun 证书需要每三月来来换一次&#xff0c;页…

<数据集>车内视角行人识别数据集<目标检测>

数据集格式&#xff1a;VOCYOLO格式 图片数量&#xff1a;6470张 标注数量(xml文件个数)&#xff1a;6470 标注数量(txt文件个数)&#xff1a;6470 标注类别数&#xff1a;1 标注类别名称&#xff1a;[pedestrian] 序号类别名称图片数框数1pedestrian647029587 使用标注…