Redis集群搭建

Redis集群搭建

Redis的集群分为主从模式、Sentinel(哨兵)模式、Cluster(分片)模式。本文介绍分片模式的安装方法。

0、单机版redis安装

0.1、搭建一个单机Redis

# 查看是否预装了gcc
gcc -v

# 如果没有预装 安装gcc
yum install -y gcc

# 下载redis安装包
wget https://download.redis.io/releases/redis-6.2.7.tar.gz

# 解压redis安装包
tar -zxvf redis-6.2.7.tar.gz

# 编译安装redis
cd redis-6.2.7
make MALLOC=libs

# 修改配置
vim redis.conf

========================= 配置文件 ========================= 
# 修改端口
port 端口

# 修改密码
requirepass 你的密码

# 修改后台启动
daemonize yes

# 注释bind 开启远程访问
#bind 127.0.0.1 -::1
========================= 配置文件 ========================= 

# 启动redis服务
cd /src
./redis-server ../redis.conf

# 开放端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 配置立即生效
firewall-cmd --reload   

0.2、完整的redis.conf参考

#去掉bind绑定访问ip信息
bind 0.0.0.0

#关闭保护模式
protected-mode no

#修改对应的端口
port 5001

#启动集群模式
cluster-enabled yes

#集群节点信息文件,这里500x最好和port对应上
cluster-config-file nodes-5001.conf

#节点离线的超时时间
cluster-node-timeout 5000

#如果要设置密码需要增加如下配置:
#设置redis访问密码
requirepass xdx97

#设置集群节点间访问密码,跟上面一致
masterauth xdx97

# 修改启动进程号存储位置
pidfile /var/run/redis_5001.pid

#指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据
dir /usr/local/redis-cluster/redis-5001

#修改为后台启动
daemonize yes

#启动AOF文件
appendonly yes

tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

0.3、设置开机自启动

生产环境一般都会设置redis的开机自启,编辑配置文件 vi /etc/systemd/system/redis.service

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

先关闭redis

ps -ef|grep redis

kill -9 pid
#启动redis服务
systemctl start redis.service  
 
#设置开机自启动
systemctl enable redis.service  
 
#停止开机自启动
systemctl disable redis.service  
 
#查看服务当前状态 
systemctl status redis.service 
 
#重新启动服务
systemctl restart redis.service   
 
#查看所有已启动的服务 
systemctl list-units --type=service 

1、主从模式

2、哨兵模式

3、分片模式

3.1、搭建6个redis,并修改额外配置

这里演示的是一台机器安装6个redis搭建集群,生产上不会这么做 所以一般端口不需要修改,相关的几个文件也不需要修改

#去掉bind绑定访问ip信息
bind 0.0.0.0

#关闭保护模式 这里也可以不关
protected-mode no

#修改对应的端口
port 5001

#启动集群模式
cluster-enabled yes

#集群节点信息文件,这里500x最好和port对应上 
cluster-config-file nodes-5001.conf

#节点离线的超时时间
cluster-node-timeout 5000

#如果要设置密码需要增加如下配置:
#设置redis访问密码
requirepass giao

#设置集群节点间访问密码,跟上面一致
masterauth giao

# 修改启动进程号存储位置
pidfile /var/run/redis_5001.pid

#指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据 这个默认的配置其实也可以
dir /usr/local/redis-cluster/redis-5001

#修改为后台启动
daemonize yes

#启动AOF文件
appendonly yes

3.2、创建集群

进入redis安装目录:

# 进入redis安装目录
cd /opt/redis-cluster/redis1/redis-6.2.7/src

开放集群通讯端口

# 开放端口
firewall-cmd --zone=public --add-port=16379/tcp --permanent
# 配置立即生效
firewall-cmd --reload   

创建集群:

# 访问任意一个redis节点的redis-cli,执行如下命令 
# ip端口根据自身情况而定,要写为每个redis节点的IP
# -a xdx97 我们之前设置的密码
# –cluster-replicas 1 主从搭配比例,1表示一主一从,2表示一主2从
./redis-cli -a redis_password --cluster create --cluster-replicas 1 192.168.2.187:6379 192.168.2.188:6379 192.168.2.189:6379 192.168.2.190:6379 192.168.2.191:6379 192.168.2.192:6379 

执行后会出现:

>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: dd52585b81df4d66d83de3fe5c37aa3b83780a67 81.70.143.79:5001
   slots:[0-5460] (5461 slots) master
M: a212d6cdebab9727cacb16949acea0667503855c 81.70.143.79:5002
   slots:[5461-10922] (5462 slots) master
M: 576e0209b28d714f74e9acb1b2f165467d18cb3d 81.70.143.79:5003
   slots:[10923-16383] (5461 slots) master
S: 383dc5338de9762c0f0d78067e392c9512a9913d 81.70.143.79:5004
   replicates 576e0209b28d714f74e9acb1b2f165467d18cb3d
S: 7f06ef66cd1441d929229742893acb8d280d252b 81.70.143.79:5005
   replicates dd52585b81df4d66d83de3fe5c37aa3b83780a67
S: ef55d891ef678fe9a057fd6a5eae2a582d5e753c 81.70.143.79:5006
   replicates a212d6cdebab9727cacb16949acea0667503855c

输入yes后提示下面内容标识安装成功:

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

4、常见问题

4.1、离线安装GCC

点击下载缺少的rpm包,密码t9lh,下载好后上传至linux服务器

# 安装离线包
rpm -Uvh *.rpm --nodeps --force

# 查询是否成功
gcc -v

参考:Redis集群之主从、哨兵、分片集群,SpringBoot整合Redis集群

参考:CentOS离线安装gcc(循环依赖、冲突解决)

参考:redis安装 配置开机自启 远程登录

参考:配置redis集群时一直是Waiting for the cluster to join.....

参考:Redis集群搭建

posted @ 2022-06-01 21:33  张瑞丰  阅读(102)  评论(0编辑  收藏  举报