【Redis】Redis集群搭建...爬坑日记

前言

说好的更新大数据…

抱歉,加班去搞了个redis集群…

我把搞redis踩的坑都给你们说一下…

BigData 和 微服务 后续更新

小编昨晚搞到晚上3点… 给点面子… 转载请注明作者~

准备

写代码跟炒菜差不多。先准备好所有的东西,知识,材料就像菜一样,准备好了再去慢慢炒菜。一开始不好吃,练习练习着会越来越有味道。并且是带着自己味道的菜。

  1. Redis-3.2.12 源码包

  2. Ruby 2.2.2以上

  3. RubyGems

  4. 三台互通主机

爬坑准备

  1. centos-release-7-5.1804.el7.centos.x86_64.rpm

  2. wget-1.14-15.el7_4.1.x86_64.rpm

踩过的坑

小编Linux不太好…所以还没深究太多,

这些坑只是表面现象。

很有可能是我自己没配置好。

  1. 我安装的centos7.4 minimal,没安装好…
  2. yum命令挂了…
  3. weg命令minimal里面没有…
  4. make install 编译有坑…
  5. Node 192.168.31.60:7000 is not empty
  6. You need tcl 8.5 or newer in order to run the Redis test

Core - Code

  • 准备三台主机

【Redis】Redis集群搭建...爬坑日记

  • 创建好用户redis,密码(账号,安装路径自己根据习惯)

    $ useradd redis
    $ passwd redis
    
  • 进入安装目录上传redis源码包上去,并解压

【Redis】Redis集群搭建...爬坑日记

$ cd /home/redis
$ tar -zxvf redis-3.2.12.tar.gz
  • 创建文件夹存放配置等文件(后面看配置文件会一一对应这些文件夹)

    # 用于存放后面一些配置文件
    $ mkdir local
    $ cd local
    $ mkdir redis-3.2.12
    $ cd redis-3.2.12
    # 创建..创建..创建..
    $ mkdir pid
    $ mkdir redis-cluster
    $ mkdir sbin
    
  • 编译一下源码

    $ cd /home/redis/redis-3.2.12/src
    $ make install PREFIX=/home/redis/local/redis-3.2.12/sbin/
    
    
  • 创建配置文件

    # 端口作为文件夹名字
    $ mkdir /home/redis/local/redis-3.2.12/redis-cluster/{7000,7001,7002}
    # 进入每个文件夹创建配置文件 7000为例
    $ cd /home/redis/local/redis-3.2.12/redis-cluster/7000
    # 方法一自己创建redis.conf文件 好处:没那么多注释,容易看
    $ touch redis.conf
    $ vi redis.conf
    # 方法二cp官方给的配置文件 (推荐) 好处: 防止自己配置错,官方就是官方
    $ cp /home/redis/redis-3.2.12/redis.conf /home/redis/local/redis-3.2.12/redis-cluster/7000/
    $ vi /home/redis/local/redis-3.2.12/redis-cluster/7000/redis.conf
    # 在vim下用/找到配置然后修改
    
  • vi redis.conf 写入(修改)以下内容

    # 端口7000,7001,7002,与目录对应
    port 7000   
    #默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
    bind 192.168.31.60
    #redis后台运行
    daemonize yes   
    #开启集群
    cluster-enabled yes 
    #集群的配置,配置文件首次启动自动生成 7000,7001,7002  
    cluster-config-file nodes_7000.conf  
    #请求超时,默认15秒,可自行设置
    cluster-node-timeout 8000  
    #开启aof持久化模式,每次写操作请求都追加到appendonly.aof文件中
    appendonly yes  
    #每次有写操作的时候都同步
    appendfsync always 
    #redis服务日志
    logfile "/home/redis/local/redis-3.2.12/pid/redis.log"
    #pidfile文件对应7000,7001,7002
    pidfile /home/redis/local/redis-3.2.12/pid/redis_7000.pid  
    
  • 编写启动节点(集群模式)脚本

    $ cd /home/redis/local/redis-3.2.12/sbin/bin/
    $ vi start-redis.sh
    
  • 脚本内容

    for((i=0;i<3;i++)); 
    do /usr/local/bin/redis-server /home/redis/local/redis-3.2.12/redis-cluster/700$i/redis.conf; 
    done
    
  • 启动脚本

    $ sh start-redis.sh
    
  • 查看进程和端口监听

     #查看是否启动成功
    $ ps -ef | grep redis  
    #可以看到redis监听端口
    $ netstat -tnlp | grep redis 
    

【Redis】Redis集群搭建...爬坑日记
【Redis】Redis集群搭建...爬坑日记

  • 另外两台主机
 # 创建用户,设置密码这些都一样
 $ scp -r /home/redis/local [email protected]:/home/redis
 $ scp -r /home/redis/local [email protected]:/home/redis
  • 需要修改的地方 redis.conf
  # redis.conf 
  # 61 主机
  bind 192.168.31.61
  # 62 主机
  bind 192.168.31.61
  # 7000端口,文件名等对应修改
  • 启动集群
  # 在其中一台主机 我在192.168.31.60
  $ /home/redis/redis-3.2.12/src/redis-trib.rb create --replicas 1 192.168.31.60:7000 192.168.31.60:7001 192.168.31.60:7002 192.168.31.61:7000 192.168.31.61:7001 192.168.31.61:7002 192.168.31.62:7000 192.168.31.62:7001 192.168.31.62:7002

【Redis】Redis集群搭建...爬坑日记

爬坑日志

稍后更新…

总结

遇到问题就查。哪怕不知道为什么可以这样解决,先记录下来吧。

我的博客为的是自己进步,也为遇到坑的人。

欢迎随时交流。

linux操作系统多学点。

学习不是Ctrl+C+V

转载注明一下,尊重下作者,谢谢