Analysis of Redis Cluster cluster operation and maintenance and core principles

Comparison of Redis cluster solutions

Sentry mode

Insert image description here

  • In versions prior to redis3.0, to implement a cluster , the sentinel tool was generally used to monitor the status of the master node.
  • If the master node is abnormal, a master-slave switch will be performed and a certain slave will be used as the master.
  • The configuration of Sentinel is slightly complex, and its performance and high availability are average in all aspects, especially when access is interrupted at the moment of master-slave switching. Moreover, Sentinel mode only has one master node to provide services to the outside world, which cannot support high concurrency. , and the memory of a single master node should not be set too large, otherwise the persistent file will be too large, affecting the efficiency of data recovery or master-slave synchronization.

High availability cluster mode

Insert image description here

  • The redis cluster is a distributed server group composed of multiple master-slave node groups. It has replication, high availability and sharding features .
  • Redis cluster can complete node removal and failover functions without sentinel.
  • Each node needs to be set to cluster mode. This cluster mode has no central node and can be expanded horizontally. According to official documents, it can be linearly expanded to tens of thousands of nodes (the official recommendation is no more than 1,000 nodes).
  • The performance and high availability of the redis cluster are better than the previous version of sentinel mode, and the cluster configuration is very simple

Redis high availability cluster construction

redis cluster construction

The redis cluster requires at least three master nodes. We build three master nodes here, and build a slave node for each master, a total of 6 redis nodes. Here we use three machines to deploy 6 redis instances, one master for each machine. First, the steps to build a cluster are as follows:

Step 1: Create the folder redis-cluster under /usr/local on the first machine, and then create 2 folders under it as follows:

(1)mkdir -p /usr/local/redis-cluster
(2)mkdir 8001 8004

Step 2: Copy the previous redis.conf configuration file to 8001 and modify the following content:

(1)daemonize yes
(2)port 8001(分别对每个机器的端口号进行设置)
(3)pidfile /var/run/redis_8001.pid  # 把pid进程号写入pidfile配置的文件
(4)dir /usr/local/redis-cluster/8001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
(5)cluster-enabled yes(启动集群模式)
(6)cluster-config-file nodes-8001.conf(集群节点信息文件,这里800x最好和port对应上)
(7)cluster-node-timeout 10000
 (8)# bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
 (9)protected-mode  no   (关闭保护模式)
 (10)appendonly yes
如果要设置密码需要增加如下配置:
 (11)requirepass zhuge     (设置redis访问密码)
 (12)masterauth zhuge      (设置集群节点间访问密码,跟上面一致)

Step 3: Copy the modified configuration file to 8004, and modify the port numbers in items 2, 3, 4, and 6. You can replace them in batches:

:%s/源字符串/目的字符串/g 

Step 4: The other two machines also need to perform the above steps. The second machine uses 8002 and 8005, and the third machine uses 8003 and 8006.

Step 5: Start 6 redis instances respectively, and then check whether the startup is successful

(1)/usr/local/redis-5.0.3/src/redis-server /usr/local/redis-cluster/800*/redis.conf
(2)ps -ef | grep redis 查看是否启动成功

Step 6: Use redis-cli to create the entire redis cluster (the cluster in versions prior to redis5 was implemented by relying on the ruby ​​script redis-trib.rb)

# 下面命令里的1代表为每个创建的主服务器节点创建一个从服务器节点
# 执行这条命令需要确认三台机器之间的redis实例要能相互访问,可以先简单把所有机器防火墙关掉,如果不关闭防火墙则需要打开redis服务端口和集群节点gossip通信端口16379(默认是在redis端口号上加1W)
# 关闭防火墙
# systemctl stop firewalld # 临时关闭防火墙
# systemctl disable firewalld # 禁止开机启动
# 注意:下面这条创建集群的命令大家不要直接复制,里面的空格编码可能有问题导致创建集群不成功
(1)/usr/local/redis-5.0.3/src/redis-cli -a zhuge --cluster create --cluster-replicas 1 192.168.0.61:8001 192.168.0.62:8002 192.168.0.63:8003 192.168.0.61:8004 192.168.0.62:8005 192.168.0.63:8006 

Step 7: Verify the cluster:

(1)连接任意一个客户端即可:./redis-cli -c -h -p (-a访问服务端密码,-c表示集群模式,指定ip地址和端口号)
    如:/usr/local/redis-5.0.3/src/redis-cli -a zhuge -c -h 192.168.0.61 -p 800*
(2)进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)
(3)进行数据操作验证
(4)关闭集群则需要逐个进行关闭,使用命令:
/usr/local/redis-5.0.3/src/redis-cli -a zhuge -c -h 192.168.0.60 -p 800* shutdown

Guess you like

Origin blog.csdn.net/beautybug1126/article/details/132737335