搭建单机和集群Redis,测试程序访问集群情况

一、搭建单机redis

下载redis3.0.2,编译安装:

wget http://download.redis.io/releases/redis-3.0.2.tar.gz

tar -xvf redis-3.0.2.tar.gz

make

make install

redis安装位置/home/happy/saas_homework/redis/redis-3.0.2

/etc/redis/redis.conf为配置文件

 

参考资料:

Ubuntu下如何安装与运行Redis

http://jingyan.baidu.com/article/bad08e1ee14ae409c85121cf.html

 

二、测试redis中所有数据类型增删改情况

执行redis-server开启redis服务器;

要在python中访问redis,需要执行sudo pip install redis

编写python程序redis_python_test.py,访问redis,并测试redis中各个数据类型的增改删方式,见python访问redis测试文档.doc

 

三、搭建集群redis

/etc/redisredis配置文件;

1.创建集群需要的目录

mkdir -p /usr/local/cluster

cd /usr/local/cluster

mkdir 7000

mkdir 7001

mkdir 7002

mkdir 7003

mkdir 7004

mkdir 7005

2.修改配置文件redis.conf
cp /etc/redis/redis.conf /usr/local/cluster
vi redis.conf
##修改配置文件中的下面选项

pidfile /var/run/redis.7000.pid #个性化

port 7000 #个性化

daemonize yes

cluster-enabled yes

cluster-config-file nodes.7000.conf #个性化

cluster-node-timeout 5000

appendonly yes

appendfilename "appendonly.7000.aof" #个性化

##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面

cp /etc/redis/redis.conf /usr/local/cluster/7000

cp /etc/redis/redis.conf /usr/local/cluster/7001

cp /etc/redis/redis.conf /usr/local/cluster/7002

cp /etc/redis/redis.conf /usr/local/cluster/7003

cp /etc/redis/redis.conf /usr/local/cluster/7004

cp /etc/redis/redis.conf /usr/local/cluster/7005

##注意:拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中 标注个性化的 参数,分别改为对应的文件夹的名称

3.分别启动这6redis实例
vi /usr/local/cluster/redis-start.sh
加入以下内容

#!/bin/sh

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7000/redis.conf &

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7001/redis.conf &

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7002/redis.conf &

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7003/redis.conf &

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7004/redis.conf &

/usr/local/redis3.0/src/redis-server /usr/local/cluster/7005/redis.conf &

然后chmod +x /usr/local/cluster/redis-start.sh

/usr/local/cluster/redis-start.sh

##启动之后使用命令查看redis的启动情况ps -ef|grep redis

happy@happy-Lenovo-IdeaPad-Y480:~$ ps -ef|grep redis

 

happy     7453  1984  0 10:29 ?        00:00:26 redis-server *:6379

happy    22668  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7001 [cluster]                  

happy    22670  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7002 [cluster]                  

happy    22671  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7005 [cluster]                  

happy    22672  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7003 [cluster]                  

happy    22673  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7000 [cluster]                  

happy    22674  1984  0 20:02 ?        00:00:00 /home/happy/saas_homework/redis/redis-3.0.2/src/redis-server *:7004 [cluster]                  

happy    22745 20727  0 20:02 pts/7    00:00:00 grep --color=auto redis

4.执行redis的创建集群命令创建集群
安装ruby ,因为./redis-trib.rb是执行的ruby的脚本,需要ruby的环境
sudo apt-get install ruby

为了执行 redis-trib.rb 需要安装 sudo gem install redis

happy@happy-Lenovo-IdeaPad-Y480:~$ sudo gem install redis

 

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/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

>>> 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: ab6de86c4dcb6191b5df8c351250593feada679e 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

M: 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 127.0.0.1:7001

   slots:5461-10922 (5462 slots) master

M: 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70 127.0.0.1:7002

   slots:10923-16383 (5461 slots) master

S: 2ebe9b9eac4a2e65f4ea5d97e975aa796001bffb 127.0.0.1:7003

   replicates ab6de86c4dcb6191b5df8c351250593feada679e

S: f07c95b329139e416d6081497850d3d8be39f3f0 127.0.0.1:7004

   replicates 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0

S: f1dc13b835038fe92eaafe385f7af4061927b9a3 127.0.0.1:7005

   replicates 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70

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: ab6de86c4dcb6191b5df8c351250593feada679e 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

M: 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 127.0.0.1:7001

   slots:5461-10922 (5462 slots) master

M: 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70 127.0.0.1:7002

   slots:10923-16383 (5461 slots) master

M: 2ebe9b9eac4a2e65f4ea5d97e975aa796001bffb 127.0.0.1:7003

   slots: (0 slots) master

   replicates ab6de86c4dcb6191b5df8c351250593feada679e

M: f07c95b329139e416d6081497850d3d8be39f3f0 127.0.0.1:7004

   slots: (0 slots) master

   replicates 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0

M: f1dc13b835038fe92eaafe385f7af4061927b9a3 127.0.0.1:7005

   slots: (0 slots) master

   replicates 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

四、测试程序访问集群情况:

1.查看集群目前状况

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/src$ redis-cli -c -p 7000

127.0.0.1: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_sent:4232

cluster_stats_messages_received:4232

2.测试存值取值:每一次操作过后可能就跳到别的里面了,通过Redirected to slot

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> set hello world

-> Redirected to slot [866] located at 127.0.0.1:7000

OK

127.0.0.1:7000> get hello

"world"

127.0.0.1:7000> set name John

-> Redirected to slot [5798] located at 127.0.0.1:7001

OK

3.检查集群状态

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/src$ ./redis-trib.rb check 127.0.0.1:7000

Connecting to node 127.0.0.1:7000: OK

Connecting to node 127.0.0.1:7005: OK

Connecting to node 127.0.0.1:7004: 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

>>> Performing Cluster Check (using node 127.0.0.1:7000)

M: ab6de86c4dcb6191b5df8c351250593feada679e 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f1dc13b835038fe92eaafe385f7af4061927b9a3 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70

S: f07c95b329139e416d6081497850d3d8be39f3f0 127.0.0.1:7004

   slots: (0 slots) slave

   replicates 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0

M: 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 127.0.0.1:7001

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

M: 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70 127.0.0.1:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 2ebe9b9eac4a2e65f4ea5d97e975aa796001bffb 127.0.0.1:7003

   slots: (0 slots) slave

   replicates ab6de86c4dcb6191b5df8c351250593feada679e

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

4.打印集群主节点和从节点信息

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/src$ ./redis-cli -p 7000 cluster nodes | grep master

ab6de86c4dcb6191b5df8c351250593feada679e 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460

24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 127.0.0.1:7001 master - 0 1493469441474 2 connected 5461-10922

69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70 127.0.0.1:7002 master - 0 1493469443475 3 connected 10923-16383

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/src$ ./redis-cli -p 7000 cluster nodes | grep slave

f1dc13b835038fe92eaafe385f7af4061927b9a3 127.0.0.1:7005 slave 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70 0 1493469452492 6 connected

 

f07c95b329139e416d6081497850d3d8be39f3f0 127.0.0.1:7004 slave 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 0 1493469453491 5 connected

 

2ebe9b9eac4a2e65f4ea5d97e975aa796001bffb 127.0.0.1:7003 slave ab6de86c4dcb6191b5df8c351250593feada679e 0 1493469452492 4 connected

5.重新分片

happy@happy-Lenovo-IdeaPad-Y480:~/saas_homework/redis/redis-3.0.2/src$ ./redis-trib.rb reshard 127.0.0.1:7000

Connecting to node 127.0.0.1:7000: OK

Connecting to node 127.0.0.1:7005: OK

Connecting to node 127.0.0.1:7004: 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

>>> Performing Cluster Check (using node 127.0.0.1:7000)

M: ab6de86c4dcb6191b5df8c351250593feada679e 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f1dc13b835038fe92eaafe385f7af4061927b9a3 127.0.0.1:7005

   slots: (0 slots) slave

   replicates 69c9c5f9b7a73e12c324a5b74ae528dbf8e0ef70

S: f07c95b329139e416d6081497850d3d8be39f3f0 127.0.0.1:7004

   slots: (0 slots) slave

   replicates 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0

M: 24a6b93a47040ddb32f78ad1d2dffe0ff7cb04f0 127.0.0.1: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.

6.使用python访问redis集群,代码见redis_python_cluster.py

sudo pip install redis-py-cluster

from rediscluster import StrictRedisCluster
import sys
def redis_cluster():
    redis_nodes =  [{'host':'127.0.0.1','port':7000},
                    {'host':'127.0.0.1','port':7001},
                    {'host':'127.0.0.1','port':7002},
                    {'host':'127.0.0.1','port':7003},
                    {'host':'127.0.0.1','port':7004},
                    {'host':'127.0.0.1','port':7005}
                   ]
    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except Exception,e:
        print "Connect Error!"
        sys.exit(1)
    redisconn.set('name','John')
    print "name is: ", redisconn.get('name')
    print "hello ", redisconn.get('hello')
redis_cluster()

运行结果:

name is:  John

hello  world

其中hello的值为前面第2步存入的值‘world’

 

参考资料:Ubuntu 15.10Redis集群部署文档

http://www.linuxidc.com/Linux/2016-06/132340.htm

Ubuntu下搭建Redis集群

http://blog.csdn.net/u012810317/article/details/51272432

python操作Redis集群

http://blog.csdn.net/bitcarmanlee/article/details/51852126

通过对python访问redisredis集群的搭建,深入学习集群的工作原理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值