redis之cluster(集群)
搭建redis cluster
1. 准备节点
2. 节点间的通信
3. 分配槽位给节点
redis-cluster架构
多个服务端,负责读写,彼此通信,redis指定了16384个槽。
多匹马儿,负责运输数据,马儿分配16384个槽位,管理数据。
ruby的脚本自动就把分配槽位这事做了
启动所有redis-cluster节点
我准备了6个节点, 配置文件如下, 除了端口不一样, 其他都一样
配置文件解释:
port 7000 daemonize yes dir "/opt/redis/data" logfile "7000.log" dbfilename "dump-7000.rdb" cluster-enabled yes #开启集群模式 cluster-config-file nodes-7000.conf #集群内部的配置文件 cluster-require-full-coverage no #redis cluster需要16384个slot都正常的时候才能对外提供服务,换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no
启动所有的节点
此时的redis虽然已经启动了, 但是还不能使用, 因为现在运行在集群模式下, 集群还没有配置完了.....
准备ruby环境
开启redis-cluster
下载ruby安装包
#下载ruby wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz #安装ruby tar -xvf ruby-2.3.1.tar.gz ./configure --prefix=/opt/ruby/ make && make install #准备一个ruby命令 #准备一个gem软件包管理命令 #拷贝ruby命令到path下/usr/local/ruby cp /opt/ruby/bin/ruby /usr/local/bin/ cp bin/gem /usr/local/bin
安装gem安装redis模块, 类似于pip install redis
wget http://rubygems.org/downloads/redis-3.3.0.gem gem install -l redis-3.3.0.gem #查看gem有哪些包 gem list -- check redis gem
安装redis-trib.rb命令
在redis/src目录下
添加到环境变量
[root@web01 05:15 /opt/redis-4.0.10/src]# cp redis-trib.rb /usr/local/bin/
一键开启redis-cluster
#每个主节点,有一个从节点,代表--replicas 1 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 #集群自动分配主从关系 7000、7001、7002为 7003、7004、7005 主动关系
[root@web01 05:15 /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 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Adding replica 127.0.0.1:7004 to 127.0.0.1:7000 Adding replica 127.0.0.1:7005 to 127.0.0.1:7001 Adding replica 127.0.0.1:7003 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: fb345360da148c168f590237e0c5557815654c34 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 23875b1844fe1af2f5287877298abaf7c8eeaea3 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: 39e2b69102c684c0a4973395acb37120f60db22c 127.0.0.1:7002 slots:10923-16383 (5461 slots) master S: 390c6bfbd8fe0a94c37b9b41c45ec0a033c58e85 127.0.0.1:7003 replicates 39e2b69102c684c0a4973395acb37120f60db22c S: 115c8166b48a6b2835a5fd89b3bcb021687e64d5 127.0.0.1:7004 replicates fb345360da148c168f590237e0c5557815654c34 S: 28b7664378d6aea262bc5a9ee890d4a857bf397e 127.0.0.1:7005 replicates 23875b1844fe1af2f5287877298abaf7c8eeaea3 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: fb345360da148c168f590237e0c5557815654c34 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 28b7664378d6aea262bc5a9ee890d4a857bf397e 127.0.0.1:7005 slots: (0 slots) slave replicates 23875b1844fe1af2f5287877298abaf7c8eeaea3 M: 23875b1844fe1af2f5287877298abaf7c8eeaea3 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 115c8166b48a6b2835a5fd89b3bcb021687e64d5 127.0.0.1:7004 slots: (0 slots) slave replicates fb345360da148c168f590237e0c5557815654c34 S: 390c6bfbd8fe0a94c37b9b41c45ec0a033c58e85 127.0.0.1:7003 slots: (0 slots) slave replicates 39e2b69102c684c0a4973395acb37120f60db22c M: 39e2b69102c684c0a4973395acb37120f60db22c 127.0.0.1:7002 slots:10923-16383 (5461 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.
查看集群状态的命令
redis-cli -p 7000 cluster info redis-cli -p 7000 cluster nodes #等同于查看nodes-7000.conf文件节点信息 集群主节点状态 redis-cli -p 7000 cluster nodes | grep master 集群从节点状态 redis-cli -p 7000 cluster nodes | grep slave
测试集群
测试写入集群数据,登录集群必须使用redis-cli -c -p 7000必须加上-c参数
redis集群部署完成