知识点

相关文章

更多

最近更新

更多

redis 集群环境搭建-redis集群管理

2019-03-20 22:22|来源: 网路

集群架构


(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.

(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot(插槽)上,cluster 负责维护node<->slot<->value


准备环境

创建一个目录,存放集群的配置文件
[root@master redis]# mkdir redis-cluster
[root@master redis]# cd redis-cluster/
[root@master redis-cluster]# mkdir 6380
[root@master redis-cluster]# mkdir 6381
[root@master redis-cluster]# mkdir 6382
[root@master redis-cluster]# pwd
/opt/redis/redis-cluster


拷贝安装源码中的redis.conf刚创建好的三个目录中

[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6380/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6381/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6382/


分别进入这三个目录,修改配置文件redis.conf

1、将端口分别设置为:6380、6381、6382
2、设置pidfile文件为不同的路径
3、开启集群,cluster-enabled yes
4、指定集群的配置文件,cluster-config-file "nodes-xxxx.conf"
以6380为例:
[root@master redis-master-slave]# vim 6380/redis.conf
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
cluster-enabled yes
cluster-config-file nodes-6380.conf
dir /opt/redis/redis-cluster/6380/


启动redis-server
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf


[root@master redis-cluster]# ps -ef | grep redis

root      2948     1  0 11:11 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6380 [cluster]                
root      2970     1  0 11:16 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6381 [cluster]                
root      2974     1  0 11:16 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6382 [cluster]  


创建redis集群

创建集群要使用redis-trib.rb脚本,执行此脚本查看使用方法
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
 del-node        host:port node_id
 info            host:port
 rebalance       host:port
                 --timeout <arg>
                 --auto-weights
                 --pipeline <arg>
                 --threshold <arg>
                 --weight <arg>
                 --simulate
                 --use-empty-masters
 check           host:port
 reshard         host:port
                 --timeout <arg>
                 --to <arg>
                 --pipeline <arg>
                 --from <arg>
                 --slots <arg>
                 --yes
 call            host:port command arg arg .. arg
 import          host:port
                 --copy
                 --from <arg>
                 --replace
 help            (show this help)
 set-timeout     host:port milliseconds
 create          host1:port1 ... hostN:portN
                 --replicas <arg>
 add-node        new_host:new_port existing_host:existing_port
                 --slave
                 --master-id <arg>
 fix             host:port
                 --timeout <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.


使用redis-trib.rb create创建集群,--replicas 0:指定了从数据的数量为0

注意:这里不能使用127.0.0.1,否则在Jedis客户端使用时无法连接到!

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382

>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.56.101:6380


修改刚创建的三个redis.conf配置文件中的bind

bind 192.168.56.101


重启redis-server

[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf
[root@master redis-cluster]# ps -ef | grep redis
root      3756     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6380 [cluster]          
root      3760     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6381 [cluster]          
root      3764     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6382 [cluster]


再创建集群

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
192.168.56.101:6380
192.168.56.101:6381
192.168.56.101:6382
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
  slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
  slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
  slots:10923-16383 (5461 slots) master
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.56.101:6380)
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
  slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
  slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
  slots:10923-16383 (5461 slots) master【集群及槽信息】
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


测试集群

[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380
192.168.56.101:6380> keys *
(empty list or set)
192.168.56.101:6380> set k1 123
(error) MOVED 12706 192.168.56.101:6382
因为k1的hash槽信息是在6382上,现在使用redis-cli连接的6380,无法完成set操作,需要客户端跟踪重定向,使用-c参数
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> set k1 123
-> Redirected to slot [12706] located at 192.168.56.101:6382
OK


获取数据

k1的hash槽信息在6382上,在6382上可以直接获取,在其他节点还是要重定向去获取
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> get k1
-> Redirected to slot [12706] located at 192.168.56.101:6382
"123"
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6382 -c
192.168.56.101:6382> get k1
"123"


执行redis-trib.rb脚本可能遇到的问题

1、/usr/bin/env: ruby: 没有那个文件或目录
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/usr/bin/env: ruby: 没有那个文件或目录
解决方案:由于redis-trib.rb是用ruby语言编写,所以需要安装ruby环境,这里推荐使用yum install ruby


2、redis-trib.rb:24:in `require': no such file to load -- rubygems

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/opt/redis/redis-3.2.1/src/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /opt/redis/redis-3.2.1/src/redis-trib.rb:24
解决方案:yum install rubygems


3、/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/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 /opt/redis/redis-3.2.1/src/redis-trib.rb:25
解决方案:缺少redis的接口,使用gem install redis或gem install redis --version 3.2.1
[root@master redis-cluster]# gem install redis --version 3.2.1
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...


备注:

如果连接不上gem服务器安装,就手动下载并安装
gem install redis --version 3.0.0  
ERROR:  Could not find a valid gem 'redis' (= 3.0.0) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
需要手工下载并安装:
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l ./redis-3.2.1.gem


书生参考于网络整理


redis知识点

redis快速入门

reids常用命令

redis数据结构

java_API_客户端

Jedis

Tlcache

redis_持久化

AOF

RDB

发布订阅(pub/sub)

redis_事件

redis事务

redis通讯协议

RESP(Redis Serialization Protocol)

redis高可用

redis哨兵

监控(Monitoring) 提醒(Notification) 自动故障迁移(Automatic failover)

redis主从复制

  • 复制模式

    1. 主从复制
    2. 从从复制
  • 复制过程

    • slave向master发送sync命令;
    • master开启子进程执行bgsave写入rdb文件;
    • master发送缓存和RDB文件给slave;
    • master发送数据发送给slave完成复制;

redis集群(Redis_Cluster)

相关问答

更多
  • 用的是什么集群方式,JedisSentinelPool 还是ShardedJedisPool。 如果用的是前者,配置是需要连接sentinel端口的(默认26379),不是连接redis端口(6379)。 或者把spring相关配置文件贴出来一下。 127.0.0.1:26379 127.0.0.1:26380 127.0.0.1:26381
  • 用的是什么集群方式,JedisSentinelPool 还是ShardedJedisPool。 如果用的是前者,配置是需要连接sentinel端口的(默认26379),不是连接redis端口(6379)。 或者把spring相关配置文件贴出来一下。
    安装redis集群需要版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本。 需要安装ruby的环境。 yum -y install ruby yum -y install rubygems redis集群管理工具redis-trib.rb [root@bogon ~]# cd redis-3.0.0 [root@bogon redis-3.0.0]# cd src [root@bogon src]# ll *.rb -rwxrwxr-x.1 r ...
  • Redis集群搭建的目的其实也就是集群搭建的目的,所有的集群主要都是为了解决一个问题,横向扩展。 在集群的概念出现之前,我们使用的硬件资源都是纵向扩展的,但是纵向扩展很快就会达到一个极限,单台机器的Cpu的处理速度,内存大 小,硬盘大小没办法一直满足需求,而且机器纵向扩展的成本是相当高的。集群的出现就是能够让多台机器像一台机器一样工作,实现了资源的横向扩展。 Redis是内存型数据库,当我们要存储的数据达到一定程度时,单台机器的内存满足不了我们的需求,搭建集群则是一种很好的解决方案。
  • 首先在 192.168.31.245 机器上 /root/software/redis-3.2.4 目录下创建 redis_cluster 目录; mkdir redis_cluster   在 redis_cluster 目录下,创建名为7000、7001、7002的目录,并将 redis.conf 拷贝到这三个目录中 mkdir 7000 7001 7002
    cp redis.conf redis_cluster/7000 cp redis.conf redis_cluster/7001 cp r ...
  • 你这个环境安装必要的工具那是系统必须的呀, 网上有这样的命令,安装所需要的工具包,这个我就不给你找了,因为我觉得你应该学习一下。 安的过程不重要,重要的是你要知道安装什么? 环境的搭建 要先安装rvm然后安装ruby 再安装rails
  • 安装redis集群需要版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本。 需要安装ruby的环境。 yum -y install ruby yum -y install rubygems redis集群管理工具redis-trib.rb [root@bogon ~]# cd redis-3.0.0 [root@bogon redis-3.0.0]# cd src [root@bogon src]# ll *.rb -rwxrwxr-x.1 r ...
  • 正如你在这里看到的,redis现在支持自动分区。 As you can see here, redis supports now automatic partitioning.
  • 看起来kue正是我们所需要的。 在Node.js集群环境中通过Redis进行的最小测试程序排队任务: // cluster-queue.js var kue = require('kue'); var cluster = require('cluster'); var numWorkers = process.argv[2]; var numParallel = process.argv[3]; var jobDelay = process.argv[4]; var numJobs ...