The environment

Operating system CentOS 7 Redis 6.0.8 Number of Machines Master * 1 slave * 2Copy the code

Install Redis

Download address

https://redis.io/download
Copy the code

The installation

CD/CD/opt mkdir redis redis wget https://download.redis.io/releases/redis-6.0.8.tar.gz tar XZF redis - 6.0.8. Tar. Gz CD Redis - 6.0.8 / makeCopy the code

I made two errors here with make

We need to update the GCC version

Yum install centos-release-scl scl-utils-build 2 Yum list all --enablerepo='centos-sclo-rh' 3 Yum install -y devtoolset-8-toolchain SCL enable devtoolset-8 bash 4 yum install -y devToolset-8-toolchain SCL Add the parameter make MALLOC=libc to makeCopy the code

Re-make and specify the directory, which I specified in the installation directory

Make install PREFIX = / opt/redis/redis - 6.0.8Copy the code

Create a log folder

mkdir logs
Copy the code

configuration

Create a new redis.conf file in the bin directory.

cd bin
vim redis.conf
Copy the code

Configuration file details

Pay attention to the masterauth, requirepass, and slave-priority parameters

Masterauth: This password refers to the requirepass value of the Mastre node. Here we are configuring master, so the masterauth and requirePass values are the same.

Requirepass: This is the password for Redis.

Slave-priority: this parameter is explained in detail below

This is the configuration of the master, the configuration of the slave is slightly changed, in the last three lines of the configuration, detailed below

# 当master服务设置了密码保护时,slav服务连接master的密码
masterauth "123456"
# 密码验证# 警告:因为Redis太快了,所以外面的人可以尝试每秒150k的密码来试图破解密码。这意味着你需要# 一个高强度的密码,否则破解太容易了
requirepass 123456
# slave的优先级是一个整数展示在Redis的Info输出中。如果master不再正常工作了,哨兵将用它来# 选择一个slave提升=升为master。# 优先级数字小的salve会优先考虑提升为master,所以例如有三个slave优先级分别为10,100,25,# 哨兵将挑选优先级最小数字为10的slave。# 0作为一个特殊的优先级,标识这个slave不能作为master,所以一个优先级为0的slave永远不会被# 哨兵挑选提升为master
slave-priority 1
# 守护进程模式
daemonize yes 
# pid file 修改pidfile指向路径,pidfile 是我们启动redis 的时候,linux 为我们分配的一个pid 进程号,如果这里不作修改,会影响后面redis服务的启动
pidfile /usr/local/redis-5.0.8/redis_master.pid
# 监听端口 默认6379 我这3个分别是6380 6381 6382
port 6379
# TCP接收队列长度,受/proc/sys/net/core/somaxconn和tcp_max_syn_backlog这两个内核参数的影响#tcp-backlog 511
# 一个客户端空闲多少秒后关闭连接(0代表禁用,永不关闭)
timeout 0
# 如果非零,则设置SO_KEEPALIVE选项来向空闲连接的客户端发送ACK
tcp-keepalive 60
# 指定服务器调试等级# 可能值:# debug (大量信息,对开发/测试有用)# verbose (很多精简的有用信息,但是不像debug等级那么多)# notice (适量的信息,基本上是你生产环境中需要的)# warning (只有很重要/严重的信息会记录下来)
loglevel notice
# 指明日志文件名 logs这个目录之前是没有的,注意添加
logfile "/opt/redis/redis-6.0.8/logs/redis6379.log"
# 设置数据库个数
databases 16
# 会在指定秒数和数据变化次数之后把数据库写到磁盘上# 900秒(15分钟)之后,且至少1次变更# 300秒(5分钟)之后,且至少10次变更# 60秒之后,且至少10000次变更
save 900 1
save 300 10
save 60 10000

# 默认如果开启RDB快照(至少一条save指令)并且最新的后台保存失败,Redis将会停止接受写操作# 这将使用户知道数据没有正确的持久化到硬盘,否则可能没人注意到并且造成一些灾难
stop-writes-on-bgsave-error yes
# 当导出到 .rdb 数据库时是否用LZF压缩字符串对象
rdbcompression yes
# 版本5的RDB有一个CRC64算法的校验和放在了文件的最后。这将使文件格式更加可靠。
rdbchecksum yes
# 持久化数据库的文件名
dbfilename dump-master.rdb
# 工作目录
dir /opt/redis/redis-6.0.8/logs/

# 当一个slave失去和master的连接,或者同步正在进行中,slave的行为可以有两种:## 1) 如果 slave-serve-stale-data 设置为 "yes" (默认值),slave会继续响应客户端请求,# 可能是正常数据,或者是过时了的数据,也可能是还没获得值的空数据。# 2) 如果 slave-serve-stale-data 设置为 "no",slave会回复"正在从master同步# (SYNC with master in progress)"来处理各种请求,除了 INFO 和 SLAVEOF 命令。
slave-serve-stale-data yes
# 你可以配置salve实例是否接受写操作。可写的slave实例可能对存储临时数据比较有用(因为写入salve# 的数据在同master同步之后将很容易被删除
slave-read-only yes
# 是否在slave套接字发送SYNC之后禁用 TCP_NODELAY?# 如果你选择“yes”Redis将使用更少的TCP包和带宽来向slaves发送数据。但是这将使数据传输到slave# 上有延迟,Linux内核的默认配置会达到40毫秒# 如果你选择了 "no" 数据传输到salve的延迟将会减少但要使用更多的带宽
repl-disable-tcp-nodelay no
# redis实例最大占用内存,不要用比设置的上限更多的内存。一旦内存使用达到上限,Redis会根据选定的回收策略(参见:# maxmemmory-policy)删除key
maxmemory 15gb
# 最大内存策略:如果达到内存限制了,Redis如何选择删除key。你可以在下面五个行为里选:# volatile-lru -> 根据LRU算法删除带有过期时间的key。# allkeys-lru -> 根据LRU算法删除任何key。# volatile-random -> 根据过期设置来随机删除key, 具备过期时间的key。 # allkeys->random -> 无差别随机删, 任何一个key。 # volatile-ttl -> 根据最近过期时间来删除(辅以TTL), 这是对于有过期时间的key # noeviction -> 谁也不删,直接在写操作时返回错误。
maxmemory-policy volatile-lru
# 默认情况下,Redis是异步的把数据导出到磁盘上。这种模式在很多应用里已经足够好,但Redis进程# 出问题或断电时可能造成一段时间的写操作丢失(这取决于配置的save指令)。## AOF是一种提供了更可靠的替代持久化模式,例如使用默认的数据写入文件策略(参见后面的配置)# 在遇到像服务器断电或单写情况下Redis自身进程出问题但操作系统仍正常运行等突发事件时,Redis# 能只丢失1秒的写操作。## AOF和RDB持久化能同时启动并且不会有问题。# 如果AOF开启,那么在启动时Redis将加载AOF文件,它更能保证数据的可靠性。
appendonly yes
# aof文件名
appendfilename "appendonly.aof"
# fsync() 系统调用告诉操作系统把数据写到磁盘上,而不是等更多的数据进入输出缓冲区。# 有些操作系统会真的把数据马上刷到磁盘上;有些则会尽快去尝试这么做。## Redis支持三种不同的模式:## no:不要立刻刷,只有在操作系统需要刷的时候再刷。比较快。# always:每次写操作都立刻写入到aof文件。慢,但是最安全。# everysec:每秒写一次。折中方案。 
appendfsync everysec
# 如果AOF的同步策略设置成 "always" 或者 "everysec",并且后台的存储进程(后台存储或写入AOF# 日志)会产生很多磁盘I/O开销。某些Linux的配置下会使Redis因为 fsync()系统调用而阻塞很久。# 注意,目前对这个情况还没有完美修正,甚至不同线程的 fsync() 会阻塞我们同步的write(2)调用。## 为了缓解这个问题,可以用下面这个选项。它可以在 BGSAVE 或 BGREWRITEAOF 处理时阻止主进程进行fsync()。# # 这就意味着如果有子进程在进行保存操作,那么Redis就处于"不可同步"的状态。# 这实际上是说,在最差的情况下可能会丢掉30秒钟的日志数据。(默认Linux设定)# # 如果你有延时问题把这个设置成"yes",否则就保持"no",这是保存持久数据的最安全的方式。
no-appendfsync-on-rewrite yes
# 自动重写AOF文件
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# AOF文件可能在尾部是不完整的(这跟system关闭有问题,尤其是mount ext4文件系统时# 没有加上data=ordered选项。只会发生在os死时,redis自己死不会不完整)。# 那redis重启时load进内存的时候就有问题了。# 发生的时候,可以选择redis启动报错,并且通知用户和写日志,或者load尽量多正常的数据。# 如果aof-load-truncated是yes,会自动发布一个log给客户端然后load(默认)。# 如果是no,用户必须手动redis-check-aof修复AOF文件才可以。# 注意,如果在读取的过程中,发现这个aof是损坏的,服务器也是会退出的,# 这个选项仅仅用于当服务器尝试读取更多的数据但又找不到相应的数据时。
aof-load-truncated yes
# Lua 脚本的最大执行时间,毫秒为单位
lua-time-limit 5000
# Redis慢查询日志可以记录超过指定时间的查询
slowlog-log-slower-than 10000
# 这个长度没有限制。只是要主要会消耗内存。你可以通过 SLOWLOG RESET 来回收内存。
slowlog-max-len 128
# redis延时监控系统在运行时会采样一些操作,以便收集可能导致延时的数据根源。# 通过 LATENCY命令 可以打印一些图样和获取一些报告,方便监控# 这个系统仅仅记录那个执行时间大于或等于预定时间(毫秒)的操作, # 这个预定时间是通过latency-monitor-threshold配置来指定的,# 当设置为0时,这个监控系统处于停止状态
latency-monitor-threshold 0
# Redis能通知 Pub/Sub 客户端关于键空间发生的事件,默认关闭
notify-keyspace-events ""
# 当hash只有少量的entry时,并且最大的entry所占空间没有超过指定的限制时,会用一种节省内存的# 数据结构来编码。可以通过下面的指令来设定限制
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
# 与hash似,数据元素较少的list,可以用另一种方式来编码从而节省大量空间。# 这种特殊的方式只有在符合下面限制时才可以用
list-max-ziplist-entries 512
list-max-ziplist-value 64
# set有一种特殊编码的情况:当set数据全是十进制64位有符号整型数字构成的字符串时。# 下面这个配置项就是用来设置set使用这种编码来节省内存的最大长度。set-max-intset-entries 512
# 与hash和list相似,有序集合也可以用一种特别的编码方式来节省大量空间。# 这种编码只适合长度和元素都小于下面限制的有序集合
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# HyperLogLog稀疏结构表示字节的限制。该限制包括# 16个字节的头。当HyperLogLog使用稀疏结构表示# 这些限制,它会被转换成密度表示。# 值大于16000是完全没用的,因为在该点# 密集的表示是更多的内存效率。# 建议值是3000左右,以便具有的内存好处, 减少内存的消耗
hll-sparse-max-bytes 3000
# 启用哈希刷新,每100个CPU毫秒会拿出1个毫秒来刷新Redis的主哈希表(顶级键值映射表)
activerehashing yes
# 客户端的输出缓冲区的限制,可用于强制断开那些因为某种原因从服务器读取数据的速度不够快的客户端
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# 默认情况下,“hz”的被设定为10。提高该值将在Redis空闲时使用更多的CPU时,但同时当有多个key# 同时到期会使Redis的反应更灵敏,以及超时可以更精确地处理
hz 10
# 当一个子进程重写AOF文件时,如果启用下面的选项,则文件每生成32M数据会被同步
aof-rewrite-incremental-fsync yes
# 这个配置是master中没有的,需要在slave中新增
# 这里的IP是master的IP地址,端口是master 6379.conf配置文件中port的值,默认值是6379
# slaveof 1.1.1.1 6379
Copy the code

Setting boot

Systemctl Configures the redis operation

vim /etc/systemd/system/redis.service
Copy the code

Copy and paste the following, and notice that ExecStart is configured as its own path

[Unit] Description=redis-server After=network.target [Service] Type=forking ExecStart = / opt/redis/redis - 6.0.8 / bin/redis server/opt/redis/redis - 6.0.8 / bin/redis. Conf PrivateTmp = true [Install] WantedBy=multi-user.targetCopy the code

Parameters for details

[Unit] means this is the basic information Description is the Description After is started After that service, Usually web services start after start [Service] said here is the Service information ExecStart is start the Service command ExecStop is stop Service instruction [Install] said this is information is to Install WantedBy is what kind of way to start: Multi-user. target indicates that this service needs to be run automatically when the system is started in multi-user mode (the default run level).Copy the code

Powered up

systemctl daemon-reload
systemctl start redis
systemctl enable redis
Copy the code

Relevant command

Systemctl start redis Starts redis systemctl restart redis restarts redis systemctl stop redis Stops redis systemctl enable redis Starts after the system starts Systemctl disable redis Disabling startup systemctl status redis Checking the status systemctl daemon-reload Reload the configuration fileCopy the code

The standalone Redis is now installed

If you want a master/slave cluster, uncomment the last line of the redis.conf file on the slave server and restart it

The sentry cluster

configuration

Create a new sentinel.conf file (as in redis. Conf).

vim bin/sentinel.conf
Copy the code

Add the following

Dir "/opt/redis/redis-6.0.8/logs/" logfile "/opt/redis/redis-6.0.8/logs/sentinel.log" daemonize yes # native IP bind X.X.X.X 127.0.0.1 port 26379 # primary node IP sentinel monitor mymaster X.X.X.X 6379 2 # requirepass sentinel auth-pass Mymaster password sentinel down-after-milliseconds myMaster 30000 sentinel PARALLel-milliseconds mymaster 1Copy the code

Configuration file details

# Example sentinel.conf # Port 26379 port 26379 # Working directory dir/TMP # IP of the redis primary node monitored by Sentinel sentinel Port # master-name Specifies the name of the master node that can be named by itself. The name consists of only letters A-z, digits 0-9, and ".-_". # sentinel monitor <master-name> < IP > <redis-port> <quorum> Sentinel Monitor myMaster 127.0.0.1 6379 2 # When requirePass Foobared is enabled in the Redis instance, all clients connected to the Redis instance must provide the password # Sentinel auth-pass <master-name> <password> Sentinel auth-pass mymaster MySUPER--secret-0123passw0rd # specifies how many milliseconds after the primary node does not respond to sentinel <master-name> <milliseconds> sentinel down-after-milliseconds mymaster 30000 # This configuration item specifies the maximum number of slaves that can synchronize the new master at the same time when a failover occurs. The smaller the number is, the longer it takes to complete the failover. However, if the number is larger, This means that the more slaves become unavailable due to Replication. Setting this value to 1 ensures that only one slave at a time is unable to process command requests. # sentinel parallel-syncs <master-name> < numSlaves > Sentinel parallel-syncs mymaster 1 It can be used in the following ways: The interval between two failover operations for the same sentinel and the same master. #2. Start time counting when a slave synchronizes data from an incorrect master. Until the slave is corrected to synchronize data to the correct master. #3. The time required to cancel an ongoing failover. #4. Maximum time required to configure all Slaves to point to the new Master when performing failover. However, even after this timeout, slaves will still be configured correctly to point to the Master, [info] [info] [info] [info] [info] [info] [info] [info] [info Mymaster 180000 # SCRIPTS EXECUTION # Configure the SCRIPTS that need to be executed when an event occurs. You can use SCRIPTS to notify administrators, for example, by sending emails when the system is not working properly. # If the script returns 1 after execution, the script will be executed again later. The current default is 10. # If the script returns 2 after execution, or a value higher than 2, the script will not be executed again. If the script is aborted during execution due to a system interrupt signal, it behaves the same as if the value is 1. The maximum execution time of a script is 60 seconds. If this time is exceeded, the script will be terminated by a SIGKILL signal and executed again. # Notification script: This script will be called when sentinel has any warning level events (such as subjective and objective failures of redis instances, etc.). In this case, this script should notify the system administrator of abnormal system operation via email, SMS, etc. When the script is called, it is passed two parameters, the type of the event and the description of the event. If the script path is configured in the sentinel.conf configuration file, the script must exist in the path and be executable, otherwise sentinel will not start successfully. # sentinel notification script # sentinel notification-script <master-name> <script-path> sentinel notification-script mymaster When a master changes due to a failover, this script will be called to inform the client that the master address has changed. The following arguments will be passed to the script when it is called: # <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port> # <role> is either "leader" or "observer". # from-ip, from-port, to-ip, to-port are used to communicate with the old master and the new master(i.e., the old slave) # This script should be generic and can be called multiple times, not specific. # sentinel client-reconfig-script <master-name> <script-path> sentinel client-reconfig-script mymaster /var/redis/reconfig.shCopy the code

Setting boot

Systemctl Configures the sentinel operation

vim /etc/systemd/system/sentinel.service
Copy the code

Copy and paste the following, and notice that ExecStart is configured as its own path

[Unit] Description=redis-sentinel After=network.target [Service] Type=forking ExecStart = / opt/redis/redis - 6.0.8 / bin/redis - sentinel/opt/redis/redis - 6.0.8 / bin/sentinel. Conf PrivateTmp = true [Install] WantedBy=multi-user.targetCopy the code

Powered up

systemctl daemon-reload
systemctl start sentinel
systemctl enable sentinel
Copy the code

At this point, the sentinel cluster is set up and the client type Info Replication to see the role changes

conclusion

Because the environment is not the same, online to find a lot of tutorials, thank you seniors:

The three clusters of redis way https://www.cnblogs.com/51life/p/10233340.html redis cluster structures (sentry) https://www.cnblogs.com/x-j-p/p/12961387.html Centos7.6 installed redis - 6.0.8 version https://blog.csdn.net/roc_wl/article/details/108662719 redis sentry mechanism -- profile sentinel. Conf explanation https://blog.csdn.net/u012441222/article/details/80751390Copy the code