(4) Redis cluster introduction, build configuration, operation

19. Introduction to redis cluster

  • The concept of cluster should be familiar. It is composed of multiple machines to provide one or more service support to solve problems such as storage space, query speed, load, etc.!
  • Redis cluster is a distributed architecture that supports horizontal expansion, that is to say, the LVS+keepalived we configured before needs to configure the basic environment and then add it to the cluster system. The current Redis distribution is ** only need to configure the Redis cluster into the current configuration to automatically work~ **

Redis Cluster Design Points

  • When redis cluster is designed, decentralization and middleware are taken into consideration , that is to say, each node in the cluster is equal and equal, and each node saves its own data and the whole The state of the cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any node in the cluster to obtain data from other nodes.

  • So how does redis reasonably allocate these nodes and data?

  • Redis Cluster does not use traditional consistent hashing to distribute data, but uses another method called hash slot to distribute data. Redis cluster allocates 16384 slots by default. When we set a key, we will use the CRC16 algorithm to take the modulo to get the corresponding slot, and then assign the key to the nodes in the hash slot interval. The specific algorithm is: CRC16(key) %16384.

  • It should be noted that there must be 3 or later master nodes, otherwise it will fail when creating the cluster, which we will practice in the future.

  • Therefore, we assume that there are now three nodes that have formed a cluster, namely: A, B, C three nodes, which can be three ports on one machine, or three different servers. Then, if 16384 slots are allocated by means of hash slots, the slot intervals borne by the three nodes are:

  • Node A covers 0-5460; Node B covers 5461-10922; Node C covers 10923-16383. As shown in the following figure:

So, now I want to set a key, say my_name:

  • set my_name xyz According to the hash slot algorithm of redis cluster: CRC16('my_name')%16384 = 2412. Then the storage of this key will be allocated to A.
  • Similarly, when I connect to any node (A, B, C) and want to get the key of my_name, this algorithm will also be used, and then I will jump to the B node to get the data.
  • This hash slot allocation method is good or bad. The advantage is that it is very clear. For example, if I want to add a new node D, the method of redis cluster is to take a part of the slot from the front of each node to D, and I will Experiment in the next practice. It will roughly look like this:
  • Node A covers 1365-5460, Node B covers 6827-10922, Node C covers 12288-16383, Node D covers 0-1364, 5461-6826, 10923-12287 It is similar to delete a node. You can delete this node after the move is completed.
  • So redis cluster is a shape like this:

Redis cluster is a distributed cluster that supports horizontal expansion. Redis only supports clustering functions from V3.0. Redis Cluster works like a RAID5 of disks.

  • Multiple redis nodes are interconnected and data shared.
  • All nodes are one master and one slave (there can be multiple slaves), which never provide services and only serve as backups (once the master goes down, the slaves will take over the work immediately!).
  • Processing multiple keys at the same time (such as mset/mget) is not supported, because redis needs to distribute keys evenly on each node, creating key values ​​at the same time in the case of high concurrency will reduce performance and lead to unpredictable behavior.
  • Support online addition and deletion of nodes.
  • Clients can read and write to any master node.

Misunderstanding : Many people think that the data on the Redis cluster is consistent, but they are wrong! The data on Redis is shared, that is, the A server does not necessarily have the B server. Similar to Raid 5, the written data may be A disk may be B disk. You can read normally, but you don't know where the real storage location is!

20. Redis cluster build configuration

1 Scene Settings

  • Two machines, respectively open three Redis services (ports)
  • There are three ports on machine A: 7000, 7002, 7004, all of which are main
  • Three ports on machine B: 7001, 7003, 7005, all from
  • Compile and install Redis on both machines, then compile and copy three different Redis.conf, set different port numbers, dir and other parameters respectively, and also need to add cluster-related parameters, and then start 6 Redis services respectively
  • **Note: **Selinux must be closed, and iptables will release the corresponding port

2 Install Ruby v2.2 (master)

Redis cluster requires ruby ​​support, you need to install ruby ​​first (Ruby only needs to run on one machine). Redis4.0 needs to use Ruby2.2, the installation method is as follows (because this machine comes with 2.0 version of ruby, so you need to use the following method to make the source package package into a yum installation package, and then use the yum tool to install ruby2.2—— upgrade ruby ​​version)

安装yum开发工具组:
[root@yt-01 ~]# yum -y groupinstall "Development Tools"

升级库文件:
[root@yt-01 ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel

[root@yt-01 ~]# cd /root/

创建制作rpm包的目录:
[root@yt-01 ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

下载Ruby的源码包:
[root@yt-01 ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES

下载specs文件,用于制作rpm包:
[root@yt-01 ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS

制作rpm包:
[root@yt-01 ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
#此处需要耐心等待,大约需要5分钟

安装Ruby2.2:
[root@yt-01 ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm
[root@yt-01 ~]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

Note: In addition to this method, you can also compile and install ruby. Well, ruby ​​2.2 has been installed.

3 Prepare the machine master (ip: 192.168.122.130)

[root@yt-01 ~]# vim /etc/redis_7000.conf
port 7000
bind 192.168.122.130
daemonize yes
pidfile /var/run/redis_7000.pid
dir /data/redis_data/7000
cluster-enabled yes
#开启cluster功能
cluster-config-file nodes_7000.conf
#该配置文件可以在dir目录下自动生成
cluster-node-timeout 10100
appendonly yes

[root@yt-01 ~]# vim /etc/redis_7002.conf
port 7002
bind 192.168.122.130
daemonize yes
pidfile /var/run/redis_7002.pid
dir /data/redis_data/7002
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes

[root@yt-01 ~]# vim /etc/redis_7004.conf
port 7004
bind 192.168.122.130
daemonize yes
pidfile /var/run/redis_7004.pid
dir /data/redis_data/7004
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes

创建各配置文件对应的数据库目录:
[root@yt-01 ~]# mkdir /data/redis_data
[root@yt-01 ~]# mkdir /data/redis_data/{7000,7002,7004}

依次启动Redis服务7000,7002,7004:
[root@yt-01 ~]# redis-server /etc/redis_7000.conf
33019:C 08 Apr 11:37:10.782 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
33019:C 08 Apr 11:37:10.782 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=33019, just started
33019:C 08 Apr 11:37:10.782 # Configuration loaded
[root@yt-01 ~]# redis-server /etc/redis_7002.conf
[root@yt-01 ~]# redis-server /etc/redis_7004.conf

启动完成后,结果如下:
[root@yt-01 ~]# ps aux |grep redis
root 33020 0.0 0.1 145264 2632 ? Ssl 11:37 0:00 redis-server 192.168.122.130:7000 [cluster]
root 33032 0.0 0.1 145264 2628 ? Ssl 11:37 0:00 redis-server 192.168.122.130:7002 [cluster]
root 33054 0.0 0.1 145264 2644 ? Ssl 11:37 0:00 redis-server 192.168.122.130:7004 [cluster]

4 Prepare slave (IP: 192168.122.131)

安装redis
先在yt-01上传redis到yt-02上
[root@yt-01 ~]# cd /usr/local/src/
[root@yt-01 src]# ll
[root@yt-01 src]# scp -r redis-4.0.9 192.168.122.131:/usr/local/src/
[email protected]'s password:
在yt-02上
[root@yt-02 ~]# cd /usr/local/src
[root@yt-02 src]# ll
[root@yt-02 src]# cd redis-4.0.9/
[root@yt-02 redis-4.0.9]# make install 
cd src && make install
make[1]: 进入目录“/usr/local/src/redis-4.0.9/src”
Hint: It's a good idea to run 'make test' ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/usr/local/src/redis-4.0.9/src”
# 因为之前在yt-01上编译过,这里只需要install就可以了

[root@yt-02 ~]# vim /etc/redis_7001.conf
port 7001
bind 192.168.122.131
daemonize yes
pidfile /var/run/redis_7001.pid
dir /data/redis_data/7001
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes

[root@yt-02 ~]# vim /etc/redis_7003.conf
port 7003
bind 192.168.122.131
daemonize yes
pidfile /var/run/redis_7003.pid
dir /data/redis_data/7003
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes

[root@yt-02 ~]# vim /etc/redis_7005.conf
port 7005
bind 192.168.122.131
daemonize yes
pidfile /var/run/redis_7005.pid
dir /data/redis_data/7005
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes

创建各配置文件对应的数据库目录:
[root@yt-02 ~]# mkdir /data/redis_data
[root@yt-02 ~]# mkdir /data/redis_data/{7001,7003,7005}

依次启动Redis服务7001,7003,7005:
[root@yt-02 ~]# redis-server /etc/redis_7001.conf
[root@yt-02 ~]# redis-server /etc/redis_7003.conf
[root@yt-02 ~]# redis-server /etc/redis_7005.conf

启动完成后结果如下:
[root@yt-02 ~]# ps aux |grep redis
root 25413 0.0 0.4 145264 7564 ? Ssl 11:50 0:00 redis-server 192.168.122.131:7001 [cluster]
root 25418 0.0 0.4 145264 7564 ? Ssl 11:50 0:00 redis-server 192.168.122.131:7003 [cluster]
root 25427 0.0 0.4 145264 7564 ? Ssl 11:50 0:00 redis-server 192.168.122.131:7005 [cluster]

5 Configure redis cluster

安装Redis配置集群的工具:
[root@yt-01 ~]# gem install redis

将命令redis-trib.rb加入环境变量目录下:
[root@yt-01 ~]# cp /usr/local/src/redis-4.0.2/src/redis-trib.rb /usr/bin/
[root@yt-01 ~]# redis-trib.rb create --replicas 1 192.168.122.130:7000 192.168.122.130:7002 192.168.122.130:7004 192.168.122.131:7001 192.168.122.131:7003 192.168.122.131:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.122.130:7000
192.168.122.131:7001
192.168.122.130:7002
Adding replica 192.168.122.131:7005 to 192.168.122.130:7000
Adding replica 192.168.122.130:7004 to 192.168.122.131:7001
Adding replica 192.168.122.131:7003 to 192.168.122.130:7002
M: 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e 192.168.122.130:7000
   slots:0-5460 (5461 slots) master
M: 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b 192.168.122.130:7002
   slots:10923-16383 (5461 slots) master
S: 702d2db77c3a9a5e5b5a2fb3348e5a894479ad93 192.168.122.130:7004
   replicates c097cca2ac912c6d867336683d31cf4dbdbb8818
M: c097cca2ac912c6d867336683d31cf4dbdbb8818 192.168.122.131:7001
   slots:5461-10922 (5462 slots) master
S: 3ef5d3b9a607259ff7b57f722c9eda9b38a1b083 192.168.122.131:7003
   replicates 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b
S: 0e3946acca8ee2f093ea5951591b592a9bee4d45 192.168.122.131:7005
   replicates 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e
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 192.168.122.130:7000)
M: 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e 192.168.122.130:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b 192.168.122.130:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 702d2db77c3a9a5e5b5a2fb3348e5a894479ad93 192.168.122.130:7004
   slots: (0 slots) slave
   replicates c097cca2ac912c6d867336683d31cf4dbdbb8818
S: 3ef5d3b9a607259ff7b57f722c9eda9b38a1b083 192.168.122.131:7003
   slots: (0 slots) slave
   replicates 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b
S: 0e3946acca8ee2f093ea5951591b592a9bee4d45 192.168.122.131:7005
   slots: (0 slots) slave
   replicates 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e
M: c097cca2ac912c6d867336683d31cf4dbdbb8818 192.168.122.131:7001
   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.

# 注意:redis-trib.rb create --replicas 1 表示一个master对应几个slave,此处的参数“1”表示master和slave一一对应
# 主从是redis-trib.rb自动分配的

Redis cluster configuration complete!

21. redis cluster operation

1 Connect to redis cluster

Because Redis Cluster is a distributed structure, any port can be connected.

连接:
[root@yt-02 ~]# redis-cli -c -h 192.168.122.130 -p 7000
#-c:=cluster,表示以集群方式连接

创建数据:
192.168.122.130:7000> set key1 123
-> Redirected to slot [9189] located at 192.168.122.131:7001
OK
#该操作会被重定向到192.168.122.131:7001

192.168.122.131:7001> set key2 abc
-> Redirected to slot [4998] located at 192.168.122.130:7000
OK
192.168.122.130:7000> set key3 cbd
OK
# 该操作会被重定向到本地

192.168.122.130:7000> set key5 test5
-> Redirected to slot [9057] located at 192.168.122.131:7001
OK

查看数据:
192.168.122.130:7000> get key1
-> Redirected to slot [9189] located at 192.168.122.131:7001
"123"
192.168.122.131:7001> get key2
-> Redirected to slot [4998] located at 192.168.122.130:7000
"abc"
192.168.122.130:7000> get key3
"cbd"
192.168.122.130:7000> get key4
-> Redirected to slot [13120] located at 192.168.122.130:7002
(nil)
192.168.122.130:7002> get key5
-> Redirected to slot [9057] located at 192.168.122.131:7001
"test5"

2 Operation of the cluster

查看集群的状态:
[root@yt-01 ~]# redis-trib.rb check 192.168.122.130:7000
>>> Performing Cluster Check (using node 192.168.122.130:7000)
M: 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e 192.168.122.130:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b 192.168.122.130:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 702d2db77c3a9a5e5b5a2fb3348e5a894479ad93 192.168.122.130:7004
   slots: (0 slots) slave
   replicates c097cca2ac912c6d867336683d31cf4dbdbb8818
S: 3ef5d3b9a607259ff7b57f722c9eda9b38a1b083 192.168.122.131:7003
   slots: (0 slots) slave
   replicates 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b
S: 0e3946acca8ee2f093ea5951591b592a9bee4d45 192.168.122.131:7005
   slots: (0 slots) slave
   replicates 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e
M: c097cca2ac912c6d867336683d31cf4dbdbb8818 192.168.122.131:7001
   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@yt-01 ~]# redis-cli -c -h 192.168.122.130 -p 7000
192.168.122.130:7000> cluster nodes
89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b 192.168.122.130:7002@17002 master - 0 1523160379000 2 connected 10923-16383
702d2db77c3a9a5e5b5a2fb3348e5a894479ad93 192.168.122.130:7004@17004 slave c097cca2ac912c6d867336683d31cf4dbdbb8818 0 1523160380000 4 connected
8c76e2bcb05c8d911b57989b973376a1a4ae8d0e 192.168.122.130:7000@17000 myself,master - 0 1523160380000 1 connected 0-5460
3ef5d3b9a607259ff7b57f722c9eda9b38a1b083 192.168.122.131:7003@17003 slave 89b6e94a7c8c5b3660aa94b8492ebf2bb8684b5b 0 1523160378767 5 connected
0e3946acca8ee2f093ea5951591b592a9bee4d45 192.168.122.131:7005@17005 slave 8c76e2bcb05c8d911b57989b973376a1a4ae8d0e 0 1523160378000 6 connected
c097cca2ac912c6d867336683d31cf4dbdbb8818 192.168.122.131:7001@17001 master - 0 1523160377000 4 connected 5461-10922

查看集群信息:
192.168.122.130:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:548
cluster_stats_messages_pong_sent:581
cluster_stats_messages_sent:1129
cluster_stats_messages_ping_received:576
cluster_stats_messages_pong_received:548
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1129

添加节点(执行该操作前先在slave创建redis_7007.conf并启动):
192.168.122.130:7000> cluster meet 192.168.122.131 7007
OK
192.168.122.130:7000> cluster nodes
84fa982c0a3314bb239eeba869238502752d2682 192.168.122.131:7007@17007 master - 0 1523160579000 0 connected
##此时7007以master身份存在

再添加一个节点:
192.168.122.130:7000> cluster meet 192.168.122.130 7006
OK
192.168.122.130:7000> cluster nodes
1daa74e086e82eee1dd091189f1a06547a149e26 192.168.122.130:7006@17006 master - 0 1523160976000 7 connected
#也是以master身份存在
#使用以上方式添加的新节点都是以master身份存在!

Set the current node as the slave of the specified node

先更换到要设置的节点:
[root@yt-01 ~]# redis-cli -c -h 192.168.122.130 -p 7006

设定为7007的从:
192.168.122.130:7006> cluster replicate 84fa982c0a3314bb239eeba869238502752d2682
OK

查看:
192.168.122.130:7006> cluster nodes
1daa74e086e82eee1dd091189f1a06547a149e26 192.168.122.130:7006@17006 myself,slave 84fa982c0a3314bb239eeba869238502752d2682 0 1523161084000 7 connected
84fa982c0a3314bb239eeba869238502752d2682 192.168.122.131:7007@17007 master - 0 1523161084000 0 connected
#对比node号,即7006为7007的从。。

移除某节点:
192.168.122.130:7006> cluster forget 84fa982c0a3314bb239eeba869238502752d2682
(error) ERR Can't forget my master!
192.168.122.130:7006> 1daa74e086e82eee1dd091189f1a06547a149e26
(error) ERR I tried hard but I can't forget myself...
# 从节点不能移除master节点和也不能移除自己

切换到一个主节点
[root@yt-01 ~]# redis-cli -c -h 192.168.122.130 -p 7000
192.168.122.130:7000> cluster forget 677f27fb209ce45c823126fe38dbcf0b9fc43d93
OK

查看:
192.168.122.130:7000> cluster nodes
#此时7006已经不存在了。

保存当前配置:
192.168.122.130:7000> CLUSTER SAVECONFIG
OK

Guess you like

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