RedisCluster cluster

Overview

RedisCluster is the official Redis cluster solution provided after Redis version 3.0. It can avoid the complicated master monitoring and election operations of the sentinel mechanism, and can also easily implement data fragmentation processing, taking advantage of the performance advantages of the cluster host, and providing more efficient Redis solution, which takes into account the decentralized architecture model. Each node is related to other nodes, and you only need to obtain the information of one node and the information of other nodes.

The Redis cluster will distribute all the saved data equally to each master host according to the existing master-slave relationship. If a master host fails to provide services due to problems, other master hosts will participate in the
election of a master host to continue to provide services.
Insert picture description here

Specific implementation steps

The cluster architecture uses 9 Redis database servers, of which three hosts have three Redis processes on each. The host information is as follows:

serial number Host name IP address description
1 node1 192.168.1.6 Redis main service x3 (6381, 6382, 6383)
1 node2 192.168.1.7 Redis main service x3 (6381, 6382, 6383)
1 node3 192.168.1.8 Redis main service x3 (6381, 6382, 6383)

node1, node2, node3 host

apt-get install ruby ruby-dev rubygems	#Ruby开发环境配置相关开发包的配置
mkdir -p /usr/data/redis/{
    
    redis-6381,redis-6382,redis-6383}/{
    
    logs,run,dbcache,config} #新建三套数据目录

#复制三套配置文件
cp /usr/local/redis/conf/redis.conf /usr/local/redis/conf/redis-6381.conf
cp /usr/local/redis/conf/redis.conf /usr/local/redis/conf/redis-6382.conf
cp /usr/local/redis/conf/redis.conf /usr/local/redis/conf/redis-6383.conf

vim /usr/local/redis/conf/redis-6381.conf #打开配置文件

#配置信息 
protect-mode no	#关闭受保护模式
port 6381	#监听端口
pidfile /usr/data/redis/redis-6381/run/redis_6381.pid	#pid保护目录
dir /usr/data/redis/redis-6381/dbcache
logfile "/usr/data/redis/redis-6381/logs/redis_6381.log"
#requirepass test	#取消密码配置
cluster-enabled yes	#配置开启cluster集群
cluster-config-file /usr/data/redis/redis-6381/config/nodes-6381.conf
cluster-node-timeout 15000	#定义连接的超时时间

vim /usr/local/redis/conf/redis-6382.conf #打开配置文件 

#配置信息 
protect-mode no	#关闭受保护模式
port 6382	#监听端口
pidfile /usr/data/redis/redis-6382/run/redis_6382.pid	#pid保护目录
dir /usr/data/redis/redis-6382/dbcache
logfile "/usr/data/redis/redis-6382/logs/redis_6382.log"
#requirepass test	#取消密码配置
cluster-enabled yes	#配置开启cluster集群
cluster-config-file /usr/data/redis/redis-6382/config/nodes-6382.conf
cluster-node-timeout 15000	#定义连接的超时时间


vim /usr/local/redis/conf/redis-6383.conf #打开配置文件 
#配置信息 
protect-mode no	#关闭受保护模式
port 6383	#监听端口
pidfile /usr/data/redis/redis-6383/run/redis_6383.pid	#pid保护目录
dir /usr/data/redis/redis-6383/dbcache
logfile "/usr/data/redis/redis-6383/logs/redis_6383.log"
#requirepass test	#取消密码配置
cluster-enabled yes	#配置开启cluster集群
cluster-config-file /usr/data/redis/redis-6383/config/nodes-6383.conf
cluster-node-timeout 15000	#定义连接的超时时间

#分别启动实例6381、6382、6383
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6381.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6382.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6383.conf


ps aux | grep redis	#查看当前主机Redis的进程

Insert picture description here

gem install redis #使用Ruby管理控制Redis 安装Redis的相关依赖包
cp redis-6.2.1/src/redis-trib.rb /usr/local/redis/bin/	#复制集群配置程序

#Redis主机加入到集群配置中
#/usr/local/redis/bin/redis-trib.rb create -replicas 2 192.168.1.6:6381 192.168.1.6:6382 192.168.1.6:6383 192.168.1.7:6381 192.168.1.7:6382 192.168.1.7:6383 192.168.1.8:6381 192.168.1.8:6382 192.168.1.8:6383 #已经被移除了
/usr/local/redis/bin/redis-cli --cluster create  192.168.1.6:6381 192.168.1.6:6382 192.168.1.6:6383 192.168.1.7:6381 192.168.1.7:6382 192.168.1.7:6383 192.168.1.8:6381 192.168.1.8:6382 192.168.1.8:6383 --cluster-replicas 2 -a test #

/usr/local/redis/bin/redis-cli -h 192.168.1.6 -p 6381
CONFIG SET protected-mode yes #打开受保护模式
ONFIG SET requirepass test	 #设置认证密码
auth test #设置密码需要登陆
CONFIG SET masterauth test	#整个集群需要一个统一的密码
CONFIG REWRITE #重写写入配置文件
SHUTDOWN #关闭当前Redis进程

Insert picture description here

#在所有的主机上重新启动redis进程
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6379.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6380.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6381.conf

vim /var/lib/gems/2.7.0/gems/redis-3.0.7/lib/redis/client.rb 	#由于通过配置命令实现了密码处理,所以此时需要手动修改Ruby配置文件,追加Redis的连接密码,才可以正常使用RedisCluster集群

Insert picture description here

#/usr/local/redis/bin/redis-trib.rb check 192.168.1.8:6382 #当前版本6.2.1不可用改用下面
redis-cli --cluster check 192.168.1.8:6382 -a test	#检测集群状态

/usr/local/redis/bin/redis-cli -h 192.168.1.8 -p 6383 -a test -c #登陆集群中的任意Master,使用-c进行集群查询
set mldn helloworld	#设置数据 客户端node3上设置数据

View data on host node1
Insert picture description here

Guess you like

Origin blog.csdn.net/sinat_27674731/article/details/114853223