Redis集群搭建


Reis集群搭建,我们搭建8台服务,一主一从模式。

Redis版本5.0

伪集群部署,8个服务部署在一台服务器,占用不同的端口号。


我们创建8个文件夹,每个放置其端口对应的配置文件


我们先启动六个服务,配置为3主3从模式

 

1.修改redis.conf文件。(拿7000端口举例,其它7个配置文件修改对应端口号即可)

查看redis.conf

port 7000 #端口号
pidfile /var/run/redis_7000.pid #pid文件
dbfilename dump7000.rdb #指定本地数据库存放目录
dir /root/myredis/redis7000 #redis数据 + log + pid文件存储的默认目录
appendfilename "appendonly7000.aof" #aof文件名
------------------------------------------------------------------------------------------
#放开集群相关配置
cluster-enabled yes 
cluster-config-file nodes-7000.conf #集群配置文件
cluster-node-timeout 15000
cluster-replica-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
cluster-replica-no-failover no

2.启动服务,先启动6台

[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7001/redis.conf 
15563:C 07 Apr 2020 17:11:57.167 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15563:C 07 Apr 2020 17:11:57.167 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15563, just started
15563:C 07 Apr 2020 17:11:57.167 # Configuration loaded
[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7002/redis.conf 
15568:C 07 Apr 2020 17:12:04.004 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15568:C 07 Apr 2020 17:12:04.004 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15568, just started
15568:C 07 Apr 2020 17:12:04.004 # Configuration loaded
[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7003/redis.conf 
15573:C 07 Apr 2020 17:12:09.332 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15573:C 07 Apr 2020 17:12:09.332 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15573, just started
15573:C 07 Apr 2020 17:12:09.332 # Configuration loaded
[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7004/redis.conf 
15578:C 07 Apr 2020 17:12:15.757 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15578:C 07 Apr 2020 17:12:15.757 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15578, just started
15578:C 07 Apr 2020 17:12:15.757 # Configuration loaded
[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7005/redis.conf 
15583:C 07 Apr 2020 17:12:19.350 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15583:C 07 Apr 2020 17:12:19.350 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15583, just started
15583:C 07 Apr 2020 17:12:19.350 # Configuration loaded

查看服务状态

[root@localhost redis-5.0.5]# ps -ef|grep redis
root      15559      1  0 17:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7000 [cluster]
root      15564      1  0 17:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7001 [cluster]
root      15569      1  0 17:12 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7002 [cluster]
root      15574      1  0 17:12 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7003 [cluster]
root      15579      1  0 17:12 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7004 [cluster]
root      15584      1  0 17:12 ?        00:00:00 /usr/local/bin/redis-server 192.168.21.3:7005 [cluster]

([cluster]说明以集群方式运行)

3.分配插槽,以及主从配置( --cluster-replicas 1 [这里的1是从节点数/主节点数的比例][3主3从])

[root@localhost redis-5.0.5]# /usr/local/bin/redis-cli --cluster create 192.168.21.3:7000 192.168.21.3:7001 192.168.21.3:7002 192.168.21.3:7003 192.168.21.3:7004 192.168.21.3:7005 --cluster-replicas 1

4.成功执行set命令(这里注意 加-c)

[root@localhost /]# /usr/local/bin/redis-cli -h 192.168.21.3 -p 7000 -c
192.168.21.3:7000> set k1 v1
-> Redirected to slot [12706] located at 192.168.21.3:7002
OK

 


 注:查看集群内节点信息

192.168.21.3:7000> cluster nodes
7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003@17003 slave 508d1f4c7af0a395690a48a4a9953815e6b63812 0 1586251695000 4 connected
3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005@17005 slave 8aa2622ea39cb2052ba378ffe66df641bb00b31d 0 1586251699000 6 connected
59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004@17004 slave d93b319b50616a129e3979539c0ec7c103c0468f 0 1586251700626 5 connected
d93b319b50616a129e3979539c0ec7c103c0468f 192.168.21.3:7002@17002 master - 0 1586251700000 3 connected 10923-16383
8aa2622ea39cb2052ba378ffe66df641bb00b31d 192.168.21.3:7000@17000 myself,master - 0 1586251698000 1 connected 0-5460
508d1f4c7af0a395690a48a4a9953815e6b63812 192.168.21.3:7001@17001 master - 0 1586251698608 2 connected 5461-10922

 集群帮助命令

[root@localhost redis-5.0.5]# /usr/local/bin/redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help       

新增两台服务

1.将剩下两台服务启动

[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7006/redis.conf 
15634:C 07 Apr 2020 18:25:17.324 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15634:C 07 Apr 2020 18:25:17.324 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15634, just started
15634:C 07 Apr 2020 18:25:17.324 # Configuration loaded
[root@localhost redis-5.0.5]# /usr/local/bin/redis-server /root/myredis/redis7007/redis.conf 
15639:C 07 Apr 2020 18:25:21.891 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15639:C 07 Apr 2020 18:25:21.891 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=15639, just started
15639:C 07 Apr 2020 18:25:21.891 # Configuration loaded

 2.将7006加入集群并设置为master节点

[root@localhost /]# /usr/local/bin/redis-cli --cluster add-node 192.168.21.3:7006 192.168.21.3:7000
>>> Adding node 192.168.21.3:7006 to cluster 192.168.21.3:7000
>>> Performing Cluster Check (using node 192.168.21.3:7000)
M: 8aa2622ea39cb2052ba378ffe66df641bb00b31d 192.168.21.3:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003
   slots: (0 slots) slave
   replicates 508d1f4c7af0a395690a48a4a9953815e6b63812
S: 3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005
   slots: (0 slots) slave
   replicates 8aa2622ea39cb2052ba378ffe66df641bb00b31d
S: 59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004
   slots: (0 slots) slave
   replicates d93b319b50616a129e3979539c0ec7c103c0468f
M: d93b319b50616a129e3979539c0ec7c103c0468f 192.168.21.3:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 508d1f4c7af0a395690a48a4a9953815e6b63812 192.168.21.3:7001
   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.
>>> Send CLUSTER MEET to node 192.168.21.3:7006 to make it join the cluster.
[OK] New node added correctly.

3.将7007加入集群并设置为从节点

1.需要先找到7006节点的ID

[root@localhost /]# /usr/local/bin/redis-cli -h 192.168.21.3 -p 7000
192.168.21.3:7000> cluster nodes
7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003@17003 slave 508d1f4c7af0a395690a48a4a9953815e6b63812 0 1586255603196 4 connected
3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005@17005 slave 8aa2622ea39cb2052ba378ffe66df641bb00b31d 0 1586255602000 6 connected
59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004@17004 slave d93b319b50616a129e3979539c0ec7c103c0468f 0 1586255603000 5 connected
4bc9e9939ede8239f8e53527527916d3cf0b393d 192.168.21.3:7006@17006 master - 0 1586255605211 0 connected

2.7007加入集群并设置为7006的从节点

[root@localhost /]# /usr/local/bin/redis-cli --cluster add-node 192.168.21.3:7007 192.168.21.3:7000 --cluster-slave --cluster-master-id 4bc9e9939ede8239f8e53527527916d3cf0b393d
>>> Adding node 192.168.21.3:7007 to cluster 192.168.21.3:7000
>>> Performing Cluster Check (using node 192.168.21.3:7000)
M: 8aa2622ea39cb2052ba378ffe66df641bb00b31d 192.168.21.3:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003
   slots: (0 slots) slave
   replicates 508d1f4c7af0a395690a48a4a9953815e6b63812
S: 3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005
   slots: (0 slots) slave
   replicates 8aa2622ea39cb2052ba378ffe66df641bb00b31d
S: 59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004
   slots: (0 slots) slave
   replicates d93b319b50616a129e3979539c0ec7c103c0468f
M: 4bc9e9939ede8239f8e53527527916d3cf0b393d 192.168.21.3:7006
   slots: (0 slots) master
M: d93b319b50616a129e3979539c0ec7c103c0468f 192.168.21.3:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 508d1f4c7af0a395690a48a4a9953815e6b63812 192.168.21.3:7001
   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.
>>> Send CLUSTER MEET to node 192.168.21.3:7007 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.21.3:7006.
[OK] New node added correctly.

查看集群中节点,有8个了

192.168.21.3:7000> cluster nodes
7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003@17003 slave 508d1f4c7af0a395690a48a4a9953815e6b63812 0 1586255942000 4 connected
3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005@17005 slave 8aa2622ea39cb2052ba378ffe66df641bb00b31d 0 1586255944826 6 connected
59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004@17004 slave d93b319b50616a129e3979539c0ec7c103c0468f 0 1586255943000 5 connected
4bc9e9939ede8239f8e53527527916d3cf0b393d 192.168.21.3:7006@17006 master - 0 1586255940795 0 connected
d93b319b50616a129e3979539c0ec7c103c0468f 192.168.21.3:7002@17002 master - 0 1586255941000 3 connected 10923-16383
8aa2622ea39cb2052ba378ffe66df641bb00b31d 192.168.21.3:7000@17000 myself,master - 0 1586255942000 1 connected 0-5460
0cc2b08fb51dcfb9373dfa7c2919eab12b2dfa8a 192.168.21.3:7007@17007 slave 4bc9e9939ede8239f8e53527527916d3cf0b393d 0 1586255942812 0 connected
508d1f4c7af0a395690a48a4a9953815e6b63812 192.168.21.3:7001@17001 master - 0 1586255943819 2 connected 5461-10922

4.重新分配集群槽位

[root@localhost /]# /usr/local/bin/redis-cli --cluster reshard 192.168.21.3:7000

 分配多少个槽位?

How many slots do you want to move (from 1 to 16384)? 4096

 分配给哪个节点?(选择7006的节点ID)

What is the receiving node ID? 4bc9e9939ede8239f8e53527527916d3cf0b393d

 分配完成后,查看集群节点信息

192.168.21.3:7000> cluster nodes
7676b8930bc7f04ffb56f425cc0090c0445e4a58 192.168.21.3:7003@17003 slave 508d1f4c7af0a395690a48a4a9953815e6b63812 0 1586256527217 4 connected
3d512efae957b73879a09118cb43229f42762f69 192.168.21.3:7005@17005 slave 8aa2622ea39cb2052ba378ffe66df641bb00b31d 0 1586256525203 6 connected
59ab0b13274df67271d0448c2a732f398d866271 192.168.21.3:7004@17004 slave d93b319b50616a129e3979539c0ec7c103c0468f 0 1586256525000 5 connected
4bc9e9939ede8239f8e53527527916d3cf0b393d 192.168.21.3:7006@17006 master - 0 1586256524000 8 connected 0-1364 5461-6826 10923-12287
d93b319b50616a129e3979539c0ec7c103c0468f 192.168.21.3:7002@17002 master - 0 1586256522000 3 connected 12288-16383
8aa2622ea39cb2052ba378ffe66df641bb00b31d 192.168.21.3:7000@17000 myself,master - 0 1586256518000 1 connected 1365-5460
0cc2b08fb51dcfb9373dfa7c2919eab12b2dfa8a 192.168.21.3:7007@17007 slave 4bc9e9939ede8239f8e53527527916d3cf0b393d 0 1586256526210 8 connected
508d1f4c7af0a395690a48a4a9953815e6b63812 192.168.21.3:7001@17001 master - 0 1586256524000 2 connected 6827-10922

 完成!

  • 作者:低调做个路人 (扫码联系作者)
  • 发表时间:2020-04-07 17:52:16
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 评论

    demo
    222