美文网首页
redis集群

redis集群

作者: 快去学习不然怎么去看aimer | 来源:发表于2019-11-26 19:44 被阅读0次

redis集群的优势
自动分割数据到不同的节点上。整个集群的部分节点失败或者不可达的情况下能够继续处理命令。
特点

  • 主从复制
  • 实现了高可用
  • 数据分片存储

meet过程

image.png

指派槽

image.png image.png

通过hash计算key,得到数据所在的slot位置

redis集群安装

集群架构图:

image.png

本实验使用容器进行,共有6个容器。其中3个为master节点,剩余的三个分别为3个master的从服务器,docker-compose见文章未部

官方工具安装

1.Ruby

下载Ruby

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

#由于使用编译安装,因此,需要在容器中安装编译软件
yum -y install  zlib-devel readline openssl-devel gcc  gcc-c++

#得到tar包,解包,编译安装
tar -xf ruby-2.6.1.tar.gz
cd ruby-2.6.1
./configure
make && make install

使用yum安装ruby时,ruby的版本过低,无法使用

2.安装rubygem redis

一个 ruby 语言实现的访问 Redis 集群的客户端

下载rubygem redis

wget https://rubygems.org/rubygems/rubygems-3.0.2.tgz
tar -xf rubygems-3.0.2.tgz
cd rubygems-3.0.2/
ruby setup.rb

#安装与Ruby相对应版本的redis
gem install redis

3.安装 redis-trib.rb

redis-trib 位于 Redis 源码的 src 文件夹中, 它是一个 Ruby 程序, 这个程序通过向实例发送特殊命令来完成创建新集群, 检查集群, 或者对集群进行重新分片(reshared)等工作。

因此,在github源码上找到源码。在/usr/bin/目录下建立redis-trib.rb文件,将源码复制到该文件中,保存退出。切记给该文件添加可执行权限

4.修改redis配置文件

cluster-enabled yes
cluster-config-file redis-cluster.conf
cluster-require-full-coverage no

启动redis-server

5.创建集群

redis-trib.rb  create  --replicas  1 172.21.0.2:6379 172.21.0.4:6379 172.21.0.3:6379 172.21.0.5:6379 172.21.0.7:6379 172.21.0.6:6379

--replicas 1 该参数是指每个master节点都有一个从服务器

image.png

集群建立成功!

建立集群容器的docker-compose

version: "3.2"
services: 
  redis-clu-m1:
    image: "redis-ruby:latest"
    container_name: "redis-clu-m1"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
  redis-clu-m2:
    image: "redis-ruby:latest"
    container_name: "redis-clu-m2"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
  redis-clu-m3:
    image: "redis-ruby:latest"
    container_name: "redis-clu-m3"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
  redis-clu-s1:
    image: "redis-ruby:latest"
    container_name: "redis-clu-s1"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
  redis-clu-s2:
    image: "redis-ruby:latest"
    container_name: "redis-clu-s2"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
  redis-clu-s3:
    image: "redis-ruby:latest"
    container_name: "redis-clu-s3"
    expose: 
      - "6379"
    tty: true
    networks:
      - jumpserver-test
networks:
  jumpserver-test:
    external:
      name: jumpserver-test

该镜像只是提前安装了redis。
tty: true 是为了让终端一直存在
external 链接到外部容器,jumpserver-test的网络模式为桥接
jmpserver-test的使用位于CI/CD项目的docker-compose中,详情

添加、删除节点

添加主从节点

查看节点的信息

[root@88c9c39edc63 /]# redis-trib.rb check 172.21.0.3:6379
>>> Performing Cluster Check (using node 172.21.0.3:6379)
M: eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14 172.21.0.3:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: f88732528a38b31036db957a948439bfdb030846 172.21.0.8:6379
   slots: (0 slots) slave
   replicates e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47
M: e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 172.21.0.9:6379
   slots: (0 slots) master
   1 additional replica(s)
S: 7dd5a5885e92ec7ebea7562f9931f59fcbac458d 172.21.0.5:6379
   slots: (0 slots) slave
   replicates 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1
S: 2dce93d4f944af4a3fc0519c75c6dd94ae700049 172.21.0.4:6379
   slots: (0 slots) slave
   replicates eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14
M: c6963e34b5f075125da57b5473f326f85bedf8d7 172.21.0.6:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 6afe14b9e8fbc495f2763a7de34f51334db43b1d 172.21.0.2:6379
   slots: (0 slots) slave
   replicates c6963e34b5f075125da57b5473f326f85bedf8d7
M: 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1 172.21.0.7:6379
   slots:10923-16383 (5461 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.

#查看从节点时会造成展示信息的丢失,因此,需要check主节点才可以看到所有节点的信息

添加主节点

[root@88c9c39edc63 /]# redis-trib.rb add-node 172.21.0.9:6379 172.21.0.2:6379
>>> Adding node 172.21.0.9:6379 to cluster 172.21.0.2:6379
>>> Performing Cluster Check (using node 172.21.0.2:6379)
S: 6afe14b9e8fbc495f2763a7de34f51334db43b1d 172.21.0.2:6379
   slots: (0 slots) slave
   replicates c6963e34b5f075125da57b5473f326f85bedf8d7
M: 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1 172.21.0.7:6379
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14 172.21.0.3:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 2dce93d4f944af4a3fc0519c75c6dd94ae700049 172.21.0.4:6379
   slots: (0 slots) slave
   replicates eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14
M: c6963e34b5f075125da57b5473f326f85bedf8d7 172.21.0.6:6379
   slots:0-5460 (5461 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.
>>> Send CLUSTER MEET to node 172.21.0.9:6379 to make it join the cluster.
[OK] New node added correctly.
# 172.21.0.9:6379 为主节点的ip与端口
# 172.21.0.2:6379 为集群中任一节点的ip与端口

为新添加的主节点添加从节点

[root@88c9c39edc63 /]# redis-trib.rb add-node --slave --master-id e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 172.21.0.8:6379 172.21.0.9:6379
>>> Adding node 172.21.0.8:6379 to cluster 172.21.0.9:6379
>>> Performing Cluster Check (using node 172.21.0.9:6379)
M: e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 172.21.0.9:6379
   slots: (0 slots) master
   0 additional replica(s)
M: 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1 172.21.0.7:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: c6963e34b5f075125da57b5473f326f85bedf8d7 172.21.0.6:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 6afe14b9e8fbc495f2763a7de34f51334db43b1d 172.21.0.2:6379
   slots: (0 slots) slave
   replicates c6963e34b5f075125da57b5473f326f85bedf8d7
S: 7dd5a5885e92ec7ebea7562f9931f59fcbac458d 172.21.0.5:6379
   slots: (0 slots) slave
   replicates 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1
M: eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14 172.21.0.3:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 2dce93d4f944af4a3fc0519c75c6dd94ae700049 172.21.0.4:6379
   slots: (0 slots) slave
   replicates eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.21.0.8:6379 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.21.0.9:6379.
[OK] New node added correctly.

# e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 该主节点的ID
# 172.21.0.8:6379 要添加的从节点的ip与端口
# 172.21.0.9:6379 该从节点所对应的主节点

为新的主节点分配存储

[root@88c9c39edc63 /]# redis-trib.rb reshard 172.21.0.9:6379
>>> Performing Cluster Check (using node 172.21.0.9:6379)
M: e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 172.21.0.9:6379
   slots: (0 slots) master
   1 additional replica(s)
M: 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1 172.21.0.7:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: f88732528a38b31036db957a948439bfdb030846 172.21.0.8:6379
   slots: (0 slots) slave
   replicates e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47
M: c6963e34b5f075125da57b5473f326f85bedf8d7 172.21.0.6:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 6afe14b9e8fbc495f2763a7de34f51334db43b1d 172.21.0.2:6379
   slots: (0 slots) slave
   replicates c6963e34b5f075125da57b5473f326f85bedf8d7
S: 7dd5a5885e92ec7ebea7562f9931f59fcbac458d 172.21.0.5:6379
   slots: (0 slots) slave
   replicates 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1
M: eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14 172.21.0.3:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 2dce93d4f944af4a3fc0519c75c6dd94ae700049 172.21.0.4:6379
   slots: (0 slots) slave
   replicates eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:all

# e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47  被分配的主节点的ID
# 4096 被分配多少槽数
# all 从那几个节点取槽,一般最好是all

删除节点

删除步骤:


image.png
image.png
image.png

若是使用ruby的集群工具也就是redis-trib.rb则不需要忘记过程

  • 将要删除的主节点的槽分配给其他主节点
redis-trib.rb reshard --from e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 --to eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14 --slots 1366 172.21.0.3:6379

# e8ae990bdf38dce76dbfcb1753d94d3a54e8ee47 从该节点分配slot
# eb1c1d36b78425c2eac7d8ea157ff60fae2a3f14  将slot分配给该节点
# --slots  要迁移的slot
# 172.21.0.3:6379 集群中任意主节点的ip与端口

重复上述操作,将被迁移的节点的slot尽可能的平均分配给每一个剩余的主节点,当然,重复是注意修改节点ID

  • 删除从、主节点(注意先删除从节点,后删除主节点)
[root@88c9c39edc63 /]# redis-trib.rb del-node 172.21.0.8:6379 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1

# 172.21.0.8:6379要删除节点的ip与端口
# 7d9dfa4ef82ed97d63ff2596c1e9e3ebd7ad67f1 要删除节点的ID

相关文章

网友评论

      本文标题:redis集群

      本文链接:https://www.haomeiwen.com/subject/oznewctx.html