沃梦达 / IT编程 / 数据库 / 正文

Redis之集群部署

部署① 环境准备准备三台机器(192.168.124.1、192.168.124.2、192.168.124.3),每台部署两个Redis实例192.168.124.1部署7001,7002端口实例的Redis 192.168.124.2部署7003,7004端口实例的Redis 192.168.124.3部署...

部署

① 环境准备

准备三台机器(192.168.124.1、192.168.124.2、192.168.124.3),每台部署两个Redis实例

  • 192.168.124.1部署7001,7002端口实例的Redis

  •  192.168.124.2部署7003,7004端口实例的Redis

  •  192.168.124.3部署7005,7006端口实例的Redis

②修改配置文件

修改每个实例的redis.conf配置文件

  • port 7001
  • bind 0.0.0.0(允许所有ip客户端的访问)
  • requirepass 123456(设置密码,也可以不设置)
  • daemonize yes
  • pidfile /opt/redis-4.0.11/7001/redis_7001.pid

③安装ruby

yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

④启动实例

与单点的redis的启动方式一样,分别启动六个redis的实例

 ./redis-server ../redis.conf &

⑤开放端口

  • redis客户端连接的端口

      192.168.124.1的7001,7002;192.168.124.2的7003,7004;192.168.124.3的7005,7006端口

  • 集群中实例相互通信的内部总线端口(redis客户端连接的端口 + 10000)

      192.168.124.1的17001,17002;192.168.124.2的17003,17004;192.168.124.3的17005,17006端口

⑥集群配置

在任意一个redis的src目录执行

./redis-trib.rb create --replicas 1 192.168.37.133:7001 192.168.37.133:7002 192.168.37.134:7003 192.168.37.134:7004 192.168.37.135:7005  192.168.37.135:7006

结果

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.37.133:7001
192.168.37.134:7003
192.168.37.135:7005
Adding replica 192.168.37.134:7004 to 192.168.37.133:7001
Adding replica 192.168.37.135:7006 to 192.168.37.134:7003
Adding replica 192.168.37.133:7002 to 192.168.37.135:7005
M: febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0 192.168.37.133:7001
   slots:0-5460 (5461 slots) master
S: 540f891d97dc0b0c4868488dfe1bc7de2813747a 192.168.37.133:7002
   replicates 0e022381183dbe47e4ee32e6a10abb19066c82ff
M: 0eae91b1a0b4db3496d15d928f6a32eca50f7cdd 192.168.37.134:7003
   slots:5461-10922 (5462 slots) master
S: 8e9f9cf9980ed5c0b59c1b7c3801645e3a13ff5a 192.168.37.134:7004
   replicates febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0
M: 0e022381183dbe47e4ee32e6a10abb19066c82ff 192.168.37.135:7005
   slots:10923-16383 (5461 slots) master
S: 541d946fb7c1143b4365bec3c668dc76611ca901 192.168.37.135:7006
   replicates 0eae91b1a0b4db3496d15d928f6a32eca50f7cdd
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.37.133:7001)
M: febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0 192.168.37.133:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 0eae91b1a0b4db3496d15d928f6a32eca50f7cdd 192.168.37.134:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0e022381183dbe47e4ee32e6a10abb19066c82ff 192.168.37.135:7005
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 8e9f9cf9980ed5c0b59c1b7c3801645e3a13ff5a 192.168.37.134:7004
   slots: (0 slots) slave
   replicates febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0
S: 541d946fb7c1143b4365bec3c668dc76611ca901 192.168.37.135:7006
   slots: (0 slots) slave
   replicates 0eae91b1a0b4db3496d15d928f6a32eca50f7cdd
S: 540f891d97dc0b0c4868488dfe1bc7de2813747a 192.168.37.133:7002
   slots: (0 slots) slave
   replicates 0e022381183dbe47e4ee32e6a10abb19066c82ff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

⑦验证

登录任意一台redis的客户端,执行

cluster info

显示

cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:202
cluster_stats_messages_pong_sent:218
cluster_stats_messages_sent:420
cluster_stats_messages_ping_received:213
cluster_stats_messages_pong_received:202
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:420

执行

cluster nodes

显示

0eae91b1a0b4db3496d15d928f6a32eca50f7cdd 192.168.37.134:7003@17003 master - 0 1538728029000 3 connected 5461-10922
0e022381183dbe47e4ee32e6a10abb19066c82ff 192.168.37.135:7005@17005 master - 0 1538728028666 5 connected 10923-16383
8e9f9cf9980ed5c0b59c1b7c3801645e3a13ff5a 192.168.37.134:7004@17004 slave febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0 0 1538728031692 4 connected
541d946fb7c1143b4365bec3c668dc76611ca901 192.168.37.135:7006@17006 slave 0eae91b1a0b4db3496d15d928f6a32eca50f7cdd 0 1538728030685 6 connected
540f891d97dc0b0c4868488dfe1bc7de2813747a 192.168.37.133:7002@17002 slave 0e022381183dbe47e4ee32e6a10abb19066c82ff 0 1538728030000 5 connected
febe3f02bfb4800b5f9f0d0b57d675dc0fb882a0 192.168.37.133:7001@17001 myself,master - 0 1538728030000 1 connected 0-5460

可以看到

7001是master,7004是其slave

7003是master,7006是其slave

7005是master,7002是其slave

使用Redis desktop manager 进行连接

出现集群的lcon

本文标题为:Redis之集群部署

上一篇: redis