旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

redis集群设置与治理的细致引见(附代码)【MySQL教程】,redis

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:28评论:0


导读:本篇文章给人人带来的内容是关于redis集群设置与治理的细致引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。Redis在3.0版本今后最先支撑...
本篇文章给人人带来的内容是关于redis集群设置与治理的细致引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

Redis在3.0版本今后最先支撑集群,经由中间几个版本的不断更新优化,最新的版本集群功用已异常完美。本文简朴引见一下Redis集群搭建的历程和设置要领,redis版本是5.0.4,操作系统是中标麒麟(和Centos内核基础一致)。

1、Redis集群道理

Redis 集群是一个提供在多个Redis间节点间同享数据的顺序集,集群节点配合构建了一个去中间化的收集,集群中的每一个节点具有同等的身份,节点各自保留各自的数据和集群状况。节点之间采纳Gossip协定举行通讯,保证了节点状况的信息同步。

Redis 集群数据经由过程分区来举行治理,每一个节点保留集群数据的一个子集。数据的分派采纳一种叫做哈希槽(hash slot)的体式格局来分派,和传统的一致性哈希不太雷同。Redis 集群有16384个哈希槽,每一个key经由过程CRC16校验后对16384取模来决议安排哪一个槽。

为了使在部份节点失利或许大部份节点没法通讯的情况下集群依然可用,集群运用了主从复制模子。读取数据时,依据一致性哈希算法到对应的 master 节点猎取数据,假如master 挂掉以后,会启动一个对应的 salve 节点来充任 master 。

2、环境预备

这里预备在一台PC上搭建一个3主3从的redis集群。

在/opt/目次下新建一个文件夹rediscluster,用来寄存集群节点目次。

然后离别新建server10、server11、server20、server21、server30、server31 6个文件夹预备6个redis节点,这些节点离别运用6379、6380、6381、6382、6383、6384端口,以server10为例设置以下:

port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file  nodes-6379.conf

其他节点只需修正端口和文件名,顺次按此举行设置即可,设置完成后启动这些节点。

[root@localhost rediscluster]# ./server10/redis-server ./server10/redis.conf &
[root@localhost rediscluster]# ./server11/redis-server ./server11/redis.conf &
[root@localhost rediscluster]# ./server20/redis-server ./server20/redis.conf &
[root@localhost rediscluster]# ./server21/redis-server ./server21/redis.conf &
[root@localhost rediscluster]# ./server30/redis-server ./server30/redis.conf &
[root@localhost rediscluster]# ./server31/redis-server ./server31/redis.conf &

检察启动状况:

[root@localhost rediscluster]# ps -ef|grep redis
root     11842     1  0 15:03 ?        00:00:12 ./server10/redis-server 127.0.0.1:6379 [cluster]
root     11950     1  0 15:03 ?        00:00:13 ./server11/redis-server 127.0.0.1:6380 [cluster]
root     12074     1  0 15:04 ?        00:00:13 ./server20/redis-server 127.0.0.1:6381 [cluster]
root     12181     1  0 15:04 ?        00:00:12 ./server21/redis-server 127.0.0.1:6382 [cluster]
root     12297     1  0 15:04 ?        00:00:12 ./server30/redis-server 127.0.0.1:6383 [cluster]
root     12404     1  0 15:04 ?        00:00:12 ./server31/redis-server 127.0.0.1:6384 [cluster]

3、集群设置

异常简朴:redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster -replicas 1

个中-replicas 1示意每一个主节点1个从节点

[root@localhost rediscluster]# ./server10/redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --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 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   replicates efa84a74525749b8ea20585074dda81b852e9c29
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 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

建立完成,主从节点分派以下:

Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381

4、集群测试

经由过程6379客户端衔接后举行测试,发明转向了6381:

[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6379
127.0.0.1:6379> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:6381
OK
127.0.0.1:6381> get foo
"bar"

在6381上衔接测试:

[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6381
127.0.0.1:6381> get foo
"bar"

效果雷同,申明集群设置一般。

5、集群节点扩容

rediscluster目次下在新增两个目次server40和server41,新增2个redis节点设置6385和6386两个端口。将6385作为新增的master节点,6386作为从节点,然后启动节点:

[root@localhost server41]# ps -ef|grep redis
root     11842     1  0 15:03 ?        00:00:18 ./server10/redis-server 127.0.0.1:6379 [cluster]
root     11950     1  0 15:03 ?        00:00:19 ./server11/redis-server 127.0.0.1:6380 [cluster]
root     12074     1  0 15:04 ?        00:00:18 ./server20/redis-server 127.0.0.1:6381 [cluster]
root     12181     1  0 15:04 ?        00:00:18 ./server21/redis-server 127.0.0.1:6382 [cluster]
root     12297     1  0 15:04 ?        00:00:17 ./server30/redis-server 127.0.0.1:6383 [cluster]
root     12404     1  0 15:04 ?        00:00:18 ./server31/redis-server 127.0.0.1:6384 [cluster]
root     30563     1  0 18:01 ?        00:00:00 ./redis-server 127.0.0.1:6385 [cluster]
root     30582     1  0 18:02 ?        00:00:00 ./redis-server 127.0.0.1:6386 [cluster]

增加主节点:

[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379
>>> Adding node 127.0.0.1:6385 to cluster 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:6385 to make it join the cluster.
[OK] New node added correctly.

检察节点列表:

[root@localhost server41]# ./redis-cli 
127.0.0.1:6379> cluster nodes
22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064037664 0 connected
efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064036000 1 connected 0-5460
d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064038666 3 connected 10923-16383
0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064035000 4 connected
ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064037000 6 connected
63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064037000 2 connected 5461-10922
fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064037000 5 connected

增加从节点:

[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6379 --cluster-slave --cluster-master-id 22e8a8e97d6f7cc7d627e577a986384d4d181a4f
>>> Adding node 127.0.0.1:6386 to cluster 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385
   slots: (0 slots) master
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:6386 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 127.0.0.1:6385.
[OK] New node added correctly.

增加胜利后,为新节点分派数据:

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385

How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? 22e8a8e97d6f7cc7d627e577a986384d4d181a4f
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all

如许就新增终了了,能够经由过程cluster nodes敕令检察一下新增后的slot散布

127.0.0.1:6379> cluster nodes
22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064706000 7 connected 0-332 5461-5794 10923-11255
efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064707000 1 connected 333-5460
d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064705000 3 connected 11256-16383
7c24e205301b38caa1ff3cd8b270a1ceb7249a2e 127.0.0.1:6386@16386 slave 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 0 1555064705000 7 connected
0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064707000 4 connected
ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064707236 6 connected
63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064706000 2 connected 5795-10922
fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064708238 5 connected

6、集群节点缩减

缩减节点时先缩减从节点:

[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6386 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e
>>> Removing node 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e from cluster 127.0.0.1:6386
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

然后举行主节点slot转移:

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385How many slots do you want to move (from 1 to 16384)? 1000What is the receiving node ID? efa84a74525749b8ea20585074dda81b852e9c29  //要移到的节点Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f   //要删除的主节点Source node #2: done

末了在缩减主节点

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385

How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? efa84a74525749b8ea20585074dda81b852e9c29  //要移到的节点
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f   //要删除的主节点
Source node #2: done

缩减完成!【相干引荐:Redis教程】

以上就是redis集群设置与治理的细致引见(附代码)的细致内容,更多请关注ki4网别的相干文章!

标签:redis


欢迎 发表评论: