redis系列(五):搭建redis-cluster集群
1、为什么要用redis-cluster
a、并发要求
redis官方声称可以达到10万每秒,但是如果业务需要每秒100万条呢?
b、数据量太大
一台服务器的内存正常是16-256G,如果业务需要500G内存怎么办?
2、搭建redis-cluster
针对上述问题,redis-cluster集群就提供了很好的解决方案。
(1)、先准备环境,开启多个redis实例
[root@localhost redis_conf]# ls redis-7000.conf redis-7002.conf redis-7004.conf redis-7001.conf redis-7003.conf redis-7005.conf [root@localhost redis_conf]#
暂时准备了6个配置文件,
daemonize yes port 7000 logfile ./data/7000/redis.log #日志存放位置 dir ./data/7000 # 数据存放位置 dbfilename dbmp.rdb # 数据文件名称 cluster-enabled yes # 开启集群模式 cluster-config-file nodes-7000.conf # 集群内部的配置文件 cluster-require-full-coverage no # redis cluster需要16384个slot都正常的时候才能对外提供服务,换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no
上面的配置为redis-7000.conf的配置文件内容,其他配置文件内容相同,只是将7000全部改成对应的端口。
可以使用如下命令快速生成:
sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
通过sed命令将redis-7000.conf中的7000修改成7001,然后写入redis-7001.conf文件中
每个节点仅仅是端口的不同。
注意:还要确保配置中的日志以及数据存放文件夹存在。
比如我的:
[root@localhost redis_conf]# mkdir -p data/{7000,7001,7002,7003,7004,7005} [root@localhost redis_conf]# tree . ├── data │ ├── 7000 │ ├── 7001 │ ├── 7002 │ ├── 7003 │ ├── 7004 │ └── 7005 ├── redis-7000.conf ├── redis-7001.conf ├── redis-7002.conf ├── redis-7003.conf ├── redis-7004.conf └── redis-7005.conf 7 directories, 6 files
(2)、运行redis实例
[root@localhost redis_conf]# redis-server redis-7000.conf [root@localhost redis_conf]# redis-server redis-7001.conf [root@localhost redis_conf]# redis-server redis-7002.conf [root@localhost redis_conf]# redis-server redis-7003.conf [root@localhost redis_conf]# redis-server redis-7004.conf [root@localhost redis_conf]# redis-server redis-7005.conf
查看是否已经启动
[root@localhost redis_conf]# ps -ef | grep redis root 3733 1 0 08:13 ? 00:00:00 redis-server *:7000 [cluster] root 3763 1 0 08:13 ? 00:00:00 redis-server *:7001 [cluster] root 3768 1 0 08:13 ? 00:00:00 redis-server *:7002 [cluster] root 3773 1 0 08:13 ? 00:00:00 redis-server *:7003 [cluster] root 3779 1 0 08:13 ? 00:00:00 redis-server *:7004 [cluster] root 3784 1 0 08:13 ? 00:00:00 redis-server *:7005 [cluster] root 3816 3514 0 08:14 pts/0 00:00:00 grep --color=auto redis [root@localhost redis_conf]#
此时集群还用不了,可以登录redis查看
[root@localhost redis_conf]# redis-cli -p 7000 127.0.0.1:7000> set name felixi (error) CLUSTERDOWN Hash slot not served 127.0.0.1:7000>
(3)、创建redis-cluster
a、准备ruby环境
下载,编译,安装ruby (ruby官网地址)
1、下载(个人用的当前的最新版本2.6.0) wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.0.tar.gz 2、解压,安装 tar -zxvf ruby-2.6.0.tar.gz cd ruby-2.6.0 ./configure --prefix=/opt/ruby/ make && make install 3、添加环境变量 export PATH=/opt/ruby/bin/:$PATH # 将这句添加到./bashrc和/etc/profile文件末尾。 source ./bashrc /etc/profile # 加载一下
b、查看是否已经安装
[root@localhost ~]# gem -v 3.0.1 [root@localhost ~]#
c、下载安装ruby操作redis的模块包
[root@localhost ~]# gem install redis Successfully installed redis-4.1.0 Parsing documentation for redis-4.1.0 Done installing documentation for redis after 1 seconds 1 gem installed [root@localhost ~]#
7、启动集群
我的redis版本是5.0.2,使用如下方式启动
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
其他旧版本可能需要如下命令:注意(redis-trib.rb可能找不到,可以通过find / -name redis-trib.rb来查找)
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
8、出现如下说明启动成功
[root@localhost ~]# redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 127.0.0.1:7003 to 127.0.0.1:7000 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:7000 slots:[0-5460] (5461 slots) master M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:7001 slots:[5461-10922] (5462 slots) master M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:7002 slots:[10923-16383] (5461 slots) master S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:7003 replicates 511bc46a1da42e3964ce41f48234076bd5743baf S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:7004 replicates bb029209525fb9aa2eeaa8c27182065ca1b29457 S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:7005 replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 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 127.0.0.1:7000) M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:7000 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:7004 slots: (0 slots) slave replicates bb029209525fb9aa2eeaa8c27182065ca1b29457 S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:7003 slots: (0 slots) slave replicates 511bc46a1da42e3964ce41f48234076bd5743baf S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:7005 slots: (0 slots) slave replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@localhost ~]#
9、查看主从状态
redis-cli -p 7000 info replication
结果如下:
[root@localhost ~]# redis-cli -p 7002 info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=7005,state=online,offset=140,lag=0 master_replid:2b20fa4941542dc58d9fc2c32fe6f3dbb6cce72b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:154 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:154 [root@localhost ~]# redis-cli -p 7003 info replication # Replication role:slave master_host:127.0.0.1 master_port:7000 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_repl_offset:154 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:ad11aa24aa797d942a54550ee77ab0e185e9d92c master_replid2:0000000000000000000000000000000000000000 master_repl_offset:154 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:154 [root@localhost ~]#
9、向redis集群写入数据,查看数据流向
redis-cli -p 7000 -c #这里会将key自动的重定向,放到某一个节点的slot槽位中
效果如下:
[root@localhost ~]# redis-cli -p 7000 -c 127.0.0.1:7000> keys * (empty list or set) 127.0.0.1:7000> 127.0.0.1:7000> set name felix -> Redirected to slot [5798] located at 127.0.0.1:7001 OK 127.0.0.1:7001> keys * 1) "name" 127.0.0.1:7001>
至此,集群就搭建好了