多個redis節點網絡互聯,數據共享
全部的節點都是一主一從(能夠是多個從),其中從不提供服務,僅做爲備用
不支持同時處理多個鍵(如mset/mget),由於redis須要把鍵均勻分佈在各個節點上,併發量很高的狀況下同時建立鍵值會下降性能並致使不可預測的行爲。
支持在線增長、刪除節點
客戶端能夠連任何一個主節點進行讀寫;
node
場景設置:
兩臺機器,分別開啓三個Redis服務(端口)
A機器上三個端口7000,7002,7004,所有爲主
B機器上三個端口7001,7003,7005,所有爲從
兩臺機器上都要編譯安裝redis,而後編輯並複製3個不一樣的redis.conf,分別設置不一樣的端口號、dir等參數,還須要增長cluster相關參數,而後分別啓動6個redis服務
具體redis配置文件你們到https://coding.net/u/aminglinux/p/yuanke_centos7/git/tree/master/21NOSQL下載或者查看linux
[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 7000 bind 192.168.60.11 daemonize yes pidfile /var/run/redis_7000.pid dir /data/redis_data/7000 cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 10100 appendonly 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 oO0OoO0OoO0Oo 18732:C 17 Oct 09:20:54.363 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=18732, just started 18732:C 17 Oct 09:20:54.364 # Configuration loaded [root@Dasoncheng ~]# ps aux |grep redis root 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 oO0OoO0OoO0Oo 23362:C 17 Oct 09:21:28.212 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=23362, just started 23362: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 oO0OoO0OoO0Oo 23367:C 17 Oct 09:21:31.739 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=23367, just started 23367:C 17 Oct 09:21:31.739 # Configuration loaded [root@Dasoncheng ~]# echo $? 0 [root@Dasoncheng ~]# ps aux |grep redis root 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}git
安裝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-deve cd /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/SOURCES wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS rpmbuild -bb rpmbuild/SPECS/ruby22x.spec yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm gem install redis cp /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包安裝文件!github
[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:7000 192.168.60.12:7001 192.168.60.11:7002 Adding replica 192.168.60.12:7003 to 192.168.60.11:7000 Adding replica 192.168.60.11:7004 to 192.168.60.12:7001 Adding replica 192.168.60.12:7005 to 192.168.60.11:7002 M: 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000 slots:0-5460 (5461 slots) master M: 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002 slots:10923-16383 (5461 slots) master S: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004 replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830d M: 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001 slots:5461-10922 (5462 slots) master S: 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003 replicates 1f39268503aeaf02e2a1d4b81a3d572f71fd389f S: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005 replicates 420058045cf5ecfb779f01978606fc25f9271c43 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 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 1f39268503aeaf02e2a1d4b81a3d572f71fd389f S: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004 slots: (0 slots) slave replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830d S: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005 slots: (0 slots) slave replicates 420058045cf5ecfb779f01978606fc25f9271c43 M: 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.rpmredis
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 123 OK 192.168.60.12:7001> set key2 123 ##操做被定向到60.11的7000端口 -> Redirected to slot [4998] located at 192.168.60.11:7000 OK 192.168.60.11:7000> set key3 123 OK 192.168.60.11:7000> set key4 123 -> Redirected to slot [13120] located at 192.168.60.11:7002 OK 192.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 1f39268503aeaf02e2a1d4b81a3d572f71fd389f S: 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004 slots: (0 slots) slave replicates 7e398f3003d9a2d1bbef32d01cc9d3c62760830d S: 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005 slots: (0 slots) slave replicates 420058045cf5ecfb779f01978606fc25f9271c43 M: 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 nodes 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508208564000 1 connected 0-5460 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 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 connected 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508208565002 2 connected 10923-16383 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508208562978 5 connected 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508208563996 4 connected 192.168.60.12:7001> 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:6 cluster_my_epoch:4 cluster_stats_messages_ping_sent:1284 cluster_stats_messages_pong_sent:1293 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:2582 cluster_stats_messages_ping_received:1293 cluster_stats_messages_pong_received:1289 cluster_stats_messages_received:2582 192.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信息 便可) OK 192.168.60.12:7001> cluster nodes 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508208878187 1 connected 0-5460 daa308972c148b6dc819ee5ba1a17654bdc1c788 192.168.60.12:7007@17007 master - 0 1508208879197 0 connected 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508208875000 4 connected 5461-10922 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508208877000 6 connected 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508208876164 2 connected 10923-16383 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508208877000 5 connected 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508208880210 4 connected 192.168.60.12:7001> cluster forget daa308972c148b6dc819ee5ba1a17654bdc1c788 OK ##cluster forget node_id移除某個節點 ##若是移除不了,看看是否爲主/主下有沒有從,將主設爲從便可移除 ##沒法移除正在登陸的節點,先退出 再刪除; 192.168.60.12:7001> cluster nodes 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 192.168.60.11:7000@17000 master - 0 1508209442000 1 connected 0-5460 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508209443000 4 connected 5461-10922 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508209444209 6 connected 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508209443000 2 connected 10923-16383 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508209444607 5 connected 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508209443600 4 connected [root@Dasoncheng ~]# redis-cli -c -h 192.168.60.12 -p 7001 192.168.60.12:7001> cluster saveconfig OK ##保存配置文件;具體保存在以下文件中! [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-5460 daa308972c148b6dc819ee5ba1a17654bdc1c788 192.168.60.12:7007@17007 master - 0 1508209726023 0 connected 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 192.168.60.12:7001@17001 myself,master - 0 1508209724000 4 connected 5461-10922 9f44617279ac675df4df29e54f3fd6f7555fa6bc 192.168.60.12:7005@17005 slave 420058045cf5ecfb779f01978606fc25f9271c43 0 1508209723000 6 connected 420058045cf5ecfb779f01978606fc25f9271c43 192.168.60.11:7002@17002 master - 0 1508209725000 2 connected 10923-16383 3933f911a2f8a8c7da25481189b7b234c24dee5b 192.168.60.12:7003@17003 slave 1f39268503aeaf02e2a1d4b81a3d572f71fd389f 0 1508209724998 5 connected 166c1180d683b847b69ed4517158aa0845dc6478 192.168.60.11:7004@17004 slave 7e398f3003d9a2d1bbef32d01cc9d3c62760830d 0 1508209727028 4 connected vars currentEpoch 6 lastVoteEpoch 0