[TOC]node
1、Redis Cluster簡單介紹
Redis集羣
搭建的方式有多種,例如Redis主從複製、Sentinel高可用集羣等,但從Redis 3.0
以後版本支持Redis-cluster集羣,Redis-Cluster採用無中心結構,每一個節點保存數據和整個集羣狀態,每一個節點都和其餘全部節點鏈接。python
其Redis-cluster結構圖以下: mysql
Redis Cluster集羣的運行機制:linux
- 全部的Redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬。
- 節點的Failover是經過集羣中超過半數的節點檢測失效時才生效。
- 客戶端與Redis節點直連,不須要中間Proxy層。客戶端不須要鏈接集羣全部節點,鏈接集羣中任何一個可用節點便可(默認slave從節點只提供備份)。
- Redis-cluster把全部的物理節點映射到[0-16383]Slot上(不必定是平均分配),Cluster 負責維護node<->slot<->value。
- Redis集羣預分好16384個桶,當須要在 Redis 集羣中放置一個 key-value 時,根據 CRC16(key) mod 16384的值,決定將一個key放到哪一個槽中。
2、背景
爲了保證Redis集羣的高可用性,即便使用Sentinel哨兵實現Failover自動切換,Redis每一個實例也是全量存儲,每一個Redis存儲的內容都是完整的數據,比較浪費內存。爲了最大化利用內存,能夠採用數據分片的形式,保存到不一樣的Redis實例上,這就是分佈式存儲。即每臺redis存儲不一樣的內容,Redis Cluster集羣架構正是知足這種分佈式存儲要求的一種體現。git
Redis Cluster集羣提供瞭如下兩個好處:github
- 將數據自動切分(split)到多個節點的能力。
- 當集羣中的一部分節點失效或者沒法進行通信時, 仍然能夠繼續處理命令請求的能力。
3、環境準備
3.1 主機環境
[root@cache06 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@cache06 ~]# uname -m x86_64
3.2 主機規劃
規劃三臺虛擬主機,每臺主機分別搭建一個master主節點和一個slave從節點。並將一個分片(主機)的兩個節點,分到不一樣的主機上,防止主節點Crash形成整個分片數據丟失。 redis
<font color='宋體' size='3'><strong>其緩存架構圖以下:</strong></font> sql
4、部署Redis
4.1 安裝Redis軟件
#下載redis [root@cache06 server]# cd /server/tools && wget http://download.redis.io/releases/redis-3.2.12.tar.gz #解壓並進入src目錄 [root@cache06 server]# tar xf redis-3.2.12.tar.gz && cd redis-3.2.12 #編譯軟件,參數MALLOC=jemalloc,2.4.4以上版本默認的內存分配器(可省略) [root@cache06 redis-3.2.12]# make #將軟件安裝到指定目錄下 [root@cache06 redis-3.2.12]# make PREFIX=/application/redis-3.2.12 install #建立軟連接 [root@cache06 redis-3.2.12]# ln -s /application/redis-3.2.12 /application/redis #修改PATH環境變量並使其生效 [root@cache06 redis]# echo 'PATH=/application/redis/bin:$PATH'>>/etc/profile && source /etc/profile #檢查安裝結果 [root@cache06 redis]# ll bin -rwxr-xr-x 1 root root 2432992 Apr 10 13:34 redis-benchmark -rwxr-xr-x 1 root root 25176 Apr 10 13:34 redis-check-aof -rwxr-xr-x 1 root root 5192440 Apr 10 13:34 redis-check-rdb -rwxr-xr-x 1 root root 2586080 Apr 10 13:34 redis-cli lrwxrwxrwx 1 root root 12 Apr 10 13:34 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 5192440 Apr 10 13:34 redis-server
命令解釋:數據庫
- redis-server:Redis 服務端的啓動程序 。
- redis-cli:Redis客戶端程序,也能夠用 telnet 根據其純文本協議來操做Redis緩存。
- redis-benchmark:Redis 性能測試工具,測試 Redis 在當前系統下的讀寫性能。
- redis-check-aof:數據修復。
- redis-check-dump:檢查導出工具。
4.2 編輯Redis配置文件
[root@cache06 redis]# mkdir /application/redis/conf [root@cache06 redis]# cp /server/tools/redis-3.2.12/redis.conf ./conf/ [root@cache06 redis]# cd conf [root@cache06 redis]# vim redis.conf #是否後臺運行 daemonize yes #默認端口 port 6379 #日誌文件位置 logfile /var/log/redis.log #持久化文件存儲位置 dir /data/6379 #RDB持久化數據文件 dbfilename dump.rdb
4.3 啓動Redis服務
4.3.1 啓動前優化內存分配策略
有三種方法進行修改,可是須要root權限:vim
- sysctl vm.overcommit_memory=1
- echo 'vm.overcommit_memory=1' >>/etc/sysctl.conf && sysctl -p
- echo 1 >/proc/sys/vm/overcommit_memory
Overcommit_Memory參數可選值含義:
- 0 表示內核將檢查是否有足夠的可用內存供應用進程使用;若是有足夠的可用內存,內存申請容許;不然,內存申請失敗,並把錯誤返回給應用進程。
- 1 表示內核容許分配全部的物理內存,而無論當前的內存狀態如何。
- 2 表示內核容許分配超過全部物理內存和交換空間總和的內存。
4.3.2 啓動redis-server
#啓動服務 [root@cache06 redis]# redis-server /application/redis/conf/redis.conf & #追加啓動命令到/etc/rc.local文件中 echo -e '#start redis-server\nredis-server /application/redis/conf/redis.conf &' >>/etc/rc.local #添加執行權限 [root@cache06 ~]# chmod +x /etc/rc.d/rc.local
4.3.3 檢查服務
#默認端口6379 [root@cache06 redis]# lsof -i :6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 10835 root 4u IPv4 26242 0t0 TCP localhost:6379 (LISTEN)
4.3.4 客戶端鏈接測試
#交互式簡單存取數據 [root@cache06 redis]# redis-cli 127.0.0.1:6379> set name oldboy OK 127.0.0.1:6379> get name "oldboy" #非交互式簡單存取數據:相似mysql -e參數 [root@cache06 redis]# redis-cli set name oldboy OK [root@cache06 redis]# redis-cli get name "oldboy" #刪除數據 [root@cache06 redis]# redis-cli del name
注:也能夠經過telnet命令經過文本協議方式,實現redis交互式存取數據。
5、構建Redis Cluster集羣
Redis Cluster集羣正常工做至少須要3個主節點,同時爲了保證數據的高可用性,加入了主從模式。所以至少建立6個節點,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點默認狀況下只負責從Master拉取數據進行備份。當這個主節點掛掉後,集羣就會經過下線檢測的方式,由從節點中選舉一個節點來充當主節點,實現故障轉移,從而保證集羣正常運行。
5.1 Redis主從複製原理
Redis-Cluster集羣的複製特性重用了 SLAVEOF 命令的代碼,因此集羣節點的複製行爲和 SLAVEOF 命令的複製行爲徹底相同。 Redis全量複製通常在Slave初始化階段執行,經過SLAVEOF命令開啓主從複製,此時Slave須要將Master上的全部數據都複製一份。 具體步驟以下:
- 從服務器鏈接主服務器,發送SYNC命令;
- 主服務器接收到SYNC命令後,開始執行BGSAVE命令生成RDB文件,並使用緩衝區記錄此後執行的全部寫命令;
- 主服務器BGSAVE執行完後,向全部從服務器發送快照文件,並在發送期間繼續記錄被執行的寫命令;
- 從服務器收到快照文件後丟棄舊的數據,載入新的快照;
- 主服務器快照發送完畢後,開始向從服務器發送緩衝區中的寫命令;
- 從服務器完成對快照的載入,開始接收命令請求,並執行來自主服務器緩衝區的寫命令;
5.2 部署集羣節點
規劃三臺虛擬主機,每臺主機分別搭建一個master主節點和一個slave從節點。並將一個分片(主機)的兩個節點,分到不一樣的主機上,防止主節點Crash形成整個分片數據丟失。 其主機規劃以下:
- 主機172.16.1.54 - master節點:172.16.1.54:6378 - slave節點:172.16.1.54:6379
- 主機172.16.1.57 - master節點:172.16.1.57:6378 - slave節點:172.16.1.57:6379
- 主機172.16.1.56 - master節點:172.16.1.56:6378 - slave節點:172.16.1.56:6379
5.2.1 建立存放多個實例的目錄(以單臺主機爲例)
#以單臺主機爲例,建立存放多個實例的目錄 [root@cache06 ~]# mkdir -p /data/{6378,6379}
5.2.2 配置redis.conf文件
#配置6378實例 [root@cache06 ~]# cd /data/6378 #編輯redis實例配置文件 [root@cache06 6378]# vim redis.conf port 6378 daemonize yes pidfile /data/6378/redis.pid logfile /data/6378/redis.log loglevel notice dir /data/6378 dbfilename dump.rdb #禁用保護模式(避免影響主從複製) protected-mode no #開啓cluster模式 cluster-enabled yes #記錄集羣信息,cluster集羣自動維護,不用手動更新、建立 cluster-config-file nodes.conf #節點超時時間,目標節點超過指定時間沒響應,就標記爲FAIL或PFAIL(可能宕機) cluster-node-timeout 5000 appendonly yes #配置6379實例 [root@cache06 6378]# cd /data/6379 #拷貝redis.conf配置文件 [root@cache06 6379]# cp /data/{6378,6379}/redis.conf #端口替換 [root@cache06 6379]# sed -i 's#6378#6379#g' redis.conf #檢查替換是否成功 [root@cache06 6379]# grep '6379' redis.conf port 6379 pidfile /data/6379/redis.pid logfile /data/6379/redis.log dir /data/6379
5.2.3 啓動實例
#啓動Redis實例 [root@cache06 data]# redis-server /data/6378/redis.conf [root@cache06 data]# redis-server /data/6379/redis.conf #查看進程是否存在 [root@cache06 data]# ps -ef|grep [r]edis root 2350 1 0 11:07 ? 00:00:01 redis-server *:6378 [cluster] root 2399 1 0 11:21 ? 00:00:00 redis-server *:6379 [cluster]
以後根據主機規劃,按照以上步驟建立其餘Redis實例。
5.3 建立Redis Cluster集羣
5.3.1 安裝redis-trib.rb須要的依賴環境
#安裝Epel源 [root@cache06 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #安裝Ruby環境 [root@cache06 ~]# yum install ruby rubygems -y #使用國內源 [root@cache06 ~]# gem sources --add http://mirrors.aliyun.com/rubygems/ --remove https://rubygems.org/ #檢查源 [root@cache06 ~]# gem sources -l *** CURRENT SOURCES *** http://mirrors.aliyun.com/rubygems/ #安裝依賴軟件 [root@cache06 ~]# gem install redis -v 3.3.3
5.3.2 建立集羣
1)建立一個包含三個主節點和三個從節點的集羣
#複製redis-trib.rb到/application/redis/bin目錄 [root@cache06 ~]# cp /server/tools/redis-3.2.12/src/redis-trib.rb /application/redis/bin #建立集羣 [root@cache06 ~]# redis-trib.rb create --replicas 1 172.16.1.54:6378 172.16.1.54:6379 172.16.1.57:6378 \ 172.16.1.57:6379 172.16.1.56:6378 172.16.1.56:6379
命令參數的含義:
- 選項 create 表示但願建立一個新的集羣。
- 選項 --replicas 1 表示但願集羣中的每一個主節點建立一個從節點。
- 以後跟着的多個host:port參數,則是實例的地址列表,但願程序使用這些地址所指示的實例來建立新集羣。
ERROR錯誤提示:若是集羣節點少於6個,使用redis-trib.rb建立集羣會提示以下信息
>>> Creating cluster ERROR: Invalid configuration for cluster creation. Redis Cluster requires at least 3 master nodes. This is not possible with 4 nodes and 1 replicas per node. At least 6 nodes are required.
2)redis-trib 會打印出一份預想中的配置,若是沒問題就能夠輸入 yes ,redis-trib 就會將這份配置應用到集羣當中
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 172.16.1.54:6378 172.16.1.57:6378 172.16.1.56:6378 Adding replica 172.16.1.57:6379 to 172.16.1.54:6378 Adding replica 172.16.1.54:6379 to 172.16.1.57:6378 Adding replica 172.16.1.56:6379 to 172.16.1.56:6378 M: a560974d9721882574aaf7fa4cf7218f590e97e0 172.16.1.54:6378 slots:0-5460 (5461 slots) master S: 4b462992200244a66c63f085ffd7ef4983a9655b 172.16.1.54:6379 replicates f2d4d17676e6166bcdc2d05d81fc891327d03319 M: f2d4d17676e6166bcdc2d05d81fc891327d03319 172.16.1.57:6378 slots:5461-10922 (5462 slots) master S: 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.57:6379 replicates a560974d9721882574aaf7fa4cf7218f590e97e0 M: ca2a21eb3b5679c61b186ca4ca661f93e11ec304 172.16.1.56:6378 slots:10923-16383 (5461 slots) master S: 78e4b73c8f5f57d55303db52e9774ba69a306bb2 172.16.1.56:6379 replicates ca2a21eb3b5679c61b186ca4ca661f93e11ec304 Can I set the above configuration? (type 'yes' to accept):
3)輸入yes並回車確認以後,集羣將會配置應用到各個節點, 並鏈接各個節點,讓節點之間開始互相通信
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 172.16.1.54:6378) M: a560974d9721882574aaf7fa4cf7218f590e97e0 172.16.1.54:6378 slots:0-5460 (5461 slots) master 1 additional replica(s) M: f2d4d17676e6166bcdc2d05d81fc891327d03319 172.16.1.57:6378 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.57:6379 slots: (0 slots) slave replicates a560974d9721882574aaf7fa4cf7218f590e97e0 S: 4b462992200244a66c63f085ffd7ef4983a9655b 172.16.1.54:6379 slots: (0 slots) slave replicates f2d4d17676e6166bcdc2d05d81fc891327d03319 S: 78e4b73c8f5f57d55303db52e9774ba69a306bb2 172.16.1.56:6379 slots: (0 slots) slave replicates ca2a21eb3b5679c61b186ca4ca661f93e11ec304 M: ca2a21eb3b5679c61b186ca4ca661f93e11ec304 172.16.1.56:6378 slots:10923-16383 (5461 slots) master 1 additional replica(s)
4)建立成功則輸出以下信息
[OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
5.3.3 測試集羣
#鏈接任一節點進行數據管理 [root@cache06 ~]# redis-cli -c -p 6378 172.16.1.56:6378> set msg 'oldboy' -> Redirected to slot [6257] located at 172.16.1.57:6378 OK 172.16.1.57:6378> get msg "oldboy"
模擬測試:實現熱點數據緩存
1)建立學生表,並插入數據
#登陸數據庫 [root@db01 ~]# mysql -uroot -p123456 #建立stu學生表 mysql> create table stu( -> sid smallint(8) unsigned zerofill not null auto_increment comment 'sid', -> sname varchar(20) not null default '' comment 'sname', -> address varchar(20) not null default '' comment 'address', -> primary key(sid) -> )character set utf8 engine innodb; #插入數據 mysql> use test; mysql> insert into stu values(null,'張三丰','湖南長沙'),(null,'張無忌','河南洛陽'),(null,'段譽','北京市'),(null,' 喬峯','廣州市'),(null,'沈浪','上海市'); #查詢數據 mysql> select * from stu; +----------+-------+----------+ | sid | sname | address | +----------+-------+----------+ | 00000001 | 張三丰 | 湖南長沙 | | 00000002 | 張無忌 | 河南洛陽 | | 00000003 | 段譽 | 北京市 | | 00000004 | 喬峯 | 廣州市 | | 00000005 | 沈浪 | 上海市 | +----------+---- ---+---------+
2)將stu表中數據導入到redis數據庫中
[root@cache06 scripts]# vim hmset_data.sh #!/bin/bash IP=172.16.1.51 script='/server/scripts' if [ ! -d $script ];then mkdir -p $script fi #從數據庫獲取學生表信息,調用concat()函數,拼接redis-cli客戶端命令 #redis-cli -c -p 6378 hmset stu_00000001 sid 00000001 sname 張三丰 address 湖南長沙 cd $script && \ /usr/bin/ssh $IP "mysql -uroot -p123456 -e 'select concat(\"hmset stu_\",sid,\" sid \",sid,\" sname \",sname,\" address \",address) from test.stu;'"|grep -v '^concat'|awk '{print "redis-cli -c -p 6378",$0}' >db.cmd #判斷上一條命令執行是否成功 if [ $? -ne 0 ];then echo 'USAGE: Please check the Data or IP.' exit 1 fi #執行命令,並將結果寫入到緩存中 cat db.cmd|while read line do $line done
3)檢查緩存結果
#查看集羣信息,保證每一個master主庫都有一個從庫 [root@cache06 scripts]# redis-trib.rb info 127.0.0.1:6378 127.0.0.1:6378 (ca2a21eb...) -> 2 keys | 5461 slots | 1 slaves. 172.16.1.57:6378 (f2d4d176...) -> 4 keys | 5462 slots | 1 slaves. 172.16.1.54:6378 (a560974d...) -> 4 keys | 5461 slots | 1 slaves. [OK] 10 keys in 3 masters. 0.00 keys per slot on average. #獲取鍵值信息,在redis中存儲中文,讀取數據時會遇到字符集的問題 [root@cache06 scripts]# redis-cli -c -p 6378 hget stu_00000009 sname "\xe6\xb1\xaa\xe6\xb6\xb5" #在redis-cli後面加--raw參數,顯示中文 [root@cache06 scripts]# redis-cli -c --raw -p 6378 hmget stu_00000009 sid sname 00000009 汪涵
5.4 集羣管理
5.4.1 故障轉移
1)查詢172.16.1.54主機上的主節點(6378端口實例)數據
[root@cache06 ~]# redis-cli -c -h 172.16.1.54 -p 6378 172.16.1.54:6378> keys * 1) "stu_00000005" 2) "stu_00000001" 3) "stu_00000008" 4) "stu_00000004" #查詢學生stu_00000005的姓名 [root@cache06 ~]# redis-cli -c --raw -p 6378 hget stu_00000005 sname 沈浪
2)中止服務
#中止172.16.1.54主機上的主節點(6378實例)服務 [root@cache06 ~]# redis-cli -c -h 172.16.1.54 -p 6378 shutdown #查看進程是否存在 [root@cache04 ~]# ps -ef|grep [r]edis root 2631 1 0 15:47 ? 00:00:29 redis-server *:6379 [cluster]
3)檢查集羣節點狀態
[root@cache06 ~]# redis-cli -c -p 6378 cluster nodes ca2a21eb3b5679c61b186ca4ca661f93e11ec304 172.16.1.56:6378 master - 0 1555066929017 5 connected 10923-16383 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.57:6379 master - 0 1555066928312 7 connected 0-5460 f2d4d17676e6166bcdc2d05d81fc891327d03319 172.16.1.57:6378 master - 0 1555066927808 3 connected 5461-10922 78e4b73c8f5f57d55303db52e9774ba69a306bb2 172.16.1.56:6379 myself,slave ca2a21eb3b5679c61b186ca4ca661f93e11ec304 0 0 6 connected 4b462992200244a66c63f085ffd7ef4983a9655b 172.16.1.54:6379 slave f2d4d17676e6166bcdc2d05d81fc891327d03319 0 1555066928008 3 connected a560974d9721882574aaf7fa4cf7218f590e97e0 172.16.1.54:6378 master,fail - 1555066721816 1555066719602 1 disconnected
能夠看到172.16.1.54的主節點顯示fail狀態,原來的172.16.1.57的從節點(6379實例),自動提高爲master。
4)再次檢索stu_00000005學生信息,能夠正常查詢
[root@cache06 ~]# redis-cli -c --raw -p 6378 127.0.0.1:6378> hgetall stu_00000005 -> Redirected to slot [5220] located at 172.16.1.57:6379 sid 00000005 sname 沈浪 address 上海市
5.4.2 添加節點
語法:redis-trib.rb add-node new_host:new_port existing_host:existing_port --slave --master-id <arg>
參數:
- --slave:以從節點身份加入集羣。
- --master-id:主節點號。
- new_host:new_port:新增節點的地址。
- existing_host:existing_port:集羣中任意一個節點的地址。
1)重置Redis實例
將摘除的Redis實例做爲從節點,再次添加到Cluster集羣中,添加節點前作以下操做。
#刪除實例下的文件,只保留redis.conf配置文件 [root@cache04 data]# rm -f 6378/{appendonly.aof,dump.rdb,nodes.conf,redis.log} #啓動實例 [root@cache04 data]# redis-server /data/6378/redis.conf #檢查進程 [root@cache04 data]# ps -ef|grep [r]edis root 2631 1 0 07:04 ? 00:00:59 redis-server *:6379 [cluster] root 3607 1 0 12:35 ? 00:00:00 redis-server *:6378 [cluster] #查看節點數據,當前節點數據爲空 [root@cache04 data]# redis-cli -c -p 6378 127.0.0.1:6378> keys * (empty list or set)
2)以slave身份加入到集羣
[root@cache04 data]# redis-trib.rb add-node --slave --master-id 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.54:6378 172.16.1.57:6379 >>> Adding node 172.16.1.54:6378 to cluster 172.16.1.57:6379 >>> Performing Cluster Check (using node 172.16.1.57:6379) M: 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.57:6379 slots:0-5460 (5461 slots) master 0 additional replica(s) S: ca2a21eb3b5679c61b186ca4ca661f93e11ec304 172.16.1.56:6378 slots: (0 slots) slave replicates 78e4b73c8f5f57d55303db52e9774ba69a306bb2 S: 4b462992200244a66c63f085ffd7ef4983a9655b 172.16.1.54:6379 slots: (0 slots) slave replicates f2d4d17676e6166bcdc2d05d81fc891327d03319 M: 78e4b73c8f5f57d55303db52e9774ba69a306bb2 172.16.1.56:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: f2d4d17676e6166bcdc2d05d81fc891327d03319 172.16.1.57:6378 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. >>> Send CLUSTER MEET to node 172.16.1.54:6378 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 172.16.1.57:6379. [OK] New node added correctly.
3)檢查集羣節點狀態
[root@cache06 ~]# redis-cli -c -p 6378 cluster nodes 78e4b73c8f5f57d55303db52e9774ba69a306bb2 172.16.1.56:6379 master - 0 1555130226500 8 connected 10923-16383 4b462992200244a66c63f085ffd7ef4983a9655b 172.16.1.54:6379 slave f2d4d17676e6166bcdc2d05d81fc891327d03319 0 1555130225999 3 connected f2d4d17676e6166bcdc2d05d81fc891327d03319 172.16.1.57:6378 master - 0 1555130227043 3 connected 5461-10922 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 172.16.1.57:6379 master - 0 1555130227547 7 connected 0-5460 69f66290fd63721965c83a71b5f673a3907d38da 172.16.1.54:6378 slave 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 0 1555130227547 7 connected ca2a21eb3b5679c61b186ca4ca661f93e11ec304 172.16.1.56:6378 myself,slave 78e4b73c8f5f57d55303db52e9774ba69a306bb2 0 0 5 connected a560974d9721882574aaf7fa4cf7218f590e97e0 :0 slave,fail,noaddr 90fc1e298bdfb55e35a9dae7137684c6217dc6ff 1555129672056 1555129671656 7 disconnected
能夠看到172.16.1.54:6378實例以從節點身份加入集羣,而且指向的主節點是172.16.1.57:6379實例。
4)檢查節點數據
#查看節點數據是否存在,加入集羣以後,會從主節點進行全量複製 [root@cache04 data]# redis-cli -c -p 6378 127.0.0.1:6378> keys * 1) "stu_00000001" 2) "stu_00000005" 3) "stu_00000004" 4) "stu_00000008" #獲取數據 [root@cache04 data]# redis-cli -c --raw -p 6378 127.0.0.1:6378> hgetall stu_00000005 -> Redirected to slot [5220] located at 172.16.1.57:6379 sid 00000005 sname 沈浪 address 上海市
<font color='red'>遇到的問題小結:</font>
ERROR1:new_host:new_port:待添加的節點,必須確保節點數據爲空或者節點在集羣中不存在,不然,會提示一下錯誤。
#使用redis-trib.rb添加新節點,在節點加入集羣以前,會對新節點的狀態進行檢查 >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [ERR] Node 172.16.1.54:6378 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
ERROR2:--slave和--master-id必須跟在add-node後面,一樣的參數,若是是如下這種寫法,會提示錯誤。
[root@cache06 ~]# redis-trib.rb add-node 172.16.1.54:6378 172.16.1.57:6379 --slave --master-id 90fc1e298bdfb55e35a9dae7137684c6217dc6ff [ERR] Wrong number of arguments for specified sub command
6、實現Python連接Redis Cluster集羣
實現Python連接及操做Redis Cluster,須要如下環境支持:
1.搭建Python開發環境,Python2.7.2以上版本才支持Redis Cluster,這裏選擇的是3.5.5版本。
- Python2 使用2.7之後的版本
- Python3 使用3.4之後的版本
2.安裝驅動程序,官方提供多種Python鏈接redis的API方式,這裏推薦使用redis-py。
3.Redis-py並無提供Redis-cluster的支持,須要下載redis-py-cluster。
6.1 搭建python開發環境
##安裝以前,檢查python版本 [root@cache04 tools]# python --version Python 2.7.5 #下載python3.5.5版本 [root@cache04 tools]# wget https://www.python.org/ftp/python/3.5.5/Python-3.5.5.tgz #源碼安裝 [root@cache04 tools]# tar xf Python-3.5.5.tgz [root@cache04 tools]# cd Python-3.5.5/ [root@cache04 Python-3.5.5]# ./configure [root@cache04 Python-3.5.5]# make && make install #檢查安裝環境,這裏使用python3命令,而不是python命令 [root@cache04 Python-3.5.5]# python3 Python 3.5.5 (default, Apr 14 2019, 11:13:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit()
6.2 安裝Redis-py驅動程序
Redis-py提供兩個類Redis和StrictRedis用於實現Redis的命令:
- StrictRedis用於實現大部分官方的命令,並使用官方的語法和命令(好比:SET命令對應與StrictRedis.set方法)。(開發推薦使用)
- Redis是StrictRedis的子類,用於向後兼容舊版本的redis-py。
#下載源碼包 [root@cache04 tools]# wget https://github.com/andymccurdy/redis-py/archive/2.10.6.tar.gz #解壓 [root@cache04 tools]# tar xf 2.10.6.tar.gz #切換到包目錄下 [root@cache04 tools]# cd redis-py-2.10.6/ #安裝驅動 [root@cache04 redis-py-2.10.6]# python3 setup.py install #導入redis包,沒有報錯,驅動安裝成功 [root@cache04 redis-py-cluster-1.3.6]# python3 Python 3.5.5 (default, Apr 14 2019, 11:13:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>>
6.3 安裝Redis-py-cluster驅動程序
Redis-py並無提供Redis-cluster的支持,須要下載redis-py-cluster包。
以上提示:redis-py3.0.x版本和redis-py-cluster版本存在不兼容的問題(目前redis-py-cluster最新版本爲1.3.6),實際使用過程當中,建議將redis-py固定爲2.10.6版本。
#使用redis-py-3.0.1版本遇到的問題,在導入類文件的時候,出現報錯 [root@cache04 redis-py-cluster-1.3.6]# python3 >>> from rediscluster import StrictRedisCluster Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/server/tools/redis-py-cluster-1.3.6/rediscluster/__init__.py", line 7, in <module> from .client import StrictRedisCluster, RedisCluster File "/server/tools/redis-py-cluster-1.3.6/rediscluster/client.py", line 10, in <module> from .connection import ( File "/server/tools/redis-py-cluster-1.3.6/rediscluster/connection.py", line 11, in <module> from .nodemanager import NodeManager File "/server/tools/redis-py-cluster-1.3.6/rediscluster/nodemanager.py", line 12, in <module> from redis._compat import b, unicode, bytes, long, basestring ImportError: cannot import name 'b'
安裝Redis-py-cluster程序。
#下載源碼包 [root@cache04 tools]# https://github.com/Grokzen/redis-py-cluster/archive/1.3.6.tar.gz #解壓 [root@cache04 tools]# tar xf 1.3.6.tar.gz #切換到包目錄下 [root@cache04 tools]# cd redis-py-cluster-1.3.6 #安裝驅動 [root@cache04 redis-py-cluster-1.3.6]# python3 setup.py install
6.4 測試API讀寫功能
#寫入數據 [root@cache04 tools]# python3 Python 3.5.5 (default, Apr 14 2019, 11:13:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from rediscluster import StrictRedisCluster >>> startup_nodes = [{"host": "127.0.0.1", "port": "6379"}] >>> conn = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) >>> conn.set("msg","Nice to meet you") True #讀取數據,檢查是否寫入成功 [root@cache06 ~]# redis-cli -c -p 6378 get msg "Nice to meet you"
<font color='red'><strong>遇到的問題小結:</strong></font> ERROR1:使用python交互模式,在命令開頭不能有任何空格,不然,會出現報錯。
>>> from rediscluster import StrictRedisCluster #命令開頭不能有空格或tab File "<stdin>", line 1 from rediscluster import StrictRedisCluster ^ IndentationError: unexpected indent