Redis6.0.9集群搭建

前提条件:

  • Redis版本:6.0.9(因为5.0之前创建用的是redis-trib,还需要ruby,ruby-gem)
  • 安装环境: Centos7

1. 准备配置文件

一个是通用文件:redis-common.conf,内容是redis目录下redis.conf相同,主要修改有如下:

bind 127.0.0.1 #注释掉
daemonize yes #开启后台运行
cluster-enable yes #开启集群模式
protect-mode no #关闭保护模式

六个配置文件,redis-6390.conf redis-6391.conf redis-6392.conf redis-6393.conf redis-6394.conf redis-6395.conf
文件内容如下(只展示一个,其他类似):

cluster-config-file nodes-6391.conf
#引用公共配置
include /opt/redis/conf/redis-common.conf
#进程编号记录文件
pidfile "/var/run/redis-6391.pid"
#进程端口号
port 6391
#日志记录文件
logfile "/opt/redis/log/redis-6391.log"
#数据记录文件
dbfilename "dump-6391.rdb"
#追加文件名称
appendfilename "appendonly-6391.aof"

vim小技巧-批量修改(比如说把上面的文件91统统改成92):%s/91/92/g

2. 运行并配置集群

当前我的配置文件在/opt/redis/conf目录下,cd到redis的src目录下,执行以下命令

./redis-server /opt/redis/conf/redis-6390.conf
./redis-server /opt/redis/conf/redis-6391.conf
./redis-server /opt/redis/conf/redis-6392.conf
./redis-server /opt/redis/conf/redis-6393.conf
./redis-server /opt/redis/conf/redis-6394.conf
./redis-server /opt/redis/conf/redis-6395.conf
#然后查看redis进程
ps -ef|grep redis
[root@localhost conf]# ps -ef|grep redis
root       3507      1  0 11:19 ?        00:00:07 ./redis-server *:6390 [cluster]
root       3713      1  0 11:26 ?        00:00:08 ./redis-server *:6391 [cluster]
root       3721      1  0 11:26 ?        00:00:08 ./redis-server *:6392 [cluster]
root       3727      1  0 11:26 ?        00:00:07 ./redis-server *:6393 [cluster]
root       3733      1  0 11:26 ?        00:00:07 ./redis-server *:6394 [cluster]
root       3739      1  0 11:26 ?        00:00:07 ./redis-server *:6395 [cluster]
root       4993   3953  0 13:00 pts/1    00:00:00 grep --color=auto redis
#设置集群 cluster-replicas 1 意思是每个主节点要有一个从节点;前三个为主节点,后三个为从节点,redis自主分配
./redis-cli --cluster create 127.0.0.1:6390 127.0.0.1:6391 127.0.0.1:6392 127.0.0.1:6393 127.0.0.1:6394 127.0.0.1:6395 --cluster-replicas 1

image
登陆redis客户端通过命令cluster nodecluster info可以查看集群相关信息。
如果想删除主从节点或把某个节点添加到另一个节点可以参考博客:https://www.cnblogs.com/zhoujinyi/p/11606935.html

如果想更加深入了解redis cli命令可以参考博客:https://www.cnblogs.com/zhoujinyi/p/11606935.html

posted @ 2021-03-14 13:09  LHX2018  阅读(128)  评论(0编辑  收藏  举报