首页 > 代码库 > Linux环境下Redis集群实践

Linux环境下Redis集群实践

环境:centos 7

一、编译及安装redis源码

源码地址:redis版本发布列表

cd redis-3.2.8
sudo make && make install

 编译安装成功之后,将源码中的 redis-trib.rb 文件拷贝到 /usr/local/bin/ 目录下

sudo cp ./redis-trib.rb /usr/local/bin/

二、创建节点

 创建Redis-Cluster 文件夹,并创建7000-7005共6个文件夹

技术分享

 

复制源码中的redis.conf文件并修改以下内容后分别放入7000-7005文件夹

bind 192.168.1.105
port 7000
cluster-enabled yes
cluster-config-file /home/jabben/Redis-Cluster/7000/nodes-7000.conf
pidfile /home/jabben/Redis-Cluster/7000/redis_7000.pid cluster-node-timeout 5000 daemonize yes

分别启动6个Redis节点:

redis-server 7000/redis.conf
redis-server 7001/redis.conf
redis-server 7002/redis.conf
redis-server 7003/redis.conf
redis-server 7004/redis.conf
redis-server 7005/redis.conf

查看redis进程是否已经启动:

ps -ef | grep redis

技术分享

三、创建集群

 复制redis-trib.rb文件到bin目录下

cd /home/jabben/Redis-Cluster/Source/redis-3.2.8/src
sudo cp redis-trib.rb /usr/local/bin/

安装ruby环境

yum install ruby

安装rubygems组件

yum install rubygems

安装gem-redis

gem install redis

创建集群

redis-trib.rb create --replicas 1 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 192.168.1.105:7003 192.168.1.105:7004 192.168.1.105:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.105:7000
192.168.1.105:7001
192.168.1.105:7002
Adding replica 192.168.1.105:7003 to 192.168.1.105:7000
Adding replica 192.168.1.105:7004 to 192.168.1.105:7001
Adding replica 192.168.1.105:7005 to 192.168.1.105:7002
M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000
   slots:0-5460 (5461 slots) master
M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105:7001
   slots:5461-10922 (5462 slots) master
M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002
   slots:10923-16383 (5461 slots) master
S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003
   replicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd
S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004
   replicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77
S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005
   replicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c
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 192.168.1.105:7000)
M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005
   slots: (0 slots) slave
   replicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c
S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003
   slots: (0 slots) slave
   replicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd
M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004
   slots: (0 slots) slave
   replicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77
M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105: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.

至此集群搭建完毕

Linux环境下Redis集群实践