Redis cluster集群模式

这里我么多配置几个节点服务,因为没有那么多服务器,所有我么在没台服务器上设置三个端口号的节点

分别是10.211.55.14:7001,7002,7003

10.211.55.16:7001,7002,7003

10.211.55.17:7001,7002,7003

 

分别创建各自的配置文件,明明为:7001.conf,7002.conf,7003.conf

存在  /home/parallels/program/cluster/7001 ~ 7003目录下

配置文件

bind 10.211.55.14

port 7001

dir /home/parallels/program/cluster/7001

cluster-enabled yes

cluster-config-file nodes-7001.conf

cluster-node-timeout 5000

appendonly yes

daemonize yes

protected-mode no

pidfile /var/run/redis_7001.pid

 

根据需要配置,这里采用最简单的配置

其他的配置可以参考

redis基本配置

redis主从同步

配置一台服务后完成后

 

将配置文件同步到另外2台服务器

scp -r cluster parallels@10.211.55.17:/home/parallels/program/

 

分别启动各自服务

[parallels@10 cluster]$ redis-server 7001/7001.conf 

[parallels@10 cluster]$ redis-server 7002/7002.conf 

[parallels@10 cluster]$ redis-server 7003/7003.conf 

 

用redis-cli命令创建集群

redis-cli –cluster  create 10.211.55.14:7001 10.211.55.14:7002 10.211.55.14:7003 10.211.55.16:7001 10.211.55.16:7002 10.211.55.16:7003 10.211.55.17:7001 10.211.55.17:7002 10.211.55.17:7003 –cluster-replicas 2

cluster-replicas 后面跟的数字表示一个master对应2个slaves

[parallels@10 cluster]$  redis-cli --cluster  create 10.211.55.14:7001 10.211.55.14:7002 10.211.55.14:7003 10.211.55.16:7001 10.211.55.16:7002 10.211.55.16:7003 10.211.55.17:7001 10.211.55.17:7002 10.211.55.17:7003 --cluster-replicas 2

 

>>> Performing hash slots allocation on 9 nodes...

Master[0] -> Slots 0 - 5460

Master[1] -> Slots 5461 - 10922

Master[2] -> Slots 10923 - 16383

Adding replica 10.211.55.16:7002 to 10.211.55.14:7001

Adding replica 10.211.55.17:7002 to 10.211.55.14:7001

Adding replica 10.211.55.14:7003 to 10.211.55.16:7001

Adding replica 10.211.55.17:7003 to 10.211.55.16:7001

Adding replica 10.211.55.16:7003 to 10.211.55.17:7001

Adding replica 10.211.55.14:7002 to 10.211.55.17:7001

M: 6567274836d4f0f513b8056ab8fd96330c646f73 10.211.55.14:7001

   slots:[0-5460] (5461 slots) master

S: 10fd6aab73045bb6f50207e8f2fc540b36655861 10.211.55.14:7002

   replicates d536089e83f87008c895ca54518df33fa03d601f

S: 3176ecbe4dbcd614a7c0ec9114480c5e97efc402 10.211.55.14:7003

   replicates c7fa8e3190d93da15b0cc65af00d6a13ad9c2495

M: c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 10.211.55.16:7001

   slots:[5461-10922] (5462 slots) master

S: 861230a9d46c8fcd8aa0e9949e72fccce1413d68 10.211.55.16:7002

   replicates 6567274836d4f0f513b8056ab8fd96330c646f73

S: 99253d39f503823b9c48b9f4c77f2b37736e01e7 10.211.55.16:7003

   replicates d536089e83f87008c895ca54518df33fa03d601f

M: d536089e83f87008c895ca54518df33fa03d601f 10.211.55.17:7001

   slots:[10923-16383] (5461 slots) master

S: 1e1a706e9c1bd9c9108e68cd8af06fc8343bfb3f 10.211.55.17:7002

   replicates 6567274836d4f0f513b8056ab8fd96330c646f73

S: 84a266cdc977f6cb4839ee2c9e85ecb4f6499421 10.211.55.17:7003

   replicates c7fa8e3190d93da15b0cc65af00d6a13ad9c2495

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 10.211.55.14:7001)

M: 6567274836d4f0f513b8056ab8fd96330c646f73 10.211.55.14:7001

   slots:[0-5460] (5461 slots) master

   2 additional replica(s)

S: 1e1a706e9c1bd9c9108e68cd8af06fc8343bfb3f 10.211.55.17:7002

   slots: (0 slots) slave

   replicates 6567274836d4f0f513b8056ab8fd96330c646f73

S: 861230a9d46c8fcd8aa0e9949e72fccce1413d68 10.211.55.16:7002

   slots: (0 slots) slave

   replicates 6567274836d4f0f513b8056ab8fd96330c646f73

S: 3176ecbe4dbcd614a7c0ec9114480c5e97efc402 10.211.55.14:7003

   slots: (0 slots) slave

   replicates c7fa8e3190d93da15b0cc65af00d6a13ad9c2495

M: d536089e83f87008c895ca54518df33fa03d601f 10.211.55.17:7001

   slots:[10923-16383] (5461 slots) master

   2 additional replica(s)

M: c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 10.211.55.16:7001

   slots:[5461-10922] (5462 slots) master

   2 additional replica(s)

S: 84a266cdc977f6cb4839ee2c9e85ecb4f6499421 10.211.55.17:7003

   slots: (0 slots) slave

   replicates c7fa8e3190d93da15b0cc65af00d6a13ad9c2495

S: 10fd6aab73045bb6f50207e8f2fc540b36655861 10.211.55.14:7002

   slots: (0 slots) slave

   replicates d536089e83f87008c895ca54518df33fa03d601f

S: 99253d39f503823b9c48b9f4c77f2b37736e01e7 10.211.55.16:7003

   slots: (0 slots) slave

   replicates d536089e83f87008c895ca54518df33fa03d601f

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

登录任意节点,使用cluster info查看群集cluster状态

这里我们登录14:7001节点

 

10.211.55.14:7001> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:9

cluster_size:3

cluster_current_epoch:9

cluster_my_epoch:1

cluster_stats_messages_ping_sent:750

cluster_stats_messages_pong_sent:746

cluster_stats_messages_sent:1496

cluster_stats_messages_ping_received:738

cluster_stats_messages_pong_received:750

cluster_stats_messages_meet_received:8

cluster_stats_messages_received:1496

 

info查看状态:

# Replication

role:master

connected_slaves:2

slave0:ip=10.211.55.16,port=7002,state=online,offset=448,lag=0

slave1:ip=10.211.55.17,port=7002,state=online,offset=448,lag=1

master_replid:4bdb3785126437d99fc32a90c1891f5aeace6716

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:448

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:448

 

刚好登陆的节点是master,有2个salves,16:7002,17:7002

 

查看群集各个节点

10.211.55.16:7002> cluster nodes

99253d39f503823b9c48b9f4c77f2b37736e01e7 10.211.55.16:7003@17003 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853197000 7 connected

1e1a706e9c1bd9c9108e68cd8af06fc8343bfb3f 10.211.55.17:7002@17002 slave 6567274836d4f0f513b8056ab8fd96330c646f73 0 1600853198576 1 connected

10fd6aab73045bb6f50207e8f2fc540b36655861 10.211.55.14:7002@17002 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853198000 7 connected

c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 10.211.55.16:7001@17001 master - 0 1600853197000 4 connected 5461-10922

84a266cdc977f6cb4839ee2c9e85ecb4f6499421 10.211.55.17:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853198576 4 connected

3176ecbe4dbcd614a7c0ec9114480c5e97efc402 10.211.55.14:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853198576 4 connected

861230a9d46c8fcd8aa0e9949e72fccce1413d68 10.211.55.16:7002@17002 myself,slave 6567274836d4f0f513b8056ab8fd96330c646f73 0 1600853198000 1 connected

d536089e83f87008c895ca54518df33fa03d601f 10.211.55.17:7001@17001 master - 0 1600853197000 7 connected 10923-16383

6567274836d4f0f513b8056ab8fd96330c646f73 10.211.55.14:7001@17001 master - 0 1600853197570 1 connected 0-5460

 

登录16:7002节点查看info信息

# Replication

role:slave

master_host:10.211.55.14

master_port:7001

master_link_status:up

master_last_io_seconds_ago:6

master_sync_in_progress:0

slave_repl_offset:574

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:4bdb3785126437d99fc32a90c1891f5aeace6716

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:574

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:574

显示角色是slave,master是14:7001

 

新增master节点可以使用: add-node <ip>:<port>

 

测试下群集的高可用

尝试断开14:7001master节点

10.211.55.16:7002> cluster nodes

99253d39f503823b9c48b9f4c77f2b37736e01e7 10.211.55.16:7003@17003 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853911000 7 connected

1e1a706e9c1bd9c9108e68cd8af06fc8343bfb3f 10.211.55.17:7002@17002 slave 861230a9d46c8fcd8aa0e9949e72fccce1413d68 0 1600853910000 10 connected

10fd6aab73045bb6f50207e8f2fc540b36655861 10.211.55.14:7002@17002 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853911532 7 connected

c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 10.211.55.16:7001@17001 master - 0 1600853911000 4 connected 5461-10922

84a266cdc977f6cb4839ee2c9e85ecb4f6499421 10.211.55.17:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853911000 4 connected

3176ecbe4dbcd614a7c0ec9114480c5e97efc402 10.211.55.14:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853912036 4 connected

861230a9d46c8fcd8aa0e9949e72fccce1413d68 10.211.55.16:7002@17002 myself,master - 0 1600853910000 10 connected 0-5460

d536089e83f87008c895ca54518df33fa03d601f 10.211.55.17:7001@17001 master - 0 1600853911000 7 connected 10923-16383

6567274836d4f0f513b8056ab8fd96330c646f73 10.211.55.14:7001@17001 master,fail - 1600853882556 1600853880000 1 disconnected

14:7001master断开了。16:7002变成了master了

 

再次启动14:7001

10.211.55.16:7002> cluster nodes

99253d39f503823b9c48b9f4c77f2b37736e01e7 10.211.55.16:7003@17003 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853979000 7 connected

1e1a706e9c1bd9c9108e68cd8af06fc8343bfb3f 10.211.55.17:7002@17002 slave 861230a9d46c8fcd8aa0e9949e72fccce1413d68 0 1600853979000 10 connected

10fd6aab73045bb6f50207e8f2fc540b36655861 10.211.55.14:7002@17002 slave d536089e83f87008c895ca54518df33fa03d601f 0 1600853980042 7 connected

c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 10.211.55.16:7001@17001 master - 0 1600853979942 4 connected 5461-10922

84a266cdc977f6cb4839ee2c9e85ecb4f6499421 10.211.55.17:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853980042 4 connected

3176ecbe4dbcd614a7c0ec9114480c5e97efc402 10.211.55.14:7003@17003 slave c7fa8e3190d93da15b0cc65af00d6a13ad9c2495 0 1600853980545 4 connected

861230a9d46c8fcd8aa0e9949e72fccce1413d68 10.211.55.16:7002@17002 myself,master - 0 1600853979000 10 connected 0-5460

d536089e83f87008c895ca54518df33fa03d601f 10.211.55.17:7001@17001 master - 0 1600853979540 7 connected 10923-16383

6567274836d4f0f513b8056ab8fd96330c646f73 10.211.55.14:7001@17001 slave 861230a9d46c8fcd8aa0e9949e72fccce1413d68 0 1600853980545 10 connected

 

14:7001变成了slave了

通过  redis-cli -h 10.211.55.16 -p 7001 -c 登录

读取数据

10.211.55.17:7001> get wang

-> Redirected to slot [2919] located at 10.211.55.16:7002

"vvv"

10.211.55.16:7002> 

1 对 “Redis cluster集群模式搭建”的想法;

发表评论

电子邮件地址不会被公开。