1. 程式人生 > >基於ubuntu搭建Redis(4.0) Cluster 高可用(HA)叢集環境

基於ubuntu搭建Redis(4.0) Cluster 高可用(HA)叢集環境

What is Redis?

Redis is often referred as a data structures server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a server-client model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.

Data structures implemented into Redis have a few special properties:

  • Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that is also non-volatile.
  • Implementation of data structures stress on memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modeled using an high level programming language.
  • Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, cluster, high availability.

Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations to work with complex data types like Lists, Sets, ordered data structures, and so forth.

If you want to know more, this is a list of selected starting points:

配置:

  • 系統:ubuntu16.04
  • redis版本:4.0
  • Jedis客戶端:2.9
  • redis客戶端圖形使用者介面:RedisDesktopManager

搭建步驟

架構方案說明:三組三從,host均為127.0.0.1,port為8001,8002,8003,8004,8005,8006,前三個為maser,後三個為slave
搭建步驟說明:愚者不會和其他部落格說的一樣去建立6個資料夾,因為最後都是通過redis-server 加上自己的配置檔案啟動,所以我只需要配置6個檔案即可,並且放在一個資料夾.

安裝redis4.0

下載安裝
### 架構模型
這裡寫圖片描述

配置redis cluster檔案

-配置master
新建master-8001.conf檔案並加入一下配置

port  8001           
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/master-8001.pid  
cluster-enabled  yes
cluster-config-file  master-8001.conf 
cluster-node-timeout  15000    
appendonly  yes       
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用
cluster-require-full-coverage no  

新建master-8001.conf檔案並加入一下配置

port  8002           
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/master-8002.pid  
cluster-enabled  yes
cluster-config-file  master-8002.conf 
cluster-node-timeout  15000    
appendonly  yes      
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用 
cluster-require-full-coverage no  

新建master-8001.conf檔案並加入一下配置

port  8003           
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/master-8003.pid  
cluster-enabled  yes
cluster-config-file  master-8003.conf 
cluster-node-timeout  15000    
appendonly  yes  
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用
cluster-require-full-coverage no  

-配置slave
新建slave-8004.conf檔案並加入一下配置

port  8004           
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/slave-8004.pid  
cluster-enabled  yes
cluster-config-file  slave-8004.conf 
cluster-node-timeout  15000    
appendonly  yes       
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用
cluster-require-full-coverage no  

新建slave-8005.conf檔案並加入一下配置

port  8005           
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/slave-8005.pid  
cluster-enabled  yes
cluster-config-file  slave-8005.conf 
cluster-node-timeout  15000    
appendonly  yes       
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用
cluster-require-full-coverage no  

新建slave-8006.conf檔案並加入一下配置

port  8006          
bind  127.0.0.1            
daemonize    yes            
pidfile  /var/run/slave-8006.pid  
cluster-enabled  yes
cluster-config-file  slave-8006.conf 
cluster-node-timeout  15000    
appendonly  yes       
#當負責一個插槽的主庫下線且沒有相應的從庫進行故障恢復時,叢集不可用
cluster-require-full-coverage no  

啟動Cluster

#啟動主從節點(根據自己安裝的redis-server和啟動配置檔案路徑自行修改)
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/master-8001.conf
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/master-8002.conf
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/master-8003.conf
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/slave-8004.conf
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/slave-8005.conf
$ ~/redis4.0.1/src/redis-server ~/redis-cluster/slave-8006.conf*
#為主從節點建立叢集關係(redis-trib.rb)
# replicas 1 表示分配幾臺從節點
$ ~/redis4.0.1/src/redis-trib.rb create --replicas 1 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 127.0.0.1:8006

這裡寫圖片描述
按照提示輸入yes就會發出建立cluster的請求,幾秒之後成功如圖所示:
這裡寫圖片描述
ps:若報錯如下則進行ruby安裝:
這裡寫圖片描述

安裝ruby(可選)

$ sudo apt-get install ruby ruby-dev
$ sudo gem install redis

驗證及其使用

驗證

#記得加上-c引數 
$ redis-cli -c -p 8000
redis 127.0.0.1:8000>cluster nodes

顯示如下:

基本使用

$ redis-cli -c -p 8000
redis 127.0.0.1:8000> set foo bar
OK
$ redis-cli -c -p 8001
redis 127.0.0.1:8001> get foo

OK

高可用測試

  • kill 8001埠的程序
  • 重啟 redis 8001 埠

RedisDesktopManager使用

用了一下RedisDesktopManager和FastoRedis,但RedisDesktopManager不能支援叢集檢視和操作,FastoRedis說是說支援叢集,但不能操作叢集的資料,求問大神有沒有更的神器推薦!
這句話來源:https://segmentfault.com/q/1010000008860361/a-1020000009224354
我會持續跟蹤,並且我從網上找了跟多redis視覺化工具,後面都會做個比較.

後記

redis官方推薦的叢集redis cluster已經搭建完畢,但是並不代表代表結束,還需要去仔細分析redis的底層原理及其實現.同時叢集配置配置也挺簡單的,但是前幾天我配置的時候花了一個晚上…..
給出官方教程:http://redisdoc.com/topic/cluster-tutorial.html

參考文章