您的位置:首页 > 数据库 > Redis

1、Redis 集群环境的搭建

2015-08-13 22:08 555 查看

前言:

csdn上第一篇博客,打算说说redis的集群 !!!
redis在3.0版本以后增加了集群特性,从以前的主从复制有了质的提升。

集群环境的搭建

首先声明一下个人的环境:mac + centos(虚拟机),环境搭建在centos上,mac作为客户端调用。redis的集群需要依赖ruby环境,首先安装 zlib 、ruby

1.zlib 的安装

[root@localhost ~]# yum install zlib


2.ruby的安装

[root@localhost ~]# yum install ruby


3.redis的安装

[root@localhost software]# mkdir redis
[root@localhost software]# wget http://download.redis.io/redis-stable.tar.gz [root@localhost software]# tar xvzf redis-stable.tar.gz
[root@localhost software]# make && make test


安装完成以后执行如下命令:

redis:[root@localhost software]# whereis redis-cli
redis-cli: /usr/local/bin/redis-cli


查看redis的相关命令所在位置

执行:

[root@localhost software]# <strong>redis-server </strong>
13237:C 13 Aug 20:48:24.171 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
13237:M 13 Aug 20:48:24.171 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-``    `.  `_.  ''-._           Redis 3.0.3 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 13237
`-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io `-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._        _.-'
`-.__.-'

13237:M 13 Aug 20:48:24.172 # Server started, Redis version 3.0.3
13237:M 13 Aug 20:48:24.173 # 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.
13237:M 13 Aug 20:48:24.173 # 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.
13237:M 13 Aug 20:48:24.174 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13237:M 13 Aug 20:48:24.174 * The server is now ready to accept connections on port 6379


表明redis安装成功!

接下来开始redis cluster环境的搭建 ~~~

· 在redis目录下/root/software/redis-3.0.3 创建my-redis-cluster目录,在目录下创建如下文件:

[root@localhost my-redis-cluster]# pwd
/root/software/redis-3.0.3/my-redis-cluster
[root@localhost my-redis-cluster]# ls
node-7000.conf  node-7001.conf  node-7002.conf  node-7003.conf  node-7004.conf  node-7005.conf


node-7000.conf 文件内容如下:

[root@localhost my-redis-cluster]# cat node-7000.conf
pidfile /root/software/redis-3.0.3/pid/node7000.pid
logfile "/root/software/redis-3.0.3/logs/node-7000.log"
dir /root/software/redis-3.0.3/data/node-7000
port <span style="color:#ff0000;">7000</span>
daemonize yes
cluster-enabled <span style="color:#ff0000;"><strong>yes</strong></span>
cluster-config-file node-7000.conf
cluster-node-timeout 5000
appendonly yes


大概解释下: 开启集群、监听端口...这个文件的内容比较简单,其他node-700x.conf 文件类似,就不一一罗列

执行如下操作,创建data目录以及子目录
[root@localhost my-redis-cluster]# cd ..
[root@localhost redis-3.0.3]# ls
00-RELEASENOTES  COPYING  dump.rdb  Makefile          pid         runtest           sentinel.conf  utils
BUGS             data     INSTALL   MANIFESTO         README      runtest-cluster   src
CONTRIBUTING     deps     logs      my-redis-cluster  redis.conf  runtest-sentinel  tests
[root@localhost redis-3.0.3]# cd data
[root@localhost data]# ls
<strong><span style="color:#ff0000;">node-7000  node-7001  node-7002  node-7003  node-7004  node-7005</span></strong>
下一步开始启动这6个node

[root@localhost my-redis-cluster]# redis-server node-7000.conf
[root@localhost my-redis-cluster]# redis-server node-7001.conf
[root@localhost my-redis-cluster]# redis-server node-7002.conf
[root@localhost my-redis-cluster]# redis-server node-7003.conf
[root@localhost my-redis-cluster]# redis-server node-7004.conf
[root@localhost my-redis-cluster]# redis-server node-7005.conf


每个Node为守护进程形式启动,在控制台无打印信息,可在logs目录下查看启动日志信息,注意,这里的logs目录为手动创建的。

查看Node Process

[root@localhost my-redis-cluster]# ps -ef|grep redis
root     12937     1  0 20:22 ?        00:00:00 redis-server *:7000 [cluster]
root     12941     1  1 20:22 ?        00:00:00 redis-server *:7001 [cluster]
root     12945     1  1 20:22 ?        00:00:00 redis-server *:7002 [cluster]
root     12949     1  0 20:22 ?        00:00:00 redis-server *:7003 [cluster]
root     12953     1  0 20:22 ?        00:00:00 redis-server *:7004 [cluster]
root     12957     1  2 20:23 ?        00:00:00 redis-server *:7005 [cluster]


从上发现Node都成功启动,但此时每个Node都是相互独立的,需要讲各个Node join cluster

Nodes Join Cluster 节点加入进去。 进入redis下的src目录,执行如下命令

./redis-trib.rb create --replicas 1 192.168.1.103:7000 192.168.1.103:7001 192.168.1.103:7002 192.168.1.103:7003 192.168.1.103:7004 192.168.1.103:7005

Note:这里不要使用localhost或者127.0.0.1,192.168.1.103是centos所在的ip地址,如果使用localhost或127.0.0.1,在mac上(也就是局域网内)是没有办法获取redis cluster的链接信息的,因为在cluster
中,使用的是加入集群的ip去通信的。
replicas 1 表示为每个master添加一个slave

执行完以上命令后,得到如下信息:

[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.103:7000 192.168.1.103:7001 192.168.1.103:7002 192.168.1.103:7003 192.168.1.103:7004 192.168.1.103:7005
>>> Creating cluster
Connecting to node 192.168.1.103:7000: OK
Connecting to node 192.168.1.103:7001: OK
Connecting to node 192.168.1.103:7002: OK
Connecting to node 192.168.1.103:7003: OK
Connecting to node 192.168.1.103:7004: OK
Connecting to node 192.168.1.103:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.103:7000
192.168.1.103:7001
192.168.1.103:7002
Adding replica 192.168.1.103:7003 to 192.168.1.103:7000
Adding replica 192.168.1.103:7004 to 192.168.1.103:7001
Adding replica 192.168.1.103:7005 to 192.168.1.103:7002
<strong><span style="color:#ff0000;">M</span></strong>: <span style="background-color: rgb(51, 255, 255);">4bc092eb4731152d15172b065c74c7a795fe6304</span> 192.168.1.103:7000
slots:<span style="color:#ff0000;"><strong>0-5460</strong></span> (5461 slots) <strong><span style="color:#ff0000;">master</span></strong>
M: f37ec54101536425ce8798e041ad75a582d7e153 192.168.1.103:7001
slots:5461-10922 (5462 slots) master
M: 7b0ca3978858454051ad572aa816eec450f31a53 192.168.1.103:7002
slots:10923-16383 (5461 slots) master
<strong><span style="color:#ff0000;">S</span></strong>: 778e649f47fa98f6d1f6b1f1043812c6685dc4a8 192.168.1.103:7003
<strong><span style="color:#ff0000;">replicates</span></strong> <span style="background-color: rgb(102, 255, 255);">4bc092eb4731152d15172b065c74c7a795fe6304</span>
S: 907feee1b665554cadc64921c7fcb8c05b8a5ab6 192.168.1.103:7004
replicates f37ec54101536425ce8798e041ad75a582d7e153
S: b2bea8ede402e2112cced7d7cea52127f18edef2 192.168.1.103:7005
replicates 7b0ca3978858454051ad572aa816eec450f31a53
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.103:7000)
M: 4bc092eb4731152d15172b065c74c7a795fe6304 192.168.1.103:7000
slots:0-5460 (5461 slots) master
M: f37ec54101536425ce8798e041ad75a582d7e153 192.168.1.103:7001
slots:5461-10922 (5462 slots) master
M: 7b0ca3978858454051ad572aa816eec450f31a53 192.168.1.103:7002
slots:10923-16383 (5461 slots) master
M: 778e649f47fa98f6d1f6b1f1043812c6685dc4a8 192.168.1.103:7003
slots: (0 slots) master
replicates 4bc092eb4731152d15172b065c74c7a795fe6304
M: 907feee1b665554cadc64921c7fcb8c05b8a5ab6 192.168.1.103:7004
slots: (0 slots) master
replicates f37ec54101536425ce8798e041ad75a582d7e153
M: b2bea8ede402e2112cced7d7cea52127f18edef2 192.168.1.103:7005
slots: (0 slots) master
replicates 7b0ca3978858454051ad572aa816eec450f31a53
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


以上的输出包含了很多的信息,在后面的文章当中会讲到,这里你只要能看出redis cluster已成功构建即可。

至此,redis的集群环境搭建完成 ~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: