1、主机

  • 172.16.12.252
  • 172.16.12.253

2、两台主机home 目录下创建文件夹

  • /home/redis-cluster/7001/log
  • /home/redis-cluster/7001/data
  • /home/redis-cluster/7002/log
  • /home/redis-cluster/7002/data
  • /home/redis-cluster/7003/log
  • /home/redis-cluster/7003/data

3、配置文件,将配置文件放入对应的端口

  1. bind 172.16.12.252
  2. protected-mode yes
  3. port 7001
  4. pidfile /var/run/redis_7001.pid
  5. logfile /home/redis-cluster/7001/log/7001.log
  6. dir /home/redis-cluster/7001/data/
  7. appendonly yes
  8. cluster-enabled yes
  9. cluster-config-file /home/redis-cluster/7001/nodes-7001.conf
  10. requirepass 12678
  • slave 节点需要改动的配置
  1. bind 172.16.12.253
  2. protected-mode yes
  3. port 7003
  4. pidfile /var/run/redis_7003.pid
  5. logfile /home/redis-cluster/7003/log/7003.log
  6. dir /home/redis-cluster/7003/data/
  7. appendonly yes
  8. cluster-enabled yes
  9. cluster-config-file /home/redis-cluster/7003/nodes-7003.conf
  10. requirepass 12678

4、下载redis 并安装

  • 下载redis redis-4.0.11.tar.gz
  • 将redis 解压在 /usr/local/redis 目录下面 tar -zxvf redis-4.0.11.tar.gz
  • 安装redis 在redis-4.0.11 目录下执行 make
  • 创建软连接,
    • ln -s /usr/local/redis/redis-4.0.11/src/redis-server /usr/local/bin/redis-server
    • ln -s /usr/local/redis/redis-4.0.11/src/redis-cli /usr/local/bin/redis-cli

5、创建redis集群

  • 启动每个redis 节点
    • redis-server /home/redis-cluster/7001/redis-7001.conf
    • redis-server /home/redis-cluster/7002/redis-7002.conf
    • redis-server /home/redis-cluster/7003/redis-7003.conf
  • 通过ps 查看节点启动情况
    redis三主三从搭建 - 图1
  • 创建集群

    • 打开端口
      1、查看防火墙策略
      firewall-cmd —list-all
      2、端口加入防火墙策略
      sudo firewall-cmd —zone=public —add-port=17001/tcp —permanent
      sudo firewall-cmd —zone=public —add-port=17002/tcp —permanent
      sudo firewall-cmd —zone=public —add-port=17003/tcp —permanent
      sudo firewall-cmd —zone=public —add-port=7001/tcp —permanent
      sudo firewall-cmd —zone=public —add-port=7002/tcp —permanent
      sudo firewall-cmd —zone=public —add-port=7003/tcp —permanent
      3、重新加载防火墙
      sudo firewall-cmd —reload
    • 安装ruby gem redis

      • 安装 yum install –y ruby
      • 安装 yum install -y rubygems
      • gem install redis
      • 此处报错,说需要ruby 的版本大于2.3.0,但是centos 只支持到2.0.0 此处需要将ruby提升至2.3.0
      • 执行以下命令即可升级
        1. $ curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
        2. $ curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
        3. $ curl -L get.rvm.io | bash -s stable
        4. $ find / -name rvm.sh
        5. $ source /etc/profile.d/rvm.sh
        6. $ rvm requirements
        7. $ rvmsudo 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
        8. $ source /usr/local/rvm/scripts/rvm
        9. $ rvm list known # 查看rvm库中已知的ruby版本
        10. $ rvm install 2.3.3 # 安装一个ruby版本
        11. $ rvm use 2.3.3 # 使用一个ruby版本
        12. $ rvm remove 2.0.0 # 卸载已知版本
        13. $ gem install redis # 安装reids
    • 此处也可以手动安装gem redis,不用执行以上ruby命令
      gem install —local redis-4.1.3.gem 下载连接redis-4.1.3.gem

    • 在 /usr/local/redis/redis-4.0.11/src 目录下执行
      ./redis-trib.rb create —replicas 1 172.16.12.253:7001 172.16.12.253:7002 172.16.12.253:7003 172.16.12.252:7001 172.16.12.252:7002 172.16.12.252:7003
    • 重启/重建Redis集群时,必须删除去/etc/redis/nodes.conf文件。

参考文档

https://blog.csdn.net/weixin_42255229/article/details/80425565