安装环境:
OS:ubuntu-14.04.1-server
虚拟机环境
1. 卸载旧版本(如没有旧的版本,不用执行此歩)
$ sudo apt-get remove redis-server
2. 准备环境
$ sudo apt-get install gcc
安装ruby
$ sudo apt-get install ruby
安装ruby client for redis$ wget http://au-m.rubygems.org/gems/redis-3.0.0.gem
$ sudo gem install -l ./redis-3.0.0.gem
3. 获取redis-3.0.0-rc1
$ sudo cd /home/redis/
$ sudo wget http://download.redis.io/releases/3.0.0-rc1.tar.gz
$ sudo tar xzf 3.0.0-rc1.tar.gz
$ sudo cd redis-3.0.0-rc1
4. 编译
$ sudo make MALLOC=libc
注意:Redis并没有自己实现内存池,没有在标准的系统内存分配器上再加上自己的东西。
5. 安装:把可运行文件复制到/usr/local/bin
$ sudo make install
make命令执行完成后,会在src目录下生成5个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump,它们的作用如下:
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis的客户端工具
redis-benchmark:Redis的性能测试工具
redis-check-aof:更新日志检查 ,加--fix参数为修复log文件
redis-check-dump:用于本地数据库检查
6. 测试安装否:默认启动端口是6379
$ sudo /usr/local/bin/redis-server &
启动成功信息:
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.9.101 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 2511
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
2511:M 17 Dec 00:42:52.657 # Server started, Redis version 2.9.101
2511:M 17 Dec 00:42:52.658 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2511:M 17 Dec 00:42:52.658 * The server is now ready to accept connections on port 6379
7. 创建Redis cluster
规划6个节点,端口号分别为:
7000,7001,7002,7003,7004,7005
在/home/redis/redis-cluster目录下创建7000,7001,7002,7003,7004,7005目录,并分别创建redis.conf(可以直接拷贝解压后redis-3.0.0-rc1目录下的redis.conf),主要修改以下几项:
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
分别启动这6个节点:
$ sudo cd /home/redis/redis-cluster/7000
$ sudo /usr/local/bin/redis-server ./redis.conf >redis.log &
8. 让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群
$ sudo ./redis-trib.rb create --replicas 1 192.168.242.129:7000 192.168.242.129:7001 192.168.242.129:7002 192.168.242.129:7003 192.168.242.129:7004 192.168.242.129:7005
注意:最少有3个节点,否则会报如下错误:
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.
9. 检查集群状态
sudo ./redis-trib.rb check 192.168.242.129:7000
10. 添加新master节点
A) 添加一个master节点:创建一个空节点:7006,如第7歩,并启动该节点
B) 加入空节点到集群
$ sudo ./redis-trib.rb add-node 192.168.242.129:7006 192.168.242.129:7000
C) 为新节点分配slot
$ sudo ./redis-trib.rb reshard 192.168.242.129:7006
#根据提示选择要迁移的slot数量(ps:这里选择500)
How many slots do you want to move (from 1 to 16384)? 500
#选择要接受这些slot的node-id
What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf
#选择slot来源:
#all表示从所有的master重新分配,
#或者数据要提取slot的master节点id,最后用done结束
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:all
#打印被移动的slot后,输入yes开始移动slot以及对应的数据.
#Do you want to proceed with the proposed reshard plan (yes/no)? yes
#结束
以上是参考http://blog.youkuaiyun.com/myrainblues/article/details/25881535
A)添加一个master节点:创建一个空节点:7007,如第7歩,并启动该节点
B)加入空节点到集群
$ sudo ./redis-trib.rb add-node 192.168.242.129:7007 192.168.242.129:7000
C)redis-cli连接上新节点shell,输入命令:cluster replicate 对应master的node-id(端口为7006)$ sudo redis-cli -c -p 7007
进入redis 客户端,如下,
127.0.0.1:7007> cluster replicate 3fd034ff071cab7b47cd5da38c8c7fd5f5f7d63f
注意:3fd034ff071cab7b47cd5da38c8c7fd5f5f7d63f是7006这个节点的ID
好了,以上就是如何配置以及部署一个Redis 集群。
下面还有一些针对Redis 集群的命令
$ sudo ./redis-cli -p 7000 cluster nodes
#用以下命令列出集群中的所有主节点:$ sudo ./redis-cli -p 7000 cluster nodes | grep master
#向端口号为7002 的主节点发送DEBUG SEGFAULT 命令,让这个主节点崩溃$ sudo ./redis-cli -p 7002 debug segfault