日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

NoSQL(二):创建、管理集群

發布時間:2025/3/21 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NoSQL(二):创建、管理集群 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建集群

集群環境

拓撲結構

IP規劃

工作原理


工作原理

  • 無論有多少臺redis服務器,一共都會有0-16383共16384個槽
  • slot的作用:用來確定存儲變量時,變量存儲在哪一臺redis服務器上的
  • 存取數據的過程:變量名與內置的crc16算法做一個hash計算,得到的數字再與16384進行取余,根據余數的結果確定在哪一臺服務器節點
  • 0-5460
  • 5461-10922
  • 10923-16383
  • 能夠存多少變量取決于本機的內存的大小

創建集群

  • 環境準備
=========================================1.確保每一臺redis都是空的,由于前次試驗在host51上操作,所以先將host51還原至空的狀態,一定要保證redis是空的狀態!!![root@host51 ~]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped [root@host51 ~]# vim /etc/redis/6379.conf #將第501行注釋掉,不使用密碼登錄501 #requirepass 123456 [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 192.168.4.51:6351> exit####################### 如果此時重啟報錯,可能是pid沖突,可以進行如下操作 [root@host51 ~]# /etc/init.d/redis_6379 start /var/run/redis_6379.pid exists, process is already running or crashed [root@host51 ~]# rm -rf /var/run/redis_6379.pid [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... #######################******************************2.接下來在host52-56主機分別準備好redis服務器,我們可以寫一些簡單的不能再簡單的腳本,以host52主機為例[root@host52 ~]# cd /opt [root@host52 opt]# for i in 53 54 55 56 > do > scp /opt/redis-4.0.8.tar.gz root@192.168.4.$i:/opt > done[root@host52 opt]# vim redis.sh #!/bin/bash yum -y install gcc #下載依賴包 tar -zxvf redis-4.0.8.tar.gz #解壓縮 cd redis-4.0.8/ make && make install #編譯安裝./utils/install_server.sh [root@host52 opt]# for i in 52 53 54 55 56 >do >scp /opt/redis.sh root@192.168.4.$i:/opt >done[root@host52 opt]# bash redis.sh [root@host52 opt]#ss -nutlp | grep 6379 [root@host52 opt]#/etc/init.d/redis_6379 stop #停止服務 [root@host52 opt]#vim +70 /etc/redis/6379.conf #修改IP地址70 bind 192.168.4.52 [root@host52 opt]#vim +93 /etc/redis/6379.conf #將端口號改為635293 port 6352 [root@host52 opt]#/etc/init.d/redis_6379 start #起服務 [root@host52 opt]#redis-cli -p 6352 -h 192.168.4.52 ***** 檢查準備工作: [root@host51 ~]# ss -nutlp | grep 6351 tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1841,fd=6)) [root@host52 opt]# ss -nutlp | grep 6352 tcp LISTEN 0 128 127.0.0.1:6352 *:* users:(("redis-server",pid=4753,fd=6)) [root@host53 opt]# ss -nutlp | grep 6353 tcp LISTEN 0 128 127.0.0.1:6353 *:* users:(("redis-server",pid=4721,fd=6)) [root@host54 opt]# ss -nutlp | grep 6354 tcp LISTEN 0 128 127.0.0.1:6354 *:* users:(("redis-server",pid=4722,fd=6)) [root@host55 opt]# ss -nutlp | grep 6355 tcp LISTEN 0 128 127.0.0.1:6355 *:* users:(("redis-server",pid=4749,fd=6)) [root@host56 opt]# ss -nutlp | grep 6356 tcp LISTEN 0 128 127.0.0.1:6356 *:* users:(("redis-server",pid=4710,fd=6))每臺redis服務器都必須是空的

redis-trib腳本

  • 用法:
  • [root@host57 ~]# redis-trib.rb <arguments…>
命令描述
create創建集群
check檢查集群
info查看集群信息
reshard重新分片
del-node刪除主機
add-node --slave添加slave主機
add-node添加master主機
rebalance平均分配hash slots
  • 創建集群
===============================================1.配置管理主機host57,也可以部署在任意一臺redis服務器上 [root@host57 opt]# rpm -q ruby 未安裝軟件包 ruby [root@host57 opt]# which gem /usr/bin/which: no gem in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) [root@host57 opt]# yum -y install rubygems ruby [root@host57 opt]# lsredis-4.0.8.tar.gz redis-3.2.1.gem [root@host57 opt]# gem install redis-3.2.1.gem Successfully installed redis-3.2.1 Parsing documentation for redis-3.2.1 Installing ri documentation for redis-3.2.1 1 gem installed[root@host57 opt]# tar -zvxf redis-4.0.8.tar.gz [root@host57 opt]# cd redis-4.0.8/ [root@host57 redis-4.0.8]# ls 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests BUGS deps MANIFESTO runtest sentinel.conf utils CONTRIBUTING INSTALL README.md runtest-cluster src [root@host57 redis-4.0.8]# ls src/*.rb #ruby腳本 src/redis-trib.rb [root@host57 redis-4.0.8]# echo $PATH #查看當前變量 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@host57 redis-4.0.8]# mkdir /root/bin #創建命令檢索目錄 [root@host57 redis-4.0.8]# cp src/redis-trib.rb /root/bin #創建管理集群腳本 [root@host57 redis-4.0.8]# chmod +x /root/bin/redis-trib.rb [root@host57 redis-4.0.8]# redis-trib.rb help #查看命令幫助 [root@host57 redis-4.0.8]# redis-trib.rb help Usage: redis-trib <command> <options> <arguments ...>create host1:port1 ... hostN:portN--replicas <arg>check host:portinfo host:portfix 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_idset-timeout host:port millisecondscall host:port command arg arg .. argimport host:port--from <arg>--copy--replacehelp (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.*************************************2.開啟集群,以host51為例,每臺redis集群都要做 [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 192.168.4.51:6351> cluster info ERR This instance has cluster support disabled #此時會報錯,因為沒有配置集群功能[root@host51 ~]# vim +815 /etc/redis/6379.conf 815 cluster-enabled yes #啟用集群功能823 cluster-config-file nodes-6379.conf #存儲集群信息的配置文件829 cluster-node-timeout 5000 #集群節點的通信超時時間[root@host51 ~]# rm -rf /var/lib/redis/6379/* #清空數據 [root@host51 ~]# vim +43 /etc/init.d/redis_6379 $CLIEXEC -h 192.168.4.51 -p 6351 shutdown :wq [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server..[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 192.168.4.51:6351> cluster info #查看集群信息,此時沒有將其他主機加入集群,所以查看集群狀態是fail cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:0 cluster_current_epoch:0 cluster_my_epoch:0 cluster_stats_messages_sent:0 cluster_stats_messages_received:0 [root@host51 ~]# netstat -nutlp | grep redis-server tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1 tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1 #16351為集群通信端口,默認端口服務+10000--------------------------------------------------------------- 或者也可以寫個腳本 [root@host51 ~]# vim redis2.sh #!/bin/bash sed -i '815s/# //' /etc/redis/6379.conf sed -i '823s/# //' /etc/redis/6379.conf sed -i '829s/# //' /etc/redis/6379.conf sed -i '829s/15000/5000/' /etc/redis/6379.conf redis-cli -h 192.168.4.$1 -p 63$1 shutdown /etc/init.d/redis_6379 start netstat -antulp | grep redis-server ----------------------------------------------------------------- ######### 如果查不到pid號,可以進行如下操作 [root@host51 ~]# ls /var/run/ auditd.pid dbus initramfs mysqld setrans tuned chrony dmeventd-client lock netreport sshd.pid udev chronyd.pid dmeventd-server log NetworkManager sudo user console ebtables.lock lvm plymouth syslogd.pid utmp crond.pid faillock lvmetad.pid redis_6379.pid systemd vmware cron.reboot firewalld mount sepermit tmpfiles.d xtables.lock[root@host51 ~]# netstat -nutlp | grep 6351 #查找pid號 tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1 tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1 [root@host51 ~]# ls /proc | grep 1964 #pid號文件在這里 1964 或者先停止redis服務,再重新啟動 [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351########****************************************3.在管理主機57創建集群(記得關防火墻)[root@host57 ~]# redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356 #replicas 1,定義從服務器個數,給每臺主服務器配置一臺從服務器,加入后面寫3,則要準備12臺redis服務器才可以 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: #選舉出三個主服務器 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 Adding replica 192.168.4.55:6355 to 192.168.4.51:6351 Adding replica 192.168.4.56:6356 to 192.168.4.52:6352 Adding replica 192.168.4.54:6354 to 192.168.4.53:6353 M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351 #M代表主服務器slots:0-5460 (5461 slots) master M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922 (5462 slots) master M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383 (5461 slots) master S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354 #S代表從服務器,replicates代表4.54的主服務器是以ce822為結尾的主服務器replicates 7747411ab79e16a5493e66d4a46515bd110ce822 S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356replicates 23b46dfa5997798b02837b365122d0315a48a84e 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.4.51:6351) M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460 (5461 slots) master1 additional replica(s) S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots: (0 slots) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383 (5461 slots) master1 additional replica(s) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922 (5462 slots) master1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #共占用了16384個hash槽,從服務器不占用槽,主服務器寫入數據,從服務器自動同步********************************************* 4.在任意一臺redis服務器本機,查看集群信息 [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 192.168.4.51:6351> cluster info #查看集群信息 cluster_state:ok #代表51本機已經在集群中 cluster_slots_assigned:16384 cluster_slots_ok: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:487 cluster_stats_messages_pong_sent:587 cluster_stats_messages_sent:1074 cluster_stats_messages_ping_received:582 cluster_stats_messages_pong_received:487 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1074 查看集群中的節點信息成員列表 192.168.4.51:6351> cluster nodes #查看集群節點信息,帶myself字樣是本機自己 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890996432 4 connected 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890997000 6 connected b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890998000 5 connected 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890998449 3 connected 10923-16383 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890998000 1 connected 0-5460 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890997442 2 connected 5461-10922[root@host51 ~]# cat /var/lib/redis/6379/nodes-6379.conf #存儲集群信息的配置文件,在Redis中查看成員列表調用的就是該文件 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890552000 4 connected 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890552000 6 connected b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890551442 5 connected 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890552550 3 connected 10923-16383 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890551000 1 connected 0-5460 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890552450 2 connected 5461-10922 vars currentEpoch 6 lastVoteEpoch 0在管理主機上查看集群統計信息 [root@host57 ~]# redis-trib.rb info 192.168.4.51:6351 192.168.4.51:6351 (25991ad6...) -> 0 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 0 keys | 5461 slots | 1 slaves. 192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average.在管理主機上看集群的詳細信息[root@host57 ~]# redis-trib.rb check 192.168.4.51:6351 >>> Performing Cluster Check (using node 192.168.4.51:6351) M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460 (5461 slots) master1 additional replica(s) S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots: (0 slots) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383 (5461 slots) master1 additional replica(s) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922 (5462 slots) master1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 如果主服務器壞了,那么對應的從服務器就會自動變成主 *****************************************************還原操作 在每一臺redis服務器上作如下操作: 1)停止redis服務 2)清空數據庫目錄 rm -rf /var/lib/redis/6379/* 3)檢查集群功能是否能啟用 4)啟動redis服務 5)查看端口,進程 6)在管理主機上創建集群 *****************************************************5.訪問集群 在客戶端可以連接集群中任意一臺redis服務器,也可以在redis服務器本機訪問 在任意一臺redis服務器上寫數據,數據都會存入相應的hash槽內,這就是分片式存儲 [root@host51 ~]# redis-cli -c -h 192.168.4.56 -p 6356 #在51主機訪問56主機的redis服務 192.168.4.56:6356> keys * (empty list or set) 192.168.4.56:6356> set a 456 #在56服務器存入變量a,發現數據存到了53的服務器上,因為redis高可用集群會自動尋找hash值對應的槽,然后放入對應的服務器中 -> Redirected to slot [15495] located at 192.168.4.53:6353 OK 192.168.4.53:6353> keys * #此時就會發現在53服務器上出現剛剛存入的變量,而根據redis集群信息查看,54主機是53主機的從服務器,所以此時,54主機也應該會有該數據 1) "a" 192.168.4.53:6353> exit [root@host51 ~]# redis-cli -c -h 192.168.4.54 -p 6354 #登錄到54的服務器 192.168.4.54:6354> keys * #查看,確有剛剛存入的新數據 1) "a" 192.168.4.54:6354> set b 784 #再次賦新的變量,會對應存入相應的hash槽 -> Redirected to slot [3300] located at 192.168.4.51:6351 OK 192.168.4.51:6351> exit [root@host51 ~]# redis-cli -c -h 192.168.4.55 -p 6355 192.168.4.55:6355> keys * #55主機是51主機的從服務器,此時存入51主機的數據會自動同步至55主機 1) "b" 192.168.4.55:6355> set x 23 #存數據時不能mset存多個,因為會找不到hash值對應的槽位 -> Redirected to slot [16287] located at 192.168.4.53:6353 OK 192.168.4.53:6353> keys * 1) "a" 2) "x" 192.168.4.53:6353> get b #此時,如果取一個不屬于當前主機的數據,也一舉可以取到 -> Redirected to slot [3300] located at 192.168.4.51:6351 "784" 192.168.4.51:6351>

管理集群

  • 測試管理集群功能
1.故障切換測試 先查看當前集群中的主服務器 [root@host57 ~]# redis-trib.rb info 192.168.4.51:6351 192.168.4.51:6351 (25991ad6...) -> 1 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 2 keys | 5461 slots | 1 slaves. 192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves. [OK] 3 keys in 3 masters. 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb check 192.168.4.51:6351 >>> Performing Cluster Check (using node 192.168.4.51:6351) M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460 (5461 slots) master1 additional replica(s) S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383 (5461 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922 (5462 slots) master1 additional replica(s) S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots: (0 slots) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.-----------------------------------------------------------------------停止master主機的redis服務,master宕機后對應的slave自動被選舉為master; [root@host51 ~]# ss -nutlp | grep 6351 tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=1099,fd=8)) tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1099,fd=6))[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown #把51主機的服務停掉,根據集群詳細信息知道,55主機是51主機的從服務器,那么此時51主機掛掉,55主機就會自動變為主服務器 [root@host51 ~]# ss -nutlp | grep 6351[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此時55主機變成了主,并且沒有從服務器 192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 1 keys | 5461 slots | 0 slaves. 192.168.4.53:6353 (7747411a...) -> 2 keys | 5461 slots | 1 slaves. [OK] 3 keys in 3 masters. 0.00 keys per slot on average.在客戶端連接任意一臺55主機寫入數據 [root@host56 ~]# redis-cli -c -h 192.168.4.55 -p 6355 192.168.4.55:6355> set ar 66 -> Redirected to slot [9934] located at 192.168.4.52:6352 OK 192.168.4.52:6352> set kk 4444 -> Redirected to slot [2589] located at 192.168.4.55:6355 OK 192.168.4.55:6355> keys * 1) "kk" 2) "b" 192.168.4.55:6355> set pp 4575 -> Redirected to slot [14030] located at 192.168.4.53:6353 OK 192.168.4.53:6353> keys * 1) "a" 2) "pp" 3) "x" 192.168.4.53:6353> set ii 11123 -> Redirected to slot [3133] located at 192.168.4.55:6355 OK 192.168.4.55:6355> keys * #可以看到55主機多了兩個變量 1) "ii" 2) "kk" 3) "b"-------------------------------------------------------------------------源master啟動后,會自動配置為當前master的slave; [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@host51 ~]# ss -nutlp | grep 6351 tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=1748,fd=8)) tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1748,fd=6))[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此時可以看到,55主機有了從服務器 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 3 masters. 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #check檢查發現,51主機變成了55主機的從 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922 (5462 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:0-5460 (5461 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383 (5461 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.連接51主機的服務器,會發現,在55主機上寫入的數據自動同步到了51主機上 [root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6351 192.168.4.51:6351> keys * 1) "b" 2) "kk" 3) "ii"用Redis集群存取數據時一定要確保每組主從服務器至少有一個是活著的,否則整個集群都會崩潰
  • 添加服務器

存儲空間不足,或者當前已有的服務器處理不了客戶并發訪問量時,可以添加新的服務器到集群中

1.添加master服務器 1)部署一臺新redis服務器host58,運行服務并啟用集群配置 [root@host58 opt]# ls /optredis-4.0.8.tar.gz [root@host58 opt]# yum -y install gcc #先安裝gcc依賴 [root@host58 opt]#tar -zvxf redis-4.0.8.tar.gz [root@host58 opt]#cd redis-4.0.8/ [root@host58 redis-4.0.8]#make && make install [root@host58 redis-4.0.8]# ./utils/install_server.sh #一路回車 Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 [root@host58 redis-4.0.8]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped[root@host58 redis-4.0.8]# vim /etc/redis/6379.conf 70 bind 192.168.4.5893 port 6358815 cluster-enabled yes #啟用集群823 cluster-config-file nodes-6379.conf #存儲集群信息文件829 cluster-node-timeout 5000[root@host58 redis-4.0.8]# /etc/init.d/redis_6379 start #起服務 Starting Redis server... [root@host58 redis-4.0.8]# ss -nutlp | grep 6358 tcp LISTEN 0 128 192.168.4.58:16358 *:* users:(("redis-server",pid=4940,fd=8)) tcp LISTEN 0 128 192.168.4.58:6358 *:* users:(("redis-server",pid=4940,fd=6)) [root@host58 redis-4.0.8]# redis-cli -h 192.168.4.58 -p 6358 192.168.4.58:6358> ping PONG 192.168.4.58:6358> cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:0 cluster_current_epoch:0 cluster_my_epoch:0 cluster_stats_messages_sent:0 cluster_stats_messages_received:0 192.168.4.58:6358> exit2)在管理主機57,執行添加新服務的操作,添加master主機,分配hash槽(slots)[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #先查看當前的主服務器 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves. [OK] 7 keys in 3 masters.[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352 #添加新的集群節點 [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息,58主機成功添加為主服務器,但沒有hash slots槽 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 0 slots | 0 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.3)重新分片(hash slot)[root@host57 ~]# redis-trib.rb reshard 192.168.4.52:6352 #重新分片 ... How many slots do you want to move (from 1 to 16384)? 4096 #移出hash槽個數,拿出4096個hash槽給主機58 What is the receiving node ID? 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #接收hash槽主機IDType 'all' to use all the nodes as source nodes for the hash slots. #鍵入“all”將所有節點用作hash槽的源節點。Type 'done' once you entered all the source nodes IDs. #輸入所有源節點ID后,鍵入“done”。 Source node #1: all #移出hash槽主機ID,從所有主服務器移除slotsDo you want to proceed with the proposed reshard plan (yes/no)? yes #同意配置否[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 0 slaves. #主服務器58hash槽4096個 [OK] 7 keys in 4 masters. 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #檢測集群 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922 (4096 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461 (4096 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383 (4096 slots) master1 additional replica(s) M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652 (4096 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.-------------------------------------------------------------------------2.添加slave角色主機到集群里 1)部署新的redis服務器192.168.4.59,運行redis服務,啟用集群功能 [root@host59 opt]# ls redis-4.0.8.tar.gz [root@host59 opt]# yum -y install gcc [root@host59 opt]# tar -zvxf redis-4.0.8.tar.gz [root@host59 opt]# cd redis-4.0.8/ [root@host59 redis-4.0.8]# make && make install && ./utils/install_server.sh Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 [root@host59 redis-4.0.8]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped[root@host59 redis-4.0.8]# vim /etc/redis/6379.conf 70 bind 192.168.4.5993 port 6359815 cluster-enabled yes #啟用集群823 cluster-config-file nodes-6379.conf #存儲集群信息文件829 cluster-node-timeout 5000 [root@host59 redis-4.0.8]# /etc/init.d/redis_6379 start Starting Redis server... [root@host59 redis-4.0.8]# ss -nutlp | grep 6359 tcp LISTEN 0 128 192.168.4.59:16359 *:* users:(("redis-server",pid=14657,fd=8)) tcp LISTEN 0 128 192.168.4.59:6359 *:* users:(("redis-server",pid=14657,fd=6))[root@host59 redis-4.0.8]# redis-cli -h 192.168.4.59 -p 6359 192.168.4.59:6359> ping PONG 192.168.4.59:6359> cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:0 cluster_current_epoch:0 cluster_my_epoch:0 cluster_stats_messages_sent:0 cluster_stats_messages_received:02)在管理主機添加slave角色主機 [root@host57 ~]# redis-trib.rb add-node --slave 192.168.4.59:6359 192.168.4.58:6358 #如果不指定主節點的id的話,會把新節點隨機添加為從節點最少的主庫 [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看信息 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves. #58主機有一個從服務器 [OK] 7 keys in 4 masters. 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #檢測集群 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922 (4096 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461 (4096 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383 (4096 slots) master1 additional replica(s) M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652 (4096 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots: (0 slots) slavereplicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.如果reshard分配不平均的話,可以執行以下操作: [root@host57 ~]# redis-trib.rb rebalance 192.168.4.52:6352
  • 移出服務器
1)移除slave主機 從服務器沒有hash槽,直接移除即可,一出事指定從服務器id值 [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #先查看當前集群中的主服務器 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. [root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #查看主服務器對應的從服務器 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922 (4096 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461 (4096 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383 (4096 slots) master1 additional replica(s) M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652 (4096 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots: (0 slots) slavereplicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.[root@host57 ~]# redis-trib.rb del-node 192.168.4.52:6352 25991ad67a2e5b7c570ca87eed3140432cc78eee #將51主機移除 >>> Removing node 25991ad67a2e5b7c570ca87eed3140432cc78eee from cluster 192.168.4.52:6352 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. #注:移除時也會同時將該主機的redis服務關閉 [root@host51 ~]# netstat -nutlp | grep redis-server #此時去51主機查看沒有服務端口,再次開啟服務也不會再存在于集群中 [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #可以看到55主機沒有從服務器了 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 0 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average. [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #再次重新開啟服務后,55主機依舊沒有從服務器 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 0 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.2)把master服務器移除集群 ①在管理主機,先刪除master服務器占用的hash槽 [root@host57 ~]# redis-trib.rb reshard 192.168.4.52:6352 How many slots do you want to move (from 1 to 16384)? 4096 #移除4096個數槽 What is the receiving node ID? b69bbe093c47927c88fa3eaf10cce898e4593cc5 #要移動給誰的id即目標主機,可以隨便寫一個master的id(此處為master55的id) Type 'all' to use all the nodes as source nodes for the hash slots. #鍵入“all”將所有節點用作hash槽的源節點。Type 'done' once you entered all the source nodes IDs. #輸入所有源節點ID后,鍵入“done”。 Source node #1:5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #從誰那移動即援助局(這里寫4.58的id) Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes #同意配置否②在管理主機查看集群信息 [root@host57 ~]# redis-trib.rb del-node 192.168.4.52:6352 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #刪除主機,刪除誰就加誰的id >>> Removing node 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 from cluster 192.168.4.52:6352 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此時已經沒有了58主機 192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 3 keys | 8192 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves. [OK] 7 keys in 3 masters. 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb rebalance 192.168.4.52:6352 #將每個主機的hash槽平均 [root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息 192.168.4.52:6352 (23b46dfa...) -> 2 keys | 5462 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 0 keys | 5461 slots | 1 slaves. 192.168.4.53:6353 (7747411a...) -> 5 keys | 5461 slots | 1 slaves. [OK] 7 keys in 3 masters. #主服務器個數3臺,沒有58 0.00 keys per slot on average.[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #此時59主機就自動變成了沒有從服務器的55主機的從服務器 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922 (5462 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652 (5461 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383 (5461 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
  • 把刪除的主機再添加到集群中
[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352 >>> Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922 (5462 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652 (5461 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383 (5461 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [ERR] Node 192.168.4.58:6358 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. #192.168.4.58主機不是空的狀態,并且集群已經知道了該節點解決辦法:1.可以將/var/lib/redis/6379/nodes-6379.conf刪除,然后將該主機的redis服務關閉重啟[root@host58 redis-4.0.8]# cat /var/lib/redis/6379/nodes-6379.conf a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359@16359 slave b69bbe093c47927c88fa3eaf10cce898e4593cc5 0 1582909828964 12 connected 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582909828964 14 connected 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582909829000 13 connected 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582909828964 14 connected 2732-4096 5462-6826 13653-16383 b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 master - 0 1582909829000 12 connected 4097-5461 6827-8192 10923-13652 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358@16358 myself,master - 0 1582909828957 8 connected 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582909828964 13 connected 0-2731 8193-10922 vars currentEpoch 14 lastVoteEpoch 02.或者可以直接在redis服務器內執行cluster reset重置 [root@host58 redis-4.0.8]# redis-cli -h 192.168.4.58 -p 6358 192.168.4.58:6358> cluster info #此時查看,發現該主機還存在集群節點 cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:7 cluster_size:3 cluster_current_epoch:14 cluster_my_epoch:8 cluster_stats_messages_ping_sent:631 cluster_stats_messages_sent:631 cluster_stats_messages_pong_received:631 cluster_stats_messages_received:631 192.168.4.58:6358> cluster reset #重置集群信息 192.168.4.58:6358> cluster info #再次查看 cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:0 cluster_current_epoch:14 cluster_my_epoch:8 cluster_stats_messages_ping_sent:719 cluster_stats_messages_sent:719 cluster_stats_messages_pong_received:719 cluster_stats_messages_received:719[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352 >>> Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352 >>> Performing Cluster Check (using node 192.168.4.52:6352) M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922 (5462 slots) master1 additional replica(s) M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652 (5461 slots) master1 additional replica(s) M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383 (5461 slots) master1 additional replica(s) S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots: (0 slots) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots: (0 slots) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5 S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots: (0 slots) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 192.168.4.58:6358 to make it join the cluster. [OK] New node added correctly.[root@host57 ~]# redis-trib.rb info 192.168.4.58:6358 192.168.4.58:6358 (5e0d1807...) -> 0 keys | 0 slots | 0 slaves. 192.168.4.53:6353 (7747411a...) -> 5 keys | 5461 slots | 1 slaves. 192.168.4.55:6355 (b69bbe09...) -> 0 keys | 5461 slots | 1 slaves. 192.168.4.52:6352 (23b46dfa...) -> 2 keys | 5462 slots | 1 slaves. [OK] 7 keys in 4 masters. 0.00 keys per slot on average.
  • 把集群中的主機還原成獨立的數據庫服務器
  • 停止redis服務
  • 清空數據庫目錄
  • 注釋集群功能
  • 啟動redis服務
  • 查看端口只有服務端口,沒有集群端口
  • cluster info 顯示enabled
  • [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown [root@host51 ~]# rm -rf /var/lib/redis/6379/* [root@host51 ~]# vim /etc/redis/6379.conf 815 #cluster-enabled yes823 #cluster-config-file nodes-6379.conf829 #cluster-node-timeout 5000 [root@host51 ~]# /etc/init.d/redis_6379 start Starting Redis server... [root@host51 ~]# netstat -nutlp | grep redis-server tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 2005/redis-server 1 [root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 192.168.4.51:6351> cluster info ERR This instance has cluster support disabled[root@host51 ~]#sed -i "815s/^/#/ ; 823s/^/#/ ; 829s/^/#/" /etc/redis/6379.conf
    部署redis腳本
    #!/bin/bash rpm -q gcc if [ $? -eq 0 ];then echo "gcc已經安裝" else yum -y install gcc expect fi redis(){tar -zxf redis-4.0.8.tar.gzcd redis-4.0.8/makemake installcd utils/ expect <<ok spawn ./install_server.sh expect "Please select the redis port for this instance" {send "exit\r"} expect "Please select the redis config file name " {send "exit\r"} expect "Please select the redis log file name " {send "exit\r"} expect "select the data directory for this instance " {send "exit\r"} expect "Please select the redis executable path " {send "exit\r"} expect "Is this ok? Then press ENTER to go on or Ctrl-C to abort." {send "exit\r"} expect "##" {send "exit\r"} expect "##" {send "exit\r"} ok } redis echo "redis服務已安裝完成"

    總結

    以上是生活随笔為你收集整理的NoSQL(二):创建、管理集群的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。