数据库
首页 > 数据库> > 4.Redis集群

4.Redis集群

作者:互联网

一、Redis主从复制

1.什么是Redis主从复制

主从复制,是将一台Redis服务器的数据,复制到其他的Redis服务器

默认情况下,每台Redis服务器都是主节点;

2.Redis主从复制的作用

3、Redis主从复制的流程

4、Redis主从复制的搭建

主机 操作系统 IP 安装包
Master CentOS7(7-4) 192.168.239.40 redis-5.0.7.tar
Slave1 CentOS7(7-5) 192.168.239.50 redis-5.0.7.tar
Slave2 CentOS7(7-6) 192.168.239.60 redis-5.0.7.tar

准备工作:

systemctl stop firewalld
setenforce 0

(1)安装Redis (所有主机)

yum install -y gcc gcc-c++ make
cd /opt
#把包拉到opt

tar zxvf redis-5.0.7.tar.gz

cd /opt/redis-5.0.7/
make && make PREFIX=/usr/local/redis install

cd /opt/redis-5.0.7/utils
./install_server.sh
......
#回车四次后,手动输入,需要一次性输入正确
Please select the redis executable path [] /usr/local/redis/bin/redis-server 

ln -s /usr/local/redis/bin/* /usr/local/bin/
netstat -natp | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

(2)修改Master节点Redis配置文件

Master: 192.168.239.40

vim /etc/redis/6379.conf
#70行,修改bind 项,0.0.0.0监听所有网段(在生产环境中建议绑定自己的物理地址)
bind 0.0.0.0
#137行,开启守护进程
daemonize yes
#172行,指定日志文件目录
logfile /var/log/redis_6379.log
#264行,指定工作目录
dir /var/lib/redis/6379
#700行,开启AOF持久化功能
appendonly yes

/etc/init.d/redis_6379 restart
netstat -natp | grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      22197/redis-server  
tcp        0      0 127.0.0.1:6379          127.0.0.1:55452         TIME_W

(3)修改Slave节点Redis配置文件

Slave: 192.168.239.50

Slave: 192.168.239.60

vim /etc/redis/6379.conf
#70行,修改bind 项,0.0.0.0监听所有网卡
bind 0.0.0.0
#137行,开启守护进程
daemonize yes
#172行,指定日志文件目录
logfile /var/log/redis_6379.log
#264行,指定工作目录
dir /var/lib/redis/6379
#288行,添加要同步的Master节点IP和端口
replicaof 192.168.239.40 6379
#700行,开启AOF持久化功能
appendonly yes

/etc/init.d/redis_6379 restart
#利用scp传给60机器
scp /etc/redis/6379.conf 192.168.239.60:/etc/redis/
#去60机器查看
vim /etc/redis/6379.conf
70G
/etc/init.d/redis_6379 restart

(4)验证主从

Master: 192.168.239.40

在Master节点看日志

tail -f /var/log/redis_6379.log
22197:M 10 Jun 2022 15:27:30.114 * Background saving terminated with success
22197:M 10 Jun 2022 15:27:30.114 * Synchronization with replica 192.168.239.50:6379 succeeded
22197:M 10 Jun 2022 15:27:34.154 * Replica 192.168.239.60:6379 asks for synchronization
22197:M 10 Jun 2022 15:27:34.154 * Full resync requested by replica 192.168.239.60:6379
22197:M 10 Jun 2022 15:27:34.154 * Starting BGSAVE for SYNC with target: disk
22197:M 10 Jun 2022 15:27:34.154 * Background saving started by pid 22488
22488:C 10 Jun 2022 15:27:34.155 * DB saved on disk
22488:C 10 Jun 2022 15:27:34.156 * RDB: 0 MB of memory used by copy-on-write
22197:M 10 Jun 2022 15:27:34.252 * Background saving terminated with success
22197:M 10 Jun 2022 15:27:34.252 * Synchronization with replica 192.168.239.60:6379 succeeded #显示这个说明主从设置成功

在Master上验证从节点

redis-cli info replication
[root@localhost utils]# redis-cli 
info replication
# Replication
role:master
connected_slaves:2  #显示端口连接地址、状态等
slave0:ip=192.168.239.50,port=6379,state=online,offset=2142,lag=1
slave1:ip=192.168.239.60,port=6379,state=online,offset=2142,lag=0
master_replid:f72c7910bc82b662b7718b1f24be959a9f2a9c0c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2142
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2142
[root@localhost utils]# redis-cli
127.0.0.1:6379> KEYS *
(empty list or set)
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> set hobby dancing
OK
127.0.0.1:6379> KEYS *
1) "hobby"
2) "name"

从服务器上也可验证

[root@localhost utils]# redis-cli
127.0.0.1:6379> KEYS *
1) "hobby"
2) "name"

二、哨兵模式

功能:在主从复制的基础上,哨兵加入了主节点的自动故障转移

1.哨兵模式的原理

2.哨兵模式的作用

3.哨兵模式的结构

哨兵结构由两部分组成,哨兵节点和数据节点:

哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式,所有节点上都需要部署哨兵模式,哨兵模式会监控所有的 Redis 工作节点是否正常,当 Master 出现问题的时候,因为其他节点与主节点失去联系,因此会投票,投票过半就认为这个 Master 的确出现问题,然后会通知哨兵间,然后从 Slaves 中选取一个作为新的 Master。

4.架构模式的搭建

环境配置

主机 操作系统 IP 安装包
Master CentOS7(7-4) 192.168.239.40 redis-5.0.7.tar
Slave1 CentOS7(7-5) 192.168.239.50 redis-5.0.7.tar
Slave2 CentOS7(7-6) 192.168.239.60 redis-5.0.7.tar
哨兵1 CentOS7(7-1) 192.168.239.10 redis-5.0.7.tar
哨兵2 CentOS7(7-2) 192.168.239.20 redis-5.0.7.tar
哨兵3 CentOS7(7-3) 192.168.239.30 redis-5.0.7.tar

1.先安装哨兵的Redis

(1)修改Redis配置文件(所有节点操作)

vim /opt/redis-5.0.7/sentinel.conf
#17行,关闭保护模式
protected-mode no
#21行,Redis哨兵默认的监听端口
port 26379
#26行,指定sentinel为后台启动
daemonize yes
#36行,指定日志存放路径
logfile "/var/log/sentinel.log"
#65行,指定数据库存放路径
dir "/var/lib/redis/6379"
#84行,指定该哨兵节点监控192.168.163.10:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关
#至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel monitor mymaster 192.168.239.40 6379 2
#113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel down-after-milliseconds mymaster 30000
#146行,故障节点的最大超时时间为180000(180秒)
sentinel failover-timeout mymaster 180000

哨兵2和3配置

cd /opt/redis-5.0.7/
cp sentinel.conf{,.bak}

把哨兵1的配置文件复制到哨兵2和3

scp sentinel.conf 192.168.239.20:`pwd`

去哨兵2和3检查一下

vim sentinel.conf

(2)启动哨兵模式

先启动master,在启动slave(三台都启动)

redis-sentinel sentinel.conf &
ps aux | grep sentinel
root      58883  0.2  0.1 153844  2636 ?        Ssl  17:04   0:00 redis-sentinel *:26379 [sentinel]
root      58901  0.0  0.0 112676   976 pts/1    R+   17:05   0:00 grep --color=auto sentinel
[1]+  完成                  redis-sentinel sentinel.conf

(3)查看哨兵模式信息

redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# 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=192.168.239.40:6379,slaves=2,sentinels=3 #主机名,状态正常,地址是192.168.239.40,2个slaves,3个哨兵

(4)故障模拟

#查看哨兵日志
[root@localhost redis-5.0.7]# tail -f /var/log/sentinel.log 
45470:X 10 Jun 2022 17:04:08.442 # Configuration loaded
45471:X 10 Jun 2022 17:04:08.443 * Increased maximum number of open files to 10032 (it was originally set to 1024).
45471:X 10 Jun 2022 17:04:08.445 * Running mode=sentinel, port=26379.
45471:X 10 Jun 2022 17:04:08.445 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
45471:X 10 Jun 2022 17:04:08.445 # Sentinel ID is 89c8955f34e05c54b49a5b35a0c3b6e5a00f7104
45471:X 10 Jun 2022 17:04:08.445 # +monitor master mymaster 192.168.239.40 6379 quorum 2
45471:X 10 Jun 2022 17:04:08.447 * +slave slave 192.168.239.50:6379 192.168.239.50 6379 @ mymaster 192.168.239.40 6379
45471:X 10 Jun 2022 17:04:08.448 * +slave slave 192.168.239.60:6379 192.168.239.60 6379 @ mymaster 192.168.239.40 6379
45471:X 10 Jun 2022 17:04:21.587 * +sentinel sentinel 4f126df4b59619ed88a72286e79540cf08301350 192.168.239.20 26379 @ mymaster 192.168.239.40 6379
45471:X 10 Jun 2022 17:04:24.912 * +sentinel sentinel 89f28a27925190e1cb291a981728beeabb0affa2 192.168.239.30 26379 @ mymaster 192.168.239.40 6379
#主服务器配置
[root@localhost ~]# ps -ef | grep redis
root      22197      1  0 15:16 ?        00:00:11 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root      60890  15267  0 15:55 pts/4    00:00:00 redis-cli
root      61673  61630  0 17:14 pts/1    00:00:00 grep --color=auto redis
[root@localhost ~]# cat /var/run/redis_6379.pid 
22197
[root@localhost ~]# kill -9 22197

(5)验证结果

#查看哨兵
45471:X 10 Jun 2022 17:20:23.200 # +sdown master mymaster 192.168.239.40 6379
45471:X 10 Jun 2022 17:20:23.289 # +new-epoch 1
45471:X 10 Jun 2022 17:20:23.290 # +vote-for-leader 4f126df4b59619ed88a72286e79540cf08301350 1
45471:X 10 Jun 2022 17:20:24.303 # +config-update-from sentinel 4f126df4b59619ed88a72286e79540cf08301350 192.168.239.20 26379 @ mymaster 192.168.239.40 6379
45471:X 10 Jun 2022 17:20:24.303 # +switch-master mymaster 192.168.239.40 6379 192.168.239.60 6379  #从40切换到60
45471:X 10 Jun 2022 17:20:24.303 * +slave slave 192.168.239.50:6379 192.168.239.50 6379 @ mymaster 192.168.239.60 6379  #对接新的Master
45471:X 10 Jun 2022 17:20:24.303 * +slave slave 192.168.239.40:6379 192.168.239.40 6379 @ mymaster 192.168.239.60 6379  #对接新的Master
45471:X 10 Jun 2022 17:20:54.372 # +sdown slave 192.168.239.40:6379 192.168.239.40 6379 @ mymaster 192.168.239.60 6379

查看哨兵状态

[root@localhost redis-5.0.7]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# 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=192.168.239.60:6379,slaves=2,sentinels=3    #此时主节点是60

查看从节点7-5的配置文件

[root@localhost utils]# vim /etc/redis/6379.conf
#看288行 replicaof 192.168.239.60 6379
#现在监听的60,原本监听的是40

查看原来是主节点(现在的从节点)7-4的配置文件

[root@localhost utils]# vim /etc/redis/6379.conf
#看288行 是空的

查看哨兵模式的配置文件

vim /opt/redis-5.0.7/sentinel.conf 
#看121行,此时监听的是60
sentinel monitor mymaster 192.168.239.60 6379 2

启动7-4的Redis

cd /var/run/
rm -f redis_6379.pid
/etc/init.d/redis_6379 start
netstat -natp | grep 6379
vim /etc/redis/6379.conf 
#到底添加了 replicaof 192.168.239.60 6379
[root@localhost run]# redis-cli 
127.0.0.1:6379> info replication
# Replication
role:slave  #当前状态slave
master_host:192.168.239.60
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:957651
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ffc4755edbbb10fdf50a13c1ead61c44e42e3133
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:957651
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:904512
repl_backlog_histlen:53140

到7-6

[root@localhost utils]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.239.50,port=6379,state=online,offset=1012055,lag=1
slave1:ip=192.168.239.40,port=6379,state=online,offset=1012055,lag=0
master_replid:ffc4755edbbb10fdf50a13c1ead61c44e42e3133
master_replid2:f72c7910bc82b662b7718b1f24be959a9f2a9c0c
master_repl_offset:1012055
second_repl_offset:206232
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:1012041

三、Redis集群模式

1、redis群集的概念

2、集群的作用

(1)数据分区

3.Redis集群的数据分片

以3个节点组成的集群为例
节点A包含0到5460号哈希槽
节点B包含5461到10922号哈希槽
节点C包含10923到16383号哈希槽
Redis集群的主从复制模型
集群中具有A、B、C三个节点,如果节点B失败了,整个集群就会因缺少5461-10922这个范围的槽而不可以用。
为每个节点添加一个从节点A1、B1、C1整个集群便有三个Master节点和三个slave节点组成,在节点B失败后,集群选举B1位为的主节点继续
服务。当B和B1都失败后,集群将不可用

4.搭建Redis集群模式

redis的集群一般需要6个节点,3主3从。
先在六台服务器上安装redis数据库

主机 操作系统 IP:端口 安装包
Master1 CentOS7 192.168.239.10:6371 redis-5.0.7.tar.gz
Master2 CentOS7 192.168.239.20:6371 redis-5.0.7.tar.gz
Master3 CentOS7 192.168.239.30:6371 redis-5.0.7.tar.gz
Slave1 CentOS7 192.168.239.40:6371 redis-5.0.7.tar.gz
Slave2 CentOS7 192.168.239.50:6371 redis-5.0.7.tar.gz
Slave3 CentOS7 192.168.239.60:6371 redis-5.0.7.tar.gz

这里使用不同的节点只是想说redis集群模式可以指定端口,不是只能使用6379端口,如果不想改的话也可以全都使用一个端口,这样创建目录和修改配置的时候更方便,所有节点除了监听的ip不一样以外没什么不同。

(1)安装Redis

(2)修改配置文件,开启群集功能

所有节点
先设置一个节点

cd /etc/redis/
mkdir -p redis-cluster/redis6371
cd /etc/redis/redis-cluster/redis6371
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis6371/
cp /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis6371/
cd /etc/redis/redis-cluster/redis6371
vim redis.conf
#69行,修改bind项,监听自己的IP
bind 192.168.239.10
#88行,修改,关闭保护模式
protected-mode no
#92行,修改,redis监听端口
port 6371
#136行,以独立进程启动
daemonize yes
#699行,修改,开启AOF持久化
appendonly yes
#832行,取消注释,开启群集功能
cluster-enabled yes
#840行,取消注释,修改,群集名称文件设置
cluster-config-file nodes-6371.conf
#846行,取消注释,群集超时时间设置
cluster-node-timeout 15000

在这个节点上配置好的文件复制过去

scp /etc/redis/redis-cluster/redis6371/redis.conf root@192.168.239.20:/etc/redis/redis-cluster/redis6371/redis.conf

其他服务器上

cd /etc/redis/redis-cluster/redis6371
vim redis.conf
#69行,修改bind项,监听自己的IP
bind 192.168.239.20
#92行,修改,redis监听端口
port 6371
#840行,取消注释,修改,群集名称文件设置
cluster-config-file nodes-6371.conf

(3)启动redis节点

#每台服务器进入对应的文件中,执行命令
cd /etc/redis/redis-cluster/redis6371/
redis-server redis.conf #启动服务

ps -ef |  grep redis #查看服务是否启动成功

(4)启动集群

redis-cli --cluster create 192.168.239.10:6371 192.168.239.20:6371 192.168.239.30:6371 192.168.239.40:6371 192.168.239.50:6371 192.168.239.60:6371 --cluster-replicas 1
#
#启动集群 前三台是主后面是从
#下面是主从对应关系

(5)测试集群

[root@localhost redis6371]# redis-cli -h 192.168.239.10 -p 6371 -c
#加-c参数,节点之间就可以互相跳转	
192.168.239.10:6371> cluster slots    #查看节点的哈希槽编号范围
1) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "192.168.239.20"
      2) (integer) 6371
      3) "fe7b4a375ba6c2a67343feec39845470bfc79fa6"
   4) 1) "192.168.239.60"
      2) (integer) 6371
      3) "e7dca64799c7bf87628779e80795f6c7065159ac"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "192.168.239.10"
      2) (integer) 6371
      3) "a4bdd363acca9da9074a97f9a218c337525b9fd2"
   4) 1) "192.168.239.50"
      2) (integer) 6371
      3) "079ed6422defd9d4b2b74d8ce99bfe9877aacd26"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "192.168.239.30"
      2) (integer) 6371
      3) "33af43db97d242d598eced95627bad155cd592bc"
   4) 1) "192.168.239.40"
      2) (integer) 6371
      3) "cfa9015d6c4bdf384787d66838389093ed504b03"
192.168.239.10:6371> set name davin   #设置一个name键
-> Redirected to slot [5798] located at 192.168.239.20:6371
OK                                 #在20写入
192.168.239.20:6371> KEYS *
1) "name"
192.168.239.20:6371> quit
[root@localhost redis6371]# redis-cli -h 192.168.239.10 -p 6371 -c
192.168.239.10:6371> KEYS *       #没有
(empty list or set)
192.168.239.10:6371> quit
[root@localhost redis6371]# redis-cli -h 192.168.239.60 -p 6371 -c
192.168.239.60:6371> KEYS *          #有,因为60是20的从
1) "name"
192.168.239.60:6371> quit
[root@localhost redis6371]# redis-cli -h 192.168.239.30 -p 6371 -c
192.168.239.30:6371> KEYS *              #没有,但是可以查到,查的20
(empty list or set)
192.168.239.30:6371> get name
-> Redirected to slot [5798] located at 192.168.239.20:6371
"davin"
192.168.239.20:6371> cluster keyslot name  #查看键对应的哈希槽编号
(integer) 5798

标签:Redis,redis,192.168,6379,集群,sentinel,6371,节点
来源: https://www.cnblogs.com/DavinWw/p/16391062.html