Redis教程

redis-cluste

本文主要是介绍redis-cluste,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

环境: centos 7.9
ip: 192.168.138.21

Redis server v=4.0.8 

同步一台服务器上,指定不同的端口启动redis-server,redis.conf基础配置文件信息如下

# cat redis-7000.conf
port 7000
daemonize yes
logfile /applications/redis/redis-7000.log
dir /applications/redis/
dbfilename 7000-dump.rdb
cluster-enabled yes
# cluster-config-file nodes*.conf 名称要不一样
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
appendfilename "appendonly-7000.aof"

 

# 拷贝redis-7000.conf 至redis-7005.conf
[root@ansb01 redis]# cp redis-7000.conf redis-7001.conf
[root@ansb01 redis]# cp redis-7000.conf redis-7002.conf
[root@ansb01 redis]# cp redis-7000.conf redis-7003.conf
[root@ansb01 redis]# cp redis-7000.conf redis-7004.conf
[root@ansb01 redis]# cp redis-7000.conf redis-7005.conf

# 修改对应端口配置文件
[root@ansb01 redis]# sed -i 's/7000/7001/g' redis-7001.conf
[root@ansb01 redis]# sed -i 's/7000/7002/g' redis-7002.conf
[root@ansb01 redis]# sed -i 's/7000/7003/g' redis-7003.conf
[root@ansb01 redis]# sed -i 's/7000/7004/g' redis-7004.conf
[root@ansb01 redis]# sed -i 's/7000/7005/g' redis-7005.conf

分别启动不同端口服务,查看服务信息

# 查看redis
[root@ansb01 redis]# ps -ef|grep redis
root      5245     1  0 21:55 ?        00:00:01 ./bin/redis-server *:7000 [cluster]
root      5348     1  0 22:05 ?        00:00:00 ./bin/redis-server *:7001 [cluster]
root      5357     1  0 22:05 ?        00:00:00 ./bin/redis-server *:7002 [cluster]
root      5365     1  0 22:06 ?        00:00:00 ./bin/redis-server *:7003 [cluster]
root      5374     1  0 22:06 ?        00:00:00 ./bin/redis-server *:7004 [cluster]
root      5380     1  0 22:06 ?        00:00:00 ./bin/redis-server *:7005 [cluster]

 准备redis-trib.rb运行环境

 

wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz

yum -y install zlib-devel
tar xvf ruby-2.5.1.tar.gz

cd ruby-2.5.1/

./configure -prefix=/usr/local/ruby

make

make install

cd /usr/local/ruby/

cp bin/ruby /usr/local/bin

cp bin/gem /usr/local/bin

 

安装rubygem redis依赖

wget http://rubygems.org/downloads/redis-3.3.0.gem

gem install -l redis-3.3.0.gem

 

 redis-trib.rb支持的操作

 

   

[root@ansb01 src]# ./redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

  

支持的操作如下:

1. create:创建集群

2. check:检查集群

3. info:查看集群信息

4. fix:修复集群

5. reshard:在线迁移slot

6. rebalance:平衡集群节点slot数量

7. add-node:添加新节点

8. del-node:删除节点

9. set-timeout:设置节点的超时时间

10. call:在集群所有节点上执行命令

11. import:将外部redis数据导入集群

 

创建集群

 

如果您使用 Redis 5 或更高版本,这很容易实现,因为我们在 Redis Cluster 命令行实用程序的帮助下嵌入到 中redis-cli,可用于创建新集群、检查或重新分片现有集群等。

 

对于 Redis 版本 3 或 4,有一个redis-trib.rb非常相似的旧工具。您可以src在Redis源代码分发目录中找到它。您需要安装redisgem 才能运行redis-trib.

# 使用redis-trib.rb用于Redis的4或3型:
[root@ansb01 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: 5cf0519f5067742f75bc84d3d9f6c6d9521fe95e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 2f6aa651087e63d5e97a03b7cdd1917fe0010ffa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: c56b3b07bec108fe51aa68214b93b4b0a6999798 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 0ee1eac88e12ba1f0305f8adcb3f8212731a68cf 127.0.0.1:7003
   replicates 2f6aa651087e63d5e97a03b7cdd1917fe0010ffa
S: 5891169f86b44ac812fab99594d81bd9f8a3081c 127.0.0.1:7004
   replicates c56b3b07bec108fe51aa68214b93b4b0a6999798
S: 8fd12dd488bcd226202ffb0814f8c5d57b62ffb3 127.0.0.1:7005
   replicates 5cf0519f5067742f75bc84d3d9f6c6d9521fe95e
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: 5cf0519f5067742f75bc84d3d9f6c6d9521fe95e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5891169f86b44ac812fab99594d81bd9f8a3081c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates c56b3b07bec108fe51aa68214b93b4b0a6999798
M: 2f6aa651087e63d5e97a03b7cdd1917fe0010ffa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 8fd12dd488bcd226202ffb0814f8c5d57b62ffb3 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 5cf0519f5067742f75bc84d3d9f6c6d9521fe95e
M: c56b3b07bec108fe51aa68214b93b4b0a6999798 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 0ee1eac88e12ba1f0305f8adcb3f8212731a68cf 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 2f6aa651087e63d5e97a03b7cdd1917fe0010ffa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

  

# 查看集群信息
[root@ansb01 redis]# ./bin/redis-cli -p 7000 cluster info
cluster_state:ok                        # 状态为ok
cluster_slots_assigned:16384            # 标记slot共有16384    
cluster_slots_ok:16384                  # 已分配slot有16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6                   # 当前集群有6个节点        
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:576
cluster_stats_messages_pong_sent:603
cluster_stats_messages_sent:1179
cluster_stats_messages_ping_received:598
cluster_stats_messages_pong_received:576
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1179


# 查看当前集群节点信息
[root@ansb01 redis]# ./bin/redis-cli -p 7000 cluster nodes
5891169f86b44ac812fab99594d81bd9f8a3081c 127.0.0.1:7004@17004 slave c56b3b07bec108fe51aa68214b93b4b0a6999798 0 1631718314377 5 connected
2f6aa651087e63d5e97a03b7cdd1917fe0010ffa 127.0.0.1:7001@17001 master - 0 1631718314000 2 connected 5461-10922
8fd12dd488bcd226202ffb0814f8c5d57b62ffb3 127.0.0.1:7005@17005 slave 5cf0519f5067742f75bc84d3d9f6c6d9521fe95e 0 1631718314000 6 connected
c56b3b07bec108fe51aa68214b93b4b0a6999798 127.0.0.1:7002@17002 master - 0 1631718314000 3 connected 10923-16383
5cf0519f5067742f75bc84d3d9f6c6d9521fe95e 127.0.0.1:7000@17000 myself,master - 0 1631718315000 1 connected 0-5460
0ee1eac88e12ba1f0305f8adcb3f8212731a68cf 127.0.0.1:7003@17003 slave 2f6aa651087e63d5e97a03b7cdd1917fe0010ffa 0 1631718315399 4 connected

  

 

这篇关于redis-cluste的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!