linux覆盤:redis集羣配置

redis集羣介紹

  • 多個redis節點網絡互聯,數據共享
  • 全部的節點都是一主一從(能夠是多個從),其中從不提供服務,僅做爲備用
  • 不支持同時處理多個鍵(如mset/mget),由於redis須要把鍵均勻分佈在各個節點上,併發量很高的狀況下同時建立鍵值會下降性能並致使不可預測的行爲。
  • 支持在線增長、刪除節點
  • 客戶端能夠連任何一個主節點進行讀寫

 唉過年過崩了node

大白話:linux

數據量很大,多臺機器組成大集羣,解決存儲空間、查詢速度負載高的瓶頸問題,Redis 上面的數據是共享式的,也就是A server有的B server不必定有。相似於Raid 5,寫入數據多是A磁盤 多是B磁盤。你能夠正常讀取,但真正的存儲位置須要查詢nginx

場景設置

  • 兩臺機器,分別開啓三個Redis服務(端口)
  • A機器上三個端口:7000、700二、7004,所有爲主
  • B機器上三個端口:700一、700三、7005,所有爲從
  • 兩臺機器上都要編譯安裝Redis,而後編譯並複製三個不一樣的Redis.conf,分別設置不一樣的端口號、dir等參數,還須要增長cluster相關參數,而後分別啓動6個Redis服務

master(IP:192.168.8.131)啓動三個端口:git

 

 

[root@root ~]# vim /etc/redis_7000.conf
port 7000
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7000.pid
dir /data/redis_data/7000
cluster-enabled yes
##開啓cluster功能
cluster-config-file nodes_7000.conf
##該配置文件能夠在dir目錄下自動生成
cluster-node-timeout 10100
appendonly yes

[root@root ~]# vim /etc/redis_7002.conf
port 7002
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7002.pid
dir /data/redis_data/7002
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes

[root@root ~]# vim /etc/redis_7004.conf
port 7004
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7004.pid
dir /data/redis_data/7004
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes

建立各配置文件對應的數據庫目錄:
[root@root ~]# mkdir /data/redis_data
[root@root ~]# mkdir /data/redis_data/{7000,7002,7004}

依次啓動Redis服務7000,7002,7004:
[root@root ~]# redis-server /etc/redis_7000.conf

啓動完成後,結果以下:
[root@root ~]# ps aux |grep redis
root     14423  0.6  0.5 145248  2640 ?        Ssl  19:35   0:00 redis-server 192.168.8.131:7000 [cluster]
root     14438  3.5  0.5 145248  2636 ?        Ssl  19:37   0:00 redis-server 192.168.8.131:7002 [cluster]
root     14443 13.8  0.5 145248  2636 ?        Ssl  19:37   0:01 redis-server 192.168.8.131:7004 [cluster]

slave(IP:192.168.8.132)啓動三個端口:github

[root@root ~]# vim /etc/redis_7001.conf
port 7001
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7001.pid
dir /data/redis_data/7001
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes

[root@root ~]# vim /etc/redis_7003.conf
port 7003
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7003.pid
dir /data/redis_data/7003
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes

[root@root ~]# vim /etc/redis_7005.conf
port 7005
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7005.pid
dir /data/redis_data/7005
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes

建立各配置文件對應的數據庫目錄:
[root@root ~]# mkdir /data/redis_data
[root@root ~]# mkdir /data/redis_data/{7001,7003,7005}

依次啓動Redis服務7001,7003,7005:
[root@root ~]# redis-server /etc/redis_7001.conf

啓動完成後結果以下:
[root@root ~]# ps aux |grep redis
root      5971  0.2  0.5 145248  2632 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7001 [cluster]
root      5976  0.1  0.5 145248  2636 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7003 [cluster]
root      5981  0.1  0.5 145248  2632 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7005 [cluster]

安裝ruby2.2 (只須要一臺機器上運行就是master):redis

Redis集羣須要ruby的支持,須要先安裝ruby(Ruby只需在一臺機器上運行)。Redis4.0須要使用Ruby2.2,安裝方法以下(由於本機自帶的是2.0版本的ruby,因此須要使用以下方法把源碼包包製做成yum安裝包,而後藉助yum工具安裝ruby2.2———升級ruby版本)數據庫

安裝yum開發工具組:
[root@root ~]# yum -y groupinstall "Development Tools"

升級庫文件:
[root@root ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel

[root@root ~]# cd /root/

建立製做rpm包的目錄:
[root@root ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

下載Ruby的源碼包:
[root@root ~]#  wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES

下載specs文件,用於製做rpm包:
[root@root ~]#  wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS

製做rpm包:
[root@root ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
##此處須要耐心等待…

安裝Ruby2.2:
[root@root ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm

[root@root ~]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

配置集羣就一步:vim

安裝Redis配置集羣的工具:
[root@root ~]# gem install redis

將命令redis-trib.rb加入環境變量目錄下:(注意本身的redis版本)
[root@root ~]# cp /usr/local/src/redis-4.0.2/src/redis-trib.rb  /usr/bin/

[root@root ~]# redis-trib.rb create --replicas 1 192.168.8.131:7000 192.168.8.131:7002 192.168.8.131:7004  192.168.8.132:7001 192.168.8.132:7003 192.168.8.132:7005
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

##注意:redis-trib.rb create --replicas 1  表示一個master對應幾個slave,此處的參數「1」表示master和slave一一對應
##而後yes就把主從分好了,雙OK表明成功
##這裏集羣配置完成,後面就能夠開始操做了

集羣配置操做

由於Redis集羣是分佈式結構,登陸任何一個端口均可新建等操做centos

鏈接:
[root@root ~]# redis-cli -c -h 192.168.8.131 -p 7000
##-c:=cluster,表示以集羣方式鏈接

建立數據:
192.168.8.131:7000> set cluster1 adaitest
-> Redirected to slot [8483] located at 192.168.8.132:7001
OK
##該操做會被重定向到192.168.8.132:7001
192.168.8.132:7001> set cluster2 adai222
-> Redirected to slot [4416] located at 192.168.8.131:7000
OK
192.168.8.131:7000> set cluster3 adaitest333
OK
192.168.8.131:7000> set cluster4 adai2323
-> Redirected to slot [12678] located at 192.168.8.131:7002
OK

查看數據:
192.168.8.131:7002> get cluster1
-> Redirected to slot [8483] located at 192.168.8.132:7001
"adaitest"
192.168.8.132:7001> get cluster2
-> Redirected to slot [4416] located at 192.168.8.131:7000
"adai222"
192.168.8.131:7000> get cluster3
"adaitest333"
192.168.8.131:7000> get cluster4
-> Redirected to slot [12678] located at 192.168.8.131:7002
"adai2323"

集羣相關操做ruby

檢測集羣狀態,任意一個節點均可以:
[root@root ~]# redis-trib.rb check 192.168.8.131:7000

列出節點:(其中是主是從一目瞭然)
[root@root ~]# redis-cli -c -h 192.168.8.131 -p 7000

192.168.8.131:7000> cluster nodes ☆☆☆☆☆☆

查看集羣信息:☆☆☆☆☆☆
192.168.8.131:7000> cluster info

添加節點(執行該操做前先在slave建立redis_7007.conf並啓動):
192.168.8.131:7000> cluster meet 192.168.8.132 7007
OK

192.168.8.131:7000> cluster nodes
52e4b3484838be21fcf53b84198e362efd54bd39 192.168.8.132:7007@17007 master - 0 1507035952000 0 connected
##此時7007以master身份存在

再添加一個節點:
192.168.8.131:7000> cluster meet 192.168.8.131 7006
OK
192.168.8.131:7000> cluster nodes
677f27fb209ce45c823126fe38dbcf0b9fc43d93 192.168.8.131:7006@17006 master - 0 1507036137147 0 connected
##一樣是以master身份存在

##即,使用以上方式添加的新節點都是以master身份存在!

把當前的節點設置爲指定節點的從

先更換到要設置的節點:
[root@root ~]# redis-cli -c -h 192.168.8.131 -p 7006

設定爲7007的從:
192.168.8.131:7006> cluster replicate 52e4b3484838be21fcf53b84198e362efd54bd39(7007的inode)
OK

查看:
192.168.8.131:7006> cluster nodes
52e4b3484838be21fcf53b84198e362efd54bd39 192.168.8.132:7007@17007 master - 0 1507036429244 7 connected
677f27fb209ce45c823126fe38dbcf0b9fc43d93 192.168.8.131:7006@17006 myself,slave 52e4b3484838be21fcf53b84198e362efd54bd39 0 1507036429000 0 connected
#對比node號,即7006爲7007的從


移除某節點:
192.168.8.131:7006> cluster forget 52e4b3484838be21fcf53b84198e362efd54bd39
(error) ERR Can't forget my master!
192.168.8.131:7006> cluster forget 677f27fb209ce45c823126fe38dbcf0b9fc43d93
(error) ERR I tried hard but I can't forget myself...
## 即,不能移除master節點和當前所在節點


[root@adailinux ~]# redis-cli -c -h 192.168.8.131 -p 7000
192.168.8.131:7000> cluster forget 677f27fb209ce45c823126fe38dbcf0b9fc43d93
OK

查看:
192.168.8.131:7000> cluster nodes
#此時7006已經不存在了。

保存當前配置:
192.168.8.131:7000> CLUSTER SAVECONFIG
OK
相關文章
相關標籤/搜索