Redis集群部署(手动安装)

Posted concurrencyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis集群部署(手动安装)相关的知识,希望对你有一定的参考价值。

1. 安装依赖包

注意:本节需要使用root用户操作

1.1 安装ruby

yum install ruby -y
yum install ruby-devel.x86_64 -y

1.2 安装rubygem

有些系统默认没有rubygems的包,可能需要手动安装,先安装好ruby-irb和ruby-rdoc,然后操作以下步骤。

(1) 下载rubygem包,https://rubygems.org/?locale=zh-CN

(2) 上传rubygems-2.4.8.zip至/redis目录

(3) 解压,安装

unzip rubygems-2.4.8.zip
cd rubygems-2.4.8
ruby setup.rb

1.3 安装redis-gem

上传redis.gem至/redis目录,执行命令gem install -l redis-3.2.2.gem

2. 安装Redis集群

本节开始可使用root用户或者你规划的用户安装redis

2.1 redis安装

上传redis-3.2.1.tar.gz到/redis目录

tar -zxvf redis-3.2.1.tar.gz
cd redis-3.2.1
make
make install PREFIX=/redis/redis

执行make命令可能会遇到这两种错误:

(1)

make[3]: gcc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: Leaving directory `/home/wxyuan/redis-3.2.1/deps/hiredis‘
make[2]: *** [hiredis] 错误 2
make[2]: Leaving directory `/home/wxyuan/redis-3.2.1/deps‘
make[1]: [persist-settings] 错误 2 (忽略)
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] 错误 127
make[1]: Leaving directory `/home/wxyuan/redis-3.2.1/src‘
make: *** [all] 错误 2

错误原因:缺少gcc包

解决方法:执行命令yum install gcc -y安装gcc包

(2)

cd src && make all
make[1]: Entering directory `/home/wxyuan/redis-3.2.1/src‘
    CC adlist.o
在包含自 adlist.c:34 的文件中:
zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
make[1]: *** [adlist.o] 错误 1
make[1]: Leaving directory `/home/wxyuan/redis-3.2.1/src‘
make: *** [all] 错误 2

原因分析和解决方法,请参考https://blog.csdn.net/bugall/article/details/45914867

2.2 创建节点目录

mkdir -p /redis/redis/cluster/7380
mkdir -p /redis/redis/cluster/7381

2.3 修改配置文件

将/redis/redis-3.2.1下的配置文件redis.conf拷贝到/redis/redis/cluster/7380下,并作如下修改

bind 192.168.1.101 127.0.0.1 #注意192.168.1.10改成你的机器IP。绑定多个IP,既可以远程访问,也可以本地客户端访问
port 7380 #端口可以修改
appendonly yes
daemonize yes
cluster-enabled yes
cluster-config-file /redis/redis/cluster/7380/nodes.conf
cluster-node-timeout 5000

2.4 复制配置文件redis.conf到其他节点

cp /redis/redis/cluster/7380/redis.conf /redis/redis/cluster/7381/redis.conf
# port、cluster-config-file需要修改下
#--------------------------------------------------------
#port 7381
#cluster-config-file /redis/redis/cluster/7381/nodes.conf
#------------------------------------------------------

2.5 启动redis

执行如下命令启动redis

[[email protected] ~]$ /redis/redis/bin/redis-server /redis/redis/cluster/7380/redis.conf
[[email protected] ~]$ /redis/redis/bin/redis-server /redis/redis/cluster/7381/redis.conf

启动后,使用命令ps -ef|grep redis可查看redis进程。

[[email protected] ~]$ ps -ef|grep redis
wxyuan     1912      1  0 21:19 ?        00:00:02 /redis/redis/bin/redis-server 192.168.1.101:7380 [cluster]        
wxyuan     1916      1  0 21:19 ?        00:00:02 /redis/redis/bin/redis-server 192.168.1.101:7381 [cluster]

注意:以上步骤,另外两台机器需要做同样的操作

3 创建redis集群

注意:创建集群前,最好关闭防火墙,如果防火墙配置不当,可能出现连接拒绝的错误

创建集群,让上面的实例互相通讯(1表示为每个master分配一个salve)

[[email protected] src]$ ./redis-trib.rb create --replicas 1 192.168.1.101:7380 192.168.1.101:7381 192.168.1.102:7380 192.168.1.102:7381 192.168.1.103:7380 192.168.1.103:7381
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.103:7380
192.168.1.102:7380
192.168.1.101:7380
Adding replica 192.168.1.102:7381 to 192.168.1.103:7380
Adding replica 192.168.1.103:7381 to 192.168.1.102:7380
Adding replica 192.168.1.101:7381 to 192.168.1.101:7380
M: 9e486a62edb7be05691219259a534a0491e376e3 192.168.1.101:7380
   slots:10923-16383 (5461 slots) master
S: 51f00a2e9639b0427ec177877d775244307b096e 192.168.1.101:7381
   replicates 9e486a62edb7be05691219259a534a0491e376e3
M: bc2d50daf9adb4e16e40048249ea97ec039dfa46 192.168.1.102:7380
   slots:5461-10922 (5462 slots) master
S: 2ad41829d6c48a0216fb4632253add6edc80b3fc 192.168.1.102:7381
   replicates 5e7a6df4a0c931ca36afced6c3c62330da026c26
M: 5e7a6df4a0c931ca36afced6c3c62330da026c26 192.168.1.103:7380
   slots:0-5460 (5461 slots) master
S: 07698f884a97e6a85a7f032c0fd95738d6e73017 192.168.1.103:7381
   replicates bc2d50daf9adb4e16e40048249ea97ec039dfa46
# 这里我们可以看到master节点为192.168.1.101:7380、192.168.1.102:7380、192.168.1.103:7380;其他三个为slave节点,输入yes确认
Can I set the above configuration? (type ‘yes‘ to accept): 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 192.168.1.101:7380)
M: 9e486a62edb7be05691219259a534a0491e376e3 192.168.1.101:7380
   slots:10923-16383 (5461 slots) master
M: 51f00a2e9639b0427ec177877d775244307b096e 192.168.1.101:7381
   slots: (0 slots) master
   replicates 9e486a62edb7be05691219259a534a0491e376e3
M: bc2d50daf9adb4e16e40048249ea97ec039dfa46 192.168.1.102:7380
   slots:5461-10922 (5462 slots) master
M: 2ad41829d6c48a0216fb4632253add6edc80b3fc 192.168.1.102:7381
   slots: (0 slots) master
   replicates 5e7a6df4a0c931ca36afced6c3c62330da026c26
M: 5e7a6df4a0c931ca36afced6c3c62330da026c26 192.168.1.103:7380
   slots:0-5460 (5461 slots) master
M: 07698f884a97e6a85a7f032c0fd95738d6e73017 192.168.1.103:7381
   slots: (0 slots) master
   replicates bc2d50daf9adb4e16e40048249ea97ec039dfa46
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到此redis集群创建完成。

4. 测试redis集群

在任意一台集群机器上执行如下操作

[[email protected] ~]$ /redis/redis/bin/redis-cli -h 192.168.1.101 -p 7380 -c
192.168.1.101:7380> set k1 "AA"
OK
192.168.1.101:7380> set k2 "BB"
-> Redirected to slot [449] located at 192.168.1.103:7380
OK
192.168.1.103:7380> get k1
-> Redirected to slot [12706] located at 192.168.1.101:7380
"AA"
192.168.1.101:7380> get k2
-> Redirected to slot [449] located at 192.168.1.103:7380
"BB"
192.168.1.103:7380> 









以上是关于Redis集群部署(手动安装)的主要内容,如果未能解决你的问题,请参考以下文章

使用Trib工具自动化部署Redis Cluster三主三从交叉复制集群 #yyds干货盘点#

Java进阶之光!手动搭建高可用的Redis5-0分片集群

Redis-Cluster 5.0.4 集群部署

redis集群部署方案

Redis集群部署

redis cluster集群部署