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.21master
192.168.1.22slave1
192.168.1.23slave2

安装编译环境

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

全网可搜《小陈运维》

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


小陈运维
27 声望12 粉丝