下面以 CentOS 7/8 为例,给你一套** Redis 集群(Redis Cluster)** 的常用搭建方式,包含原生集群(推荐)和关键配置说明。
最少 3 主 3 从(生产建议)
| 节点 | IP | 端口 |
|---|---|---|
| redis-1 | 192.168.1.11 | 7000 |
| redis-2 | 192.168.1.12 | 7000 |
| redis-3 | 192.168.1.13 | 7000 |
| redis-4 | 192.168.1.14 | 7000 |
| redis-5 | 192.168.1.15 | 7000 |
| redis-6 | 192.168.1.16 | 7000 |
也可在一台机器上用不同端口模拟(学习用)
yum install -y epel-release
yum install -y redis
或源码安装(推荐新版本):
yum install -y gcc make
wget https://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make && make install
mkdir -p /etc/redis/cluster/7000
redis.confport 7000
bind 0.0.0.0
daemonize yes
pidfile /var/run/redis_7000.pid
logfile /var/log/redis_7000.log
dir /var/lib/redis/7000
dbfilename dump.rdb
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
requirepass yourpassword
masterauth yourpassword
requirepass和masterauth必须一致(有密码时)
redis-server /etc/redis/cluster/7000/redis.conf
确认:
ps -ef | grep redis
redis-cli -a yourpassword \
--cluster create \
192.168.1.11:7000 \
192.168.1.12:7000 \
192.168.1.13:7000 \
192.168.1.14:7000 \
192.168.1.15:7000 \
192.168.1.16:7000 \
--cluster-replicas 1
--cluster-replicas 1:每个主 1 个从
redis-cli -c -h 192.168.1.11 -p 7000 -a yourpassword
> cluster info
> cluster nodes
firewall-cmd --add-port=7000/tcp --permanent
firewall-cmd --reload
集群通信还需 +10000 端口(如 17000)
# 查看集群状态
redis-cli -a pwd --cluster check ip:7000
# 重新分片
redis-cli -a pwd --cluster reshard ip:7000
# 添加节点
redis-cli -a pwd --cluster add-node newip:7000 ip:7000
appendonlyvm.overcommit_memory=1)如果你需要:
告诉我你的使用场景即可。