Redis 主从 + Predixy 读写分离部署文档(Docker Compose 三节点)
Redis + Predixy 三节点主从集群部署文档(使用 Docker Compose)
📌 节点规划
角色 | IP地址 | 服务 |
---|---|---|
主节点 | 192.168.0.98 | Redis(主)、Predixy |
从节点A | 192.168.0.97 | Redis(从) |
从节点B | 192.168.0.96 | Redis(从) |
📁 目录结构规划
三台服务器统一目录结构如下:
# Redis 数据与配置路径
/usr/local/redis/
└── data/ # Redis 数据目录
# Predixy 配置路径(仅主节点需要)
/usr/local/predixy/
├── predixy.conf # Predixy 主配置文件
└── ... # 其他配置文件(如需)
# docker-compose 文件路径(统一为绝对路径)
/usr/local/redis/docker-compose.yml # Redis docker-compose(各节点)
/usr/local/predixy/docker-compose.yml # Predixy docker-compose(主节点)
🐳 Redis Docker Compose
主节点docker-compose.yml文件
version: '3.8'
services:
redis:
image: redis:7.4.2-alpine
container_name: redis
ports:
- "16379:16379"
volumes:
- /usr/local/redis/data:/data
command:
- redis-server
- --port 16379
- --requirepass 123456
- --masterauth 123456
- --appendonly yes
- --daemonize no
- --bind 0.0.0.0
- --protected-mode no
restart: always
network_mode: host
从节点docker-compose.yml文件
version: '3.8'
services:
redis:
image: redis:7.4.2-alpine
container_name: redis
ports:
- "16379:16379"
volumes:
- /usr/local/redis/data:/data
command:
- redis-server
- --port 16379
- --requirepass 123456
- --masterauth 123456
- --replicaof 192.168.0.98 16379
- --appendonly yes
- --daemonize no
- --bind 0.0.0.0
- --protected-mode no
restart: always
network_mode: host
🚪 Predixy 配置(只部署在主节点 98)
⚙️ Predixy 安装与配置
📥 安装依赖(以 Ubuntu 为例)
apt update
apt install build-essential cmake libssl-dev zlib1g-dev git -y
🏗️ 编译安装 Predixy
编译之前修改predixy/src目录下的String.h文件,如下图,在#include <string>
下面添加#include <ctime>
,不然编译会报错
cd /usr/local/src
git clone https://wget.la/https://github.com/joyieldInc/predixy.git
cd predixy
make -j$(nproc)
📁 安装目录整理
mkdir -p /usr/local/predixy/bin
cp /usr/local/src/predixy/src/predixy /usr/local/predixy/bin
cp -r /usr/local/src/predixy/conf /usr/local/predixy
📝 Predixy 配置文件 /usr/local/predixy/conf/predixy.conf
################################### GENERAL ####################################
## Predixy configuration file example
## Specify a name for this predixy service
## redis command INFO can get this
Name PredixyExample
## Specify listen address, support IPV4, IPV6, Unix socket
## Examples:
# Bind 127.0.0.1:7617
# 指定端口7617
Bind 0.0.0.0:7617
# Bind /tmp/predixy
## Default is 0.0.0.0:7617
# Bind 0.0.0.0:7617
## Worker threads
# 指定线程数
WorkerThreads 4
## Memory limit, 0 means unlimited
## Examples:
# MaxMemory 100M
# MaxMemory 1G
# MaxMemory 0
## MaxMemory can change online by CONFIG SET MaxMemory xxx
## Default is 0
# MaxMemory 0
## Close the connection after a client is idle for N seconds (0 to disable)
## ClientTimeout can change online by CONFIG SET ClientTimeout N
## Default is 0
ClientTimeout 300
## IO buffer size
## Default is 4096
# BufSize 4096
################################### LOG ########################################
## Log file path
## Unspecify will log to stdout
## Default is Unspecified
# Log ./predixy.log
## LogRotate support
## 1d rotate log every day
## nh rotate log every n hours 1 <= n <= 24
## nm rotate log every n minutes 1 <= n <= 1440
## nG rotate log evenry nG bytes
## nM rotate log evenry nM bytes
## time rotate and size rotate can combine eg 1h 2G, means 1h or 2G roate a time
## Examples:
# LogRotate 1d 2G
# LogRotate 1d
## Default is disable LogRotate
## In multi-threads, worker thread log need lock,
## AllowMissLog can reduce lock time for improve performance
## AllowMissLog can change online by CONFIG SET AllowMissLog true|false
## Default is true
# AllowMissLog false
## LogLevelSample, output a log every N
## all level sample can change online by CONFIG SET LogXXXSample N
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1
################################### AUTHORITY ##################################
Include auth.conf
################################### SERVERS ####################################
# Include cluster.conf
# Include sentinel.conf
# Include try.conf
Include standalone.conf
################################### DATACENTER #################################
## LocalDC specify current machine dc
# LocalDC bj
## see dc.conf
# Include dc.conf
################################### COMMAND ####################################
## Custom command define, see command.conf
#Include command.conf
################################### LATENCY ####################################
## Latency monitor define, see latency.conf
Include latency.conf
📝 配置引入的standalone.conf文件
## redis standalone server pool define
##StandaloneServerPool {
## [Password xxx] #default no
## [Databases number] #default 1
## Hash atol|crc16
## [HashTag "xx"] #default no
## Distribution modula|random
## [MasterReadPriority [0-100]] #default 50
## [StaticSlaveReadPriority [0-100]] #default 0
## [DynamicSlaveReadPriority [0-100]] #default 0
## RefreshMethod fixed|sentinel #
## [RefreshInterval number[s|ms|us]] #default 1, means 1 second
## [ServerTimeout number[s|ms|us]] #default 0, server connection socket read/write timeout
## [ServerFailureLimit number] #default 10
## [ServerRetryTimeout number[s|ms|us]] #default 1
## [KeepAlive seconds] #default 0, server connection tcp keepalive
## Sentinels [sentinel-password] {
## + addr
## ...
## }
## Group xxx {
## [+ addr] #if RefreshMethod==fixed: the first addr is master in a group, then all addrs is slaves in this group
## ...
## }
##}
## Examples:
#StandaloneServerPool {
# Databases 16
# Hash crc16
# HashTag "{}"
# Distribution modula
# MasterReadPriority 60
# StaticSlaveReadPriority 50
# DynamicSlaveReadPriority 50
# RefreshMethod sentinel
# RefreshInterval 1
# ServerTimeout 1
# ServerFailureLimit 10
# ServerRetryTimeout 1
# KeepAlive 120
# Sentinels {
# + 10.2.2.2:7500
# + 10.2.2.3:7500
# + 10.2.2.4:7500
# }
# Group shard001 {
# }
# Group shard002 {
# }
#}
#StandaloneServerPool {
# Databases 16
# Hash crc16
# HashTag "{}"
# Distribution modula
# MasterReadPriority 60
# StaticSlaveReadPriority 50
# DynamicSlaveReadPriority 50
# RefreshMethod fixed
# ServerTimeout 1
# ServerFailureLimit 10
# ServerRetryTimeout 1
# KeepAlive 120
# Group shard001 {
# + 10.2.3.2:6379
# }
#}
## Predixy 主从配置示例(Fixed 刷新模式)
StandaloneServerPool {
Databases 16
# 数据库密码
Password "123456"
Hash crc16
HashTag "{}"
Distribution modula
# 读优先级(可调)
MasterReadPriority 60
StaticSlaveReadPriority 50
DynamicSlaveReadPriority 50
# 固定刷新方式,非 Sentinel
RefreshMethod fixed
RefreshInterval 1
ServerTimeout 1
ServerFailureLimit 10
ServerRetryTimeout 1
KeepAlive 120
# 主从分组配置(Group 名称自定义即可),其中第一个为主节点
Group shard001 {
+ 192.168.0.98:16379 # 主节点
+ 192.168.0.97:16379 # 从节点
+ 192.168.0.96:16379 # 从节点
}
}
📝 配置引入的auth.conf文件
## Authority control
## Authority {
## Auth [password] {
## Mode read|write|admin
## [KeyPrefix Prefix1 Prefix2...]
## [ReadKeyPrefix Prefix1 Prefix2...]
## [WriteKeyPrefix Prefix1 Prefix2...]
## }...
## }
## Example:
# Authority {
##------------------------------------------------------------------------
# Auth {
# Mode read
# }
#### password is empty, this Auth is default auth
#### Mode read, client connection is readonly,
#### No KeyPrefix or ReadKeyPrefix defined, all key can be visit
##------------------------------------------------------------------------
# Auth abc {
# Mode write
# }
#### password is "abc", the client must send command Auth abc
#### Mode write, client connection can read and write
#### No KeyPrefix, ReadKeyPrefix, WriteKeyPrefix define, all key can be visit
##------------------------------------------------------------------------
# Auth bcd {
# Mode admin
# }
#### password is "bcd", the client must send command Auth bcd
#### Mode admin, client connection can read and write and admin,
#### the CONFIG command need admin permission
#### No KeyPrefix, ReadKeyPrefix, WriteKeyPrefix define, all key can be visit
##------------------------------------------------------------------------
# Auth cde {
# Mode read
# KeyPrefix User
# }
#### password is "cde", the client must send command Auth cde
#### Mode read, client connection is readonly,
#### KeyPrefix User, client can read UserXXX key, eg: GET User.123,
#### if client request GET hello, will be deny
##------------------------------------------------------------------------
# Auth def {
# Mode write
# ReadKeyPrefix User Stats
# WriteKeyPrefix User
# }
#### password is "def", the client must send command Auth def
#### Mode read, client connection can read and write, but read and write
#### keyspace is diffrent, client can GET User.123 and also
#### SET User.123 SomeValue, but SET Stats.123 will be deny
##------------------------------------------------------------------------
# }
## if no Authority spcified, equality below Authority
# Authority {
# Auth {
# Mode admin
# }
# }
Authority {
Auth "123456" {
Mode write
}
# Auth "#a complex password#" {
# Mode admin
# }
}
🧩 四、使用 systemd 管理 Predixy
📄 创建服务文件 /etc/systemd/system/predixy.service
[Unit]
Description=Predixy Redis Proxy Service
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/predixy/bin/predixy /usr/local/predixy/conf/predixy.conf
WorkingDirectory=/usr/local/predixy
Restart=on-failure
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
✅ 启动管理命令
# 重新加载 systemd
systemctl daemon-reload
# 启动服务
systemctl start predixy
# 开机自启
systemctl enable predixy
# 查看状态
systemctl status predixy
🎯 客户端连接示例配置
业务系统(如 Nacos)应连接 Predixy 代理服务,而非直接连接 Redis 节点:
redis:
host: 192.168.0.98
port: 7617
password: 123456
database: 6
ssl: false
timeout: 30s
navicat17连接示例:
说明:
host
为 Predixy 监听的 IP 地址(本例为 192.168.0.98)port
为 Predixy 监听端口(本例为 7617)password
为 Redis 密码,Predixy 会自动处理认证- 其他参数根据实际需要配置