2

Redis集群搭建教程

一、概述

Redis3.0之后的版本支持集群。
1.1 Redis cluster现状

  • 节点自动发现

  • slave->master选举,集群容错

  • Hot resharding:在线分片

  • 集群管理,cluster xxx

  • 给予配置(nodes-port.conf)的集群管理

  • ASK转向/MOVED转向机制

二、redis cluster安装

服务器:

  • 192.168.200.140

  • 192.168.200.141

  • 192.168.200.117

1.下载安装包

- `wget http://download.redis.io/releases/redis-3.2.1.tar.gz`
- 或者是在官网下载tar包,然后上传
- `tar -zxvf /redis-3.2.1.tar.gz

2.因为redis是用c实现的,所有要进行编译

make && make install
相当于:
- make
- make install
  1. 创建节点

  • 192.168.200.140

    • 7001

    • 7002

    • 7003

  • 192.168.200.141

    • 7004

    • 7005

    • 7006

  • 192.168.200.117

    • 7007

    • 7008

    • 7009

这个节点就相当于端口号,其实这三台服务器上的节点都可以是7001,7002,7003或者其他,但是为了方便记忆,我就这样排序了。

  1. 创建节点(当前位置在redis-3.2.1)

mkdir redis-cluster
mkdir redis7001 redis7002 redis7003
然后将redis.conf拷贝到各个节点:
cp /redis.conf ../redis7001
cp /redis.conf ../redis7002
cp /redis.conf ../redis7003

文件结构是这样的:

clipboard.png

之后的两台服务器也是相同的模式。

然后就是对节点的配置:

daemonize    yes                          //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7002,7003
port  7000                                //端口7000,7002,7003
cluster-enabled  yes                      //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf      //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  5000                //请求超时  设置5秒够了
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
bind 192.168.200.140 绑定ip
  • 注意需要这里注意的是,bind需要绑定当前服务器的IP,否则的话在集群通信的时候会出现:[ERR] Sorry, can't connect to node 192.168.200.140:7001

4、启动节点

  • 注意:redis编译完成后主要的命令在src目录下

我们可以拷贝出来在redis主目录:

cp src/redis-server 
./redis-server ../redis7001/redis.conf
./redis-server ../redis7001/redis.conf
./redis-server ../redis7001/redis.conf

5、服务命令

./redis-server redis.conf  #启动redis节点
./redis-cli -h ip -p port shutdown #终止服务
ps -ef | grep redis   #查看是否启动成功
netstat -tnlp | grep redis #可以看到redis监听端口

我们可以写个脚本批量处理启动节点和关闭:

cd /redis-3.2.1
vim startall.sh #启动脚本
./redis-server ../redis7001/redis.conf
./redis-server ../redis7002/redis.conf
./redis-server ../redis7003/redis.conf
chmod +x startall.sh #变成可执行脚本
vim startall.sh  #关闭脚本
./redis-cli -h 192.168.200.140 -p 7001 shutdown
./redis-cli -h 192.168.200.140 -p 7002 shutdown
./redis-cli -h 192.168.200.140 -p 7003 shutdown
chmod +x shutdown.sh

 三、创建集群

之前redis节点已经准备好了,但是要将redis串联起来就得需要redis的一个工具:redis-trib.rb,这个工具也在src目录下。

  • 所以我们需要安装ruby环境:

yum -y install ruby ruby-devel rubygems rpm-build

  • 在用gem命令安装redis接口,gem是ruby的一个工具包

gem install redis 比较慢

  • 安装完成之后运行一下redis-trib.rb命令:

/usr/local/redis-3.2.1/src/redis-trib.rb
   Usage: redis-trib <command> <options> <arguments ...>
   reshard        host:port
                  --to <arg>
                  --yes
                  --slots <arg>
                  --from <arg>
  check          host:port
  call            host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  add-node        new_host:new_port existing_host:existing_port
                  --master-id <arg>
                  --slave
  del-node        host:port node_id
  fix            host:port
  import          host:port
                  --from <arg>
  help            (show this help)
  create          host1:port1 ... hostN:portN
                  --replicas <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

出现这些数据就说明ruby环境是可靠的。

  • 启动所有的节点

./redis-trib.rb  create  --replicas  1  192.168.200.140:7001 192.168.200.140:7002  192.168.200.140:7003 192.168.200.141:7004 192.168.200.141:7005 192.168.200.141:7006 192.168.200.117:7007 192.168.200.117:7008 192.168.200.117:7009 

解释:

  • --replicas 1 表示自动为每个master节点分配一个slave节点

  • 防火墙一定要开放监听的端口,否则会创建失败

在运行过程中会提示:

Can I set the above configuration? (type 'yes' to accept): yes    //输入yes
  • 提示 Waiting for the cluster to join..........

创建完毕之后会提示:

[shopinlog@redhat6-140 redis-3.2.1]$ ./startall.sh 
[shopinlog@redhat6-140 redis-3.2.1]$ ./redis-trib.rb  create  --replicas  1  192.168.200.140:7001 192.168.200.140:7002  192.168.200.140:7003 192.168.200.141:7004 192.168.200.141:7005 192.168.200.141:7006 192.168.200.117:7007 192.168.200.117:7008 192.168.200.117:7009 
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
192.168.200.140:7001
192.168.200.141:7004
192.168.200.117:7007
192.168.200.140:7002
Adding replica 192.168.200.141:7005 to 192.168.200.140:7001
Adding replica 192.168.200.117:7008 to 192.168.200.141:7004
Adding replica 192.168.200.140:7003 to 192.168.200.117:7007
Adding replica 192.168.200.141:7006 to 192.168.200.140:7002
Adding replica 192.168.200.117:7009 to 192.168.200.140:7001
M: d72e8fd938a3da231bd41b951c8077e98a112d20 192.168.200.140:7001
   slots:0-4095 (4096 slots) master
M: a2b4d9eb9cf78f6db188a4ae24894f86612a9686 192.168.200.140:7002
   slots:12288-16383 (4096 slots) master
S: c655c00c573012a66ff093fd709a68efda42a7ab 192.168.200.140:7003
   replicates f00562a53d0283b27a47d4eff5abfca26472ac85
M: c2b43c1643f7dc4a0d8decd936b2c97033719654 192.168.200.141:7004
   slots:4096-8191 (4096 slots) master
S: 34c6a1631b26a963a7980c657a4aedf9f5def54f 192.168.200.141:7005
   replicates d72e8fd938a3da231bd41b951c8077e98a112d20
S: 0910feef4580f7527198c8efc9dd2d6c5eda47e3 192.168.200.141:7006
   replicates a2b4d9eb9cf78f6db188a4ae24894f86612a9686
M: f00562a53d0283b27a47d4eff5abfca26472ac85 192.168.200.117:7007
   slots:8192-12287 (4096 slots) master
S: 79ffed0ef0685434aea6649acc4dddefc8418c08 192.168.200.117:7008
   replicates c2b43c1643f7dc4a0d8decd936b2c97033719654
S: 40e2ba3e5533d925d00d94c1ae8c4d84c24a17d6 192.168.200.117:7009
   replicates d72e8fd938a3da231bd41b951c8077e98a112d20
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.200.140:7001)
M: d72e8fd938a3da231bd41b951c8077e98a112d20 192.168.200.140:7001
   slots:0-4095 (4096 slots) master
M: a2b4d9eb9cf78f6db188a4ae24894f86612a9686 192.168.200.140:7002
   slots:12288-16383 (4096 slots) master
M: c655c00c573012a66ff093fd709a68efda42a7ab 192.168.200.140:7003
   slots: (0 slots) master
   replicates f00562a53d0283b27a47d4eff5abfca26472ac85
M: c2b43c1643f7dc4a0d8decd936b2c97033719654 192.168.200.141:7004
   slots:4096-8191 (4096 slots) master
M: 34c6a1631b26a963a7980c657a4aedf9f5def54f 192.168.200.141:7005
   slots: (0 slots) master
   replicates d72e8fd938a3da231bd41b951c8077e98a112d20
M: 0910feef4580f7527198c8efc9dd2d6c5eda47e3 192.168.200.141:7006
   slots: (0 slots) master
   replicates a2b4d9eb9cf78f6db188a4ae24894f86612a9686
M: f00562a53d0283b27a47d4eff5abfca26472ac85 192.168.200.117:7007
   slots:8192-12287 (4096 slots) master
M: 79ffed0ef0685434aea6649acc4dddefc8418c08 192.168.200.117:7008
   slots: (0 slots) master
   replicates c2b43c1643f7dc4a0d8decd936b2c97033719654
M: 40e2ba3e5533d925d00d94c1ae8c4d84c24a17d6 192.168.200.117:7009
   slots: (0 slots) master
   replicates d72e8fd938a3da231bd41b951c8077e98a112d20
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 运行./redis-trib.rb check 192.168.200.140:7001

  • 集群初步搭建成功


sailfishc
47 声望9 粉丝

喜欢孤独与自由