redis-cluster安装及测试

        前几天看了一前辈的关于redis的PPT,觉得这东西确实不错,想着要是对这个项目能贡献点啥也是一件有趣的事,但越往后做就越发现真想贡献点啥还真需要硬件环境的支撑,这是硬伤啊,没钱买机器做实验就只能先研究源码了。

        本文所述安装及测试全是在虚拟机上完成,基本配置如下:

操作系统 Fedora 23
redis版本 3.0.5
        第一步:安装ruby

        1.1、查看ruby是否安装

                # ruby --version

                或

                # yum list ruby

        1.2、安装ruby

                # yum install ruby

        1.3、gem安装redis

                # gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/

                # gem sources -l

                # gem install redis -V

         第二步:配置redis的conf文件

         因为redis cluster至少需要3个master节点,以及每个master节点至少需要一个slave节点,故而一个redist cluster至少需要6个节点。本文用port来代替node,即在机器上开6个端口的redis-server。具体操作如下:

        2.1、创建6个文件夹,在每个文件夹下放置如下配置文件redis.conf

                mkdir redis-cluster

                cd redis-cluster

                mkdir 7000 7001 7002 7003 7004 7005

                文件夹7000下redis.conf文件内容如下:

port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
                文件夹7000下redis.conf文件内容如下:

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

                文件夹7002下redis.conf文件内容如下:

port 7002
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
                文件夹7003下redis.conf文件内容如下:

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

                文件夹7004下redis.conf文件内容如下:

port 7004
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
                文件夹7005下redis.conf文件内容如下:

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

        2.2、开启redis-server服务

                在7000,7001,7002,7003,7004,7005文件夹下执行如下命令:

                # /usr/local/redis/bin/redis-server ./redis.conf

        2.3、创建cluster

                命令如下所示:

                # /usr/local/redis/bin/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: ea2ef0c03eef48f0a33ac266101df0f98a3eded3 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 980305c7f95037a742b943d598466fffdb49ac1b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 0f94f4440aeb76a8c05bc630db6e8b95fdcd8de5 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: f2694e12889be9cb959b43e4855d22c95e129b23 127.0.0.1:7003
   replicates ea2ef0c03eef48f0a33ac266101df0f98a3eded3
S: ba5506d0ebb023c3ee3fc8397899753a2e04b060 127.0.0.1:7004
   replicates 980305c7f95037a742b943d598466fffdb49ac1b
S: 4a0b7dc62021a9f1982ffe4ca1437bed40c8c4bb 127.0.0.1:7005
   replicates 0f94f4440aeb76a8c05bc630db6e8b95fdcd8de5
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: ea2ef0c03eef48f0a33ac266101df0f98a3eded3 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 980305c7f95037a742b943d598466fffdb49ac1b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 0f94f4440aeb76a8c05bc630db6e8b95fdcd8de5 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
M: f2694e12889be9cb959b43e4855d22c95e129b23 127.0.0.1:7003
   slots: (0 slots) master
   replicates ea2ef0c03eef48f0a33ac266101df0f98a3eded3
M: ba5506d0ebb023c3ee3fc8397899753a2e04b060 127.0.0.1:7004
   slots: (0 slots) master
   replicates 980305c7f95037a742b943d598466fffdb49ac1b
M: 4a0b7dc62021a9f1982ffe4ca1437bed40c8c4bb 127.0.0.1:7005
   slots: (0 slots) master
   replicates 0f94f4440aeb76a8c05bc630db6e8b95fdcd8de5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

        至此,redis cluster已经创建成功。

        第三步、简单测试

                具体操作步骤如下:

                # /usr/local/redis/bin/redis-cli -c -p 7001

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


        

你可能感兴趣的:(云计算,分布式系统)