博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
21.22 redis集群介绍
阅读量:5874 次
发布时间:2019-06-19

本文共 13620 字,大约阅读时间需要 45 分钟。

hot3.png

21.22 redis集群介绍

多个redis节点网络互联,数据共享

所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
支持在线增加、删除节点
客户端可以连任何一个主节点进行读写;
mark

21.23 redis集群搭建配置(上)

场景设置:

两台机器,分别开启三个Redis服务(端口)
A机器上三个端口7000,7002,7004,全部为主
B机器上三个端口7001,7003,7005,全部为从
两台机器上都要编译安装redis,然后编辑并复制3个不同的redis.conf,分别设置不同的端口号、dir等参数,还需要增加cluster相关参数,然后分别启动6个redis服务
具体redis配置文件大家到

[root@Dasoncheng ~]# ll /etc/redis*     ##主上面创建三个配置文件,并创建相应文件夹等-rw-r--r-- 1 root root 57815 Oct 14 09:06 /etc/redis2.conf-rw-r--r-- 1 root root   199 Oct 17 09:00 /etc/redis_7000.conf-rw-r--r-- 1 root root   199 Oct 17 09:01 /etc/redis_7002.conf-rw-r--r-- 1 root root   199 Oct 17 09:03 /etc/redis_7004.conf-rw-r--r-- 1 root root 57790 Oct 14 08:46 /etc/redis.conf[root@Dasoncheng ~]# cat /etc/redis_7000.conf port 7000bind 192.168.60.11daemonize yespidfile /var/run/redis_7000.piddir /data/redis_data/7000cluster-enabled yescluster-config-file nodes_7000.confcluster-node-timeout 10100appendonly yes[root@Dasoncheng ~]# mkdir -p /data/redis_data/{7000,7002,7004}[root@Dasoncheng ~]# redis-server /etc/redis_7000.conf 18732:C 17 Oct 09:20:54.363 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo18732:C 17 Oct 09:20:54.363 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=18732, just started18732:C 17 Oct 09:20:54.364 # Configuration loaded[root@Dasoncheng ~]# ps aux |grep redisroot      18733  0.2  0.2 142280  2524 ?        Ssl  09:20   0:00 redis-server 192.168.60.11:7000 [cluster]root      19915  0.0  0.0 112660   968 pts/0    S+   09:21   0:00 grep --color=auto redis[root@Dasoncheng ~]# redis-server /etc/redis_7002.conf 23362:C 17 Oct 09:21:28.212 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo23362:C 17 Oct 09:21:28.212 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=23362, just started23362:C 17 Oct 09:21:28.212 # Configuration loaded[root@Dasoncheng ~]# redis-server /etc/redis_7004.conf 23367:C 17 Oct 09:21:31.738 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo23367:C 17 Oct 09:21:31.739 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=23367, just started23367:C 17 Oct 09:21:31.739 # Configuration loaded[root@Dasoncheng ~]# echo $?0[root@Dasoncheng ~]# ps aux |grep redisroot      18733  0.1  0.2 142280  2524 ?        Ssl  09:20   0:00 redis-server 192.168.60.11:7000 [cluster]root      23363  0.2  0.2 142280  2524 ?        Ssl  09:21   0:00 redis-server 192.168.60.11:7002 [cluster]root      23368  0.1  0.2 142280  2528 ?        Ssl  09:21   0:00 redis-server 192.168.60.11:7004 [cluster]root      23385  0.0  0.0 112660   968 pts/0    S+   09:29   0:00 grep --color=auto redis

小提示:创建多个文件夹另类方法!

mkdir -p /data/redis_data/{7000,7002,7004}

21.24 redis集群搭建配置(下)

安装ruby2.2 (只需要一台机器上运行) yum -y groupinstall "Development Tools"yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devecd /root/mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCESwget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECSrpmbuild -bb rpmbuild/SPECS/ruby22x.specyum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpmgem install rediscp /usr/local/src/redis-4.0.1/src/redis-trib.rb  /usr/bin/redis-trib.rb create --replicas 1 192.168.133.130:7000 192.168.133.130:7002 192.168.133.130:7004 192.168.133.132:7001 192.168.133.132:7003 192.168.133.132:7005

安装高版本ruby:

yum里面的版本太低,我们这里下载源码包,将其制作成rpm包安装文件!

[root@Dasoncheng ~]# yum -y groupinstall "Development Tools"##安装拓展工具;[root@Dasoncheng ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve[root@Dasoncheng ~]# cd /root/[root@Dasoncheng ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}[root@Dasoncheng ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES[root@Dasoncheng ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS[root@Dasoncheng ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec[root@Dasoncheng ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm[root@Dasoncheng ~]# gem install redis   ##安装redis的工具trib;[root@Dasoncheng ~]# cp /usr/local/src/redis-4.0.1/src/redis-trib.rb  /usr/bin/  ##拷贝到PATH里面,方便命令直接执行;[root@Dasoncheng ~]# redis-trib.rb create --replicas 1 192.168.60.11:7000 192.168.60.11:7002 192.168.60.11:7004 192.168.60.12:7001 192.168.60.12:7003 192.168.60.12:7005>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:192.168.60.11:7000192.168.60.12:7001192.168.60.11:7002Adding replica 192.168.60.12:7003 to 192.168.60.11:7000Adding replica 192.168.60.11:7004 to 192.168.60.12:7001Adding replica 192.168.60.12:7005 to 192.168.60.11:7002M: 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000   slots:0-5460 (5461 slots) masterM: 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002   slots:10923-16383 (5461 slots) masterS: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004   replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830dM: 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001   slots:5461-10922 (5462 slots) masterS: 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003   replicates 1f39268503aeaf02e2a1d4b81a3d572f71fd389fS: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005   replicates 420058045cf5ecfb779f01978606fc25f9271c43Can 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 clusterWaiting for the cluster to join.....>>> Performing Cluster Check (using node 192.168.60.11:7000)M: 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000   slots:0-5460 (5461 slots) master   1 additional replica(s)M: 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002   slots:10923-16383 (5461 slots) master   1 additional replica(s)S: 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003   slots: (0 slots) slave   replicates 1f39268503aeaf02e2a1d4b81a3d572f71fd389fS: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004   slots: (0 slots) slave   replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830dS: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005   slots: (0 slots) slave   replicates 420058045cf5ecfb779f01978606fc25f9271c43M: 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001   slots:5461-10922 (5462 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.##这里我要说一下:  ##生产环境一般不会一台机器多个端口,因为这样没有了集群的意义(一台机器宕机 端口都不能用);##我们可以看到,主从已经分配了(其实是按端口顺序来的,如果需要指定60.11所有redis都为主的话,我们可以修改端口达到目的)

小提示:yum安装本地rpm包,直接解决依赖关系!

yum localinstall -y custom.rpm

21.25 redis集群操作

redis-cli -c -h 192.168.133.130 -p 7000//-c说明以集群的方式登录 任意一个节点都可以创建key,或者查看key(演示) redis-trib.rb check  192.168.133.130:7000//检测集群状态 cluster nodes//列出节点 cluster info//查看集群信息 cluster meet ip port //添加节点 cluster forget node_id //移除某个节点 cluster replicate node_id//将当前节点设置为指定节点的从 cluster saveconfig//保存配置文件
[root@Dasoncheng ~]# redis-cli -c -h 192.168.60.12 -p 7001##-c是以集群的方式登录,如果没有-c则以单独redis登录;不会有集群效果192.168.60.12:7001> set key1 123OK192.168.60.12:7001> set key2 123  ##操作被定向到60.11的7000端口-> Redirected to slot [4998] located at 192.168.60.11:7000OK192.168.60.11:7000> set key3 123OK192.168.60.11:7000> set key4 123-> Redirected to slot [13120] located at 192.168.60.11:7002OK192.168.60.11:7002> get key2  ##查询也是从60.11的7000里面查询;-> Redirected to slot [4998] located at 192.168.60.11:7000"123"192.168.60.11:7000> get key4-> Redirected to slot [13120] located at 192.168.60.11:7002"123"192.168.60.11:7002> quit[root@Dasoncheng ~]# redis-trib.rb check  192.168.60.11:7000  ##检测集群状态;>>> Performing Cluster Check (using node 192.168.60.11:7000)M: 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000   slots:0-5460 (5461 slots) master   1 additional replica(s)M: 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002   slots:10923-16383 (5461 slots) master   1 additional replica(s)S: 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003   slots: (0 slots) slave   replicates 1f39268503aeaf02e2a1d4b81a3d572f71fd389fS: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004   slots: (0 slots) slave   replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830dS: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005   slots: (0 slots) slave   replicates 420058045cf5ecfb779f01978606fc25f9271c43M: 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001   slots:5461-10922 (5462 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.[root@Dasoncheng ~]# redis-cli -c -h 192.168.60.12 -p 7001  ##列出节点;192.168.60.12:7001> cluster nodes1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508208564000 1 connected 0-54607e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508208561000 4 connected 5461-10922  ##这一行说明了myself;即本机登录9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508208563000 6 connected420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508208565002 2 connected 10923-163833933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508208562978 5 connected166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508208563996 4 connected  192.168.60.12:7001> cluster info  ##查看集群信息;cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:4cluster_stats_messages_ping_sent:1284cluster_stats_messages_pong_sent:1293cluster_stats_messages_meet_sent:5cluster_stats_messages_sent:2582cluster_stats_messages_ping_received:1293cluster_stats_messages_pong_received:1289cluster_stats_messages_received:2582192.168.60.12:7001> cluster meet 192.168.60.12 7007  ##添加节点;下面我们cluster nodes可以看到该节点为主(若再添加一个新节点,也是master那么多和弄slave从呢?)下面有法子设置slave从;##cluster replicate node_id//将当前节点设置为指定节点的从(登录一个想要成为master的节点,执行命令cluster replicate node_id--节点的node信息 即可)OK192.168.60.12:7001> cluster nodes1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508208878187 1 connected 0-5460daa308972c148b6dc819ee5ba1a17654bdc1c788 192.168.60.12:7007@17007 master - 0 1508208879197 0 connected7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508208875000 4 connected 5461-109229f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508208877000 6 connected420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508208876164 2 connected 10923-163833933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508208877000 5 connected166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508208880210 4 connected192.168.60.12:7001> cluster forget daa308972c148b6dc819ee5ba1a17654bdc1c788OK##cluster forget node_id移除某个节点##如果移除不了,看看是否为主/主下有没有从,将主设为从即可移除##无法移除正在登录的节点,先退出  再删除;192.168.60.12:7001> cluster nodes1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508209442000 1 connected 0-54607e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508209443000 4 connected 5461-109229f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508209444209 6 connected420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508209443000 2 connected 10923-163833933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508209444607 5 connected166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508209443600 4 connected[root@Dasoncheng ~]# redis-cli -c -h 192.168.60.12 -p 7001192.168.60.12:7001> cluster saveconfigOK##保存配置文件;具体保存在如下文件中![root@localhost ~]# ls /data/redis_data/7001/appendonly.aof  dump.rdb  nodes_7001.conf[root@localhost ~]# cat /data/redis_data/7001/nodes_7001.conf 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508209726000 1 connected 0-5460daa308972c148b6dc819ee5ba1a17654bdc1c788 192.168.60.12:7007@17007 master - 0 1508209726023 0 connected7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508209724000 4 connected 5461-109229f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508209723000 6 connected420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508209725000 2 connected 10923-163833933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508209724998 5 connected166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508209727028 4 connectedvars currentEpoch 6 lastVoteEpoch 0

转载于:https://my.oschina.net/u/3651233/blog/1551756

你可能感兴趣的文章
上海办理房产税变更
查看>>
每天一个linux命令(52):scp命令
查看>>
CMOS Sensor Interface(CSI)
查看>>
linq中的contains条件
查看>>
HDU 5590 ZYB's Biology 水题
查看>>
memcached 分布式聚类算法
查看>>
言未及之而言,谓之躁;言及之而不言,谓之隐;未见颜色而言,谓之瞽(gǔ)...
查看>>
MYSQL查询一周内的数据(最近7天的)
查看>>
Redis的缓存策略和主键失效机制
查看>>
禁止body滚动允许div滚动防微信露底
查看>>
Xtreme8.0 - Kabloom dp
查看>>
jquery css3问卷答题卡翻页动画效果
查看>>
MDK5.00中*** error 65: access violation at 0xFFFFFFFC : no 'write' permission的一种解决方法
查看>>
Android 集成支付宝支付详解
查看>>
SQL分布式查询、跨数据库查询
查看>>
C#------连接SQLServer和MySQL字符串
查看>>
Arcgis Licensemanager 不能启动的原因之一(转载)
查看>>
(原)Android在子线程用handler发送的消息,主线程是怎么loop到的?
查看>>
$digest already in progress 解决办法——续
查看>>
虚拟机 centos设置代理上网
查看>>