• Redis集群搭建


    一、操作说明

    需要在192.168.110.66机器上安装redis环境之后再搭建集群

    二、安装ruby

    1、yum安装ruby和依赖的包。

     

    [root@localhost redis_cluster]# yum -y install ruby ruby-devel rubygems rpm-build

     

    2、使用gem这个命令来安装redis接口

     

    [root@localhost redis_cluster]# gem install redis

    ERROR:  Error installing redis:

    redis requires Ruby version >= 2.2.2.

     

    这一步骤中出现了bug,度娘告诉我是Ruby版本太低,需要升级版本。

     

    3、升级Ruby的版本

     

    安装rvm,我不知道这是个什么东西,但是感觉像是Ruby的一个包管理器。

     

    [root@localhost redis_cluster]# curl -L get.rvm.io | bash -s stable

      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                     Dload  Upload   Total   Spent    Left  Speed

    100 24090  100 24090    0     0  10919      0  0:00:02  0:00:02 --:--:-- 91242

    Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

    Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

    gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17

    gpg: 无法检查签名:No public key

    Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found.Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

    GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' -'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the publickey:

        gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

    or if it fails:

        command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

    the key can be compared with:

        https://rvm.io/mpapis.asc

        https://keybase.io/mpapis

    NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgradeor upgrade to newer version (if available) or use the second method described above.

     

    这一操作得到了:

     

    gpg2 –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

     

    然后利用该密钥下载Ruby并升级。

     

    [root@localhost redis_cluster]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

    gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 keys.gnupg.net

    gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库

    gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <mpapis@gmail.com>”已导入

    gpg: 没有找到任何绝对信任的密钥

    gpg: 合计被处理的数量:1

    gpg:           已导入:1  (RSA: 1)

    [root@localhost redis_cluster]# curl -sSL https://get.rvm.io | bash -s stable

    Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

    Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

    gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17

    gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”

    gpg:               亦即“Michal Papis <mpapis@gmail.com>”

    gpg:               亦即“[jpeg image of size 5015]”

    gpg: 警告:这把密钥未经受信任的签名认证!

    gpg:       没有证据表明这个签名属于它所声称的持有者。

    主钥指纹: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3

    子钥指纹: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17

    GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'

    Creating group 'rvm'

    Installing RVM to /usr/local/rvm/

    Installation of RVM in /usr/local/rvm/ is almost complete:

      * First you need to add all users that will be using rvm to 'rvm' group,

        and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

      * To start using RVM you need to run `source /etc/profile.d/rvm.sh`

        in all your open shell windows, in rare cases you need to reopen all shell windows.

     

    接着,source环境,让rvm可用。

     

    [root@localhost ~]# source /usr/local/rvm/scripts/rvm

     

    查看Ruby可用版本

     

    [root@localhost ~]# rvm list known

    # MRI Rubies

    [ruby-]1.8.6[-p420]

    [ruby-]1.8.7[-head] # security released on head

    [ruby-]1.9.1[-p431]

    [ruby-]1.9.2[-p330]

    [ruby-]1.9.3[-p551]

    [ruby-]2.0.0[-p648]

    [ruby-]2.1[.10]

    [ruby-]2.2[.7]

    [ruby-]2.3[.4]

    [ruby-]2.4[.1]

    ruby-head

    # for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

    # JRuby

    jruby-1.6[.8]

    jruby-1.7[.27]

    jruby[-9.1.13.0]

    jruby-head

    # Rubinius

    rbx-1[.4.3]

    rbx-2.3[.0]

    rbx-2.4[.1]

    rbx-2[.5.8]

    rbx-3[.84]

    rbx-head

    # Opal

    opal

    # Minimalistic ruby implementation - ISO 30170:2012

    mruby-1.0.0

    mruby-1.1.0

    mruby-1.2.0

    mruby-1[.3.0]

    mruby[-head]

    # Ruby Enterprise Edition

    ree-1.8.6

    ree[-1.8.7][-2012.02]

    # Topaz

    topaz

    # MagLev

    maglev[-head]

    maglev-1.0.0

    # Mac OS X Snow Leopard Or Newer

    macruby-0.10

    macruby-0.11

    macruby[-0.12]

    macruby-nightly

    macruby-head

    # IronRuby

    ironruby[-1.1.3]

    ironruby-head

     

    可以看到最新的版本是2.4.1,本文安装2.3.0

     

    至此,我们升级了Ruby的版本。

     

    [root@localhost ~]# rvm install 2.3.0

    Searching for binary rubies, this might take some time.

    Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/6/x86_64/ruby-2.3.0.tar.bz2

    Checking requirements for centos.

    Installing requirements for centos.

    Installing required packages: autoconf, automake, bison, libffi-devel, libtool, readline-devel, sqlite-devel, libyaml-devel..........

    Requirements installation successful.

    ruby-2.3.0 - #configure

    ruby-2.3.0 - #download

      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                     Dload  Upload   Total   Spent    Left  Speed

    100 21.9M  100 21.9M    0     0   266k      0  0:01:24  0:01:24 --:--:--  278k

    No checksum for downloaded archive, recording checksum in user configuration.

    ruby-2.3.0 - #validate archive

    ruby-2.3.0 - #extract

    ruby-2.3.0 - #validate binary

    ruby-2.3.0 - #setup

    ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0@global

    ruby-2.3.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................

    ruby-2.3.0 - #generating global wrappers........

    ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0

    ruby-2.3.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list

    ruby-2.3.0 - #generating default wrappers........

     

    4、安装gem redis接口

     

    [root@localhost ~]# rvm use 2.3.0

    Using /usr/local/rvm/gems/ruby-2.3.0

    [root@localhost ~]# rvm remove 1.8.7

    ruby-1.8.7-head - #already gone

    Using /usr/local/rvm/gems/ruby-2.3.0

    [root@localhost ~]#  ruby --version

    ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

    [root@localhost ~]# gem install redis

    Fetching: redis-4.0.1.gem (100%)

    Successfully installed redis-4.0.1

    Parsing documentation for redis-4.0.1

    Installing ri documentation for redis-4.0.1

    Done installing documentation for redis after 0 seconds

    1 gem installed

     

    5、安装rubygems

     

    [root@localhost ~]# yum install -y rubygems

     

    到目前为止,我们的Ruby和运行redis-trib.rb需要的环境安装完成了。

    6、将redis-trib.rb命令添加到系统可执行目录

    [root@localhost ~]# cd /usr/local/redis/src

    [root@localhost ~]# cp redis-trib.rb /usr/local/bin

    三、集群部署

    [root@localhost ~]# redis-trib.rb  create  --replicas 1 192.168.110.56:7000 192.168.110.56:7001 192.168.110.56:7002 192.168.110.56:7003 192.168.110.63:7004 192.168.110.63:7005 192.168.110.63:7006 192.168.110.63:7007

    >>> Creating cluster

    >>> Performing hash slots allocation on 8 nodes...

    Using 4 masters:

    192.168.110.56:7000

    192.168.110.63:7004

    192.168.110.56:7001

    192.168.110.63:7005

    Adding replica 192.168.110.63:7006 to 192.168.110.56:7000

    Adding replica 192.168.110.56:7002 to 192.168.110.63:7004

    Adding replica 192.168.110.63:7007 to 192.168.110.56:7001

    Adding replica 192.168.110.56:7003 to 192.168.110.63:7005

    M: 33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000

       slots:0-4095 (4096 slots) master

    M: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

       slots:8192-12287 (4096 slots) master

    S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

       replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

    S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

       replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

    M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

       slots:4096-8191 (4096 slots) master

    M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

       slots:12288-16383 (4096 slots) master

    S: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

       replicates 33571cb37ef6b868b8413d570c2c212572694204

    S: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

       replicates 949607fc0e1ff1f3ca6f6e037c17c655e225cc59

    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.110.56:7000)

    M: 33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000

       slots:0-4095 (4096 slots) master

    M: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

       slots:8192-12287 (4096 slots) master

    M: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

       slots: (0 slots) master

       replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

    M: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

       slots: (0 slots) master

       replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

    M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

       slots:4096-8191 (4096 slots) master

    M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

       slots:12288-16383 (4096 slots) master

    M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

       slots: (0 slots) master

       replicates 33571cb37ef6b868b8413d570c2c212572694204

    M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

       slots: (0 slots) master

       replicates 949607fc0e1ff1f3ca6f6e037c17c655e225cc59

    [OK] All nodes agree about slots configuration.

    >>> Check for open slots...

    >>> Check slots coverage...

    [OK] All 16384 slots covered

     

    说明:

    (1)上面的命令创建的集群中有4个master为:

    主节点

    192.168.110.56:7000

    192.168.110.56:7001

    192.168.110.63:7004

    192.168.110.63:7005

    (2)有4个replicas为:

    从节点

    192.168.110.56:7002

    192.168.110.56:7003

    192.168.110.63:7006

    192.168.110.63:7007

    (3)cluster集群中默认是平均分配主节点和从节点的,上面的结果中显示两台redis服务器主从节点各半

    四、验证集群状态

    1、CLUSTER INFO命令是查看集群状态的,我们在192.168.110.66机器上任意登录一个集群中的节点进行验证

    [root@localhost ~]# redis-cli -h 192.168.110.56 -c -p 7000

    192.168.110.56:7000> KEYS *

    (empty list or set)

    192.168.110.56:7000> CLUSTER INFO

    cluster_state:ok

    cluster_slots_assigned:16384

    cluster_slots_ok:16384

    cluster_slots_pfail:0

    cluster_slots_fail:0

    cluster_known_nodes:8

    cluster_size:4

    cluster_current_epoch:8

    cluster_my_epoch:1

    cluster_stats_messages_sent:208

    cluster_stats_messages_received:208

    192.168.110.56:7000> CLUSTER NODES

    25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521013866398 6 connected 12288-16383

    740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 slave 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 0 1521013863374 8 connected

    153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521013865389 5 connected 4096-8191

    33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000 myself,master - 0 0 1 connected 0-4095

    d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521013865893 5 connected

    949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 master - 0 1521013867406 2 connected 8192-12287

    8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521013862870 6 connected

    72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 slave 33571cb37ef6b868b8413d570c2c212572694204 0 1521013864382 7 connected

    192.168.110.56:7000>

    说明:

    (1)我们可以看到在输出的信息中主从节点的显示

    (2)在cluster中不管在哪个节点进行操作只要在连接redis的时候加上-c参数,都会默认去寻找其中的一个主节点进行同样的操作,然后将数据同步到其他主从节点上

    (3)在cluster中如果有从节点down掉之后,会自动在集群中剔除,去连接其他可用节点

    (4)在cluster中如何主节点down掉之后,会按照默认规则将最后一个从节点升为主节点

    2、可以测试连接从节点然后进行插入数据的操作,然后在其他的从节点和主节点去get查看键值是否存在

         -c 参数:连接集群结点时使用,此选项可防止moved和ask异常。

  • 相关阅读:
    v-cloak
    MVVM
    初识ajax
    装瓶学习法
    回调函数(call back)
    如何让学习变得纯粹?
    异步
    grep用法
    Shell中的&&与||的区别
    shell中使用>/dev/null 2>&1 丢弃信息
  • 原文地址:https://www.cnblogs.com/bazingafraser/p/8579497.html
Copyright © 2020-2023  润新知