一、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 ——40的ID
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all
给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? 94334ee417e6536aaae592a95dfce014e60cf95f(10ID)
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: 48f2f7aaf79ca9a0076d3f2e77722bf7d1bc1936(40ID)
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.