zoukankan      html  css  js  c++  java
  • Redis CLuster(redis集群)

      准备6台机器,三主三从 ,Redis CLuster 是自带选举功能的不需要哨兵

      演示IP:

          master:192.168.1.4,192.168.1.5,192.168.1.6

          slave:   192.168.1.7,192.168.1.8,192.168.1.9

          redis端口:6379,redis集群端口:16379,redis哨兵端口:26379 

      创建Redis Cluster集群的前提:

          1. 每个 redis node 节点采用相同的硬件配置、相同的密码
          2. 每个节点必须开启的参数
              cluster-enabled yes #必须开启集群状态,开启后 redis 进程会有 cluster 显示
              cluster-config-file nodes-6380.conf #此文件有 redis cluster 集群自动创建和维护,不需要任何手动操作
          3. 所有 redis 服务器必须没有任何数据
          4. 先启动为单机 redis 且没有任何 key value

      一、配置6台redis服务器的yum源

        1、进入yum源所在目录

    cd /etc/yum.repos.d/

        2、下载阿里云的base源(有base光盘也可以用本地源)

    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

        3、安装epel 源

    yum install epel-release -y

        4、清除原来的yum源

    yum clean all

      二、配置6台redis服务器(默认master)

        1、安装redis包

    yum install redis -y

    #这里使用的都是epel源中带的redis3.2.12版本,编译的最新的在其他博客中也有讲,步骤基本相同

        2、修改redis配置文件

    vim /etc/redis.conf

        2.1、添加密码

    1 # masterauth <master-password>
    2 修改为
    3  masterauth 123456

    #一定要设置密码,没有密码的话可以直接通过Telnet连接到redis上,直接做增删改操作,这里为了演示,密码简单一点

        2.2、打开redis cluster节点

    1 # cluster-enabled yes
    2 修改为
    3 cluster-enabled yes
    4 ------------------
    5 # cluster-config-file nodes-6379.conf
    6 修改为:
    7 cluster-config-file nodes-6379.conf

    #表示开启cluster节点,开启后 redis 进程会有 cluster 显示

        2.3、打开守护进程,后台执行,(默认前台执行)

    1 daemonize no
    2 修改为
    3 daemonize yes

        2.4、修改监听端口为0.0.0.0

    1 bind 127.0.0.1
    2 修改为
    3 bind 0.0.0.0

        3、开启redis服务

    redis-server /etc/redis.conf 

    #必须带着配置文件开启

        4、检查redis cluster是否开启

        4.1、使用redis客户端连接

    redis-cli 
    

        4.2、info查看信息

    info

    查看cluster-enabled 为1 表示开启集群服务,为0表示没开启集群服务(启动服务才能使用redis-cli连接进去)

    # Cluster
    cluster_enabled:1

      每一台redis服务器,必须是master ,cluster_enabled 必须是1 ,里面必须没有任何数据,有数据的话使用创建集群是加不进去的,使用FLUSHDB 清空当前数据库里所有信息,FLUSHALL 清空redis所有库所有数据

      三、创建集群

      Redis 3 和 4 版本:
        需要使用到集群管理工具 redis-trib.rb,这个工具是 redis 官方推出的管理 redis 集群的工具,集成在redis 的源码 src 目录下,是基于 redis 提供的集群命令封装成简单、便捷、实用的操作工具,redis-trib.rb是 redis 作者用 ruby 完成的,

         1、编译安装状态

    cp /usr/local/src/redis-版本号/src/redis-trib.re /usr/bin/

        2、yum安装的需要手动安装ruby rubygems (编译安装的不用,)(只需要在redis集群中的任意一台上安装即可)

         2.1、安装gcc编译器和zlib依赖包

    yum install gcc zlib-devel -y

        2.2、下载ruby包,默认yum源带的是2.0 的不行(测试2.3以上的版本都行)

    wget https://cache.ruby-lang.org/pub/ruby/ruby-2.5.0.tar.gz

        2.3、解压缩ruby

    tar xf ruby-2.5.0.tar.gz 

         2.4 进入ruby目录

    cd ruby-2.5.0

         2.4、编译安装

    1 [17:48:20 root@localhost ruby-2.5.0]#./configure 
    2 [17:48:20 root@localhost ruby-2.5.0]#make
    3 [17:49:20 root@localhost ruby-2.5.0]#make install

        2.5、安装redis模块,

        2.5.1、下载ruby包

    wget https://rubygems.org/downloads/redis-4.2.3.gem

         2.5.2、查看ruby版本

    1 [11:57:23 root@localhost ruby-2.5.0]#ruby -v
    2 ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

        2.5.3、查找gem 命令在哪,(默认直接可以使用gem命令)

    [11:58:08 root@localhost ruby-2.5.0]#find / -name gem
    /root/ruby-2.5.0/spec/rubyspec/fixtures/code/gem
    /root/ruby-2.5.0/spec/ruby/fixtures/code/gem
    /root/ruby-2.5.0/bin/gem
    /usr/share/locale/gem
    /usr/local/bin/gem

        2.5.4、安装指定的redis模块(也可以直接安装gem install redis)

    gem install -l redis-4.2.3.gem 

        3.1、下载redis-trib.rb命令

    https://github.com/beebol/redis-trib.rb

    这个我看别人是有的,但我安装3版本的时候一直没有,去github上找了个免费的可以用的

         3.2、安装git软件

    yum install git -y

        3.3、克隆GitHub上的

    git clone git://github.com/beebol/redis-trib.rb.git

        3.4、复制redis-trib.rb

    cp redis-trib.rb /usr/bin/

        3.5、设置权限

    chmod 777 /usr/bin/redis-trib.rb 

        4、创建集群

    redis-trib.rb create --replicas 1 192.168.1.9:6379 192.168.1.8:6379 192.168.1.7:6379 192.168.1.6:6379 192.168.1.5:6379 192.168.1.4:6379

         4.1、

    redis-trib.rb create --replicas 1 192.168.1.9:6379 192.168.1.8:6379 192.168.1.7:6379 192.168.1.6:6379 192.168.1.5:6379 192.168.1.4:6379
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:#创建了三个主节点
    192.168.1.9:6379
    192.168.1.8:6379
    192.168.1.7:6379
    Adding replica 192.168.1.6:6379 to 192.168.1.9:6379  ### 192.168.1.6是192.168.1.9的从节点
    Adding replica 192.168.1.5:6379 to 192.168.1.8:6379
    Adding replica 192.168.1.4:6379 to 192.168.1.7:6379
    M: 73a51b03d3ae1ae60573924a36e35a4160ea2a8a 192.168.1.9:6379 # M表示master 主节点,S表示slave 从节点
       slots:0-5460 (5461 slots) master
    M: bc591a36af254e510488ab184b0e638e7298ff6d 192.168.1.8:6379 ##M后的一串数字是随机生成的,在集群中,以ID号区分主机,不以IP地址区分
       slots:5461-10922 (5462 slots) master ## slots是按照顺序分的,每台上的应该是差不多的,也会有原因导致无序分配,
    M: 7226c301d028168f22f60930db0bf8b4260c2905 192.168.1.7:6379
       slots:10923-16383 (5461 slots) master
    S: 3bcd400f071643f814d45479bfc3a56612eca471 192.168.1.6:6379
       replicates 73a51b03d3ae1ae60573924a36e35a4160ea2a8a  ## 这里的ID就是master的ID
    S: 7a1ad13e2d2e3ad84a5da2d001e1680fcf8b2255 192.168.1.5:6379
       replicates bc591a36af254e510488ab184b0e638e7298ff6d  ##slave节点是没有slots的,master宕机后会自动继承
    S: d53b5724df5ae6f6f5305cebb419e6b5ec2a6096 192.168.1.4:6379
       replicates 7226c301d028168f22f60930db0bf8b4260c2905
    Can I set the above configuration? (type 'yes' to accept):
    yes

    ##配置文件中dir /var/lib/redis这里定义了放在哪,这个配置就会保存在这个位置,配置文件自动生成,不需要手动更改

      

     ----------------------------------------------- ----------------------------------------------- ----------------------------------------------- ----------------------------------------------- ----------------------------------------------- ----------------------------------------------- ----------------------------------

    修改密码:

     vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-版本号/lib/redis/client.rb #修改密码为redis 登录密码 

     redis5版本

    redis-cli -a 123456  --cluster create 192.168.1.4:6379   192.168.1.5:6379  192.168.1.6:6379   192.168.1.7:6379  1 
    92.168.1.8:6379   192.168.1.9:63879 --cluster-replicas 1  

    报错如下:

      ERROR: Loading command: install (LoadError)
      cannot load such file -- zlib
      ERROR: While executing gem ... (NoMethodError)
      undefined method `invoke_with_build_args' for nil:NilClass

     解决办法:

    yum install zlib-devel -y

    进入对应的ruby目录下

    cd /root/ruby-2.5.0/ext/zlib

    执行

    [12:41:58 root@localhost zlib]#ruby extconf.rb 
    checking for deflateReset() in -lz... yes
    checking for zlib.h... yes
    checking for crc32_combine() in zlib.h... yes
    checking for adler32_combine() in zlib.h... yes
    checking for z_crc_t in zlib.h... yes
    creating Makefile

    编译

    make

    报错如下:

    make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'.  Stop.

    解决办法:

    [12:44:17 root@localhost zlib]#vim Makefile

    找到下面这行(最下面几行)

    zlib.o: $(top_srcdir)/include/ruby.h

    替换成

    zlib.o: ../../include/ruby.h

    执行:

    [12:47:14 root@localhost zlib]#make
    compiling zlib.c
    linking shared-object zlib.so
    [12:47:16 root@localhost zlib]#make install
    /usr/bin/install -c -m 0755 zlib.so /usr/local/lib/ruby/site_ruby/2.5.0/x86_64-linux

    重新安装:

    [12:47:32 root@localhost ~]#gem install -l redis-4.2.3.gem 
    Successfully installed redis-4.2.3
    Parsing documentation for redis-4.2.3
    Installing ri documentation for redis-4.2.3
    Done installing documentation for redis after 0 seconds
    1 gem installed

    也可以直接将目录打包到其他服务器上

      1、关闭进程

    kill -9 `ps -ef|grep redis|tr -s " " |cut -d" " -f2`

      2、删除redis的data里的aof和rdb文件

    rm -rf /apps/redis/data/*

      3、删除run里的pid数据

    rm -rf /apps/redis/run/*

      4、删除日志文件

    rm -rf /apps/redis/logs/*

      5、将redis打包

    tar zcvf redis.tar.gz /apps/redis/*

    /apps/redis/ 是我编译安装的位置,data这些文件夹都是我自己创建的

      6、将redis打包压缩包拷贝到使用的机器上

    scp redis.tar.gz root@192.168.1.10:/apps/redis/
    bc591a36af254e510488ab184b0e638e7298ff6d 
    ------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~ QQ--2382990774
  • 相关阅读:
    团队-团队编程项目作业名称-最终程序
    《团队-中国象棋-项目总结》
    课后作业-阅读任务-阅读提问-4
    《20171130-构建之法:现代软件工程-阅读笔记4》
    《软件工程课程总结》
    《20171117-构建之法:现代软件工程-阅读笔记-3》
    -课后作业-阅读任务-阅读提问-3
    《团队-中国象棋-团队一阶段互评》
    《团队-中国象棋-开发文档》
    《结对-贪吃蛇-结对项目总结》
  • 原文地址:https://www.cnblogs.com/alexlv/p/15044612.html
Copyright © 2011-2022 走看看