Redis Cluster
To create a cluster, the first thing we need is to have a few empty Redis instances running in cluster mode. This basically means that clusters are not created using normal Redis instances as a special mode needs to be configured so that the Redis instance will enable the Cluster specific features and commands.
要创建集群,首先需要在集群模式下运行一些空的 Redis 实例。这基本上意味着集群不是使用普通的 Redis 实例创建的,因为需要配置一个特殊的模式,以便 Redis 实例将启用特定于集群的特性和命令。
The following is a minimal Redis cluster configuration file:
下面是一个最小的 Redis 集群配置文件:
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
请注意,正常工作的最小集群要求至少包含三个主节点。对于您的第一个测试,强烈建议启动一个包含三个主节点和三个副本的六个节点集群。
安装Redis-4
开始搭建
创建测试目录
以实例的端口号命名的下列目录,我们将在任何给定的目录中运行这些目录。
#进入redis安装目录
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 0
drwxr-xr-x. 2 root root 274 3月 1 16:26 bin
#创建伪集群目录cluster-test
[root@localhost redis]# mkdir cluster-test
[root@localhost redis]# cd cluster-test/
#分别创建7000-7005
[root@localhost cluster-test]# mkdir 7000 7001 7002 7003 7004 7005
移动文件
#分别复制reids安装目录bin下所有的文件至7000-7005
[root@localhost cluster-test]# cp ../bin/* 7000/
#分别删除7000-7005冗余文件
[root@localhost 7000]# rm -rf appendonly.aof dump.rdb redis.conf redis.log sentinel.conf sentinel.log
配置redis.conf
#分别编辑7000-7005目录下的redis.conf
[root@localhost cluster-test]# vim 7000/redis.conf
#添加内容如下
#7000-7005对应端口
port 7000
#后台启动
daemonize yes
#7000-7005对应日志文件
logfile "/usr/local/redis/cluster-test/7000/7000.log"
#开启集群
cluster-enabled yes
cluster-config-file "/usr/local/redis/cluster-test/7000/nodes.conf"
cluster-node-timeout 5000
#开启aof
appendonly yes
appendfilename "7000appendonly.aof"
#保存退出
启动
#分别启动7000-7005服务
[root@localhost cluster-test]# 7000/./redis-server 7000/redis.conf
#查看启动日志
[root@localhost cluster-test]# tail -f 7000/7000.log
[root@localhost cluster-test]# 25169:C 04 Mar 14:40:18.426 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25169:C 04 Mar 14:40:18.426 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=25169, just started
25169:C 04 Mar 14:40:18.426 # Configuration loaded
**25169:M 04 Mar 14:40:18.429 * Node configuration loaded, I'm 8b69944f7c4b3fef00c275f203da56047d1e56d8**
查看状态
#查看redis状态
[root@localhost cluster-test]# ps -ef |grep redis
root 3555 1 0 15:14 ? 00:00:00 7000/./redis-server *:7000 [cluster]
root 3658 1 0 15:14 ? 00:00:00 7001/./redis-server *:7001 [cluster]
root 3841 1 0 15:14 ? 00:00:00 7002/./redis-server *:7002 [cluster]
root 4016 1 0 15:14 ? 00:00:00 7003/./redis-server *:7003 [cluster]
root 4212 1 0 15:15 ? 00:00:00 7004/./redis-server *:7004 [cluster]
root 4439 1 0 15:15 ? 00:00:00 7005/./redis-server *:7005 [cluster]
root 5803 23131 0 15:16 pts/0 00:00:00 grep --color=auto redis
开始创建
For Redis version 3 or 4, there is the older tool called redis-trib.rb which is very similar. You can find it in the src directory of the Redis source code distribution. You need to install redis gem to be able to run redis-trib.
对于 Redis 版本3或4,有一个较老的工具叫做 Redis-trib。这是很相似的 rb。您可以在 Redis 源代码分发版的 src 目录中找到它。您需要安装 redis gem 才能运行 redis-trib。
gem install redis
移动redis-trib
[root@localhost src]# cp redis-trib.rb /usr/local/redis/cluster-test/
安装gem
[root@localhost cluster-test]# gem install redis
-bash: gem: 未找到命令
[root@localhost cluster-test]# yum install ruby -y
[root@localhost cluster-test]# yum install rubygems -y
[root@localhost cluster-test]# gem install redis
Fetching: redis-4.6.0.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.4.0.
更新gem
Building from Source
Of course, you can install Ruby from source. Download and unpack a tarball, then just do this:
$ ./configure
$ make
$ sudo make install
By default, this will install Ruby into /usr/local. To change, pass the --prefix=DIR option to the ./configure script.
创建集群
[root@localhost cluster-test]# ./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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 466608efb11842da848a69c931a3078a725ad918 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 8c967011eafddd2588c6ede2b81ec5096a08a714 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: 00fbc92cbe2c0e8e2a87816d890310e02c6d0828 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: 4705c1995e587be4380623a0a34abb6ceb4f2930 127.0.0.1:7003
replicates 8c967011eafddd2588c6ede2b81ec5096a08a714
S: 633f2d23bc8fc90bbfa67114dc19c71793d0fa36 127.0.0.1:7004
replicates 00fbc92cbe2c0e8e2a87816d890310e02c6d0828
S: cdc4c1957be2275aba98725a69cf29c6fdbe1be7 127.0.0.1:7005
replicates 466608efb11842da848a69c931a3078a725ad918
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:7000)
M: 466608efb11842da848a69c931a3078a725ad918 127.0.0.1:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: cdc4c1957be2275aba98725a69cf29c6fdbe1be7 127.0.0.1:7005
slots: (0 slots) slave
replicates 466608efb11842da848a69c931a3078a725ad918
M: 00fbc92cbe2c0e8e2a87816d890310e02c6d0828 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 633f2d23bc8fc90bbfa67114dc19c71793d0fa36 127.0.0.1:7004
slots: (0 slots) slave
replicates 00fbc92cbe2c0e8e2a87816d890310e02c6d0828
M: 8c967011eafddd2588c6ede2b81ec5096a08a714 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4705c1995e587be4380623a0a34abb6ceb4f2930 127.0.0.1:7003
slots: (0 slots) slave
replicates 8c967011eafddd2588c6ede2b81ec5096a08a714
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看状态
#查看节点状态,可知7000,7001,7002是master
127.0.0.1:7002> cluster nodes
4705c1995e587be4380623a0a34abb6ceb4f2930 127.0.0.1:7003@17003 slave 8c967011eafddd2588c6ede2b81ec5096a08a714 0 1646385905000 4 connected
cdc4c1957be2275aba98725a69cf29c6fdbe1be7 127.0.0.1:7005@17005 slave 466608efb11842da848a69c931a3078a725ad918 0 1646385905994 6 connected
633f2d23bc8fc90bbfa67114dc19c71793d0fa36 127.0.0.1:7004@17004 slave 00fbc92cbe2c0e8e2a87816d890310e02c6d0828 0 1646385905593 5 connected
00fbc92cbe2c0e8e2a87816d890310e02c6d0828 127.0.0.1:7002@17002 myself,master - 0 1646385904000 3 connected 10923-16383
466608efb11842da848a69c931a3078a725ad918 127.0.0.1:7000@17000 master - 0 1646385905000 1 connected 0-5460
8c967011eafddd2588c6ede2b81ec5096a08a714 127.0.0.1:7001@17001 master - 0 1646385905000 2 connected 5461-10922
测试读写
#任意连接一个
[root@localhost cluster-test]# ../bin/./redis-cli -c -p 7000
127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get foo
"bar"
测试容灾
#关闭一个主节点
[root@localhost 7000]# ./redis-cli -p 7002 debug segfault
Error: Server closed the connection
#查看集群状态
[root@localhost 7000]# ./redis-cli -p 7000 cluster nodes
cdc4c1957be2275aba98725a69cf29c6fdbe1be7 127.0.0.1:7005@17005 slave 466608efb11842da848a69c931a3078a725ad918 0 1646388074000 6 connected
00fbc92cbe2c0e8e2a87816d890310e02c6d0828 127.0.0.1:7002@17002 master,fail - 1646387949894 1646387948490 3 disconnected
633f2d23bc8fc90bbfa67114dc19c71793d0fa36 127.0.0.1:7004@17004 master - 0 1646388073599 7 connected 10923-16383
466608efb11842da848a69c931a3078a725ad918 127.0.0.1:7000@17000 myself,master - 0 1646388073000 1 connected 0-5460
8c967011eafddd2588c6ede2b81ec5096a08a714 127.0.0.1:7001@17001 master - 0 1646388074001 2 connected 5461-10922
4705c1995e587be4380623a0a34abb6ceb4f2930 127.0.0.1:7003@17003 slave 8c967011eafddd2588c6ede2b81ec5096a08a714 0 1646388075003 4 connected
#启动7002
[root@localhost cluster-test]# 7002/./redis-server 7002/redis.conf
#7002变slave了
[root@localhost cluster-test]# 7000/./redis-cli -p 7000 cluster nodes
cdc4c1957be2275aba98725a69cf29c6fdbe1be7 127.0.0.1:7005@17005 slave 466608efb11842da848a69c931a3078a725ad918 0 1646388230000 6 connected
00fbc92cbe2c0e8e2a87816d890310e02c6d0828 127.0.0.1:7002@17002 slave 633f2d23bc8fc90bbfa67114dc19c71793d0fa36 0 1646388230579 7 connected
633f2d23bc8fc90bbfa67114dc19c71793d0fa36 127.0.0.1:7004@17004 master - 0 1646388229000 7 connected 10923-16383
466608efb11842da848a69c931a3078a725ad918 127.0.0.1:7000@17000 myself,master - 0 1646388230000 1 connected 0-5460
8c967011eafddd2588c6ede2b81ec5096a08a714 127.0.0.1:7001@17001 master - 0 1646388230579 2 connected 5461-10922
4705c1995e587be4380623a0a34abb6ceb4f2930 127.0.0.1:7003@17003 slave 8c967011eafddd2588c6ede2b81ec5096a08a714 0 1646388230000 4 connected