Redis集群

集群架构

(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

1

2

3

4

5

6

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的gem

gem install redis

linux 在线升级ruby

一、使用RVM也就是Ruby Version Manager,Ruby版本管理器来升级ruby,RVM包含了Ruby的版本管理和Gem库管理(gemset)。

1.先更新一下包

$ yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel

2、RVM安装

$ curl -L get.rvm.io | bash -s stable

(如果失败更新证书)

3、安装

$ source /etc/profile.d/rvm.sh

4、 测试是否安装正常

       $ rvm -v

二、使用RVM升级Ruby

1、利用国内镜像

 

   $ vim /usr/local/rvm/user/db

   写入:ruby_url=https://cache.ruby-china.org/pub/ruby

 

2、查看当前ruby版本

  $ ruby -v

这一步会显示出来当前ruby的版本

3、列出已知ruby的版本

    $ rvm list known

直接安装2.6版本

4、安装ruby 2.6,rvm会自行寻找此版本

     $ rvm install 2.6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值