首页 > 数据库 >Redis主从模式部署

Redis主从模式部署

时间:2024-06-16 20:25:34浏览次数:25  
标签:slave 部署 Redis redis 192.168 master usr local 主从

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、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

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

标签:slave,部署,Redis,redis,192.168,master,usr,local,主从
From: https://www.cnblogs.com/chenby/p/18251171

相关文章

  • Redis Sentinel哨兵模式部署
    RedisSentinel哨兵模式部署主从模式的弊端就是不具备高可用性,当master挂掉以后,Redis将不能再对外提供写入操作,因此sentinel模式应运而生。sentinel中文含义为哨兵,顾名思义,它的作用就是监控redis集群的运行状况,此模式具有如下一些特点:sentinel模式是建立在主从模式的基础上,如果......
  • Redis Cluster集群模式部署
    RedisCluster模式部署Redis的哨兵模式基本已经可以实现高可用,读写分离,但是在这种模式下每台Redis服务器都存储相同的数据,很浪费内存,所以在redis3.0上加入了Cluster集群模式,实现了Redis的分布式存储,也就是说每台Redis节点上存储不同的内容。下面是Cluster集群模式的一......
  • Redis单实例安装
    Redis单实例安装Redis(RemoteDictionaryServer)是一个开源的内存数据库,遵守BSD协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。性能极高:Redis以其极高的性能而著称,能够支持每秒数十万次的读写操作24。这使得Redis成为处理高并发......
  • 在window 使用 docker 安装redis 踩坑记
    1.安装REDIS在安装的时候,使用dockerpullredis就可以了。但是实际上发现镜像居然拉不下来。修改了一下docker镜像。配置如下:"registry-mirrors":["https://dockerproxy.com","https://docker.m.daocloud.io","https://docker.mirrors.ustc.edu.cn&quo......
  • Docker部署SpringBoot项目
    准备服务器安装Docker下载dockerWindows版本并登录根据项目需要在项目根目录下创建Dockerfile文件#使用官方的OpenJDK8作为基础镜像FROMopenjdk:8-jdk-alpine#维护者信息LABELmaintainer="name"#添加一个应用程序的工作目录WORKDIR/app#将JAR文件添加到......
  • Linux下部署MySQL5.7.35
    1.MySQL下载(1)登录到以下网站 https://downloads.mysql.com/archives/community/(2)选择需要的版本,以及操作系统,这里是RedHatEnterpriseLinux/OracleLinux5.7.35版本。(3)Mysql安装需要5个rpm包,如下图    mysql-community-common-5.7.35-1.el7.x86_64.rpm......
  • Redis分布式锁详解及电商秒杀功能示例
    Redis分布式锁是一种在分布式系统中,利用Redis的原子操作特性实现的锁机制,用于保护共享资源的并发访问。原理原子性与互斥性Redis分布式锁的核心原理在于利用Redis的某些原子操作(如`SETNX`、`GETSET`、`SET`带特定选项等)来确保锁的获取与释放操作是原子性的,从而保证了锁的......
  • redis常用5种数据类型及其常见问题(缓存穿透,缓存击穿,缓存雪崩)
    1.字符串(String)2.哈希(hash):redishash是一个string类型的字段和value的映射表,hash特别适合存储对象3.列表(List):字符串列表,按照插入的顺序。可以添加一个元素到列表的头部或尾部4.集合(set):String类型的无序集合。集合成员不可重复,redis中集合通过哈希表实现的,添加,删除,查找复杂度......
  • Windows系统上安装部署苹果系统(Mac OS)的几种方法
    /*MacOS苹果系统,正常情况下,只能安装到苹果公司自己出品的Mac电脑,俗称白苹果,不能安装到各种组装机或者其他品牌的品牌机上,黑苹果的的原理,就是通过一些“破解补丁”工具欺骗macOS系统,让苹果系统认为你的电脑其实是一台苹果电脑,从而可以安装运行。*/1.购买苹果笔记本或苹果一......
  • Ubuntu server 24 (Linux) 安装部署samba服务器 共享文件目录 windows访问
    1安装sudoaptupdatesudoapt-getinstallsamba#启动服务sudosystemctlrestartsmbd.servicesudosystemctlenablesmbd.service#查看服务2创建用户#创建系统用户sudouseraddtest2#配置用户密码sudosmbpasswd-atest2#smbpasswd:-a添加用户-......