搭建redis集群

tech2025-08-30  6

搭建redis集群

1.准备工作(redis单机版)

(1)安装gc

Redis 是 c 语言开发的。安装 redis 需要 c 语言的编译环境。如果没有 gcc 需要在线安装。

yum install gcc-c++

(2)使用yum命令安装 ruby (我们需要使用ruby脚本来实现集群搭建)【此步省略】

yum install ruby yum install rubygems ----- 知识点小贴士 ----- Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。由 Ruby 语言本身还发展出了JRuby(Java平台)、IronRuby(.NET平台)等其他平台的 Ruby 语言替代品。Ruby的作者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。因为Perl发音与6月诞生石pearl(珍珠)相同,因此Ruby以7月诞生石ruby(红宝石)命名 RubyGems简称gems,是一个用于对 Ruby组件进行打包的 Ruby 打包系统

(3)将redis源码包上传到 linux 系统 ,解压redis源码包

创建一个文件夹,6个redis都放到该文件夹中

[root@localhost ~]# mkdir /usr/local/redis-cluster

将redis的压缩包放入该文件夹中,解压

[root@localhost ~]# cd /usr/local/redis-cluster [root@localhost redis-cluster]# ls redis-3.0.0.tar.gz [root@localhost redis-cluster]# tar -zxvf redis-3.0.0.tar.gz

(4)编译redis源码 ,进入redis源码文件夹

[root@localhost redis-cluster]# ls redis-3.0.0 redis-3.0.0.tar.gz [root@localhost redis-cluster]# cd redis-3.0.0 [root@localhost redis-3.0.0]# make

(5)创建目录/usr/local/redis-cluster/redis目录, 先装好一个redis,其余的redis都复制这个

[root@localhost redis-3.0.0]# mkdir /usr/local/redis-cluster/redis [root@localhost redis-3.0.0]# make install PREFIX=/usr/local/redis-cluster/redis

(6)复制配置文件 将 /redis-3.0.0/redis.conf 复制到redis下的bin目录下

[root@localhost redis-3.0.0]# ls 00-RELEASENOTES CONTRIBUTING deps Makefile README runtest runtest-sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis/bin

(7)修改配置文件,设置redis后台启动

[root@localhost redis-3.0.0]# cd .. [root@localhost redis-cluster]# ls redis redis-3.0.0 redis-3.0.0.tar.gz [root@localhost redis-cluster]# cd redis/bin [root@localhost bin]# ls redis-benchmark redis-check-aof redis-check-dump redis-cli redis.conf redis-sentinel redis-server [root@localhost bin]# vi redis.conf

(8) 启动reidis

[root@localhost bin]# ./redis-server ./redis.conf

2.redis集群

[root@localhost bin]# cd .. [root@localhost redis]# cd .. [root@localhost redis-cluster]# ls redis redis-3.0.0 redis-3.0.0.tar.gz

(1)搭建一个样本的redis

复制redis文件夹,-r表示包括里面所有文件夹,复制产生的文件夹的名字为redis-1

[root@localhost redis-cluster]# cp -r redis ./redis-1 [root@localhost redis-cluster]# ls redis redis-1 redis-3.0.0 redis-3.0.0.tar.gz

修改配置文件,更改端口号

[root@localhost redis-cluster]# cd redis-1/bin [root@localhost bin]# ls redis-benchmark redis-check-aof redis-check-dump redis-cli redis.conf redis-sentinel redis-server [root@localhost bin]# vi redis.conf

去掉前面的#号,开启集群功能

(2)复制5份

[root@localhost bin]# cd .. [root@localhost redis-1]# cd .. [root@localhost redis-cluster]# cp -r redis-1 ./redis-2 [root@localhost redis-cluster]# cp -r redis-1 ./redis-3 [root@localhost redis-cluster]# cp -r redis-1 ./redis-4 [root@localhost redis-cluster]# cp -r redis-1 ./redis-5 [root@localhost redis-cluster]# cp -r redis-1 ./redis-6 [root@localhost redis-cluster]# ls redis redis-1 redis-2 redis-3 redis-3.0.0 redis-3.0.0.tar.gz redis-4 redis-5 redis-6

修改另外五份的端口,改为7002,7003,7004,7005,7006

[root@localhost redis-cluster]# vi redis-2/bin/redis.conf [root@localhost redis-cluster]# vi redis-3/bin/redis.conf [root@localhost redis-cluster]# vi redis-4/bin/redis.conf [root@localhost redis-cluster]# vi redis-5/bin/redis.conf [root@localhost redis-cluster]# vi redis-6/bin/redis.conf

(3)挨个启动redis

[root@localhost redis-cluster]# cd /usr/local/redis-cluster/redis-1/bin [root@localhost bin]# ls redis-benchmark redis-check-aof redis-check-dump redis-cli redis.conf redis-sentinel redis-server [root@localhost bin]# ./redis-server ./redis.conf [root@localhost bin]# cd /usr/local/redis-cluster/redis-2/bin [root@localhost bin]# ./redis-server ./redis.conf [root@localhost bin]# cd /usr/local/redis-cluster/redis-3/bin [root@localhost bin]# ./redis-server ./redis.conf [root@localhost bin]# cd /usr/local/redis-cluster/redis-4/bin [root@localhost bin]# ./redis-server ./redis.conf [root@localhost bin]# cd /usr/local/redis-cluster/redis-5/bin [root@localhost bin]# ./redis-server ./redis.conf [root@localhost bin]# cd /usr/local/redis-cluster/redis-6/bin [root@localhost bin]# ./redis-server ./redis.conf

查看redis是否启动成功

[root@localhost bin]# ps -ef|grep redis

杀死几个集群的redis

[root@localhost bin]# kill -9 38158 39509 39570 39610 39658 39741

(4)使用sh文件同时启动多个redis

新建一个startRedisAll.sh文件

[root@localhost redis-cluster]# vi startRedisAll.sh

里面写入命令

cd /usr/local/redis-cluster/redis-1/bin ./redis-server ./redis.conf cd /usr/local/redis-cluster/redis-2/bin ./redis-server ./redis.conf cd /usr/local/redis-cluster/redis-3/bin ./redis-server ./redis.conf cd /usr/local/redis-cluster/redis-4/bin ./redis-server ./redis.conf cd /usr/local/redis-cluster/redis-5/bin ./redis-server ./redis.conf cd /usr/local/redis-cluster/redis-6/bin ./redis-server ./redis.conf

查看该文件的权限

[root@localhost redis-cluster]# ll

为该文件添加可执行权限

[root@localhost redis-cluster]# chmod +x startRedisAll.sh

(5)启动所有redis

[root@localhost redis-cluster]# ./startRedisAll.sh [root@localhost redis-cluster]# ps -ef|grep redis systemd+ 3190 3158 0 17:52 ? 00:00:10 redis-server *:6379 root 46503 1 0 19:29 ? 00:00:00 ./redis-server *:7002 [cluster] root 46507 1 0 19:29 ? 00:00:00 ./redis-server *:7003 [cluster] root 46511 1 0 19:29 ? 00:00:00 ./redis-server *:7004 [cluster] root 46515 1 0 19:29 ? 00:00:00 ./redis-server *:7005 [cluster] root 46519 1 0 19:29 ? 00:00:00 ./redis-server *:7006 [cluster] root 49260 1 0 19:36 ? 00:00:00 ./redis-server *:7001 [cluster] root 49374 37182 0 19:36 pts/1 00:00:00 grep --color=auto redis

(6)关闭防火墙,并禁止开机启动

[root@localhost redis-cluster]# systemctl stop firewalld.service [root@localhost redis-cluster]# systemctl disable firewalld.service

(7)上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本。

[root@localhost redis-cluster]# ls redis redis-1 redis-2 redis-3 redis-3.0.0 redis-3.0.0.gem redis-3.0.0.tar.gz redis-4 redis-5 redis-6 startredisall.sh [root@localhost redis-cluster]# gem install redis-3.0.0.gem

(8)使用 ruby 脚本搭建集群。

进入redis源码目录中的src目录

[root@localhost redis-cluster]# cd redis-3.0.0/src

执行下面的命令

redis-trib.rb ruby工具,可以实现Redis集群

create创建集群

--replicas创建主从关系

1:是否随机创建(是)

[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.43.33:7001 192.168.43.33:7002 192.168.43.33:7003 192.168.43.33:7004 192.168.43.33:7005 192.168.43.33:7006

3.连接Redis-Cluster

(1)客户端工具连接

Redis-cli 连接集群:

redis-cli -p ip地址 -p 端口 -c

-c:代表连接的是 redis 集群

测试值的存取:

(1)从本地连接到集群redis 使用7001端口 加 -c 参数

(2)存入name值为abc ,系统提示此值被存入到了7002端口所在的redis (槽是5798)

(3)提取name的值,可以提取。

(4)退出(quit)

(5)再次以7001端口进入 ,不带-c

(6)查询name值,无法获取,因为值在7002端口的redis上

(7)我们以7002端口进入,获取name值发现是可以获取的,而以其它端口进入均不能获取

(2) java连接(yml文件中加入)

redis: cluster: nodes: - 192.168.43.33:7001 - 192.168.43.33:7002 - 192.168.43.33:7003 - 192.168.43.33:7004 - 192.168.43.33:7005 - 192.168.43.33:7006

4.哨兵模式

Redis在使用过程中服务器毫无征兆的宕机,是一个麻烦的事情,如何保证备份的机器是原始服务器的完整备份呢?这时候就需要哨兵和复制。

Sentinel(哨兵)可以管理多个Redis服务器,它提供了监控,提醒以及自动的故障转移的功能,

Replication(复制)则是负责让一个Redis服务器可以配备多个备份的服务器。

Redis也是利用这两个功能来保证Redis的高可用的

(1)将安装包中的sentinel.conf复制到每一个redis的bin中

[root@localhost redis-cluster]# cd redis-3.0.0 [root@localhost redis-3.0.0]# ls 00-RELEASENOTES CONTRIBUTING deps Makefile README runtest runtest-sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests [root@localhost redis-3.0.0]# cp -r sentinel.conf /usr/local/redis-cluster/redis-1/bin

(2)修改配置文件

(3)启动哨兵模式

[root@localhost bin]# ./redis-sentinel ./sentinel.conf

最新回复(0)