redis集群和哨兵_-Redis主从、哨兵与集群搭建 - #masterauth

📅 2026-12-01 👤 拼多多助力营销技巧 📂 pdd助力 👁️ 463 次阅读

一、Redis主从模式

主从模式搭建:

编号 ···········IP ···········Redis版本·角色

redis01 192.168.88.111 redis-7.4.0 master

redis02 192.168.88.112 redis-7.4.0 slave

安装Redis:

wget https://download.redis.io/releases/redis-7.4.0.tar.gz

tar -zxvf redis-7.4.0.tar.gz

cd redis-7.4.0

make

make PREFIX=/usr/local/redis install

mkdir /usr/local/redis/conf -p

对以下配置作出修改:

cp redis.conf /usr/local/redis/conf

bind 0.0.0.0 # 允许所有IP连接

daemonize yes # 允许后台运行

#过量使用内存设置为0!在低内存环境下,后台保存可能失败

sudo vim /etc/sysctl.conf

vm.overcommit_memory = 1

sysctl -p

第一步:配置master

#vim /usr/local/redis/conf/redis.conf

开启监听,在网络中与其他服务器进行网络交互的网卡,默认ens33。bind的ip指的是其他的主机需要和这个主机通讯的ip:

PowerShell

88行 bind 0.0.0.0 # 允许所有IP连接

112行 protected-mode no # 关闭redis安全保护机制,允许主从、哨兵、集群中各节点之间的相互访问,没有安全限制

第二步:配置slave

#vim /usr/local/redis/conf/redis.conf

主从配置:设置master redis信息

redis5/6/7 版本 # replicaof 192.168.88.111 6379

第三步:重启redis服务器,查看是否成功

bin/redis-cli

127.0.0.1:6379>info replication

第四步:测试redis主从,主写从读

127.0.0.1:6379 master > set title itheima

127.0.0.1:6379 slave > keys *

127.0.0.1:6379 slave > get title

注:slave不允许写操作,因为从服务器redis配置文件里进行了replica-read-only设置,也是符合业务的使用需求

replica-read-only yes

二、Redis哨兵模式

一、Redis哨兵概述

主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费时费力,还会造成一段时间内服务不可用。这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式。

哨兵作用:就是当主服务器宕机后,自动把从服务器升级为主服务器过程。(健康检查、故障切换)

1、什么是哨兵

哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。

2、哨兵的作用

这里的哨兵有两个作用

•·健康检查:通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。

•·故障切换:当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。

然而一个哨兵进程对Redis服务器进行监控,可能会出现问题,为此,我们可以使用多个哨兵进行监控。各个哨兵之间还会进行监控,这样就形成了多哨兵模式。

用文字描述一下故障切换(failover)的过程。假设主服务器宕机,哨兵1先检测到这个结果,系统并不会马上进行failover过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象成为主观下线。当后面的哨兵也检测到主服务器不可用,并且数量达到一定值时,那么哨兵之间就会进行一次投票,投票的结果由一个哨兵发起,进行failover操作。切换成功后,就会通过发布订阅模式,让各个哨兵把自己监控的从服务器实现切换主机,这个过程称为客观下线。这样对于客户端而言,一切都是透明的。

3、前期准备

配置3个哨兵和1主2从的Redis服务器来演示这个过程。

服务类型 是否是主服务器 ·····IP地址 ·········端口

Redis ·············是 ········192.168.88.111 ···6379

Redis ·············否 ········192.168.88.112 ···6379

Redis ·············否 ········192.168.88.113 ···6379

Sentinel ·········- ···········192.168.88.111 ··26379

Sentinel ·········- ···········192.168.88.112 ··26379

Sentinel ·········- ···········192.168.88.113 ··26379

特别注意:使用Redis哨兵模式,最少需要3个节点(一主多从结构)

二、Redis主从复制搭建

在搭建redis哨兵模式前,我们应该先对redis服务器进行主从搭建

哨兵模式 + 集群模式,都配置密码,默认是123,所以建议大家也使用123作为默认密码。

1、Redis主从配置

① 安装redis

第一步:找到对应的安装包资源,使用wget命令下载,这里安装的7.4.0版本。

安装包资源地址:https://download.redis.io/releases/

第二步:上传Redis到Linux系统中

dnf install wget -y

wget https://download.redis.io/releases/redis-7.4.0.tar.gz

第三步:配置=>编译=>安装

shell > tar -zxvf redis-7.4.0.tar.gz

shell > cd redis-7.4.0

shell>make

shell>make PREFIX=/usr/local/redis install

② redis01/redis02/redis03修改配置

sudo mkdir -p /usr/local/redis/conf

sudo cp redis.conf /usr/local/redis/conf/

#修改配置

sudo vim /usr/local/redis/conf/redis.conf

88: bind 127.0.0.1 -::1 -->bind 0.0.0.0

1050: requirepass foobared -->requirepass 123

310: daemonize no -->daemonize yes

#添加redis到环境变量

sudo vim /etc/profile

export PATH="$PATH:/usr/local/redis/bin"

source /etc/profile

#过量使用内存设置为0!在低内存环境下,后台保存可能失败

sudo vim /etc/sysctl.conf

vm.overcommit_memory = 1

sysctl -p

③ 启动 redis

sudo vim /etc/systemd/system/redis.service

[Unit]

Description=redis-server

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf

PrivateTmp=true

[Install]

WantedBy=multi-user.target

#systemctl start redis

#添加redis到环境变量

sudo vim /etc/profile

export PATH="$PATH:/usr/local/redis/bin"

source /etc/profile

#过量使用内存设置为0!在低内存环境下,后台保存可能失败

sudo vim /etc/sysctl.conf

vm.overcommit_memory = 1

sysctl -p

我们可以看到,redis 现在的角色是一个master 启动的服务。

master主节点设置:

bind 0.0.0.0 # 允许所有IP连接

protected-mode no # 禁用保护模式(确保访问受防火墙控制)

requirepass 123 # 设置从服务器连接需要使用密码

masterauth 123 # 连接主节点时需要使用的主节点密码

----------------------------------------------------------------------

protected-mode:默认代表只允许127.0.0.1访问,集群、哨兵等模式必须关闭此参数

2、配置slave

和上面配置 master一样,我们需要修改端口号和pid 文件,在修改完之后,我们有两种方法配置从服务。

① 在配置文件中配置从服务

################################# REPLICATION #################################

#Master-Replica replication. Use replicaof to make a Redis instance a copy of

#another Redis server. A few things to understand ASAP about Redis replication.

#+------------------+ +---------------+

#| Master | --->| Replica |

#| (receive writes) | | (exact copy) |

#+------------------+ +---------------+

#

#1) Redis replication is asynchronous, but you can configure a master to

#stop accepting writes if it appears to be not connected with at least

#a given number of replicas.

#2) Redis replicas are able to perform a partial resynchronization with the

#master if the replication link is lost for a relatively small amount of

#time. You may want to configure the replication backlog size (see the next

#sections of this file) with a sensible value depending on your needs.

#3) Replication is automatic and does not need user intervention. After a

#network partition replicas automatically try to reconnect to masters

#and resynchronize with them.

#

#replicaof

replicaof 192.168.88.111 6379

我们可以在配置文件中直接修改 slaveof 属性,我们直接配置主服务器的IP地址和端口号,如果这里主服务器有配置密码。

可以通过配置masterauth 来设置链接密码:

#If the master is password protected (using the "requirepass" configuration

#directive below) it is possible to tell the slave to authenticate before 拼多多帮助力入口

#starting the replication synchronization process, otherwise the master will

#refuse the slave request.

#

#masterauth

masterauth 123

② 启动redis 服务:

sudo vim /etc/systemd/system/redis.service

[Unit]

Description=redis-server

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf

PrivateTmp=true

[Install]

WantedBy=multi-user.target

#systemctl start redis

#添加redis到环境变量

sudo vim /etc/profile

export PATH="$PATH:/usr/local/redis/bin"

source /etc/profile

#过量使用内存设置为0!在低内存环境下,后台保存可能失败

sudo vim /etc/sysctl.conf

vm.overcommit_memory = 1

sysctl -p

使用info命令,查看一下slave主机的状态:

#Replication

role:slave

master_host:127.0.0.1

master_port:6379

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:71

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

我们可以看到,现在的redis 是一个从服务的角色,连接着6379的服务。接下来我们再来看一下目前master 的状态:

#Replication

role:master

connected_slaves:2

slave0:ip=192.168.88.112,port=6379,state=online,offset=785,lag=0

slave1:ip=192.168.88.113,port=6379,state=online,offset=785,lag=0

master_repl_offset:785

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:784

我们如果需要设置读写分离,只需要在主服务器中设置:

#Note: read only slaves are not designed to be exposed to untrusted clients

#on the internet. It's just a protection layer against misuse of the instance.

#Still a read only slave exports by default all the administrative commands

#such as CONFIG, DEBUG, and so forth. To a limited extent you can improve

#security of read only slaves using 'rename-command' to shadow all the

#administrative / dangerous commands.

slave-read-only yes

三、Sentinel哨兵

1、配置Sentinel端口

在sentinel.conf 配置文件中, 我们可以找到port 属性,这里是用来设置sentinel 的端口,一般情况下,至少会需要三个哨兵对 redis 进行监控。

cd /root/redis-7.4.0

cp sentinel.conf /usr/local/redis/conf/

vim /usr/local/redis/conf/sentinel.conf

protected-mode no

port 26379

daemonize no

说明:daemonize no仅用于测试环境,生产环境下需要设置为daemonize yes,代表后台运行。

2、配置主服务器的IP和端口

redis01/redis02/redis03都要配置:

https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel/

#sentinel monitor

#

#Tells Sentinel to monitor this master, and to consider it in O_DOWN

#(Objectively Down) state only if at leastsentinels agree.

#

#Note that whatever is the ODOWN quorum, a Sentinel will require to

#be elected by the majority of the known Sentinels in order to

#start a failover, so no failover can be performed in minority.

#

#Slaves are auto-discovered, so you don't need to specify slaves in

#any way. Sentinel itself will rewrite this configuration file adding

#the slaves using additional configuration options.

#Also note that the configuration file is rewritten when a

#slave is promoted to master.

#

#Note: master name should not include special characters or spaces.

#The valid charset is A-z 0-9 and the three characters ".-_".

92行 sentinel monitor mymaster 192.168.88.111 6379 2

93行 sentinel auth-pass mymaster 123

注:这里的 2 是权值(阈值),表示至少需要 2 个哨兵确认,才能将主节点判定为客观下线。其原理是:某个哨兵首先发现 master 主节点无法连接(无响应),此时只会将其标记为主观下线;当确认故障的哨兵数量达到 2 台及以上时,才会标记为客观下线,并触发故障转移。高可用:在一个集群中(最少2台及以上节点),某个节点出现故障,集群依然可以对外提供相关服务(可用)

故障转移:failover,当主节点宕机,从节点升级为主节点

高可用往往包含健康检查以及故障转移等特性

3、启动所有的Sentinel进程

[root@yunwei ~] # redis-sentinel /usr/local/redis/conf/sentinel.conf

redis 和 sentinel是不同的,两者相互独立。redis有一个进程redis-server,sentinel有一个进程redis-sentinel。

4、手工关闭master

新创建一个mx窗口,针对redis01,然后手工关闭redis,模拟redis主节点故障

pkill redis-sever

我们手动关闭Master 之后,sentinel 在监听master 确实是断线了之后,将会开始计算权值,然后重新分配主服务器。

128799:X 29 May 12:08:35.657# +failover-end master mymaster 192.168.88.111 6379

128799:X 29 May 12:08:35.657# +switch-master mymaster 192.168.88.111 6379 192.168.88.113 6379

查看状态,发现已经发生故障转移,88.113升级为主节点

一旦failover发生时,系统会自动调整2个文件,redis.conf更改主节点信息,sentinel.conf最末端会写入一些选举等信息。

5、重连master

大家可能会好奇,如果master 重连之后,会不会抢回属于他的位置,答案是否定的,就比如你被一个小弟抢了你老大的位置,他肯给回你这个位置吗。因此当master回来之后,他也只能当个小弟。

6、Sentinel工作原理

① Master 状态监测

② 如果某个哨兵发现 Master 异常,先主观下线,超过节点阈值为2(代表至少2个哨兵发现Master异常),则标记客观下线。就会进行Master-Slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave

③ Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换。

哨兵模式:就是在主从架构的基础上,增加了一个failover故障切换功能,可以在秒级实现故障转移。从而实现Redis高可用架构。

三、Redis集群搭建

一、Redis集群概述

1、什么是Redis集群

2018年十月 Redis 发布了稳定版本的 5.0 版本,推出了各种新特性,其中一点是放弃 Ruby的集群方式,改为 使用 C语言编写的 redis-cli的方式,使集群的构建方式复杂度大大降低。关于集群的更新可以在 Redis5 的版本说明中看到,如下:

The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check redis-cli --cluster help for more info.

可以查看Redis官网查看集群搭建方式,连接如下

https://redis.io/topics/cluster-tutorial

以下步骤是在一台 Linux 服务器上搭建有6个节点的 Redis集群

注:实际运维工作中,大概需要3台机器,每台机器2个Redis实例。但是详细规划主从关系,尽量不要把一组主从放在同一台服务器中

redis01 redis02 redis03

···1主·······················

·············1从·············

·············2主······2从···

···3从················3从···

2、规划基础环境

IP地址:192.168.88.111 ~ 88.113(自行定义)

主机名称:redis01/redis02/redis03

3、下载源码并解压编译

dnf install wget -y

wget https://download.redis.io/releases/redis-7.4.0.tar.gz

tar xzf redis-7.4.0.tar.gz

cd redis-7.4.0

make

make PREFIX=/usr/local/redis install

4、清理Redis集群环境

redis01/redis02/redis03:

systemctl stop redis

cd /usr/local/redis

rm -rf conf/*

rm -rf dump.rdb

rm -rf appendonlydir

如果哨兵是后台运行,还可以使用pkill删除

pkill redis-sentinel

三、Redis集群配置

1、Redis集群实现

第一步:环境规划(3主3从,对外提供服务的一共是3个节点)

前提:Redis集群往往在搭建环境时必须要提前设计,而且Redis集群要求,在创建集群之前,任何都不能有数据!!!

编号 ·主机名称·····IP地址··········角色

··1·····redis01 192.168.88.111 redis7001

··2·····redis01 192.168.88.111 redis7002

··3·····redis02 192.168.88.112 redis7003

··4·····redis02 192.168.88.112 redis7004

··5·····redis03 192.168.88.113 redis7005

··6·····redis03 192.168.88.113 redis7006

设置之前,把Redis01、Redis02、Redis03机器上的所有Redis全部停止,删除所有配置文件redis.conf、sentinel.conf

第二步:在/usr/local/redis/conf目录中创建redis7001.conf…redis7006.conf

Redis01 => redis7001.conf/redis7002.conf

Redis02 => redis7003.conf/redis7004.conf

Redis03 => redis7005.conf/redis7006.conf

节点配置类似:

port 7001

cluster-enabled yes

cluster-config-file nodes_7001.conf

cluster-node-timeout 5000

appendonly yes

daemonize yes

protected-mode no

pidfile /var/run/redis_7001.pid

port 7002

cluster-enabled yes

cluster-config-file nodes_7002.conf

cluster-node-timeout 5000

appendonly yes

daemonize yes

protected-mode no

pidfile /var/run/redis_7002.pid

port 7003

cluster-enabled yes

cluster-config-file nodes_7003.conf

cluster-node-timeout 5000

appendonly yes

daemonize yes

protected-mode no

pidfile /var/run/redis_7003.pid

参考以上配置,准备7004 ~ 7006

注意更改:port、cluster-config-file、pidfile

第三步:配置完成后,启动Reids6个节点

#cd /usr/local/redis

#bin/redis-server conf/redis7001.conf

#bin/redis-server conf/redis7002.conf

#cd /usr/local/redis

#bin/redis-server conf/redis7003.conf

#bin/redis-server conf/redis7004.conf

#cd /usr/local/redis

#bin/redis-server conf/redis7005.conf

#bin/redis-server conf/redis7006.conf

第四步:创建集群(集群最少需要3个主节点)

Redis01 ··Redis02 ··Redis03

1主7001··1从7003····

··············2主7004··2从7005

3从7002················3主7006

bin/redis-cli --cluster create 192.168.88.111:7001 192.168.88.112:7004 192.168.88.113:7006 192.168.88.112:7003 192.168.88.113:7005 192[168.88.111:7002 --cluster-replicas 1

参数说明:

–cluster create创建集群,后面跟Redis及端口号

–cluster-replicas 1,每个主节点默认都有1个从节点

集群模式不需要提前搭建主从架构,而是在创建集群时,系统会自动配置主从,不需要人工干预

bin/redis-cli --cluster create ①主节点 ②主节点 ③主节点 ①从节点 ②从节点 ③从节点

常见问题说明:

问题描述:Redis集群要求Redis中不能有数据,包括appendonlydir、dump.rdb、nodes_700X.conf

解决方案:哪个节点报错,就清除哪个节点(以7001为例)

ps -ef |grep redis-server

找到7001对应的进程

kill -9 进程号

rm -rf appendonlydir

rm -rf dump.rdb

rm -rf node_7001.conf

清除完成后,重启Redis

bin/redis-server conf/redis7001.conf

2、测试集群

bin/redis-cli -c -h 主节点IP地址 -p 7001

选项说明:

-c代表已集群的方式进行连接

举个例子:

bin/redis-cli -c -h 192.168.88.111 -p 7001

3、关闭集群

#bin/redis-cli -c -h 192.168.88.111 -p 7001 shutdown

#bin/redis-cli -c -h 192.168.88.111 -p 7002 shutdown

#bin/redis-cli -c -h 192.168.88.112 -p 7003 shutdown

#bin/redis-cli -c -h 192.168.88.112 -p 7004 shutdown

#bin/redis-cli -c -h 192.168.88.113 -p 7005 shutdown

#bin/redis-cli -c -h 192.168.88.113 -p 7006 shutdown

4、集群重启

重启步骤非常简单,只需要把每台服务器的各个节点依次启动即可

#cd /usr/local/redis

#bin/redis-server conf/7001.conf

#bin/redis-server conf/7002.conf

#bin/redis-server conf/7003.conf

#bin/redis-server conf/7004.conf

#bin/redis-server conf/7005.conf

#bin/redis-server conf/7006.conf

5、代码端绑定Redis集群

配置好了Redis集群,Java/Python/PHP代码,如何实现绑定Redis集群?

Java

import redis.clients.jedis.HostAndPort;

import redis.clients.jedis.JedisCluster;

import java.util.HashSet;

import java.util.Set;

public class RedisClusterExample {

public static void main(String[] args) {

Setnodes = new HashSet<>();

nodes.add(new HostAndPort("192.168.88.111", 7001));

nodes.add(new HostAndPort("192.168.88.111", 7002));

nodes.add(new HostAndPort("192.168.88.112", 7003));

nodes.add(new HostAndPort("192.168.88.112", 7004));

nodes.add(new HostAndPort("192.168.88.113", 7005));

nodes.add(new HostAndPort("192.168.88.113", 7006));

// 构造 JedisCluster 客户端

JedisCluster jedisCluster = new JedisCluster(nodes);

// 写入和读取示例

jedisCluster.set("key1", "Hello Redis Cluster");

String value = jedisCluster.get("key1");

System.out.println("key1 = " + value);

}

}

Spring Boot框架,通过yaml进行配置,application.yaml

spring:

redis:

cluster:

nodes:

- 192.168.88.111:7001

- 192.168.88.111:7002

- 192.168.88.112:7003

- 192.168.88.112:7004

- 192.168.88.113:7005

- 192.168.88.113:7006

timeout: 5000

Python

pip install redis-py-cluster

------------------------------------------------------

from redis.cluster import RedisCluster

#Redis 节点列表(可用任意一部分节点,会自动发现集群拓扑)

startup_nodes = [

{"host": "192.168.88.111", "port": 7001},

{"host": "192.168.88.112", "port": 7004},

{"host": "192.168.88.113", "port": 7006}

]

#创建 RedisCluster 实例

rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

#测试写入与读取

rc.set("key3", "Hello from Python Redis Cluster")

print(rc.get("key3"))

PHP

$redis = new RedisCluster(

NULL,

[

'192.168.88.111:7001',

'192.168.88.112:7004',

'192.168.88.113:7006'

]

);

$redis->set("key5", "Hello from PHP phpredis cluster");

echo $redis->get("key5");

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧