Linux

CentOS 7 搭建Redis 5.0.5 三主三从集群

2020-07-07  本文已影响0人  天不生我小金

前言:该博客主要是记录自己学习的过程,方便以后查看,当然也希望能够帮到大家。

开整!!

第一步,官网下载压缩包,选择 5.0.5 版本

https://download.redis.io/releases/

第二步,准备6台虚拟机,系统为CentOS 7,如下

IP 节点端口
172.16.31.181 7001
172.16.31.182 7002
172.16.31.183 7003
172.16.31.184 7004
172.16.31.185 7005
172.16.31.186 7006

第三步,创建存储目录 /usr/local/redis,将下载的压缩包上传至此,并输入 tar -vxzf redis-5.0.5.tar.gz 命令解压缩包,6台虚拟机都要

mkdir /usr/local/redis
tar -vxzf redis-5.0.5.tar.gz

第四步,解压后进入 /usr/local/redis/redis-5.0.5,执行 make && make install 命令进行编译安装,6台虚拟机都要

cd /usr/local/redis/redis-5.0.5
make && make install

第五步,找到 /usr/local/redis/redis-5.0.5/redis.conf 配置文件,修改为 redis-700x.conf,后面的 x 根据不同的虚拟机 ip 对应的节点端口进行修改。6台虚拟机都要,并修改以下内容

redis-7001.conf

# 后台方式启动
daemonize yes
# 端口
port 7001
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7001.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7001.conf

redis-7002.conf

# 后台方式启动
daemonize yes
# 端口
port 7002
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7002.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7002.conf

redis-7003.conf

# 后台方式启动
daemonize yes
# 端口
port 7003
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7003.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7003.conf

redis-7004.conf

# 后台方式启动
daemonize yes
# 端口
port 7004
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7004.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7004.conf

redis-7005.conf

# 后台方式启动
daemonize yes
# 端口
port 7005
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7005.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7005.conf

redis-7006.conf

# 后台方式启动
daemonize yes
# 端口
port 7006
# 只允许本机连接,注释掉
# bind 127.0.0.1
# 关闭秘密保护模式
protected-mode no
# pidfile文件对应
pidfile /var/run/redis_7006.pid
# 开启集群,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成 
cluster-config-file nodes_7006.conf

第六步,启动redis-server,6台虚拟机都要,如下

172.16.31.181

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7001.conf

172.16.31.182

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7002.conf

172.16.31.183

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7003.conf

172.16.31.184

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7004.conf

172.16.31.185

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7005.conf

172.16.31.186

/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis-7006.conf

如下则表示启动成功

image.png

第七步,挑选任意虚拟机节点创建集群,我这边选择在 172.16.31.171 上创建,执行下面命令

/usr/local/redis/redis-5.0.5/src/redis-cli --cluster create 172.16.31.181:7001 172.16.31.182:7002 172.16.31.183:7003 172.16.31.184:7004 172.16.31.185:7005 172.16.31.186:7006 --cluster-replicas 1

该过程需要手动输入 yes 才能继续执行,结果如下则表示创建成功

image.png

第八步,使用以下命令查看集群状态

/usr/local/redis/redis-5.0.5/src/redis-cli -c -p 7001 cluster nodes
image.png

使用以下命令可关闭集群节点

/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.181 -p 7001 shutdown
/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.182 -p 7002 shutdown
/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.183 -p 7003 shutdown
/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.184 -p 7004 shutdown
/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.185 -p 7005 shutdown
/usr/local/redis/redis-5.0.5/src/redis-cli -c -h 172.16.31.186 -p 7006 shutdown

后记:本次分享到此结束,本人水平有限,难免有错误或遗漏之处,望大家指正和谅解,欢迎评论留言。

上一篇下一篇

猜你喜欢

热点阅读