搭建环境是vmware虚拟机+ubuntu-14.04,以redis伪集群的方式搭建搭建,一共实现了6台机器集群的搭建,三个master节点和三个slave节点。
<pre name="code" class="cpp">#首先安装redis的集群管理需要ruby和zlib
sudo apt-get install zlib ruby
#以及安装和redis有关的ruby package
sudo gem install redis
#下载安装包,编译安装redis-3.0.0
mkdir redis
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar -zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make & make test &make install
编译安装以后,运行redis-server如果打印出以下的结果就表示安装成功了。
<pre name="code" class="cpp">61702:C 27 Oct 11:11:12.324 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
61702:M 27 Oct 11:11:12.325 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
61702:M 27 Oct 11:11:12.325 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
61702:M 27 Oct 11:11:12.326 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 61702
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
61702:M 27 Oct 11:11:12.332 # Server started, Redis version 3.0.0
61702:M 27 Oct 11:11:12.332 # 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.
61702:M 27 Oct 11:11:12.332 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
61702:M 27 Oct 11:11:12.333 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
61702:M 27 Oct 11:11:12.333 * The server is now ready to accept connections on port 6379
然后我们关闭掉已经打开的redis-server程序,开始伪集群的搭建。
首先在redis-3.0.0目录下创建cluster目录
<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ mkdir cluster
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ cd cluster
#创建node-7000.conf node-7001.conf node-7002.conf node-7003.conf node-7004.conf node-7005.conf
#这些分别是7000,7001,7002,7003,7004,7005六个节点的redis-server配置文件
#配置文件内容如下
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7000.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7000.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7000.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7000
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file node-7000.conf
cluster-node-timeout 5000
appendonly yes
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7001.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7001.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7001.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7001
port 7001
daemonize yes
cluster-enabled yes
cluster-config-file node-7001.conf
cluster-node-timeout 5000
appendonly yes
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7002.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7002.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7002.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7002
port 7002
daemonize yes
cluster-enabled yes
cluster-config-file node-7002.conf
cluster-node-timeout 5000
appendonly yes
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7003.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7003.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7003.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7003
port 7003
daemonize yes
cluster-enabled yes
cluster-config-file node-7003.conf
cluster-node-timeout 5000
appendonly yes
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7004.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7004.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7004.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7004
port 7004
daemonize yes
cluster-enabled yes
cluster-config-file node-7004.conf
cluster-node-timeout 5000
appendonly yes
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7005.conf
pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7005.pid
logfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7005.log"
dir /home/ubuntu/redis-3.0.0/cluster/data/node-7005
port 7005
daemonize yes
cluster-enabled yes
cluster-config-file node-7005.conf
cluster-node-timeout 5000
appendonly yes
最后在cluster目录下创建log data文件夹,以及node-7000.log node-7001.log node-7002.log node-7003.log #node-7004.log node-7005.log等6哥log文件,最后cluster目录下的文件如下:
<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ ls
data node-7000.conf node-7001.conf node-7002.conf node-7003.conf node-7004.conf node-7005.conf
logs node-7000.log node-7001.log node-7002.log node-7003.log node-7004.log node-7005.log
最后,配置文件都ok了,开始启动6个”单机”redis-server服务,启动命令如下:
<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7000.conf
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7001.conf
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7002.conf
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7003.conf
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7004.conf
ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7005.conf
把每一台机器加入集群中
<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ ./src/redis-trib.rb create --replicas 1 192.168.39.153:7000 192.168.39.153:7001 192.168.39.153:7002 192.168.39.153:7003 192.168.39.153:7004 192.168.39.153:7005
打印出以下的结果,就代表集群启动成功了。
<pre name="code" class="cpp">3:7003 192.168.39.153:7004 192.168.39.153:7005
>>> Creating cluster
Connecting to node 192.168.39.153:7000: OK
Connecting to node 192.168.39.153:7001: OK
Connecting to node 192.168.39.153:7002: OK
Connecting to node 192.168.39.153:7003: OK
Connecting to node 192.168.39.153:7004: OK
Connecting to node 192.168.39.153:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.39.153:7000
192.168.39.153:7001
192.168.39.153:7002
Adding replica 192.168.39.153:7003 to 192.168.39.153:7000
Adding replica 192.168.39.153:7004 to 192.168.39.153:7001
Adding replica 192.168.39.153:7005 to 192.168.39.153:7002
M: 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd 192.168.39.153:7000
slots:0-5460 (5461 slots) master
M: adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd 192.168.39.153:7001
slots:5461-10922 (5462 slots) master
M: 314cbd10bbc4c1339fb737575af41f04408c2087 192.168.39.153:7002
slots:10923-16383 (5461 slots) master
S: fb2d1830a4b429fcb80da7edbbdacc50ed439d63 192.168.39.153:7003
replicates 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd
S: 1384a16ccb208828d9634f49695556ae11ec57a7 192.168.39.153:7004
replicates adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd
S: 9d9350c96c50fe88621575047a6a07250a287b51 192.168.39.153:7005
replicates 314cbd10bbc4c1339fb737575af41f04408c2087
Can I set the above configuration? (type 'yes' to accept): yes#这里输入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.39.153:7000)
M: 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd 192.168.39.153:7000
slots:0-5460 (5461 slots) master
M: adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd 192.168.39.153:7001
slots:5461-10922 (5462 slots) master
M: 314cbd10bbc4c1339fb737575af41f04408c2087 192.168.39.153:7002
slots:10923-16383 (5461 slots) master
M: fb2d1830a4b429fcb80da7edbbdacc50ed439d63 192.168.39.153:7003
slots: (0 slots) master
replicates 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd
M: 1384a16ccb208828d9634f49695556ae11ec57a7 192.168.39.153:7004
slots: (0 slots) master
replicates adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd
M: 9d9350c96c50fe88621575047a6a07250a287b51 192.168.39.153:7005
slots: (0 slots) master
replicates 314cbd10bbc4c1339fb737575af41f04408c2087
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
至此,一个集群就搭建起来了,从上边的信息我们也可以看出来,192.168.39.153:7000,192.168.39.153:7001,192.168.39.153:7002是master节点,另外三个是slave节点。