1.概要
- 呼唤集群
- 数据分布
- 搭建集群
- 集群伸缩
- 客户端路由
- 集群原理
- 开发运维常见问题
2.呼唤集群
3.数据分布
4.节点取余
5.一致性哈希
6.虚拟槽分区
7.基本架构
8.安装
- 配置Redis.conf
port 7000 daemonize yes pidfile /var/run/redis-7000.pid dbfilename "dump-7000.rdb" logfile "7000.log" dir "/opt/soft/redis/redis/data/" cluster-enabled yes cluster-config-file nodes-7000.conf cluster-require-full-coverage no
- 搭建集群-meet
[root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7001 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7002 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7003 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7004 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7005 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster nodes c09e9379e37ee282219a07399bfb0dcbb933159a 127.0.0.1:7001@17001 master - 0 1567952635000 1 connected df740a3f64985bb4645c1f9fff276d29ad1bcb25 127.0.0.1:7000@17000 myself,master - 0 1567952636000 0 connected 2e6827954a8dd1b7a1455e9aec057cb89fd56fa4 127.0.0.1:7003@17003 master - 0 1567952636853 3 connected 0fee682eb0247d98bcffe694fe745af194f708dd 127.0.0.1:7005@17005 master - 0 1567952636000 5 connected 97969f534d03b6c7c346df035e35b676c79e2d51 127.0.0.1:7004@17004 master - 0 1567952637000 4 connected 80602baaf52498f27be91a8b9169dc78de6e6e07 127.0.0.1:7002@17002 master - 0 1567952637855 2 connected [root@master redis-cluster-config]# redis-cli -p 7000 cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 cluster_current_epoch:5 cluster_my_epoch:0 cluster_stats_messages_ping_sent:208 cluster_stats_messages_pong_sent:68 cluster_stats_messages_meet_sent:6 cluster_stats_messages_sent:282 cluster_stats_messages_ping_received:68 cluster_stats_messages_pong_received:64 cluster_stats_messages_received:132
- 分配槽
vim addslots.sh
start=$1 end=$2 port=$3 for slot in `seq ${start} ${end}` do echo "port:${port},slot:${slot}" redis-cli -p ${port} cluster addslots ${slot} done
[root@master redis-cluster-config]# sh addslots.sh 0 5461 7000 [root@master redis-cluster-config]# sh addslots.sh 5462 10922 7001 [root@master redis-cluster-config]# sh addslots.sh 10923 16383 7002 [root@master redis-cluster-config]# redis-cli -p 7000 cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:5 cluster_my_epoch:0 cluster_stats_messages_ping_sent:948 cluster_stats_messages_pong_sent:830 cluster_stats_messages_meet_sent:6 cluster_stats_messages_sent:1784 cluster_stats_messages_ping_received:830 cluster_stats_messages_pong_received:804 cluster_stats_messages_received:1634
- 搭建集群-主从分配
[root@master redis-cluster-config]# redis-cli -p 7004 cluster replicate c09e9379e37ee282219a07399bfb0dcbb933159a OK [root@master redis-cluster-config]# redis-cli -p 7005 cluster replicate 80602baaf52498f27be91a8b9169dc78de6e6e07 OK [root@master redis-cluster-config]# redis-cli -p 7000 cluster nodes c09e9379e37ee282219a07399bfb0dcbb933159a 127.0.0.1:7001@17001 master - 0 1567953967031 1 connected 5462-10922 df740a3f64985bb4645c1f9fff276d29ad1bcb25 127.0.0.1:7000@17000 myself,master - 0 1567953964000 0 connected 0-5461 2e6827954a8dd1b7a1455e9aec057cb89fd56fa4 127.0.0.1:7003@17003 slave df740a3f64985bb4645c1f9fff276d29ad1bcb25 0 1567953966027 3 connected 0fee682eb0247d98bcffe694fe745af194f708dd 127.0.0.1:7005@17005 slave 80602baaf52498f27be91a8b9169dc78de6e6e07 0 1567953965024 5 connected 97969f534d03b6c7c346df035e35b676c79e2d51 127.0.0.1:7004@17004 slave c09e9379e37ee282219a07399bfb0dcbb933159a 0 1567953968035 4 connected 80602baaf52498f27be91a8b9169dc78de6e6e07 127.0.0.1:7002@17002 master - 0 1567953966000 2 connected 10923-16383 [root@master redis-cluster-config]# redis-cli -p 7000 cluster slots 1) 1) (integer) 5462 2) (integer) 10922 3) 1) "127.0.0.1" 2) (integer) 7001 3) "c09e9379e37ee282219a07399bfb0dcbb933159a" 4) 1) "127.0.0.1" 2) (integer) 7004 3) "97969f534d03b6c7c346df035e35b676c79e2d51" 2) 1) (integer) 0 2) (integer) 5461 3) 1) "127.0.0.1" 2) (integer) 7000 3) "df740a3f64985bb4645c1f9fff276d29ad1bcb25" 4) 1) "127.0.0.1" 2) (integer) 7003 3) "2e6827954a8dd1b7a1455e9aec057cb89fd56fa4" 3) 1) (integer) 10923 2) (integer) 16383 3) 1) "127.0.0.1" 2) (integer) 7002 3) "80602baaf52498f27be91a8b9169dc78de6e6e07" 4) 1) "127.0.0.1" 2) (integer) 7005 3) "0fee682eb0247d98bcffe694fe745af194f708dd" [root@master redis-cluster-config]# redis-cli -c -p 7000 127.0.0.1:7000> set hello world OK 127.0.0.1:7000> exit
9.Ruby
[root@master ~]# gem install redis-4.1.2.gem
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
解决问题
[root@master ~]# yum install zlib-devel
[root@master ~]# cd /root/ruby-2.6.4/ext/zlib
[root@master zlib]# ruby extconf.rb
[root@master zlib]# vim Makefile
# 将 $(top_srcdir)/include/ruby.h 改为 ../../include/ruby.h
[root@master zlib]# make && make install
[root@master zlib]# yum install openssl-devel
[root@master zlib]# cd ../openssl/
[root@master openssl]# ruby extconf.rb
[root@master openssl]# vim Makefile
# 将 $(top_srcdir)/include/ruby.h 改为 ../../include/ruby.h
[root@master openssl]# make && make install
[root@master ~]# gem install redis-4.1.2.gem
Successfully installed redis-4.1.2
Parsing documentation for redis-4.1.2
Installing ri documentation for redis-4.1.2
Done installing documentation for redis after 1 seconds
1 gem installed
[root@master src]# ./redis-trib.rb
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster info 127.0.0.1:7000
To get help about all subcommands, type:
redis-cli --cluster help
[root@master ~]# redis-cli –cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
10.原生命令与redis-trib.rb对比
emmm,Redis5.0 把redis-trib.rb集成到了redis-cli中