Redis主从模式部署

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介: Redis主从模式部署

Redis主从模式部署

主从模式是Redis三种集群模式中最简单的,主数据库(master)和从数据库(slave)。其中,主从复制有如下特点:

  • 主数据库可以进行读写操作,当读写操作导致数据变化时会自动将数据同步给从数据库;
  • 从数据库一般是只读的,并且接收主数据库同步过来的数据;
  • 一个master可以拥有多个slave,但是一个slave只能对应一个master;
  • slave挂了不影响其他slave的读和master的读和写,重新启动后会将数据从master同步过来;
  • master挂了以后,不影响slave的读,但redis不再提供写服务,master重启后redis将重新对外提供写服务;
  • master挂了以后,不会在slave节点中重新选一个master;

工作机制:

  • 当slave启动后,主动向master发送SYNC命令。master接收到SYNC命令后在后台保存快照(RDB持久化)和缓存保存快照这段时间的命令,然后将保存的快照文件和缓存的命令发送给slave。slave接收到快照文件和命令后加载快照文件和缓存的执行命令。
  • 复制初始化后,master每次接收到的写命令都会同步发送给slave,保证主从数据一致性。

环境

IP 角色
192.168.1.21 master
192.168.1.22 slave1
192.168.1.23 slave2

安装编译环境

# ubuntu
apt install make gcc
# centos
yum install make gcc

安装 Redis

# 查看 Redis 版本
http://download.redis.io/releases/

# 下载 Redis
wget http://download.redis.io/releases/redis-7.2.5.tar.gz

# 解压
tar xvf redis-7.2.5.tar.gz 
cd redis-7.2.5/

# 进行编译
make && make install

配置服务

cat << EOF > /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

配置停止脚本

mkdir /usr/local/redis
vim /usr/local/redis/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
        $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
        $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi

授权启动服务

chmod +x /usr/local/redis/redis-shutdown
useradd -s /sbin/nologin redis

cp /root/redis-7.2.5/redis.conf /usr/local/redis/ && chown -R redis:redis /usr/local/redis
mkdir -p /usr/local/redis/data && chown -R redis:redis /usr/local/redis/data

修改配置

vim /usr/local/redis/redis.conf
# master节点配置
bind 0.0.0.0 -::1                # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"                # 日志路径
dir /usr/local/redis/data                 # 数据库备份文件存放目录
masterauth 123123               # slave连接master密码,master可省略
requirepass 123123              # 设置master连接密码,slave可省略
appendonly yes                  # 在/usr/local/redis/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中

vim /usr/local/redis/redis.conf
#slave1节点配置
bind 0.0.0.0 -::1                # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"      # 日志路径
dir /usr/local/redis/data                 # 数据库备份文件存放目录
replicaof 192.168.1.21 6379   #  replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点。就是设置master节点
masterauth 123123               # slave连接master密码,master可省略
requirepass 123123              # 设置master连接密码,slave可省略
appendonly yes                  # 在/usr/local/redis/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中

vim /usr/local/redis/redis.conf
#slave2节点配置
bind 0.0.0.0 -::1                # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"                # 日志路径
dir /usr/local/redis/data                 # 数据库备份文件存放目录
replicaof 192.168.1.21 6379   #  replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点。就是设置master节点
masterauth 123123               # slave连接master密码,master可省略
requirepass 123123              # 设置master连接密码,slave可省略
appendonly yes                  # 在/usr/local/redis/data目录生成appendonly.aof文件,将每

修改linux内核参数

# 临时生效
sysctl  -w  vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
### 可选值:0,1,2。
# 0,:表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
# 1:表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
# 2: 表示内核允许分配超过所有物理内存和交换空间总和的内存。

启动 Redis

systemctl daemon-reload
systemctl enable redis
systemctl stop redis
systemctl start redis
systemctl status redis

查看集群

# 交互式
redis-cli -h 192.168.1.21 -a 123123
1192.168.1.21:6379> info replication
role:master
connected_slaves:2
slave0:ip=192.168.1.22,port=6379,state=online,offset=14,lag=0
slave1:ip=192.168.1.23,port=6379,state=online,offset=14,lag=0
master_failover_state:no-failover
master_replid:449440daec10a3eb742b13e690de4adb26b20a07
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
192.168.1.21:6379>


# 交互式
redis-cli -h 192.168.1.21
192.168.1.21:6379> 
192.168.1.21:6379> info replication
NOAUTH Authentication required.
192.168.1.21:6379> 
192.168.1.21:6379> auth 123123
OK
192.168.1.21:6379> 
192.168.1.21:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.22,port=6379,state=online,offset=56,lag=0
slave1:ip=192.168.1.23,port=6379,state=online,offset=56,lag=0
master_failover_state:no-failover
master_replid:449440daec10a3eb742b13e690de4adb26b20a07
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
192.168.1.21:6379> 

# 非交互式
redis-cli -h 192.168.1.21 -a 123123 info replication

压测

root@cby:~# redis-benchmark -t set,get -n 100000 -a 123123 -h 192.168.1.21
====== SET ======                                                     
  100000 requests completed in 0.98 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 3600 1 300 100 60 10000
  host configuration "appendonly": yes
  multi-thread: no

Latency by percentile distribution:
0.000% <= 0.103 milliseconds (cumulative count 8)
50.000% <= 0.343 milliseconds (cumulative count 50319)
75.000% <= 0.399 milliseconds (cumulative count 75102)
87.500% <= 0.431 milliseconds (cumulative count 88783)
93.750% <= 0.447 milliseconds (cumulative count 93936)
96.875% <= 0.463 milliseconds (cumulative count 96878)
98.438% <= 0.487 milliseconds (cumulative count 98770)
99.219% <= 0.503 milliseconds (cumulative count 99227)
99.609% <= 0.615 milliseconds (cumulative count 99619)
99.805% <= 0.815 milliseconds (cumulative count 99807)
99.902% <= 1.071 milliseconds (cumulative count 99906)
99.951% <= 1.175 milliseconds (cumulative count 99954)
99.976% <= 1.247 milliseconds (cumulative count 99976)
99.988% <= 1.295 milliseconds (cumulative count 99989)
99.994% <= 1.319 milliseconds (cumulative count 99995)
99.997% <= 1.327 milliseconds (cumulative count 99997)
99.998% <= 1.335 milliseconds (cumulative count 99999)
99.999% <= 1.343 milliseconds (cumulative count 100000)
100.000% <= 1.343 milliseconds (cumulative count 100000)

Cumulative distribution of latencies:
0.008% <= 0.103 milliseconds (cumulative count 8)
1.338% <= 0.207 milliseconds (cumulative count 1338)
35.037% <= 0.303 milliseconds (cumulative count 35037)
78.556% <= 0.407 milliseconds (cumulative count 78556)
99.227% <= 0.503 milliseconds (cumulative count 99227)
99.604% <= 0.607 milliseconds (cumulative count 99604)
99.736% <= 0.703 milliseconds (cumulative count 99736)
99.804% <= 0.807 milliseconds (cumulative count 99804)
99.842% <= 0.903 milliseconds (cumulative count 99842)
99.884% <= 1.007 milliseconds (cumulative count 99884)
99.922% <= 1.103 milliseconds (cumulative count 99922)
99.966% <= 1.207 milliseconds (cumulative count 99966)
99.991% <= 1.303 milliseconds (cumulative count 99991)
100.000% <= 1.407 milliseconds (cumulative count 100000)

Summary:
  throughput summary: 102249.49 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.343     0.096     0.343     0.455     0.495     1.343
====== GET ======                                                     
  100000 requests completed in 0.81 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 3600 1 300 100 60 10000
  host configuration "appendonly": yes
  multi-thread: no

Latency by percentile distribution:
0.000% <= 0.063 milliseconds (cumulative count 9)
50.000% <= 0.263 milliseconds (cumulative count 52284)
75.000% <= 0.319 milliseconds (cumulative count 77215)
87.500% <= 0.351 milliseconds (cumulative count 90174)
93.750% <= 0.367 milliseconds (cumulative count 95109)
96.875% <= 0.383 milliseconds (cumulative count 97068)
98.438% <= 0.407 milliseconds (cumulative count 98532)
99.219% <= 0.487 milliseconds (cumulative count 99222)
99.609% <= 0.711 milliseconds (cumulative count 99619)
99.805% <= 0.919 milliseconds (cumulative count 99806)
99.902% <= 1.127 milliseconds (cumulative count 99908)
99.951% <= 1.231 milliseconds (cumulative count 99953)
99.976% <= 1.343 milliseconds (cumulative count 99976)
99.988% <= 1.391 milliseconds (cumulative count 99989)
99.994% <= 1.415 milliseconds (cumulative count 99995)
99.997% <= 1.423 milliseconds (cumulative count 99997)
99.998% <= 1.431 milliseconds (cumulative count 99999)
99.999% <= 1.439 milliseconds (cumulative count 100000)
100.000% <= 1.439 milliseconds (cumulative count 100000)

Cumulative distribution of latencies:
0.034% <= 0.103 milliseconds (cumulative count 34)
24.823% <= 0.207 milliseconds (cumulative count 24823)
70.395% <= 0.303 milliseconds (cumulative count 70395)
98.532% <= 0.407 milliseconds (cumulative count 98532)
99.251% <= 0.503 milliseconds (cumulative count 99251)
99.458% <= 0.607 milliseconds (cumulative count 99458)
99.608% <= 0.703 milliseconds (cumulative count 99608)
99.707% <= 0.807 milliseconds (cumulative count 99707)
99.795% <= 0.903 milliseconds (cumulative count 99795)
99.855% <= 1.007 milliseconds (cumulative count 99855)
99.895% <= 1.103 milliseconds (cumulative count 99895)
99.945% <= 1.207 milliseconds (cumulative count 99945)
99.966% <= 1.303 milliseconds (cumulative count 99966)
99.993% <= 1.407 milliseconds (cumulative count 99993)
100.000% <= 1.503 milliseconds (cumulative count 100000)

Summary:
  throughput summary: 122850.12 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.265     0.056     0.263     0.367     0.431     1.439

root@cby:~#

关于

https://www.oiox.cn/

https://www.oiox.cn/index.php/start-page.html

CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

文章主要发布于微信公众号

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
22天前
|
消息中间件 负载均衡 NoSQL
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
18 0
|
24天前
|
缓存 NoSQL Redis
Redis主从架构
当看到图示中红色标记的,就代表从节点挂载成功了。
17 0
|
2天前
|
监控 NoSQL Ubuntu
|
2天前
|
数据库 NoSQL Redis
Redis访问模式
【7月更文挑战第17天】
15 2
|
25天前
|
NoSQL Java 关系型数据库
考考你Redis主从,就知道有没深入Redis
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天继续给大家带来新的一篇Java进阶指南。如果是单机版的数据库,像MySQL、Redis,看起来实现并不复杂。只要支持保存一条数据,同时要能够查询出来。但如果是多机版的数据库呢,各个节点的配合联调是比较复杂的过程,看起来就不是那么简单喽。要考你对Redis深不深入理解,问问Redis多机版相关的问题就知道了。我们今天就来指南下Redis主从架构。
考考你Redis主从,就知道有没深入Redis
|
14天前
|
前端开发 NoSQL 数据库
部署常用的流程,可以用后端,连接宝塔,将IP地址修改好,本地只要连接好了,在本地上前后端跑起来,前端能够跑起来,改好了config.js资料,后端修改好数据库和连接redis,本地上跑成功了,再改
部署常用的流程,可以用后端,连接宝塔,将IP地址修改好,本地只要连接好了,在本地上前后端跑起来,前端能够跑起来,改好了config.js资料,后端修改好数据库和连接redis,本地上跑成功了,再改
|
16天前
|
消息中间件 负载均衡 NoSQL
|
14天前
|
NoSQL JavaScript Redis
若依后端部署---若依部署,Redis在D盘的project的应用工具当中,在连接过程中,先用Xshell连接若依,RDM在应用工具里,同时host的主机也要写好
若依后端部署---若依部署,Redis在D盘的project的应用工具当中,在连接过程中,先用Xshell连接若依,RDM在应用工具里,同时host的主机也要写好
|
28天前
|
缓存 NoSQL Java
windows下Springboot部署redis
windows下Springboot部署redis
19 0
|
14天前
|
NoSQL Java Redis
软件开发常见流程之宝塔初始化安装环境配置,Lam前面不选,直接跳商城,在宝塔内点击软件商城,安Mysql5.7,安java项目管理器,安Ngnix最新版,安Redis
软件开发常见流程之宝塔初始化安装环境配置,Lam前面不选,直接跳商城,在宝塔内点击软件商城,安Mysql5.7,安java项目管理器,安Ngnix最新版,安Redis