CentOS 7 builds Redis cluster - single machine with multiple instances

1 Preparation

Redis 3.0 or higher has been installed, if not, please refer to https://blog.csdn.net/li90hou/article/details/79698954

2 Build a cluster

2.1 Create a directory

# 创建一个独立目录
mkdir /home/software/redis-cluster

# 进入目录
cd /home/software/redis-cluste

# 在 redis-cluster 目录中以端口好为名,创建 6 个目录
mkdir 7000 7001 7002 7003 7004 7005

2.2 Modify the configuration file

# 进入 Redis 安装主目录,拷贝一份配置文件
cp redis.conf redis-cluster.conf

# 修改配置文件
vim redis-cluster.conf
# 找到 bind 127.0.0.1 并注释掉
#bind 127.0.0.1

# 找到 protected-mode yes,修改
protected-mode no

# 找到 "# cluster-enabled yes",取消注释
cluster-enabled yes

# 找到 "# cluster-config-file nodes-6379.conf",取消注释,修改
cluster-config-file nodes.conf

# 找到 "# cluster-node-timeout 15000",取消注释,修改
cluster-node-timeout 5000

# 找到 appendonly no,修改
appendonly yes

Copy the modified configuration file redis-cluster.conf to the 7000 7001 7002 7003 7004 7005 directory

cp redis-cluster.conf /home/software/redis-cluster/7000
cp redis-cluster.conf /home/software/redis-cluster/7001
cp redis-cluster.conf /home/software/redis-cluster/7002
cp redis-cluster.conf /home/software/redis-cluster/7003
cp redis-cluster.conf /home/software/redis-cluster/7004
cp redis-cluster.conf /home/software/redis-cluster/7005

Enter the 7000 7001 7002 7003 7004 7005 directory in turn, and modify the configuration file, the port number is changed to the same as the directory name

cd /home/software/redis-cluster/7000
vim redis-cluster.conf

modify port

port 7000

2.3 Start the Redis node

Enter 7000 7001 7002 7003 7004 7005 respectively and execute the startup command

redis-server redis-cluster.conf &

After all start, execute the following command

# 看到如图显示,启动成功
ps -aux|grep redis

write picture description here
注意:一定要进入各自目录启动 Redis 实例,因为自动生成的 node.conf 等文件将存在命令执行的目录中

2.4 Create a cluster

Create a Redis cluster and use the src/redis-trib.rb program in the Redis home directory. This program is written in Ruby, so you need to install the Ruby runtime environment

2.4.1 Install the Ruby runtime environment

yum install centos-release-scl 
yum --enablerepo=centos-sclo-rh -y install rh-ruby23
# 查看版本
ruby -v
gem install redis

2.4.2 Create

# 进入 Redis 主目录 src 目录中执行
./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

As shown in the figure, enter "yes" to start creating the cluster
write picture description here

After successful creation, view all nodes

redis-cli -p 7000 cluster nodes

It can be seen that among the 6 nodes, 3 masters and 3 slaves
write picture description here

3 References

[1] Redis official website https://redis.io/topics/cluster-tutorial
[2] "CentOS 7 Configuring Ruby Language Development Environment" https://blog.csdn.net/wh211212/article/details/70060854

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529244&siteId=291194637