首页服务器教程Centos搭建Redis集群实践教程

Centos搭建Redis集群实践教程

2024-04-03 47

CentOS系统上部署Redis集群可以提高Redis的可用性和性能。通过将Redis配置为集群模式,可以实现数据的分布式存储和负载均衡,从而更好地满足大规模应用的需求。本教程将介绍在CentOS系统上部署Redis集群的实践过程,包括配置集群、启动节点、启动集群等关键步骤。

一、简要说明

Redis5.0版是Redis产品的重大版本发布,推出了各种新特性,其中一点是放弃 Ruby的集群方式,改为使用 C语言编写的 redis-cli的方式,是集群的构建方式复杂度大大降低。关于集群的更新可以在 Redis5 的版本说明中看到,如下:

The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check `redis-cli --cluster help ` for more info.

可以查看Redis官网查看集群搭建方式,连接如:https://redis.io/topics/cluster-tutorial。

  • 集群中应该至少有三个节点,每个节点有一备份节点,需要6台服务器;
  • 如果条件有限,可以搭建伪分布式,以下步骤是在一台 Linux 服务器上搭建有6个节点的 Redis集群。

二、创建集群步骤

1、创建目录

mkdir /usr/local/redis-cluster

2、下载源码并解压编译

#wget http://download.redis.io/releases/redis-5.0.0.tar.gz
#tar xzf redis-5.0.0.tar.gz
#cd redis-5.0.0
#make
#make install PREFIX=/usr/local/redis

安装无报错,执行make test命令测试下:

#make test

Centos搭建Redis集群实践教程

三、创建6个Redis配置文件

6个配置文件不能在同一个目录,此处我们定义如下:

/root/software/redis/redis-cluster-conf/7001/redis.conf
/root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
/root/software/redis/redis-cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis.conf

一些操作命令仅供参考:

cp redis.conf /usr/local/redis/bin
cd /usr/local/redis/
cp -r bin ../redis-cluster/redis01
cd /usr/local/redis-cluster/redis01
rm dump.rdb #删除快照
vim redis.conf

配置文件的内容为:

port 7001  #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile  /var/run/redis_7001.pid
bind 61.132.227.16 #127.0.0.1改为本机ip地址,可用ifconfig查看ip

其中 port 和 pidfile 需要随着 文件夹的不同调增。

创建剩余5个实例:

[root@master redis-cluster]# cp -r redis01/ redis02
[root@master redis-cluster]# cp -r redis01/ redis03
[root@master redis-cluster]# cp -r redis01/ redis04
[root@master redis-cluster]# cp -r redis01/ redis05
[root@master redis-cluster]# cp -r redis01/ redis06

分别修改redis02 ~ redis06 的 redis.conf下的port 和 pidfile

四、启动节点

分别进入redis01、redis02、…redis06目录,执行:

./redis-server ./redis.conf

创建一个批处理文件,同时启动着六个Redis:

vim startall.sh

添加如下内容:

cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..

然后执行chmod u+x start-all.sh将start-all.sh变成可执行文件。

在当前目录下启动:

./startall.sh

查看:

ps aux|grep redis

Centos搭建Redis集群实践教程

五、启动集群

因为我们使用的5.0.0的版本的Redis搭建的集群只需要把编译后的redis目录中的这个redis-cli文件拷贝到redis-cluster目录过来即可。(Redis版本5.0以后都是用C语言直接启动)

/usr/local/redis-cluster/redis-cli --cluster create 61.132.227.16:7001 61.132.227.16:7002 61.132.227.16:7003 61.132.227.16:7004 61.132.227.16:7005 61.132.227.16:7006 --cluster-replicas 1

启动后,可看到成功信息,如下:

Centos搭建Redis集群实践教程

[root@localhost redis-cluster]# /usr/local/redis-cluster/redis-cli --cluster create 61.132.227.16:7001 61.132.227.16:7002 61.132.227.16:7003 61.132.227.16:7004 61.132.227.16:7005 61.132.227.16:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 61.132.227.16:7004 to 61.132.227.16:7001
Adding replica 61.132.227.16:7005 to 61.132.227.16:7002
Adding replica 61.132.227.16:7006 to 61.132.227.16:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 057fdf576d1ddaa87604c4c3aaf172f2d281a869 61.132.227.16:7001
slots:[0-5460] (5461 slots) master
M: 326dbd2094f351b15ab63f1c5d55353e160a4fdf 61.132.227.16:7002
slots:[5461-10922] (5462 slots) master
M: dac17acf82833efc7d9ca85c3dcb1a5d8822de13 61.132.227.16:7003
slots:[10923-16383] (5461 slots) master
S: 7bfd90361c2da58c8bb6c104274896ff969930a1 61.132.227.16:7004
replicates 326dbd2094f351b15ab63f1c5d55353e160a4fdf
S: a36100c9b6f6aba00906aa8566dc8e558351e687 61.132.227.16:7005
replicates dac17acf82833efc7d9ca85c3dcb1a5d8822de13
S: aa336483815035a2472a317cb8118c7340fa91bb 61.132.227.16:7006
replicates 057fdf576d1ddaa87604c4c3aaf172f2d281a869

输入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 61.132.227.16:7001)
M: 057fdf576d1ddaa87604c4c3aaf172f2d281a869 61.132.227.16:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 7bfd90361c2da58c8bb6c104274896ff969930a1 61.132.227.16:7004
slots: (0 slots) slave
replicates 326dbd2094f351b15ab63f1c5d55353e160a4fdf
S: aa336483815035a2472a317cb8118c7340fa91bb 61.132.227.16:7006
slots: (0 slots) slave
replicates 057fdf576d1ddaa87604c4c3aaf172f2d281a869
M: dac17acf82833efc7d9ca85c3dcb1a5d8822de13 61.132.227.16:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: a36100c9b6f6aba00906aa8566dc8e558351e687 61.132.227.16:7005
slots: (0 slots) slave
replicates dac17acf82833efc7d9ca85c3dcb1a5d8822de13
M: 326dbd2094f351b15ab63f1c5d55353e160a4fdf 61.132.227.16:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-cluster]#

至此,Reids5 集群通过命令形式搭建完成。

  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。

相关文章