Detailed tutorial on installing and configuring redis under Linux, and configuring sentinel mode, redis Chinese detailed explanation

The redis version is redis-3.2.12. Use tools to upload the installation package to the data directory.

Create a folder redis in the data directory and install redis in this directory.

The first step: unzip.

cd data
tar -zxvf redis-3.2.12.tar.gz

Step 2: Installation, PREFIX=/data/redis is used to set the installation directory.

cd redis-3.2.12/
make
make install PREFIX=/data/redis

At this point, redis has been installed, and the rest is to configure and start the service.

Enter the redis directory, create four directories of configuration file conf, log logs, database dump, and process number pid to store corresponding files. These four directories can also be stored in other folders, as long as they are consistent with the configuration in the configuration file. Yes, otherwise an error will be reported when starting the service. The bin directory is some command files after the successful installation of redis.

cd redis
mkdir conf
mkdir logs
mkdir dump
mkdir pid

The redis service is configured with one master and two slaves, sentinel mode. Note: If the sentinel mode is not used in actual development, the redis service can be configured with one master and one slave.

Main Redis configuration: redis_6379.conf

# Redis配置文件

# 请注意,为了读取配置文件,Redis必须以文件路径作为第一个参数启动:
# ./redis-server /data/redis/conf/redis_6379.conf

# 加载更多配置文件
# include /path/to/local.conf
# include /path/to/other.conf

# 默认Redis不以守护进程的方式运行。如果需要开启修改为"yes"。
# 在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中
# 此时redis将一直运行,除非手动kill该进程。
daemonize yes 

# 以守护进程运行时,Redis默认会将pid存入/var/run/redis.pid文件中。
# 指定pid存放位置和文件名
pidfile /data/redis/pid/redis_6379.pid

# 接收指定端口的连接,默认是6379。
port 6379

# 在每秒高请求的环境中,您需要提高backlog的配置,来避免客户端连接速度慢的问题。
# 注意,Linux内核会截断这个值,因此同时修改/proc/sys/net/core/somaxconn和tcp_max_syn_backlog的值,来获得期待的效果。
tcp-backlog 6000 

# 默认Redis监听所有能够链接服务器的网络链接。
# 可以使用"bind"配置指令只监听一个或多个接口,后面可以跟一个或多个ip地址。
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
bind 0.0.0.0

# 客户端空闲N秒后连接关闭(0表示永不超时)
timeout 0

# TCP保活机制,就是为了保证连接的有效性,探测连接的对端是否存活的作用,在间隔一定的时间发探测包,根据回复来确认该连接是否有效。
# Redis3.2.1版本之后默认值为300秒
tcp-keepalive 300

# 指定日志级别
# debug(大量新秀,针对开发/测试)
# verbose(大量的有用信息,但不像debug级别那样混乱)
# notice(一般的信息,可以在生产中使用)
# warning(只记录非常重要和关键的消息)
loglevel notice

# 指定日志存放位置和日志文件名称
logfile "/data/redis/logs/redis_6379.log"

# 设置数据库编号
databases 16

# 将数据库数据保存到磁盘策略:
#   save <seconds> <changes>
#   如果给定的秒数和对数据库的修改操作数两个条件同时满足,则都会触发保存。
#   下面的示例中会触发保存:
#   900秒(15分钟)后,如果有1个key被修改
#   300秒(5分钟)后,如果有10个key被修改
#   60秒后,如果有10000个key被修改
save 900 1
save 300 10
save 60 10000

# 最近一次保存失败,Redis将停止接受写入。
# 如果保存过程中Redis再次运行,RDB将会允许再次写入。
stop-writes-on-bgsave-error yes

# 开启RDB文件压缩,Redis会采用LZF算法进行压缩。
# 如果不想消耗CPU性能来进行文件压缩的话,可以设置为关闭此功能,这样的缺点是需要更多的磁盘空间来保存文件。
rdbcompression yes

# 自从RDB 5版本之后CRC64校验被放置在文件末尾。
# 开启RDB文件检查,检查是否有无损坏,如果在启动是检查发现损坏,则停止启动。
rdbchecksum yes

# 转储数据库的文件名
dbfilename dump_6379.rdb

# 指定转储数据库存放目录,必须指定
dir /data/redis/dump

# 主从复制关系,主redis不需要配置,从redis需要配置
# slaveof <masterip> <masterport>

# 如果主redis设置了密码,则从redis必须设置masterauth,否则主从数据同步会被拒绝。
# masterauth nxredis@YH

# 当从redis与主redis失去连接时,或者正在同步数据时,从机有两种工作方式:
# 1.如果slave serve stale data设置为“yes”(默认值),则slave仍会回复客户端请求,可能会包含过期数据,或者如果这是第一次同步,则数据集可能为空。
# 2.如果slave serve stale data设置为“no”,则slave将对除INFO和SLAVEOF之外的所有类型的命令回复错误“正在与主机同步”。
slave-serve-stale-data yes

# Redis2.6版本之后默认情况下从机为只读。
slave-read-only yes

# 主从同步策略: 是否开启无磁盘交互模式.
# 1) 磁盘备份: 主Redis创建一个新进程,将RDB文件写入磁盘。然后该文件由父进程以增量方式同步到从Redis内存。
# 2) 无磁盘交互模式: 主Redis创建一个新进程直接将RDB文件同步到从Redis内存。
repl-diskless-sync no

# 在进行无磁盘交互模式下Redis从库的延迟时间
repl-diskless-sync-delay 5

# 从Redis以设定的间隔时间向主Redis发送ping,默认为10秒。
# repl-ping-slave-period 10

# 主从同步超时时间,该值必须大于ping的时间间隔。
# repl-timeout 60

# 第一次数据同步后是否采用延迟策略。
# 设置成yes,则redis会合并小的TCP包从而节省带宽,但会增加同步延迟(40ms),造成master与slave数据不一致。
# 设置成no,则redis master会立即发送同步数据,没有延迟
repl-disable-tcp-nodelay no

# 缓冲复制队列大小
# repl-backlog-size 1mb

# 缓冲复制队列存活时长,0表示永远有效
# repl-backlog-ttl 3600

# 主Redis宕机后,选取从Redis时根据该设置的优先级选取,该值越小优先级越大。
# 如果多个从Redis优先级相同,则选取复制内容越完整的当选。
# 如果优先级和内容复制都相同,则选取runid最小的。
# 设置为0表示永不能当选为主Redis。
# 默认值为100
slave-priority 100

# 设置访问密码,密码尽可能复杂,以免被攻击破解。
# 注意,主redis设置了密码,则从redis必须设置masterauth,否则主从数据同步会被拒绝。
requirepass nxredis@YH

#设置客户端同时最大连接数,默认情况下10000。
maxclients 1024

# 内存大小配置
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
# 单位不区分大小写,所以1GB 1Gb 1gB时相同的含义
maxmemory 1073741824

# 内存回收策略
# 1.volatile-lru:从设置了过期时间的数据集中,选择最近最久未使用的数据释放。
# 2.allkeys-lru:从数据集中(包括设置过期时间以及未设置过期时间的数据集中),选择最近最久未使用的数据释放。
# 3.volatile-random:从设置了过期时间的数据集中,随机选择一个数据进行释放。
# 4.allkeys-random:从数据集中(包括了设置过期时间以及未设置过期时间)随机选择一个数据进行入释放。
# 5.volatile-ttl:从设置了过期时间的数据集中,选择马上就要过期的数据进行释放操作。
# 6.noeviction:不删除任意数据(但redis还会根据引用计数器进行释放呦~),这时如果内存不够时,会直接返回错误。
# 默认回收策略为:noeviction
# maxmemory-policy noeviction

# LRU和TTL不是精确算法,而是近似算法。
# Redis默认将检查5个key并选择释放最近使用最少的key,因此可以通过调整该值来提高速度或精度。
# 该配置只对1-5的回收策略有效。
# 10非常接近真实的LRU,但需要更多的CPU。
# 3很快,但不是很准确。
# maxmemory-samples 5

# Redis默认采用异步的方式将数据存放到磁盘上,这个模式对大部份应用来说是足够好的。
# AOF与RDB模式可以同时启用,默认AOF模式不启用,所以以下众多AOF配置不起作用。
appendonly no

appendfilename "appendonly_6379.aof"

# no: 不即时同步,由操作系统控制何时刷写到磁盘上,这种模式速度最快。
# always: 每次只写日志,速度较慢,但最安全。
# everysec: 每秒钟同步一次,折中的方案。
appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

# lua脚本的最大运行时间,单位是毫秒。
lua-time-limit 5000

# slog log是用来记录redis运行中执行比较慢的命令耗时。slog log保存在内存中。
# 执行时间比slowlog-log-slower-than大的请求命令记录到slowlog里面,单位是微秒,所以1000000就是1秒。
slowlog-log-slower-than 10000

# 最大保存命令条数
slowlog-max-len 128

# 延迟监控,默认关闭。
latency-monitor-threshold 0

# 使得客户端可以通过订阅频道或模式,来接收那些以某种方式改动了 Redis 数据集的事件。
# 因为开启键空间通知功能需要消耗一些 CPU ,所以在默认配置下,该功能处于关闭状态。
# 该配置可以是以下字符的任意组合
#  K     Keyspace events, published with __keyspace@<db>__ prefix.
#  E     Keyevent events, published with __keyevent@<db>__ prefix.
#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
#  $     String commands
#  l     List commands
#  s     Set commands
#  h     Hash commands
#  z     Sorted set commands
#  x     Expired events (events generated every time a key expires)
#  e     Evicted events (events generated when a key is evicted for maxmemory)
#  A     Alias for g$lshzxe, so that the "AKE" string means all the events.
notify-keyspace-events ""

# ziplist中允许存储的最大条目个数,默认为512,建议为128
hash-max-ziplist-entries 512

# ziplist中允许条目value值最大字节数,默认为64,建议为1024
hash-max-ziplist-value 64

# -5: 每个quicklist节点上的ziplist大小不能超过64 Kb。
# -4: 每个quicklist节点上的ziplist大小不能超过32 Kb。
# -3: 每个quicklist节点上的ziplist大小不能超过16 Kb。
# -2: 每个quicklist节点上的ziplist大小不能超过8 Kb。(-2是Redis给出的默认值)
# -1: 每个quicklist节点上的ziplist大小不能超过4 Kb。
list-max-ziplist-size -2

# quicklist两端不被压缩的节点个数。
list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128
zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

# Redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。
# 当你的使用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。
# 如果没有这么严格的实时性要求,可以设置为yes,以便能够尽可能快的释放内存。
activerehashing yes

# normal -> normal clients including MONITOR clients
# slave  -> slave clients
# pubsub -> clients subscribed to at least one pubsub channel or pattern
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

# 默认值为10。
# Redis会调用一个内部函数来执行许多后台任务,例如在超时时关闭客户端的连接,清除从未请求的过期密钥等等。
# 该值越大,后台任务执行的越及时,但是会消耗更多的CPU。
hz 10

# 在aof重写的时候,如果打开了aof-rewrite-incremental-fsync开关,系统会每32MB执行一次fsync。
# 这对于把文件写入磁盘是有帮助的,可以避免过大的延迟峰值
aof-rewrite-incremental-fsync yes

From the Redis configuration, redis_6380.conf and redis_6381.conf are basically the same as the main Redis configuration. The difference lies in the name of the port, database, log, and pid file, all of which are marked with 6380 or 6381. The most important thing is to establish the main Validate from relationships and synchronization.

# 设置主Redis的地址和端口,该Redis为从节点
# 主从复制关系,主redis不需要配置,从redis需要配置
slaveof 127.0.0.1 6379

# 设置访问密码,密码尽可能复杂,以免被攻击破解。
# 注意,主redis设置了密码,则从redis必须设置masterauth,否则主从数据同步会被拒绝。
# requirepass nxredis@YH
# 如果主redis设置了密码,则从redis必须设置masterauth,否则主从数据同步会被拒绝。
masterauth nxredis@YH

Note: For only redis service, you only need to configure requirepass in the main Redis, and configure masterauth in the slave Redis. The password should be consistent and the password should be as complex as possible to avoid being cracked by attacks.

Note: For only using redis service, if it is necessary to add access authentication from Redis, you can also set requirepass, and the password can be different from the main Redis password.

Place the configuration file in the /data/redis/conf directory, and then start the service.

The startup services should be started sequentially in master-slave order.

cd /data/redis/bin
./redis-server /data/redis/conf/redis_6379.conf
./redis-server /data/redis/conf/redis_6380.conf
./redis-server /data/redis/conf/redis_6381.conf

Check the service startup status:

ps -ef|grep redis

You can also check the log file to confirm whether the service started normally.

Log in to Redis through the client to verify data synchronization:

Main Redis login verification, setting data:

cd /data/redis/bin
./redis-cli
auth nxredis@YH
set name zqz

Log in from Redis to get data: There is no password set from Redis, so it can be operated without authentication.

./redis-cli -p 6380
get name

Configure sentinel mode:

The Redis Sentinel cluster usually consists of 3 to 5 nodes. If individual nodes hang up, the cluster can still operate normally. Sentinel is responsible for monitoring the health of the Redis cluster.

If the main Redis hangs up, the Sentinel cluster will select a new main Redis by voting. When the original master Redis recovers, it will rejoin the Redis cluster as the slave Redis of the new master Redis.

Set the password for connecting master and slave. It should be noted that sentinel cannot set different passwords for master and slave respectively, so the passwords of master and slave must be set the same. That is to say, both the master Redis and the slave Redis must set requirepass and masterauth, and the passwords must be the same.

sentinel.conf configuration information:

protected-mode no
port 26379
sentinel myid a2b0854207a29e03e256b9628a18538c9a16a38b
# 监控的主节点的名字、IP 和端口,最后一个1表示有1台 Sentinel 发现有问题,就会发生故障转移
sentinel monitor redisYH 127.0.0.1 6379 1
# sentinel日志
logfile "/data/redis/logs/sentinel.log"
# Generated by CONFIG REWRITE
dir "/data/redis/"
# 密码验证,通过添加主节点的密码,对主Redis的运行状态做监控
sentinel auth-pass redisYH nxredis@YH
sentinel config-epoch redisYH 0
sentinel leader-epoch redisYH 115744
sentinel current-epoch 115744

Place the configuration file in the /data/redis/conf directory and start the sentinel service:

./server-sentinel /data/redis/conf/sentinel.conf

To verify whether sentinel works, you can manually shut down the main Redis.

./redis-cli
auth nxredis@YH
shutdown

At this time, if you want to access the main Redis synchronization data from Redis, you will be prompted with an error message:

Connecting to MASTER 127.0.0.1:6379
MASTER <-> SLAVE sync started
Error condition on socket for SYNC: Connection refused

After Sentinel detects that the main Redis is down, through elections, a slave Redis is selected as the new main Redis. By viewing the sentinel log, we can find that 6380 is selected as the new master Redis, and the other two Redis are used as slave Redis.

switch-master redisYH 127.0.0.1 6379 127.0.0.1 6380
slave slave 127.0.0.1:6381 127.0.0.1 6381 @ redisYH 127.0.0.1 6380
slave slave 127.0.0.1:6379 127.0.0.1 6379 @ redisYH 127.0.0.1 6380

Note: After selecting 6380 as the master Redis, all configuration files will be modified, mainly to re-establish the master-slave relationship.

6379 will add: slaveof 127.0.0.1 6380

6380 will delete: slaveof 127.0.0.1 6379

6381 will modify: slaveof 127.0.0.1 6380

Since the 6379 service has been shut down, although sentinel regards 6379 as the slave service of 6380, it has not really been established.

Restart the 6379 service, then sentinel will rebuild and establish a master-slave relationship:

convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ redisYH 127.0.0.1 6380

Guess you like

Origin blog.csdn.net/kzhzhang/article/details/125571394