redis单机集群搭建

redis单机集群搭建

配置环境

  1. 系统版本
    CentOS Linux release 7.5.1804
  2. redis版本
    redis-4.0.2

安装步骤

1.安装redis
准备安装包:链接:https://pan.baidu.com/s/121x43TWBuvKPaXQaUA9U-w
提取码:hfmo (这是4.0.2版本,可直接到官网下载最新版本:https://redis.io/download)

解压

cd /opt/tools
tar -zxvf redis-4.0.2.tar.gz

安装gcc-c++

yum install gcc-c++

切换到解压redis后的路径

cd /opt/tools/redis-4.0.2

当前目录编译

make

选择编译路径

make install PREFIX=/opt/tools/redis

Reids安装包里有个集群工具,要复制到/opt/tools/redis/bin里去
redis-trib.rb是在redis中专门部署集群用的工具包

cp /opt/tools/redis-4.0.2/src/redis-trib.rb /opt/tools/redis/bin
  1. 修改/opt/tools/redis/conf/redis.conf配置文件,在本次单机集群中将创建7001-7006留个节点服务
  • port 7001 //六个节点配置文件分别是7001-7006

  • daemonize yes //redis后台运行

  • pidfile /var/run/redis_7001.pid //pidfile文件对应7001-7006

  • cluster-enabled yes //开启集群

  • cluster-config-file nodes_7001.conf //保存节点配置,自动创建,自动更新对应7001-7006

  • cluster-node-timeout 5000 //集群超时时间,节点超过这个时间没反应就断定是宕机

  • appendonly yes //存储方式,aof,将写操作记录保存到日志中

  1. 7001下的修改完后,我们把7001下的配置分别复制到7002-7006 然后对应的再修改下配置即可;
[root@hadoop-6 config]# cp redis7001.conf redis7002.conf
[root@hadoop-6 config]# cp redis7001.conf redis7003.conf
[root@hadoop-6 config]# cp redis7001.conf redis7004.conf
[root@hadoop-6 config]# cp redis7001.conf redis7005.conf
[root@hadoop-6 config]# cp redis7001.conf redis7006.conf
  1. 修改其他节点中的端口配置
sed -i 's/7001/7002/g' redis7002.conf
sed -i 's/7001/7003/g' redis7003.conf
sed -i 's/7001/7004/g' redis7004.conf
sed -i 's/7001/7005/g' redis7005.conf
sed -i 's/7001/7006/g' redis7006.conf

启动server,其他节点类似

[root@hadoop-6 redis]# ./bin/redis-server ./config/redis7001.conf
1743:C 29 Sep 00:48:49.618 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1743:C 29 Sep 00:48:49.619 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=1743, just started
1743:C 29 Sep 00:48:49.619 # Configuration loaded

检查节点信息

[root@hadoop-6 redis]# ps -ef | grep redis
root      70846      1  0 20:05 ?        00:00:00 ./bin/redis-server 127.0.0.1:7001 [cluster]
root      70858      1  0 20:05 ?        00:00:00 ./bin/redis-server 127.0.0.1:7002 [cluster]
root      70869      1  0 20:06 ?        00:00:00 ./bin/redis-server 127.0.0.1:7003 [cluster]
root      70878      1  0 20:06 ?        00:00:00 ./bin/redis-server 127.0.0.1:7004 [cluster]
root      70904      1  0 20:06 ?        00:00:00 ./bin/redis-server 127.0.0.1:7005 [cluster]
root      70915      1  0 20:06 ?        00:00:00 ./bin/redis-server 127.0.0.1:7006 [cluster]
root       1787   1582  0 00:51 pts/0    00:00:00 grep --color=auto redis

创建集群

  • 安装ruby
yum -y install ruby ruby-devel rubygems rpm-build

在这里插入图片描述

  • 安装Redis接口
  • 在安装Redis接口的时候可能会遇到如上的问题,以下错误信息告诉我们Ruby版本太低,
    在这里插入图片描述

查看ruby版本

[root@hadoop-6 bin]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

如果直接安装不成功,通过安装RVM,对ruby的版本进行管理,RVM需要通过CRUL来进行下载,安装crul

[root@hadoop-6 bin]# yum install curl

在这里插入图片描述

使用curl安装rvm ,输入命令 " curl -L get.rvm.io | bash -s stable " 进行安装

[root@hadoop-6 bin]# curl -L get.rvm.io | bash -s stable

在这里插入图片描述
使用source让当前shell读入路径为" /usr/local/rvm/scripts/rvm "

[root@hadoop-6 bin]# source /usr/local/rvm/scripts/rvm

安装ruby

[root@hadoop-6 bin]# rvm install 2.3.4
[root@hadoop-6 bin]# rvm use 2.3.4

安装Redis接口

[root@hadoop-6 bin]# gem install redis
Fetching redis-4.2.2.gem
Successfully installed redis-4.2.2
Parsing documentation for redis-4.2.2
Installing ri documentation for redis-4.2.2
Done installing documentation for redis after 0 seconds
1 gem installed

创建集群


出现以下情况c
在这里插入图片描述
修改配置文件

[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7001.conf
[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7002.conf
[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7003.conf
[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7004.conf
[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7005.conf
[root@hadoop-6 config]# sed -i 's/# cluster-enabled yes/cluster-enabled yes'/g redis7006.conf

重启redis-server
如果出现下面问题,取消密码配置
在这里插入图片描述
配置redis密码(1234为我本机配置的密码,可以自行设置)

[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7001.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7001.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7002.conf
[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7002.conf
[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7003.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7003.conf
[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7004.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7004.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7005.conf
[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7005.conf
[root@hadoop-6 config]# sed -i 's/# masterauth <master-password>/masterauth 1234/g' redis7006.conf
[root@hadoop-6 config]# sed -i 's/# requirepass foobared/requirepass 1234/g' redis7006.conf

修改ruby连接redis密码
找到下面这个文件,修改密码配置
在这里插入图片描述
修改client.rb中的端口和密码
在这里插入图片描述

创建集群

[root@hadoop-6 redis]# ./bin/redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: c7b8a0f0bcab291d95d398b5cd8004a5a8d8f33a 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: fbf53e64dc8f7a413169c11661d659b252d1b566 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: f858498e9f93a603d02f9f891d566efe8da69988 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 753e28ea0c651c9b22a92a08fe0b6df05efb90d3 127.0.0.1:7004
   replicates c7b8a0f0bcab291d95d398b5cd8004a5a8d8f33a
S: 21c0b8e81174aa8509508f4de61fbd9b63593a4d 127.0.0.1:7005
   replicates fbf53e64dc8f7a413169c11661d659b252d1b566
S: 792ec956110cc7b6de611b9960a6655db975f252 127.0.0.1:7006
   replicates f858498e9f93a603d02f9f891d566efe8da69988
Can I set the above configuration? (type 'yes' to accept): 

从运行结果看 主节点就是7001 7002 7003 从节点分别是7004 7005 7006

7001分配到的哈希槽是 0-5460

7002分配到的哈希槽是 5461-10922

7003分配到的哈希槽是 10923-16383
最后问我们是否接受上面的设置,输入yes 就表示接受,我们输入yes

>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: c7b8a0f0bcab291d95d398b5cd8004a5a8d8f33a 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: f858498e9f93a603d02f9f891d566efe8da69988 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 753e28ea0c651c9b22a92a08fe0b6df05efb90d3 127.0.0.1:7004
   slots: (0 slots) slave
   replicates c7b8a0f0bcab291d95d398b5cd8004a5a8d8f33a
S: 21c0b8e81174aa8509508f4de61fbd9b63593a4d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates fbf53e64dc8f7a413169c11661d659b252d1b566
M: fbf53e64dc8f7a413169c11661d659b252d1b566 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 792ec956110cc7b6de611b9960a6655db975f252 127.0.0.1:7006
   slots: (0 slots) slave
   replicates f858498e9f93a603d02f9f891d566efe8da69988
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试数据集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值