Redis集群部署

女爷i 2022-04-18 06:33 535阅读 0赞

准备

CentOS:7

Redis:4.0.11

测试在3台虚拟机上进行,ip 分别为:

  • 172.16.105.128
  • 172.16.105.129
  • 172.16.105.130

主从复制

1.分别在三台虚拟机上安装 redis,单机安装部署参考(CentOS 7安装Redis)

2.在从服务器( 172.16.105.129172.16.105.130 )配置文件 redis.conf 中添加主从配置

  1. # slaveof 主服务器IP 端口
  2. slaveof 172.16.105.128 6379

3.启动 172.16.105.128172.16.105.129172.16.105.130 三个节点服务。

4.在主从节点上登录客户端,使用命令 info replication 可以看到相关节点信息。

主节点

  1. 127.0.0.1:6379> info replication
  2. # Replication
  3. role:master # 角色
  4. connected_slaves:2 # 连接从服务器数
  5. slave0:ip=172.16.105.129,port=6379,state=online,offset=84,lag=0 # 从服务器列表信息
  6. slave1:ip=172.16.105.130,port=6379,state=online,offset=84,lag=1
  7. master_replid:c6451f091badfc49b9c001a27e6aae7086db08bb
  8. master_replid2:0000000000000000000000000000000000000000
  9. master_repl_offset:84 # 增量复制offset
  10. second_repl_offset:-1
  11. repl_backlog_active:1
  12. repl_backlog_size:1048576
  13. repl_backlog_first_byte_offset:1
  14. repl_backlog_histlen:84

从节点

  1. 127.0.0.1:6379> info replication
  2. # Replication
  3. role:slave
  4. master_host:172.16.105.128
  5. master_port:6379
  6. master_link_status:up
  7. master_last_io_seconds_ago:2
  8. master_sync_in_progress:0
  9. slave_repl_offset:406
  10. slave_priority:100
  11. slave_read_only:1
  12. connected_slaves:0
  13. master_replid:c6451f091badfc49b9c001a27e6aae7086db08bb
  14. master_replid2:0000000000000000000000000000000000000000
  15. master_repl_offset:406
  16. second_repl_offset:-1
  17. repl_backlog_active:1
  18. repl_backlog_size:1048576
  19. repl_backlog_first_byte_offset:1
  20. repl_backlog_histlen:406

5.在主服务器上执行 set hello world 命令,可以看到相应同步操作执行。

  1. Slave 172.16.105.129:6379 asks for synchronization
  2. 2587:M 09 Nov 03:51:13.590 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '4c65ba0873afc39788457866070142cb456fc581', my replication IDs are 'c0199fe05490d9fd6c3b49d78a2ab63571f37abf' and '0000000000000000000000000000000000000000')
  3. 2587:M 09 Nov 03:51:13.590 * Starting BGSAVE for SYNC with target: disk
  4. 2587:M 09 Nov 03:51:13.590 * Background saving started by pid 2595
  5. 2595:C 09 Nov 03:51:13.592 * DB saved on disk
  6. 2595:C 09 Nov 03:51:13.592 * RDB: 0 MB of memory used by copy-on-write
  7. 2587:M 09 Nov 03:51:13.656 * Background saving terminated with success
  8. 2587:M 09 Nov 03:51:13.657 * Synchronization with slave 172.16.105.129:6379 succeeded
  9. 2587:M 09 Nov 03:51:40.071 * Slave 172.16.105.130:6379 asks for synchronization
  10. 2587:M 09 Nov 03:51:40.071 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '4c65ba0873afc39788457866070142cb456fc581', my replication IDs are 'c6451f091badfc49b9c001a27e6aae7086db08bb' and '0000000000000000000000000000000000000000')
  11. 2587:M 09 Nov 03:51:40.071 * Starting BGSAVE for SYNC with target: disk
  12. 2587:M 09 Nov 03:51:40.071 * Background saving started by pid 2596
  13. 2596:C 09 Nov 03:51:40.073 * DB saved on disk
  14. 2596:C 09 Nov 03:51:40.074 * RDB: 0 MB of memory used by copy-on-write
  15. 2587:M 09 Nov 03:51:40.138 * Background saving terminated with success
  16. 2587:M 09 Nov 03:51:40.139 * Synchronization with slave 172.16.105.130:6379 succeeded

5.在从服务器客户端执行 get hello 命令,可以返回在主服务器上设置的信息。

  1. 127.0.0.1:6379> get hello
  2. "world"

哨兵机制

1.备份哨兵配置文件

  1. [root@localhost redis-4.0.11]# cp sentinel.conf sentinel.conf.bak

2.设置哨兵配置文件

  1. port 26379
  2. # sentinel monitor 自定义master名字 master节点IP 端口 最低通过票数(具体需要几个哨兵节点统一才行)
  3. sentinel monitor mymaster 172.16.105.128 6379 1
  4. sentinel down-after-milliseconds mymaster 5000 # 设置mymaster超过指定时间午相应就认为sdown
  5. sentinel failover-timeout mymaster 15000 # 设置mymaster超过指定时间没活过来,则启动failover,从剩余slave中选举master

3.在主服务器上启动哨兵监控,可以看到相关信息

方式

  • ./redis-sentinel ../sentinel.conf
  • ./redis-server ../sentinel.conf --sentinel

    2632:X 09 Nov 04:27:36.922 # Sentinel ID is af1657d3b6dce9213a422d474dd52464ee287484
    2632:X 09 Nov 04:27:36.922 # +monitor master mymaster 172.16.105.128 6379 quorum 1
    2632:X 09 Nov 04:27:36.923 +slave slave 172.16.105.129:6379 172.16.105.129 6379 @ mymaster 172.16.105.128 6379
    2632:X 09 Nov 04:27:36.924
    +slave slave 172.16.105.130:6379 172.16.105.130 6379 @ mymaster 172.16.105.128 6379

4.停止 master 上的服务,查看哨兵服务日志信息

  1. 2632:X 09 Nov 04:30:03.657 # +sdown master mymaster 172.16.105.128 6379
  2. 2632:X 09 Nov 04:30:03.657 # +odown master mymaster 172.16.105.128 6379 #quorum 1/1
  3. 2632:X 09 Nov 04:30:03.657 # +new-epoch 1
  4. 2632:X 09 Nov 04:30:03.657 # +try-failover master mymaster 172.16.105.128 6379 # 哨兵开始进行故障恢复
  5. 2632:X 09 Nov 04:30:03.659 # +vote-for-leader af1657d3b6dce9213a422d474dd52464ee287484 1
  6. 2632:X 09 Nov 04:30:03.659 # +elected-leader master mymaster 172.16.105.128 6379
  7. 2632:X 09 Nov 04:30:03.659 # +failover-state-select-slave master mymaster 172.16.105.128 6379
  8. 2632:X 09 Nov 04:30:03.731 # +selected-slave slave 172.16.105.130:6379 172.16.105.130 6379 @ mymaster 172.16.105.128 6379
  9. 2632:X 09 Nov 04:30:03.731 * +failover-state-send-slaveof-noone slave 172.16.105.130:6379 172.16.105.130 6379 @ mymaster 172.16.105.128 6379
  10. 2632:X 09 Nov 04:30:03.832 * +failover-state-wait-promotion slave 172.16.105.130:6379 172.16.105.130 6379 @ mymaster 172.16.105.128 6379
  11. 2632:X 09 Nov 04:30:04.712 # +promoted-slave slave 172.16.105.130:6379 172.16.105.130 6379 @ mymaster 172.16.105.128 6379
  12. 2632:X 09 Nov 04:30:04.712 # +failover-state-reconf-slaves master mymaster 172.16.105.128 6379
  13. 2632:X 09 Nov 04:30:04.767 * +slave-reconf-sent slave 172.16.105.129:6379 172.16.105.129 6379 @ mymaster 172.16.105.128 6379
  14. 2632:X 09 Nov 04:30:05.771 * +slave-reconf-inprog slave 172.16.105.129:6379 172.16.105.129 6379 @ mymaster 172.16.105.128 6379
  15. 2632:X 09 Nov 04:30:05.771 * +slave-reconf-done slave 172.16.105.129:6379 172.16.105.129 6379 @ mymaster 172.16.105.128 6379
  16. 2632:X 09 Nov 04:30:05.841 # +failover-end master mymaster 172.16.105.128 6379 # 哨兵完成故障恢复
  17. 2632:X 09 Nov 04:30:05.841 # +switch-master mymaster 172.16.105.128 6379 172.16.105.130 6379
  18. 2632:X 09 Nov 04:30:05.842 * +slave slave 172.16.105.129:6379 172.16.105.129 6379 @ mymaster 172.16.105.130 6379
  19. 2632:X 09 Nov 04:30:05.842 * +slave slave 172.16.105.128:6379 172.16.105.128 6379 @ mymaster 172.16.105.130 6379
  20. 2632:X 09 Nov 04:30:10.874 # +sdown slave 172.16.105.128:6379 172.16.105.128 6379 @ mymaster 172.16.105.130 6379

Redis Cluster

集群搭建

1.分别在3台虚拟机上各安装配置1主1从,共6个节点,便于区分采用不同端口

  • 172.16.105.128 节点端口:7001、7002
  • 172.16.105.129 节点端口:7003、7004
  • 172.16.105.130 节点端口:7005、7006

2.依次修改各实例配置文件 redis.conf

  1. port 7001 # 根据上面修改为指定端口
  2. bind 0.0.0.0 # 设置不限制访问来源IP
  3. pidfile /var/run/redis_7001.pid # pidfile文件分别对应7001,7002,7003,7004,7005,7006
  4. cluster-enabled yes # 开启集群模式
  5. cluster-config-file nodes.conf # 设置集群配置文件
  6. cluster-node-timeout 5000 # 集群节点超时时间
  7. appendonly yes

3.分别启动全部6个实例,因为集群不存在配置文件 nodes.conf ,所以会为每个实例分配一个唯一的节点id

  1. [root@localhost redis-7001]# ./src/redis-server ./redis.conf &
  2. 7684:M 11 Nov 02:29:59.975 * No cluster configuration found, I'm 00bee510e285e1951863d15e646a5fcccd10e28f

4.安装 ruby 环境( redis-trib 依赖于 redis gemgem依赖于 ruby

  1. [root@localhost redis-7001]# yum install ruby
  2. [root@localhost redis-7001]# yum install rubygems

5.gem 安装 redis

  1. [root@localhost redis-7001]# gem install redis
  2. Fetching: redis-4.0.3.gem (100%)
  3. ERROR: Error installing redis:
  4. redis requires Ruby version >= 2.2.2.

CentOS 7 yumruby 版本只支持到 2.0.0 ,而 gem 安装 redis 需要 2.2.2 版本以上。

解决方法:采用rvm更新ruby

  • 安装 rvm

    [root@localhost redis-7001]# gpg2 —keyserver hkp://keys.gnupg.net —recv-keys D39DC0E3
    [root@localhost redis-7001]# curl -L get.rvm.io | bash -s stable

    [root@localhost redis-7001]# find / -name rvm -print
    /usr/local/rvm
    /usr/local/rvm/src/rvm
    /usr/local/rvm/src/rvm/bin/rvm
    /usr/local/rvm/src/rvm/lib/rvm
    /usr/local/rvm/src/rvm/scripts/rvm
    /usr/local/rvm/bin/rvm
    /usr/local/rvm/lib/rvm
    /usr/local/rvm/scripts/rvm

    [root@localhost redis-7001]# source /usr/local/rvm/scripts/rvm

  • 查看 rvm 库已经 ruby 版本列表

    [root@localhost redis-7001]# rvm list known

    MRI Rubies

    [ruby-]1.8.6[-p420]
    [ruby-]1.8.7[-head] # security released on head
    [ruby-]1.9.1[-p431]
    [ruby-]1.9.2[-p330]
    [ruby-]1.9.3[-p551]
    [ruby-]2.0.0[-p648]
    [ruby-]2.1[.10]
    [ruby-]2.2[.10]
    [ruby-]2.3[.7]
    [ruby-]2.4[.4]
    [ruby-]2.5[.1]
    [ruby-]2.6[.0-preview2]
    ruby-head

  • 选择一个较高版本(2.5.1)进行安装

    [root@localhost redis-7001]# rvm install 2.5.1

  • 使用一个 ruby 版本

    [root@localhost redis-7001]# rvm use 2.5.1
    Using /usr/local/rvm/gems/ruby-2.5.1
    [root@localhost redis-7001]# ruby —version
    ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

可选操作

  1. rvm use 2.5.1 --default # 设置ruby默认版本
  2. rvm remove 2.0.0 # 卸载指定版本

再次执行 gem 安装 redis ,成功安装

  1. [root@localhost redis-7001]# gem install redis
  2. Fetching: redis-4.0.3.gem (100%)
  3. Successfully installed redis-4.0.3
  4. Parsing documentation for redis-4.0.3
  5. Installing ri documentation for redis-4.0.3
  6. Done installing documentation for redis after 0 seconds
  7. 1 gem installed

6.使用 redis-trib.rb 创建集群(一个 master 主节点拥有一个 slave 从节点)

  • replicas :设置每个 master 主节点拥有 slave 从节点个数

    [root@localhost redis-7001]# ./src/redis-trib.rb create —replicas 1 172.16.105.128:7001 \172.16.105.128:7002 172.16.105.129:7003 172.16.105.129:7004 172.16.105.130:7005 172.16.105.130:7006

    Creating cluster
    Performing hash slots allocation on 6 nodes…
    Using 3 masters:
    172.16.105.128:7001
    172.16.105.129:7003
    172.16.105.130:7005
    Adding replica 172.16.105.129:7004 to 172.16.105.128:7001
    Adding replica 172.16.105.130:7006 to 172.16.105.129:7003
    Adding replica 172.16.105.128:7002 to 172.16.105.130:7005
    M: 00bee510e285e1951863d15e646a5fcccd10e28f 172.16.105.128:7001
    slots:0-5460 (5461 slots) master
    S: c85ebc85fee43048b8135ac5b73c3337ef6b9dd0 172.16.105.128:7002
    replicates 93ba46762a030b853ba3a81634fc79e92e17d6d8
    M: 162cf6816ffdd75f22af63359a1da8a2eb7725a2 172.16.105.129:7003
    slots:5461-10922 (5462 slots) master
    S: e9ebec2d60087531517e08ae102d66e3d8db39eb 172.16.105.129:7004
    replicates 00bee510e285e1951863d15e646a5fcccd10e28f
    M: 93ba46762a030b853ba3a81634fc79e92e17d6d8 172.16.105.130:7005
    slots:10923-16383 (5461 slots) master
    S: 633b4c938e3773f90abc11261452d6638f31c7ed 172.16.105.130:7006
    replicates 162cf6816ffdd75f22af63359a1da8a2eb7725a2
    Can I set the above configuration? (type ‘yes’ to accept): yes
    Nodes configuration updated
    Assign a different config epoch to each node
    7743:M 11 Nov 04:07:33.471 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
    7748:M 11 Nov 04:07:33.471 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
    Sending CLUSTER MEET messages to join the cluster
    7743:M 11 Nov 04:07:33.486 # IP address for this node updated to 172.16.105.128
    7748:M 11 Nov 04:07:33.650 # IP address for this node updated to 172.16.105.128
    Waiting for the cluster to join…
    7748:S 11 Nov 04:07:37.498 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
    7748:S 11 Nov 04:07:37.498 # Cluster state changed: ok
    Performing Cluster Check (using node 172.16.105.128:7001)
    M: 00bee510e285e1951863d15e646a5fcccd10e28f 172.16.105.128:7001
    slots:0-5460 (5461 slots) master
    1 additional replica(s)
    M: 162cf6816ffdd75f22af63359a1da8a2eb7725a2 172.16.105.129:7003
    slots:5461-10922 (5462 slots) master
    1 additional replica(s)
    M: 93ba46762a030b853ba3a81634fc79e92e17d6d8 172.16.105.130:7005
    slots:10923-16383 (5461 slots) master
    1 additional replica(s)
    S: 633b4c938e3773f90abc11261452d6638f31c7ed 172.16.105.130:7006
    slots: (0 slots) slave
    replicates 162cf6816ffdd75f22af63359a1da8a2eb7725a2
    S: c85ebc85fee43048b8135ac5b73c3337ef6b9dd0 172.16.105.128:7002
    slots: (0 slots) slave
    replicates 93ba46762a030b853ba3a81634fc79e92e17d6d8
    S: e9ebec2d60087531517e08ae102d66e3d8db39eb 172.16.105.129:7004
    slots: (0 slots) slave
    replicates 00bee510e285e1951863d15e646a5fcccd10e28f
    [OK] All nodes agree about slots configuration.
    Check for open slots…
    Check slots coverage…
    [OK] All 16384 slots covered.

集群创建完成后,可以看到分配的三个 master 节点和三个 slave 节点,以及对应的槽位区间。

集群测试

客户端连接集群需要指定 -c 参数

  1. [root@localhost redis-7001]# ./src/redis-cli -c -p 7001

7001 主节点设置 set name clusterTest ,根据 key 值计算槽位分布在节点 7003(5461-10922)

  1. [root@localhost redis-7001]# ./src/redis-cli -c -p 7001
  2. 127.0.0.1:7001> set name clusterTest
  3. -> Redirected to slot [5798] located at 172.16.105.129:7003
  4. OK
  5. 172.16.105.129:7003> get name
  6. "clusterTest"
  7. 172.16.105.129:7003>

在节点 7006 获取 name 值,自动跳转到节点 7003 获取值。

  1. [root@localhost redis-7006]# ./src/redis-cli -c -p 7006
  2. 127.0.0.1:7006> get name
  3. -> Redirected to slot [5798] located at 172.16.105.129:7003
  4. "clusterTest"
  5. 172.16.105.129:7003>

集群节点选举

杀掉节点 7005 ,查看集群变化

  1. [root@localhost redis-7006]# ps -ef | grep redis
  2. root 7768 1211 0 03:17 pts/0 00:00:07 ./src/redis-server 0.0.0.0:7005 [cluster]
  3. root 7773 1211 0 03:18 pts/0 00:00:07 ./src/redis-server 0.0.0.0:7006 [cluster]
  4. root 7848 7831 0 04:50 pts/1 00:00:00 grep --color=auto redis
  5. [root@localhost redis-7006]# kill 7768

可以看到集群连接不上节点 7005 ,节点 7002 角色由 slave 转换为 master

  1. [root@localhost redis-7001]# ./src/redis-trib.rb check 172.16.105.130:7005
  2. [ERR] Sorry, can't connect to node 172.16.105.130:7005
  3. [root@localhost redis-7001]# ./src/redis-trib.rb check 172.16.105.130:7006
  4. >>> Performing Cluster Check (using node 172.16.105.130:7006)
  5. S: 633b4c938e3773f90abc11261452d6638f31c7ed 172.16.105.130:7006
  6. slots: (0 slots) slave
  7. replicates 162cf6816ffdd75f22af63359a1da8a2eb7725a2
  8. S: e9ebec2d60087531517e08ae102d66e3d8db39eb 172.16.105.129:7004
  9. slots: (0 slots) slave
  10. replicates 00bee510e285e1951863d15e646a5fcccd10e28f
  11. M: 00bee510e285e1951863d15e646a5fcccd10e28f 172.16.105.128:7001
  12. slots:0-5460 (5461 slots) master
  13. 1 additional replica(s)
  14. M: c85ebc85fee43048b8135ac5b73c3337ef6b9dd0 172.16.105.128:7002
  15. slots:10923-16383 (5461 slots) master
  16. 0 additional replica(s)
  17. M: 162cf6816ffdd75f22af63359a1da8a2eb7725a2 172.16.105.129:7003
  18. slots:5461-10922 (5462 slots) master
  19. 1 additional replica(s)
  20. [OK] All nodes agree about slots configuration.
  21. >>> Check for open slots...
  22. >>> Check slots coverage...
  23. [OK] All 16384 slots covered.

参考

Redis Cluster Tutorial
Redis Cluster分片集群搭建
RVM安装指定版本Ruby

发表评论

表情:
评论列表 (有 0 条评论,535人围观)

还没有评论,来说两句吧...

相关阅读

    相关 redis部署

    一、概述     Redis3.0版本之后支持Cluster. 1.1、redis cluster的现状    目前redis支持的cluster特性:   1):节点

    相关 redis部署

    本节着重讲解redis集群部署,如未清楚单节点部署的请浏览本人的关于单节点部署的文章。 地址:[https://blog.csdn.net/wohiusdashi/artic