redis5.0.3 集群教程

redis5.0.3 集群教程参考redis中文网 http://www.redis.cn/topics/cluster-tutorial.htmlRedis集群介绍Redis 集群是一个提供在多个Redis间节点间共享数据的程序集。Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误....
摘要由CSDN通过智能技术生成

redis5.0.3 集群教程

参考redis中文网 http://www.redis.cn/topics/cluster-tutorial.html

Redis集群介绍

Redis 集群是一个提供在多个Redis间节点间共享数据的程序集。

Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.

Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令. Redis 集群的优势:

  • 自动分割数据到不同的节点上。
  • 整个集群的部分节点失败或者不可达的情况下能够继续处理命令。

注意点

Redis 集群没有使用一致性hash, 而是引入了哈希槽的概念。

Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽.集群的每个节点负责一部分hash槽。这种结构很容易添加或者删除节点,并且无论是添加删除或者修改某一个节点,都不会造成集群不可用的状态。

使用哈希槽的好处就在于可以方便的添加或移除节点。

当需要增加节点时,只需要把其他节点的某些哈希槽挪到新节点就可以了;

当需要移除节点时,只需要把移除节点上的哈希槽挪到其他节点就行了;

在这一点上,我们以后新增或移除节点的时候不用先停掉所有的 redis 服务。

搭建并使用Redis集群

启动多实例

搭建集群的第一件事情我们需要一些运行在 集群模式的Redis实例. 这意味这集群并不是由一些普通的Redis实例组成的,集群模式需要通过配置启用,开启集群模式后的Redis实例便可以使用集群特有的命令和特性了.

下面是一个最少选项的集群的配置文件:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes

文件中的 cluster-enabled 选项用于开实例的集群模式, 而 cluster-conf-file 选项则设定了保存节点配置文件的路径, 默认值为 nodes.conf.节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新。

要让集群正常运作至少需要三个主节点,不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。

首先, 让我们进入一个新目录, 并创建六个以端口号为名字的子目录, 稍后我们在将每个目录中运行一个 Redis 实例: 命令如下:

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

在文件夹 7000 至 7005 中, 各创建一个 redis.conf 文件, 文件的内容可以使用上面的示例配置文件, 但记得将配置中的端口号从 7000 改为与文件夹名字相同的号码。

从 Redis Github 页面 的 unstable 分支中取出最新的 Redis 源码, 编译出可执行文件 redis-server , 并将文件复制到 cluster-test 文件夹, 然后使用类似以下命令, 启动6个实例:

cd 7000
redis-server redis.conf

查看已启动的实例

[root@localhost 7005]# ps aux| grep redis
root      7258  0.1  0.7 153892  7680 ?        Ssl  05:06   0:00 redis-server *:7000 [cluster]
root      7263  0.2  0.7 153892  7680 ?        Ssl  05:06   0:00 redis-server *:7001 [cluster]
root      7269  0.6  0.7 153892  7680 ?        Ssl  05:06   0:00 redis-server *:7002 [cluster]
root      7274  0.1  0.7 153892  7680 ?        Ssl  05:06   0:00 redis-server *:7003 [cluster]
root      7279  0.5  0.7 153892  7676 ?        Ssl  05:06   0:00 redis-server *:7004 [cluster]
root      7284  0.4  0.7 153892  7676 ?        Ssl  05:06   0:00 redis-server *:7005 [cluster]
root      7289  0.0  0.0 112724   984 pts/1    R+   05:06   0:00 grep --color=auto redis

实例打印的日志显示, 因为 nodes.conf 文件不存在, 所以每个节点都为它自身指定了一个新的 ID :

[root@localhost 7005]# cat nodes.conf
1b770b31964854cf46258c40ab371864ece6d7b0 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

实例会一直使用同一个 ID , 从而在集群中保持一个独一无二(unique)的名字。

搭建集群

现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群, 并为每个节点编写配置文件。

通过使用 Redis 集群命令行工具 redis-trib , 编写节点配置文件的工作可以非常容易地完成: redis-trib 位于 Redis 源码的 src 文件夹中, 它是一个 Ruby 程序, 这个程序通过向实例发送特殊命令来完成创建新集群, 检查集群, 或者对集群进行重新分片(reshared)等工作。

./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

如果出现如下错误,则按照ruby

[root@localhost src]# ./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
/usr/bin/env: ruby: 没有那个文件或目录
[root@localhost src]# yum -y install ruby

安装完成后执行命令,会给出警告 ,此种方式是redis3.0.0版本使用的,当前版本5.0.3需要使用redis-cli替换

[root@localhost src]# ./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
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 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 --cluster-replicas 1

To get help about all subcommands, type:
redis-cli --cluster help

命令和结果如下

redis-5.0.3搭建集群命令

[root@localhost src]# redis-cli --cluster create 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 --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:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 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: c94773d4e45f9819a08e2919bc28fd90a3c79e01 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: 7da1582a32c205eb50fb3ae50f845be6e9e04237 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: e8f6e97682d8168cd00f020220f056b838556077 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: ce965b7e705e55b92657c17dc5c0d56dc7edc451 127.0.0.1:7003
   replicates e8f6e97682d8168cd00f020220f056b838556077
S: 98dbd39bc9a18274df62e972721220b39a51caa1 127.0.0.1:7004
   replicates c94773d4e45f9819a08e2919bc28fd90a3c79e01
S: 1b770b31964854cf46258c40ab371864ece6d7b0 127.0.0.1:7005
   replicates 7da1582a32c205eb50fb3ae50f845be6e9e04237
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: c94773d4e45f9819a08e2919bc28fd90a3c79e01 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 98dbd39bc9a18274df62e972721220b39a51caa1 127.0.0.1:7004
   slots: (0 slots) slave
   replicates c94773d4e45f9819a08e2919bc28fd90a3c79e01
S: ce965b7e705e55b92657c17dc5c0d56dc7edc451 127.0.0.1:7003
   slots: (0 slots) slave
   replicates e8f6e97682d8168cd00f020220f056b838556077
S: 1b770b31964854cf46258c40ab371864ece6d7b0 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 7da1582a32c205eb50fb3ae50f845be6e9e04237
M: 7da1582a32c205eb50fb3ae50f845be6e9e04237 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: e8f6e97682d8168cd00f020220f056b838556077 127.0.0.1:7002
   slots:[10923-16383] (5461 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@localhost src]#

以上提示表示集群搭建成功

连接集群测试

-c 参数表示连接集群

-p 参数表示指定连接端口

[root@localhost src]# redis-cli -c -p 7000
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7001> set name "hello redis cluster"
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> keys *
1) "name"
127.0.0.1:7001> get name
"hello redis cluster"
127.0.0.1:7001>

远程连接

>redis-cli -h 192.168.25.254 -p 7000 -c
192.168.25.254:7000> keys *
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
192.168.25.254:7000>
异常原因说明

redis-5.0.3 默认开启保护模式 (protected mode is enabled) ,关闭它即可

在6个配置文件中添加如下配置

protected-mode no  # 关闭保护模式
关闭集群

找遍了redis cluster官方文档,没发现有关集群重启和关闭的方法。为啥会没有呢,猜测redis cluster至少要三个节点才能运行,三台同时挂掉的可能性比较小,只要不同时挂掉,挂掉的机器修复后在加入集群,集群都能良好的运作,万一同时挂掉,数据又没有备份的话,就有大麻烦了。

redis cluster集群中的节点基本上都对等的,没有管理节点。如果要让所有节点都关闭,只能关闭进程了# pkill -9 redis

[root@localhost 7005]# kill -9 7263 7269 7274 7279 7284 7345

把所有集群都关闭,然后在重新启动,会报以下错误

[root@localhost src]# redis-cli --cluster create 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 --cluster-replicas 1
[ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值