搭建redis cluster集群过程(详细)

  |   0 评论   |   0 浏览   |   蓝汝丶琪

搭建 Redis cluster 集群

服务器版本

CentOS Linux release 7.5.1804 (Core)

Redis cluster 规模

  • 三主三从
  • 192.168.24.241:6001,192.168.241.6002
  • 192.168.24.242:6001,192.168.242.6002
  • 192.168.24.243:6001,192.168.243.6002

下载 Redis

出现的错误

  1. zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录 这个错误

解决方法:make MALLOC=libc

修改 redis.conf 配置

  1. 创建 redis_cluster 目录(每台服务器)
    mkdir redis_cluster
  2. 在 redis_cluster 目录里创建两个目录 6001 6002
    mkdir 6001
    mkdir 6002
  3. 修改 redis.conf 配置
#配置
#redis后台运行
daemonize    yes   
            
#pidfile文件 6001~6003
pidfile  /var/run/redis_6000.pid   
  
#6001~6003
port  7000  
           
#开启集群
cluster-enabled  yes   
        
#配置文件首次启动自动生成
cluster-config-file  nodes_6001.conf 
  
#请求超时
cluster-node-timeout  5000  
   
#aof日志
appendonly  no  

#绑定地址,需要别的机器能ping通的地址
bind 192.168.21.137
  1. 将 redis.conf 赋值到 6001 和 6002 中
    cp redis.conf redis_cluster/6001
    cp redis.conf redis_cluster/6002

验证 Redis 安装成功

  1. 启动 Redis
    进去每个目录 cd redis_cluster/6001
    启动 Redis redis-server redis.conf
  2. 查看 Redis 进程
    ps -ef |grep redis
    如果看到 Redis 进程,则代表 Redis 启动成功

安装 redis-trib.rb

redis-trib.rb 是官方提供的用于搭建集群的工具

安装 Ruby

redis-trib.rb 是基于 Ruby 写的

  • 安装 Ruby yum -y install ruby ruby-devel rubygems rpm-build
    由于 CentOS7 yum 库中 Ruby 的版本支持到 2.0.0,但是 redis-trib.rb 需要 >2.2.2 版本,因此需要用 rvm 来更新 Ruby
  • 更新 Ruby
    • 安装 rvm
      • gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
      • curl -L get.rvm.io | bash -s stable
      • find / -name rvm -print
    • 更新配置 source /usr/local/rvm/scripts/rvm
    • 查看 rvm 库中已知的 Ruby 版本 rvm list known
    • 安装一个 Ruby 版本 rvm install 2.3.3
    • 使用一个 Ruby 版本 rvm install 2.3.3
    • 设置默认版本 rvm use 2.3.3 --default
    • 卸载 yum 下载的版本 rvm remove 2.0.0

下载 Redis 接口

  • 利用 gem 下载 gem install redis

确认安装成功

  • redis-trib.rb 是在 Redis 的 src 目录下
  • /opt/redis/redis-4.0.8/src/redis-trib.rb

创建集群

用 redis-trib.rb 创建集群

  • redis-trib.rb 使用
  • 利用 create 指令来创建集群,**--replicas 1 ** 指的是每个主节点都有一个从节点,后面跟着的是 Redis 的 ip:port
  • ./redis-trib.rb create --replicas 1 192.168.24.241:6001 192.168.24.241:6002 192.168.24.242:6001 192.168.24.242:6002 192.168.24.243:6001 192.168.24.243:6002
[root@sy1 src]# ./redis-trib.rb create --replicas  1 192.168.24.241:6001 192.168.24.241:6002 192.168.24.242:6001 192.168.24.242:6002 192.168.24.243:6001 192.168.24.243:6002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.24.241:6001
192.168.24.242:6001
192.168.24.243:6001
Adding replica 192.168.24.242:6002 to 192.168.24.241:6001
Adding replica 192.168.24.243:6002 to 192.168.24.242:6001
Adding replica 192.168.24.241:6002 to 192.168.24.243:6001
M: f829b83670df08aa0289a5ffef9efa32386e92d2 192.168.24.241:6001
   slots:0-5460 (5461 slots) master
S: 184f6dc341d0d49fbf17739233b7078606b399c3 192.168.24.241:6002
   replicates 130f3b8025c422820afa222f27949cf9acdc5f24
M: 63458c1953bdaa3484c482af23b1a1fa7e6f4f71 192.168.24.242:6001
   slots:5461-10922 (5462 slots) master
S: 8c8a6e54ee4c50e296f95faa7cda1cda0328fbe8 192.168.24.242:6002
   replicates f829b83670df08aa0289a5ffef9efa32386e92d2
M: 130f3b8025c422820afa222f27949cf9acdc5f24 192.168.24.243:6001
   slots:10923-16383 (5461 slots) master
S: 3c8b17d350ffafc7bafe1a0189deac5164fa45f1 192.168.24.243:6002
   replicates 63458c1953bdaa3484c482af23b1a1fa7e6f4f71
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.24.241:6001)
M: f829b83670df08aa0289a5ffef9efa32386e92d2 192.168.24.241:6001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 130f3b8025c422820afa222f27949cf9acdc5f24 192.168.24.243:6001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 184f6dc341d0d49fbf17739233b7078606b399c3 192.168.24.241:6002
   slots: (0 slots) slave
   replicates 130f3b8025c422820afa222f27949cf9acdc5f24
S: 3c8b17d350ffafc7bafe1a0189deac5164fa45f1 192.168.24.243:6002
   slots: (0 slots) slave
   replicates 63458c1953bdaa3484c482af23b1a1fa7e6f4f71
S: 8c8a6e54ee4c50e296f95faa7cda1cda0328fbe8 192.168.24.242:6002
   slots: (0 slots) slave
   replicates f829b83670df08aa0289a5ffef9efa32386e92d2
M: 63458c1953bdaa3484c482af23b1a1fa7e6f4f71 192.168.24.242:6001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

连接集群

  • 命令:redis-cli -h 192.168.24.241 -p 6001 -c
    • -h:Ip
    • -p:端口,默认 6379
    • -c:集群模式,访问集群必须带上
[root@sy2 6001]# redis-cli -h 192.168.24.241 -p 6001 -c
192.168.24.241:6001> set key1 key1
-> Redirected to slot [9189] located at 192.168.24.242:6001
OK
192.168.24.242:6001> get key1
"key1"
192.168.24.242:6001> set kk 241
-> Redirected to slot [2589] located at 192.168.24.241:6001
OK
192.168.24.241:6001> set kjkjkj 1231231
OK
192.168.24.241:6001> get kk
"241"

标题:搭建redis cluster集群过程(详细)
作者:蓝汝丶琪
地址:https://blog.doiduoyi.com/articles/1585826325165.html

评论

发表评论