redis3.0集群

操作系统环境

[[email protected] ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[[email protected] ~]# getconf LONG_BIT
64

redis安装请参考redis安装一遍博文,redis集群是在3.0中推出的集群功能...........

由于资源有限,只好在一台机器上搭建redis集群环境,redis版本3.0.6,要让集群正常运作至少需要三个主节点,不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。

redis dir port
6381 /opt/6381 6381
6382 /opt/6382 6382
6383 /opt/6383 6383
6384 /opt/6384 6384
6385 /opt/6385 6385
6386 /opt/6386 6386

创建redis相应的目录

[[email protected] ~]# mkdir /opt/638{1..6}
[[email protected] ~]# tree /opt/
/opt/
├── 6381
├── 6382
├── 6383
├── 6384
├── 6385
└── 6386
6 directories, 0 files

拷贝配置文件以及redis启动redis-server文件

[[email protected] ~]# cp /usr/local/src/redis-3.0.6/redis.conf /opt/6381/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6381/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6382/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6383/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6384/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6385/
[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6386/
[[email protected] ~]# cd /opt/6381/

6381配置文件

[[email protected] 6381]# vim redis.conf
daemonize yes
pidfile /var/run/redis-6381.pid
port 6381
tcp-keepalive 60
logfile "/var/log/redis/6381.log"
dbfilename dump.rdb
dir /opt/6381/
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly-6381.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000

拷贝redis配置文件到其他redis对应的目录下,并修改redis的配置文件

[[email protected] ~]# cp /opt/6381/redis.conf /opt/6382/
[[email protected] ~]# cp /opt/6381/redis.conf /opt/6383/
[[email protected] ~]# cp /opt/6381/redis.conf /opt/6384/
[[email protected] ~]# cp /opt/6381/redis.conf /opt/6385/
[[email protected] ~]# cp /opt/6381/redis.conf /opt/6386/
[[email protected] ~]# sed -i "s#6381#6382#g" /opt/6382/redis.conf 
[[email protected] ~]# sed -i "s#6381#6383#g" /opt/6383/redis.conf 
[[email protected] ~]# sed -i "s#6381#6384#g" /opt/6384/redis.conf 
[[email protected] ~]# sed -i "s#6381#6385#g" /opt/6385/redis.conf 
[[email protected] ~]# sed -i "s#6381#6386#g" /opt/6386/redis.conf

分别启动6381..6386

[[email protected] ~]# /opt/6381/redis-server /opt/6381/redis.conf 
[[email protected] ~]# /opt/6382/redis-server /opt/6382/redis.conf
[[email protected] ~]# /opt/6383/redis-server /opt/6383/redis.conf 
[[email protected] ~]# /opt/6384/redis-server /opt/6384/redis.conf 
[[email protected] ~]# /opt/6385/redis-server /opt/6385/redis.conf 
[[email protected] ~]# /opt/6386/redis-server /opt/6386/redis.conf

查看redis进程

[[email protected] ~]# ps -ef|grep redis|grep -v grep 
root        809      1  0 09:17 ?        00:00:00 /opt/6381/redis-server *:6381 [cluster]    
root        815      1  0 09:18 ?        00:00:00 /opt/6382/redis-server *:6382 [cluster]    
root        821      1  0 09:18 ?        00:00:00 /opt/6383/redis-server *:6383 [cluster]    
root        825      1  0 09:18 ?        00:00:00 /opt/6384/redis-server *:6384 [cluster]    
root        829      1  0 09:18 ?        00:00:00 /opt/6385/redis-server *:6385 [cluster]    
root        833      1  0 09:18 ?        00:00:00 /opt/6386/redis-server *:6386 [cluster]
注:以上只是把各个redis节点启动成功

创建redis集群

[[email protected] ~]# redis-trib.rb create ----replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.1:6381
172.17.0.1:6382
172.17.0.1:6383
Adding replica 172.17.0.1:6384 to 172.17.0.1:6381
Adding replica 172.17.0.1:6385 to 172.17.0.1:6382
Adding replica 172.17.0.1:6386 to 172.17.0.1:6383
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
S: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
S: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
S: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
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 172.17.0.1:6381)
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
M: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   slots: (0 slots) master
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
M: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   slots: (0 slots) master
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
M: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   slots: (0 slots) master
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

登陆任意redis节点

[[email protected] ~]# redis-cli -c -p 6381
127.0.0.1:6381> CLUSTER NODES //各个节点以及主从对应的信息
54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385 slave be37cf292b638d919e9e39fc3ebe3984697f6026 0 1451889401026 5 connected
5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383 master - 0 1451889401529 3 connected 10923-16383
ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384 slave d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 0 1451889400017 4 connected
d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381 myself,master - 0 0 1 connected 0-5460
b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386 slave 5e5f54956d424ac491820f10ef0e8be443e1d98f 0 1451889402033 6 connected
be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382 master - 0 1451889400522 2 connected 5461-10922

登陆redis 6381

[[email protected] ~]# redis-cli -c -p 6381
127.0.0.1:6381> set name zxl
-> Redirected to slot [5798] located at 172.17.0.1:6382
OK
172.17.0.1:6382> set age 33
-> Redirected to slot [741] located at 172.17.0.1:6381
OK
172.17.0.1:6381> KEYS *
1) "age"
172.17.0.1:6381> GET age
"33"
172.17.0.1:6381> 
[[email protected] ~]# redis-cli -c -p 6382
127.0.0.1:6382> KEYS *
1) "name"
127.0.0.1:6382> get name
"zxl"
127.0.0.1:6382>

注:以上就是redis集群创建

创建redis集群时,错误总结如下:

[[email protected] ~]# cp /usr/local/src/redis-3.0.6/src/redis-trib.rb /usr/local/bin/
[[email protected] ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/bin/env: ruby: No such file or directory

注:缺少ruby环境,需安装

[[email protected] ~]# yum install ruby -y

提示如下:

[[email protected] ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/local/bin/redis-trib.rb:24:in `require‘: no such file to load -- rubygems (LoadError)
from /usr/local/bin/redis-trib.rb:24

注:需安装rubygems

[[email protected] ~]# yum install rubygems -y

提示如下:

[[email protected] ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘: no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require‘
from /usr/local/bin/redis-trib.rb:25

注:提示不能加载redis,是因为缺少redis和ruby的接口,使用gem安装

[[email protected] ~]# gem install redis
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...
时间: 2024-08-24 11:33:49

redis3.0集群的相关文章

Redis3.0 集群

1  Redis3.0集群 Redis3.0之前项目架构

redis3.0集群部署方案

redis3.0集群部署方案redis1:192.168.1.10:6379       192.168.1.10:6380redis2:192.168.1.11:6379       192.168.1.11:6380redis3:192.168.1.12:6379       192.168.1.12:6380关闭 selinux ,防火墙允许6379 6380端口通过, 先启动各服务器的redis节点在制作集群       redis1配置:yum -y insall gcc ruby r

Redis3.0集群方案分析

在Redis3.0集群出来之前,大家都对作者antirez寄予厚望,因为Redis从来没有让我们失望过.现在Redis3.0集群出来了,网上出了很多评论文章,都说他的功能多么强大,包括下面这张图是彻底把我欺骗了. 等到我把Redis3.0客户端库hiredis编译好集成到公司系统,访问其中一台Redis3.0服务器居然返回"MOVED 2318 10.12.8.156:6379",这才了解到访问其他Redis3.0服务器的Key需要二次定位,这就是Redis3.0所谓的ASK 转向/M

redis3.0集群说明与搭建

Redis3.0集群说明: 1.  Redis集群提供一种运行Redis的方式,数据被自动的分片到多个Redis节点. 2. 集群不支持处理多个键的命令,因为这需要在Redis节点间移动数据,使得                 Redis集群不能提供像Redis单点那样的性能,在高负载下会表现得不可预知. 3.  集群可在多个节点间自动拆分数据集. 当部分节点正在经历失败或者不能与集群其他节点通信时可继续运转 4 Redis集群不保证数据强一致性. redis-server 集群以哈希槽的方式

redis3.0集群部署和测试

redis3.0集群部署和测试 环境介绍 两台Centos7的虚拟机模拟6个节点,A台3个master节点,B台3个slave节点A地址:172.16.81.140B地址:172.16.81.141redis版本:redis-3.2.4 部署前准备 关闭防火墙--->systemctl stop firewalld开机禁用防火墙--->systemctl disable firewalld临时关闭selinux--->setenforce 0永久关闭selinux--->sed -

redis3.0集群搭建

Redis集群搭建 redis cluster介绍 节点自动发现.集群容错slave选举.Cluster管理.集群配置管理. 集群中的每个Redis节点需要2个TCP连接端口,如6379端口用于Client连接,16379端口用于集群数据通信 集群采用Hash Slot方案,而不是一致性哈希,共16384个Hashslot.如果有3台机器,那么NodeA在0-5500,NodeB 在5501-11000,NodeC在11001-16384.这种设计下,添加,删除新Node比较方便. 由于Hash

redis3.0集群安装部署

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 PREFIX=/usr/local/redis install mkdir -p /usr/local/redis/run mkdir -p /usr/local/redis/logs mkdir -p /usr/local/red

redis3.0 集群实战1 -- 安装和配置

本文主要是在centos7上安装和配置redis集群实战 参考: http://hot66hot.iteye.com/blog/2050676 集群教程: http://redisdoc.com/topic/cluster-tutorial.html#id2 1 安装准备 1.1 下载redis3.0版本 到redis官网redis.io或者redis中文官网www.redis.cn上(http://www.redis.cn/download.html)去下载最新的版本,我下载的是redis3.

CentOS完美搭建Redis3.0集群并附测试

线上的统一聊天和推送项目使用的是redis主从,redis版本2.8.6 redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口:slaveof 192.168.1.197 6379,然后启动主从,主从就搭建好了redis主从中如果主节点发生故障,不会自动切换,需要借助redis的Sentinel或者keepalive来实现主的故障转移 redis集群是一个无中心的分布式redis存储架构,可以在多个节点之间进行数据共享,解决了redis高