Redis(7)Creating and Using Cluster Mode

1. Documents
Cluster will not support SELECT, it only contains database 0.

All the nodes use TCP bus and binary protocol to communicate. Every node connect to cluster bus. The use gossip to talk.

Some times we lost writing data
1. master node dies right before the slave node get the newly writing data.
2. master node dies, and slave node become master, and receive a writing data. when the old master recover, it does not have the newly writing data.

2. Configuration the Cluster mode
Check out the latest codes from here 
Switch to the branch unstable

>make distclean
>make

>mkdir /Users/carl/tool/redis-unstable
>cp redis-server /Users/carl/tool/redis-unstable/
>cp redis-cli /Users/carl/tool/redis-unstable/
>cp redis-benchmark /Users/carl/tool/redis-unstable/
>cd ..
>cp redis.conf /Users/carl/tool/redis-unstable/

>sudo ln -s /Users/carl/tool/redis-unstable /opt/redis-unstable
>sudo rm -fr /opt/redis
>sudo ln -s /opt/redis-unstable /opt/redis

Prepare the Configuration files

>mkdir cluster-conf
>cd cluster-conf
>mkdir 7000 7001 7002 7003 7004 7005 7006

Change the redis.conf there and properties as follow for references;

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

Then start all the nodes one by one

>../../redis-server ./redis.conf

Every time, we will see these message after we start one node.
[23303] 01 May 15:28:47.602 * No cluster configuration found, I‘m c5a855fba006f6b3302f7c162ba3b6a71d548b58

After we start 7000 to 7005, 6 nodes, execute this command from src directory to configure cluster.

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

Error Message:
cannot load such file -- redis (LoadError)

Solution:

>ruby -v

ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

>gem -v

2.0.14

1. Use a recent ruby (1.9.3 or 2.0)
2. Install the redid gem

>sudo gem install redis

Then

>ruby -rubygems ./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 Connecting to node 127.0.0.1:7000: OK Connecting to node 127.0.0.1:7001: OK Connecting to node 127.0.0.1:7002: OK Connecting to node 127.0.0.1:7003: OK Connecting to node 127.0.0.1:7004: OK Connecting to node 127.0.0.1:7005: OK >>> 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: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 M: c5a855fba006f6b3302f7c162ba3b6a71d548b58 127.0.0.1:7000   slots:0-5460 (5461 slots) master M: d0a6d910adeffa2b0ba69ddc69d9e69bdda1a41e 127.0.0.1:7001   slots:5461-10922 (5462 slots) master M: 376e13f055d4119ca45824aff3e7722a3043ecf7 127.0.0.1:7002   slots:10923-16383 (5461 slots) master S: 5295fe0fae941d7f964a674c5e5a4de37738e412 127.0.0.1:7003   replicates c5a855fba006f6b3302f7c162ba3b6a71d548b58 S: 64a3ae73e370d1e016a01adaedce1606d3b7d2ea 127.0.0.1:7004   replicates d0a6d910adeffa2b0ba69ddc69d9e69bdda1a41e S: 4324a0c7c90a460b4fc107f66e61656ec23c316b 127.0.0.1:7005   replicates 376e13f055d4119ca45824aff3e7722a3043ecf7 Can I set the above configuration? (type ‘yes‘ to accept): yes >>> Nodes configuration updated >>> 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: c5a855fba006f6b3302f7c162ba3b6a71d548b58 127.0.0.1:7000   slots:0-5460 (5461 slots) master M: d0a6d910adeffa2b0ba69ddc69d9e69bdda1a41e 127.0.0.1:7001   slots:5461-10922 (5462 slots) master M: 376e13f055d4119ca45824aff3e7722a3043ecf7 127.0.0.1:7002   slots:10923-16383 (5461 slots) master M: 5295fe0fae941d7f964a674c5e5a4de37738e412 127.0.0.1:7003   slots: (0 slots) master   replicates c5a855fba006f6b3302f7c162ba3b6a71d548b58 M: 64a3ae73e370d1e016a01adaedce1606d3b7d2ea 127.0.0.1:7004   slots: (0 slots) master   replicates d0a6d910adeffa2b0ba69ddc69d9e69bdda1a41e M: 4324a0c7c90a460b4fc107f66e61656ec23c316b 127.0.0.1:7005   slots: (0 slots) master   replicates 376e13f055d4119ca45824aff3e7722a3043ecf7 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.

—replicas 1 indicate that every master has a replica node.

It will create 3 masters and 3 slaves.

Right now, my version of branch unstable is 2.9.11.

Test and Verify the Cluster

>./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" 127.0.0.1:7002> set hello world -> Redirected to slot [866] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get hello "world"

Add New Node

redis>cluster nodes 

This command will show all the nodes.
eg:

>./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

Connecting to node 127.0.0.1:7006: OK >>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster. [OK] New node added correctly.

Or add slave node

>./redis-trib.rb add-node --slave 127.0.0.1:7006 127.0.0.1:7000

Or

>./redis-trib.rb add-node --slave --master-id adsfadsfadsfsadfsdaf 127.0.0.1:7006 127.0.0.1:7000

Remove a Node

>./redis-trib.rb del-node 127.0.0.1:7000 ‘591d10c5251abd0ef655a458f8e16a6cca182da6‘

First parameter is any node host and port, the second parameter the id of the node we plan to remove.

Error Message:
[ERR] Node 127.0.0.1:7006 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

Solution:
I delete the node, I plan to add it again. But I meet that error message.

I do not know how to fix this right now. But an easy way, delete all the files under 7006 directory except redis.conf, they it will generate a new node id.

3. Scala Client
Check the Status

redis>info

redis_mode:cluster
redis_version:2.9.11

Open project easynosqlscala

It runs great.

package com.sillycat.easynosqlscala.appimport redis.clients.jedis.{ JedisCluster, HostAndPort, Jedis }
import Java.util.HashSet

object TestRedisDBConnectionApp extends App {
  //  val jedis = new Jedis("172.0.0.1", 7000)
  //  jedis.set("name", "sillycat")
  //  println(jedis.get("name"))

  val jedisClusterNodes = new HashSet[HostAndPort]
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005))
  jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7006))
  val jc = new JedisCluster(jedisClusterNodes)
  jc.set("age", "32")
  println("name = " + jc.get("name") + " age = " + jc.get("age"))

}

Redis Monitor
https://github.com/LittlePeng/redis-monitor

References:
Redis 1~ 6
http://sillycat.iteye.com/blog/1549504   Installation redis on windows, ubuntu, redhat
http://sillycat.iteye.com/blog/1553507   Data Type, String, Int, List, Set
http://sillycat.iteye.com/blog/1553508   Data Type, Ordered Set, Hash, Publish/Subscribe, Data Expiration
http://sillycat.iteye.com/blog/1553509   Java DAO Layer
http://sillycat.iteye.com/blog/2028180   Installation on MAC, HA Solution sentinel master slave
http://sillycat.iteye.com/blog/2033094   Scala Client

http://redis.io/topics/cluster-tutorial
http://redis.io/topics/cluster-spec

http://www.redis.cn/topics/cluster-tutorial.html
http://www.redis.cn/topics/cluster-spec.html

Tips
http://in.sdo.com/?p=1378
http://www.cnblogs.com/lulu/archive/2013/06/10/3130878.html
http://www.oschina.NET/translate/utopian-redis-cluster

Cluster

http://hot66hot.iteye.com/blog/2050676

时间: 2024-10-05 23:42:06

Redis(7)Creating and Using Cluster Mode的相关文章

redis单节点安装及cluster的安装

单点安装 wget http://download.redis.io/releases/redis-4.0.2.tar.gz tar zxvf redis-4.0.1.tar.gz -C /usr/local cd /usr/local/redis-4.0.2 make make test make PREFIX=/usr/local/redis install cd /usr/local/redis mkdir conf log data cd /usr/local/redis/conf cp

redis源码分析(六)--cluster消息

Redis集群消息 作为支持集群模式的缓存系统,Redis集群中的各个节点需要定期地进行通信,以维持各个节点关于其它节点信息的实时性与一致性.如前一篇文章介绍的,Redis在专用的端口监听集群其它节点的连接,将集群内部的的通信与客户端的通信区分开来,任意两个节点之间建立了两个tcp连接,形成一条全双工的通道.这篇文章将从集群消息方面进行介绍,主要介绍消息的格式.种类与不同场景下的消息处理. 1. 消息格式 首先,Redis集群通信使用的消息可分为消息头与消息体两部分:消息头包含了发送消息的节点的

Redis Cluster 3.0搭建与使用

Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 Redis 节点之间移动数据, 并且在高负载的情况下, 这些命令将降低 Redis 集群的性能, 并导致不可预测的行为. Redis 集群通过分区(partition)来提供

redis演练(9) redis Cluster 集群管理&failover情况

<redis演练(8) redis Cluster 集群环境安装>,简单阐述了如何安装redis集群环境. 集群环境,主要包括2部分. 1.配置每个节点的配置信息(redis.conf),尤其开启cluster 2.创建集群redis-trib.rb创建集群. 过程非常简单,但非常繁琐,尤其配置各个集群节点的配置信息,如果有一定数量,工作量也不小. 没关系,redis提供了一款cluster工具,能快速构造集群环境.本章的主要内容是介绍redis提供的集群工具. 1.使用create-clus

Redis Cluster 的实现 - 初始化(1)

首先从 redis.c 源码的 main() 函数开始, 在调用的 initServer 函数中除了初始化 redis 节点本身的一些配置和环境之外,会根据是否设置 cluster_enabled 参数来对 cluster 进行初始化,如下: initServer    // 也就是 redis.conf 配置中的参数 cluster-enabled 如果设置为 yes,则进入 cluster 模式   -> if (server.cluster_enabled) clusterInit();

Redis Essentials 读书笔记 - 第九章: Redis Cluster and Redis Sentinel (Collective Intelligence)

Chapter 9. Redis Cluster and Redis Sentinel (Collective Intelligence) 上一章介绍了复制,一个master可以对应一个或多个slave(replica), 在以下的情况下是够用的: 1. master有足够内存容纳所有key 2. 通过slave可以扩展读,解决网络吞吐量的问题 3. 允许停止master的维护窗口时间 4. 通过slave做数据冗余 但复制解决不了自动failover和自动resharding的问题,在以下的情

Redis Cluster 简单安装配置

1 先安装ruby-2.3.1.tar.gz 2 解压 redis-3.2.1.zip 得到redis-3.2.1.gem 3 gem install --local redis-3.2.1.gem -V 4 解压 redis-3.2.0.tar.gz 拷贝到/app/redis/redis-3.2.0 5 安装 make / make install 6 创建集群需要的目录mkdir /app/redis/redis-nodescd /app/redis/redis-nodesmkdir 70

redis cluster搭建

redis cluster 是 redis 官方一个高可用解决方案,cluster 中 redis 共有 2^14 (16384)个slot 槽.创建 cluster 后会平均分配到每个 redis 上.cluster 可以对标 分片机制. 这里介绍为本机启动6个redis集群服务,并使用 redis-trib.rb 创建 3主3从的 cluster . 环境准备 六台服务器: CentOS7 x64(如只是做实验可在同一台机器上部署6个redis实例,使用不同端口运行) 安装包: redis-

Redis Protocol specification

Redis Protocol specification Redis clients communicate with the Redis server using a protocol called RESP (REdis Serialization Protocol). While the protocol was designed specifically for Redis, it can be used for other client-server software projects