源码安装
[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-10.11.6-
1.el9.x86_64 -y
#执行编译命令
[root@redis-node1 redis-7.4.0]# make
[root@redis-node1 redis-7.4.0]# make install
[root@redis-node1 redis-7.4.0]# cd utils/
[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
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
[root@redis-node1 utils]# vim install_server.sh
把以下注释
[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 71315 43749/redis-server
tcp6 0 0 :::6379 :::* LISTEN 0 71316 43749/redis-server
[root@redis-node2 utils]# redis-cli
配置slave节点
root@redis-node2 & 3 ~]# vim /etc/redis/6379.conf
replicaof 192.168.136.10 6379
[root@redis-node2 & 3 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
测试效果
#在master节点
[root@redis-node1 utils]# redis-cli
127.0.0.1:6379> set name wen
OK
#在slave节点
[root@redis-node2 utils]# redis-cli
127.0.0.1:6379> get name
"wen"
哨兵的实验过程
在所有阶段中关闭 protected-mode no
1.在master节点中
#编辑配置文件
[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
protected-mode no #关闭保护模式
port 26379 #监听端口
daemonize no #进入不打如后台
pidfile /var/run/redis-sentinel.pid #sentinel进程pid文件
loglevel notice #日志级别
sentinel monitor mymaster 172.25.254.100 6379 2 #创建sentinel监控监控master主
机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 10000 #master中断时长,10秒连不上视为
master下线
sentinel parallel-syncs mymaster 1 #发生故障转移后,同时开始同步新
master数据的slave数量
sentinel failover-timeout mymaster 180000 #整个故障切换的超时时间为3分钟
####复制配置文件到其他阶段
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.201:/etc/redis/
root@172.25.254.201's password:
sentinel.conf
100% 14KB 9.7MB/s 00:00
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.200:/etc/redis/
root@172.25.254.200's password:
sentinel.conf
启动服务
在每台redis上开启
[root@redis-node1 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node2 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node3 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf
测试:
在开一个master节点终端
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
node2变成主
node3上查看
127.0.0.1:6379> slaveof 192.168.136.20 6379
OK Already connected to specified master
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.136.20
master_port:6379
master_link_status:up
Redis Cluster(无中心化设计)
Redis Cluster 工作原理
在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master, 从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受 限于单机的内存大小、并发数量、网卡速率等因素。 redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存 当前节点数据和整个集群状态,每个节点都和其他所有节点连接
Redis Cluster特点如下
- 所有Redis节点使用(PING机制)互联
- 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
- 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
- redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担 16384/N个槽位
- dis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使 用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点 上,从而有效解决单机瓶颈。
部署redis cluster
在所有redis主机中
[root@redis-node110 redis-7.4.0]# vim /etc/redis/redis.conf
bind * -::*
masterauth "123456" #集群主从认证
requirepass "123456" #redis登陆密码 redis-cli 命令连接redis后要用“auth 密码”进行认证
cluster-enabled yes #开启cluster集群功能
cluster-config-file nodes-6379.conf #指定集群配置文件
cluster-node-timeout 15000 #节点加入集群的超时时间单位是ms
[root@redis-node110 redis-7.4.0]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@redis-node110 redis-7.4.0]# netstat -launpt | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1794/redis-server *
tcp6 0 0 :::6379 :::* LISTEN 1794/redis-server *
创建redis-cluster
[root@redis-node1 redis-7.4.0]# redis-cli --cluster create -a 123456 \
192.168.136.10:6379 192.168.136.20:6379 192.168.136.30:6379 \ 192.168.136.110:6379 192.168.136.120:6379 192.168.136.130:6379 \ --cluster-replicas 1
集群检测状态
[root@redis-node2 redis-7.4.0]# redis-cli --cluster info -a 123456 192.168.136.30:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.136.30:6379 (85928e72...) -> 0 keys | 5461 slots | 1 slaves.
192.168.136.10:6379 (5da76046...) -> 0 keys | 5461 slots | 1 slaves.
192.168.136.20:6379 (4cb98fbc...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
写入数据
[root@redis-node1 redis-7.4.0]# 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 k1 v1
(error) MOVED 12706 192.168.136.30:6379 ##被分配到30的hash槽位上
[root@redis-node3 redis-7.4.0]# 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 k1 v1
OK
集群扩容
[root@redis-node1 redis-7.4.0]# redis-cli -a 123456 --cluster add-node 192.168.136.40:6379 192.168.136.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.136.40:6379 to cluster 192.168.136.10:6379
>>> Performing Cluster Check (using node 192.168.136.10:6379)
M: 5da76046c8cce9925bef467b06155561bdae19c9 192.168.136.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 4cb98fbc01e9d3c740ac79c8a98a2a4a4d847bc0 192.168.136.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9d3716e91c95a056c7699cfb56cd4f80dc00eb3b 192.168.136.110:6379
slots: (0 slots) slave
replicates 85928e72c75434ca003e0e89354e0c3a7d3deb1d
M: 85928e72c75434ca003e0e89354e0c3a7d3deb1d 192.168.136.30:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3022a4a6d05e55111261699bea46234008710ba9 192.168.136.120:6379
slots: (0 slots) slave
replicates 5da76046c8cce9925bef467b06155561bdae19c9
S: 1314726e5f3156b1adb086b50b1b4bdb211bb0e8 192.168.136.130:6379
slots: (0 slots) slave
replicates 4cb98fbc01e9d3c740ac79c8a98a2a4a4d847bc0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.136.40:6379 to make it join the cluster.
[OK] New node added correctly.
分配槽位
[root@redis-node1 redis-7.4.0]# redis-cli -a 123456 --cluster reshard 192.168.136.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.136.10:6379)
M: 5da76046c8cce9925bef467b06155561bdae19c9 192.168.136.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 4cb98fbc01e9d3c740ac79c8a98a2a4a4d847bc0 192.168.136.20:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 70a8cbeb421698b737592c053343db8a68848a14 192.168.136.40:6379
slots: (0 slots) master
S: 9d3716e91c95a056c7699cfb56cd4f80dc00eb3b 192.168.136.110:6379
slots: (0 slots) slave
replicates 85928e72c75434ca003e0e89354e0c3a7d3deb1d
M: 85928e72c75434ca003e0e89354e0c3a7d3deb1d 192.168.136.30:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3022a4a6d05e55111261699bea46234008710ba9 192.168.136.120:6379
slots: (0 slots) slave
replicates 5da76046c8cce9925bef467b06155561bdae19c9
S: 1314726e5f3156b1adb086b50b1b4bdb211bb0e8 192.168.136.130:6379
slots: (0 slots) slave
replicates 4cb98fbc01e9d3c740ac79c8a98a2a4a4d847bc0
[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? 70a8cbeb421698b737592c053343db8a68848a14
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
yes
添加salve
[root@redis-node1 redis-7.4.0]# redis-cli -a 123456 --cluster add-node 192.168.136.140:6379 192.168.136.10:6379 --cluster-slave --cluster-master-id 70a8cbeb421698b737592c053343db8a68848a14
检查集群
clsuter集群维护
添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相 反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如 果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。
Do you want to proceed with the proposed reshard plan (yes/no)? yes