CentOS完美搭建Redis3.0叢集並附測試

來源:互聯網
上載者:User

標籤:用戶端   yum   速度   app   redis   http   cal   libtool   資料庫   

線上的統一聊天和推送項目使用的是redis主從,redis版本2.8.6

redis主從和mysql主從目的差不多,但redis主從配置很簡單,主要在從節點設定檔指定主節點ip和連接埠:slaveof 192.168.1.197 6379,然後啟動主從,主從就搭建好了redis主從中如果主節點發生故障,不會自動切換,需要藉助redis的Sentinel或者keepalive來實現主的容錯移轉

redis叢集是一個無中心的分布式redis儲存架構,可以在多個節點之間進行資料共用,解決了redis高可用、可擴充等問題,redis叢集提供了以下兩個好處
1、將資料自動切分(split)到多個節點
2、當叢集中的某一個節點故障時,redis還可以繼續處理用戶端的請求。
一個 Redis 叢集包含 16384 個雜湊槽(hash slot),資料庫中的每個資料都屬於這16384個雜湊槽中的一個。叢集使用公式 CRC16(key) % 16384 來計算鍵 key 屬於哪個槽。叢集中的每一個節點負責處理一部分雜湊槽。
叢集中的主從複製
叢集中的每個節點都有1個至N個複製品,其中一個為主節點,其餘的為從節點,如果主節點下線了,叢集就會把這個主節點的一個從節點設定為新的主節點,繼續工作。這樣叢集就不會因為一個主節點的下線而無法正常工作


下面開始搭建redis叢集

由於最小的redis叢集需要3個主節點,一台機器可運行多個redis執行個體,我搭建時使用兩台機器,6個redis執行個體,其中三個主節點,三個從節點作為備份
網上很多使用單台伺服器開6個連接埠,操作差不多,只是配置基本相對簡單點,多台伺服器更接近生產環境

redis 6個節點的ip和連接埠對應關係
server1:
192.168.1.198:7000
192.168.1.198:7001
192.168.1.198:7002
server2:
192.168.1.199:7003
192.168.1.199:7004
192.168.1.199:7005

1、安裝需要的依賴包


[[email protected] ~]# yum install gcc gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip git -y

2、兩台機器分別下載redis並安裝


[[email protected] src]# cd /usr/local/
[[email protected] local]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz
[[email protected] local]# tar xzf redis-3.0.6.tar.gz
[[email protected] local]# cd redis-3.0.6
[[email protected] redis-3.0.6]# make


3、建立叢集需要的目錄

server1執行:

mkdir -p /usr/local/cluster
cd /usr/local/cluster
mkdir 7000
mkdir 7001
mkdir 7002server2執行:

mkdir -p /usr/local/cluster
cd /usr/local/cluster
mkdir 7003
mkdir 7004
mkdir 7005
4、修改設定檔redis.conf
cp /usr/local/redis-3.0.6/redis.conf  /usr/local/cluster
cd /usr/local/cluster
vi redis.conf

##注意每個執行個體的連接埠號碼不同
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes##修改完redis.conf設定檔中的這些配置項之後把這個設定檔分別拷貝到7000/7001/7002/7003/7004/7005節點目錄下
server1執行:

cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
cp /usr/local/cluster/redis.conf /usr/local/cluster/7002server2執行:

cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
cp /usr/local/cluster/redis.conf /usr/local/cluster/7005##注意:拷貝完成之後要分別修改7001/7002/7003/7004/7005目錄下面redis.conf檔案中的port參數,分別改為對應的檔案夾的名稱

5、分別啟動這6個redis執行個體,並查看是否成功:ps -ef|grep redis
server1執行:

[[email protected] cluster]# cd /usr/local/cluster/7000
[[email protected] 7000]# redis-server redis.conf
[[email protected] 7000]# cd /usr/local/cluster/7001
[[email protected] 7001]# redis-server redis.conf
[[email protected] 7001]# cd /usr/local/cluster/7002
[[email protected] 7002]# redis-server redis.conf
[[email protected] 7002]# ps -ef|grep redis
root      2741    1  0 09:39 ?        00:00:00 redis-server *:7000 [cluster]
root      2747    1  0 09:40 ?        00:00:00 redis-server *:7001 [cluster]
root      2751    1  0 09:40 ?        00:00:00 redis-server *:7002 [cluster]
root      2755  2687  0 09:40 pts/0    00:00:00 grep redisserver2執行:
[[email protected] cluster]# cd /usr/local/cluster/7003
[[email protected] 7003]# redis-server redis.conf
[[email protected] 7003]# cd /usr/local/cluster/7004
[[email protected] 7004]# redis-server redis.conf
[[email protected] 7004]# cd /usr/local/cluster/7005
[[email protected] 7005]# redis-server redis.conf
[[email protected] 7005]# ps -ef|grep redis
root      1619    1  0 09:40 ?        00:00:00 redis-server *:7003 [cluster]
root      1623    1  0 09:40 ?        00:00:00 redis-server *:7004 [cluster]
root      1627    1  0 09:41 ?        00:00:00 redis-server *:7005 [cluster]
root      1631  1563  0 09:41 pts/0    00:00:00 grep redis


6、執行redis的建立叢集命令建立叢集(注意ip地址和連接埠號碼)

[[email protected] cluster]# cd /usr/local/redis-3.0.6/src
[[email protected] src]# ./redis-trib.rb  create --replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 192.168.1.199:7003 192.168.1.199:7004 192.168.1.199:70056.1到這一步因為前面第1步裝了依賴包,未提示ruby和rubygems的錯誤,但還是會報錯,提示不能載入redis,是因為缺少redis和ruby的介面,使用gem 安裝
錯誤內容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘: no such file to load -- redis (LoadError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require‘
        from ./redis-trib.rb:25
解決:gem install redis
6.2 再次執行第6步的命令,正常執行,提示是否允許修改設定檔,輸入yes,然後整個叢集配置完成!
[[email protected] src]# ./redis-trib.rb  create --replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 192.168.1.199:7003 192.168.1.199:7004 192.168.1.199:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.199:7003
192.168.1.198:7000
192.168.1.199:7004
Adding replica 192.168.1.198:7001 to 192.168.1.199:7003
Adding replica 192.168.1.199:7005 to 192.168.1.198:7000
Adding replica 192.168.1.198:7002 to 192.168.1.199:7004
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
  slots:5461-10922 (5462 slots) master
S: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
  replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
S: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
  replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
M: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
  slots:0-5460 (5461 slots) master
M: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
  slots:10923-16383 (5461 slots) master
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
  replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
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.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
  slots:5461-10922 (5462 slots) master
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
  slots: (0 slots) master
  replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
  slots: (0 slots) master
  replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
M: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
  slots:0-5460 (5461 slots) master
M: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
  slots:10923-16383 (5461 slots) master
M: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
  slots: (0 slots) master
  replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


7、測試叢集

server1上登入redis用戶端並執行

[[email protected] src]# redis-cli -c -p 7000
127.0.0.1:7000> get key
-> Redirected to slot [12539] located at 192.168.1.199:7004
"val"
192.168.1.199:7004> set name test
-> Redirected to slot [5798] located at 192.168.1.198:7000
OK
192.168.1.198:7000> set adress shanghai
-> Redirected to slot [1562] located at 192.168.1.199:7003
OK
192.168.1.199:7003>server2上登入redis用戶端並執行


[[email protected] src]# redis-cli -c -p 7003
127.0.0.1:7003> set key val
-> Redirected to slot [12539] located at 192.168.1.199:7004
OK
192.168.1.199:7004> get keyv
"val"
192.168.1.199:7004> set key2 val2
-> Redirected to slot [4998] located at 192.168.1.199:7003
OK
192.168.1.199:7003> get key2
"val2"
192.168.1.199:7003>


從中可以發現存時是分布式儲存,取時也是從叢集中取,測試成功


8、redis cluster 架構

1)redis-cluster架構圖

架構細節:

(1)所有的redis節點彼此互聯(PING-PONG機制),內部使用二進位協議最佳化傳輸速度和頻寬.

(2)節點的fail是通過叢集中超過半數的節點檢測失效時才生效.

(3)用戶端與redis節點直連,不需要中間proxy層.用戶端不需要串連叢集所有節點,串連叢集中任何一個可用節點即可

(4)redis-cluster把所有的物理節點映射到[0-16383]slot上,cluster 負責維護node<->slot<->value

2) redis-cluster選舉:容錯

(1)領著選舉過程是叢集中所有master參與,如果半數以上master節點與master節點通訊超過(cluster-node-timeout),認為當前master節點掛掉.

(2):什麼時候整個叢集不可用(cluster_state:fail),當叢集不可用時,所有對叢集的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)錯誤

    a:如果叢集任意master掛掉,且當前master沒有slave.叢集進入fail狀態,也可以理解成進群的slot映射[0-16383]不完成時進入fail狀態.

    b:如果進群超過半數以上master掛掉,無論是否有slave叢集進入fail狀態.

下面關於Redis的文章您也可能喜歡,不妨參考下:

Ubuntu 14.04下Redis安裝及簡單測試 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主從複製基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis叢集明細文檔 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis系列-安裝部署維護篇 http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3安裝Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis安裝部署學習筆記 http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis設定檔redis.conf 詳解 http://www.linuxidc.com/Linux/2013-11/92524.htm

Redis 的詳細介紹:請點這裡 
Redis 的:請點這裡

CentOS完美搭建Redis3.0叢集並附測試

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.