02 redis高可用集群
Redis消息队列
消息队列主要分为两种,分别是生产者消费者模式、发布者订阅者模式、这两种模式Redis都支持
生产者消费者模式
一个消息只能被一个服务所消费一次。生产者消费者模式下,多个消费者同时监听一个队列,但是一个消息只能被最先抢到消息的消费者消费,即消息任务是一次性读取和处理,此模式在分布式业务架构中非常常用,比较常用的软件还有RabbitMQ、Kafka、RocketMQ、ActiveMQ等。
[root@db01 ~]# redis-cli -h `hostname -i` -a 123456
# 先进先出FIFO
192.168.15.201:6379> LPUSH CHANNEL1 msg1
(integer) 1
192.168.15.201:6379> LPUSH CHANNEL1 msg2
(integer) 2
192.168.15.201:6379> LPUSH CHANNEL1 msg3
(integer) 3
192.168.15.201:6379> LLEN CHANNEL1
(integer) 3
# 查看
192.168.15.201:6379> LRANGE CHANNEL1 0 -1
1) "msg3"
2) "msg2"
3) "msg1"
# 取数据
192.168.15.201:6379> RPOP CHANNEL1
"msg1"
192.168.15.201:6379> RPOP CHANNEL1
"msg2"
192.168.15.201:6379> RPOP CHANNEL1
"msg3"
发布者订阅者模式
在发布者订阅者模式下,发布者将消息发布到指定的channel里面,凡是监听到该channel的消费者都会收到同样的一份消息。同一个消息可以被多个消费者所消费。类似于收音机的广播模式,凡是收听这个频道的都能收到相同的消息内容
# 先订阅
192.168.15.201:6379> SUBSCRIBE channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
# 在channel1里面发送消息
192.168.15.201:6379> PUBLISH channel1 msg1
(integer) 1
# 查看
192.168.15.201:6379> SUBSCRIBE channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "msg1"
# 监听所有频道
192.168.15.201:6379> PSUBSCRIBE *
Redis的基本命令
# config
# 查看当前的redis配置
192.168.15.201:6379> config get *
192.168.15.201:6379> config set maxclients 1024 #临时更改,永久生效改配置文件
# info
# 当前的运行状态
# select
# 切换数据库
# keys
# 查看当前库下的key
# bgsave
# 手动在后台执行RDB持久化操作
# dbsize
# 查看有多少key
Redis高可用与集群
redis虽然可以实现单机的数据持久化,但是无论是RDB模式还是AOF模式,都解决不了单点故障宕机的问题,一旦单台的Redis服务器本身出现系统故障、硬件故障等问题后,就会直接造成数据的丢失,因此需要使用另外的技术来解决单点问题。
配置Redis的主从
主备模式,应用程序连接到高可用的负载的vip,然后连接到负载服务器设置的Redis后端real server。
# 注意
slave也要开启数据持久化并且要和master配置同样的连接密码,因为后期slave会有提升为master的可能,master宕机切换的话,原master的数据会被全部清空,会同步新的master的数据。
确保三台主机的redis版本必须保持一致,避免冲突,选择3.0以上的,支持增量同步
配置从库
3和4版本的命令
slaveof
127.0.0.1:6379> SLAVEOF 192.168.15.201 6379
OK
127.0.0.1:6379> CONFIG SET masterauth 123456
OK
# 最好是在配置文件中去配置masterauth的密码
slaveof 192.168.15.201 6379
masterauth 123456
127.0.0.1:6379> info
# Replication
role:slave
master_host:192.168.15.201
master_port:6379
master_link_status:up
# 4以上
replicaof
# 主库插入数据
#!/bin/bash
for i in `seq 1 100`
do
redis-cli -h `hostname -i` -a 123456 SET ${i} v${i}
echo "${i}写入成功"
done
127.0.0.1:6379> get 99
"v99"
# 发现数据已经到了
127.0.0.1:6379> SLAVEOF no one # 手动提升为master
优化
...
repl-diskless-sync no
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
...
# 详见上一章,可以自定义
哨兵
哨兵需要做好一主两从的架构为基础
配置文件
[root@db01 redis-4.0.9]# grep "^[a-Z]" sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "sentinel_26379.log"
dir "/apps/redis/data"
sentinel monitor mymaster 192.168.15.201 6379 2 # 选举人数,有几个slave认为master down了就进行故障转移
sentinel auth-pass mymaster 123456 # 密码
sentinel down-after-milliseconds mymaster 30000 # 主观下线的时间
sentinel parallel-syncs mymaster 1 # 发生故障转移的时候向新的master同步数据的slave的数量,数字越小总的同步时间就越长
sentinel failover-timeout mymaster 180000 # 所有slave指向新的master所需要的超时时间
sentinel deny-scripts-reconfig yes # 禁止修改脚本
bind 0.0.0.0
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "sentinel_26379.log"
dir "/apps/redis/data"
sentinel monitor mymaster 192.168.15.201 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
# 查看日志验证启动
[root@db01 data]# cat sentinel_26379.log
4583:X 25 Aug 21:27:15.301 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4583:X 25 Aug 21:27:15.302 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4583, just started
4583:X 25 Aug 21:27:15.302 # Configuration loaded
4584:X 25 Aug 21:27:15.352 * Running mode=sentinel, port=26379.
4584:X 25 Aug 21:27:15.353 # Sentinel ID is a5a9fed5d725b066c7acef9ee43ea5745fb6ffc9
4584:X 25 Aug 21:27:15.353 # +monitor master mymaster 192.168.15.201 6379 quorum 2
4584:X 25 Aug 21:27:15.354 * +slave slave 192.168.15.202:6379 192.168.15.202 6379 @ mymaster 192.168.15.201 6379
4584:X 25 Aug 21:27:15.355 * +slave slave 192.168.15.204:6379 192.168.15.204 6379 @ mymaster 192.168.15.201 6379
4584:X 25 Aug 21:27:22.067 * +sentinel sentinel b05ddcebecec3cf1e109157c99221030b5c52248 192.168.15.202 26379 @ mymaster 192.168.15.201 6379
4584:X 25 Aug 21:27:24.853 * +sentinel sentinel c3826bee68d435e3d75af5f9fec352564e4e1aff 192.168.15.204 26379 @ mymaster 192.168.15.201 6379
# 查看端口
[root@db01 data]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 *:26379 *:*
测试
[root@db01 data]# systemctl stop redis
# 查看日志
4725:X 25 Aug 21:35:12.480 # +switch-master mymaster 192.168.15.201 6379 192.168.15.202 6379
4725:X 25 Aug 21:35:12.480 * +slave slave 192.168.15.204:6379 192.168.15.204 6379 @ mymaster 192.168.15.202 6379
4725:X 25 Aug 21:35:12.481 * +slave slave 192.168.15.201:6379 192.168.15.201 6379 @ mymaster 192.168.15.202 6379
# 修复完毕后以新的slave加入
# 查看日志,看宕机的原因,redis日志,系统日志
# 如果是内存不足,那么就添加内存条
Redis cluster
早期的redis分布式集群部署方案
1、客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己处理写入分配,高可用管理和故障转移,一般不会用的,了解就行。
2、代理方案,基于第三方软件实现redis proxy,客户端先连接代理层,代理层再分配,但是性能不是很高,而且代理有单点故障的风险。
# 打开集群配置
cluster-enabled yes
cluster-config-file nodes-6379.conf
[root@db01 etc]# echo "/apps/redis/bin/redis-server /apps/redis/etc/redis_6380.conf" > /usr/local/bin/redis_6380
[root@db01 redis]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5153/redis-server 0
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 5159/redis-server 0
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1108/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1437/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1668/master
tcp 0 0 0.0.0.0:16379 0.0.0.0:* LISTEN 5153/redis-server 0
tcp 0 0 0.0.0.0:16380 0.0.0.0:* LISTEN 5159/redis-server 0
初始化
# 进入源码包
[root@db01 redis-4.0.9]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
# 在src目录下有个redis-trib.rb 拷贝走
[root@db01 src]# cp redis-trib.rb /usr/bin/
# 装ruby
# 安装依赖
[root@db01 ~]# yum install zlib-devel -y
[root@db01 ~]# yum install openssl-devel
[root@db01 ~]# wget https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.2.tar.gz
[root@db01 ruby-3.0.2]# ./configure
[root@db01 ruby-3.0.2]# make
[root@db01 ruby-3.0.2]# make install
# 装gem命令
[root@db01 ruby-3.0.2]# yum install rubygems -y
[root@db01 ruby-3.0.2]# gem install redis
# 装不下来也可以离线安装
https://rubygems.org/gems/redis
[root@db01 ~]# gem install -l redis-4.2.4.gem
# 验证
[root@db01 ~]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN # 创建集群
--replicas <arg> # master副本数
check host:port # 检查集群信息
info host:port # 查看集群主机信息
fix host:port # 修复集群
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port# 添加主机
--slave
--master-id <arg>
del-node host:port node_id #删除主机
set-timeout host:port milliseconds # 设置超时时间
call host:port command arg arg .. arg
import host:port # 导入外部redis服务器的数据到当前集群
--from <arg>
--copy
--replace
配置
# 修改密码redis的登陆密码
vim /usr/local/lib/ruby/gems/3.0.0/gems/redis-4.2.4/lib/redis/client.rb
# 所以密码必须要统一
[root@db01 ~]# redis-trib.rb create --replicas 1 192.168.15.201:6379 192.168.15.201:6380 192.168.15.202:6379 192.168.15.202:6380 192.168.15.204:6379 192.168.15.204:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.15.201:6379
192.168.15.202:6379
192.168.15.204:6379
Adding replica 192.168.15.202:6380 to 192.168.15.201:6379
Adding replica 192.168.15.204:6380 to 192.168.15.202:6379
Adding replica 192.168.15.201:6380 to 192.168.15.204:6379
M: b39863243f14b11cb6db2ec7c87f211d8de9d8ae 192.168.15.201:6379
slots:0-5460 (5461 slots) master
S: e238d456c6a823d673e3feff0214d2c7489dc63a 192.168.15.201:6380
replicates cb22d64402d7097fb0b4dfbe35895261b8ce51ac
M: 89392df901c8362612c5900c9b3765641f40e448 192.168.15.202:6379
slots:5461-10922 (5462 slots) master
S: e238d456c6a823d673e3feff0214d2c7489dc63a 192.168.15.202:6380
replicates b39863243f14b11cb6db2ec7c87f211d8de9d8ae
M: cb22d64402d7097fb0b4dfbe35895261b8ce51ac 192.168.15.204:6379
slots:10923-16383 (5461 slots) master
S: e238d456c6a823d673e3feff0214d2c7489dc63a 192.168.15.204:6380
replicates 89392df901c8362612c5900c9b3765641f40e448
Can I set the above configuration? (type 'yes' to accept):
# 如果是redis5的话
redis-cli -a 123456 --cluster create 192.168.15.201:6379 192.168.15.201:6380 192.168.15.202:6379 192.168.15.202:6380 192.168.15.204:6379 192.168.15.204:6380
# 验证
127.0.0.1:6379> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:491
cluster_stats_messages_pong_sent:489
cluster_stats_messages_sent:980
cluster_stats_messages_ping_received:484
cluster_stats_messages_pong_received:489
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:978
127.0.0.1:6379> CLUSTER NODES
89392df901c8362612c5900c9b3765641f40e448 192.168.15.202:6379@16379 master - 0 1629905954000 3 connected 5461-10922
b39863243f14b11cb6db2ec7c87f211d8de9d8ae 192.168.15.201:6379@16379 myself,master - 0 1629905955000 1 connected 0-5460
e238d456c6a823d673e3feff0214d2c7489dc63a 192.168.15.204:6380@16380 slave 89392df901c8362612c5900c9b3765641f40e448 0 1629905954901 6 connected
cb22d64402d7097fb0b4dfbe35895261b8ce51ac 192.168.15.204:6379@16379 master - 0 1629905955918 5 connected 10923-16383
写入测试
[root@db01 ~]# redis-cli -a 123456 # 在201里面写入数据报错
192.168.15.201:6379> set key1 value1
(error) MOVED 9189 192.168.15.202:6379
[root@db02 apps]# redis-cli -a 123456 # 在202里面写入数据成功
192.168.15.202:6379> set key1 value1
OK
192.168.15.204:6380> KEYS * # 在从节点查询的到数据
1) "key1"