1. 程式人生 > >redis cluster的簡單學習筆記

redis cluster的簡單學習筆記

 redis cluster
(1)自動將資料進行分片,每個master上放一部分資料
(2)提供內建的高可用支援,部分master不可用時,還是可以繼續工作的
redis cluster不用一致性hash,而是使用hash slots來替代。

為了實現效果:實現了讀寫分離(master-slave),高可用(redis-cluster本身的特性),多個master協同(水平擴容)。做如下試驗:

在三臺虛擬機器上啟動6個redis例項,3個為master,3個為slave

1),編寫配置檔案

redis cluster叢集,要求至少3個master,去組成一個高可用,健壯的分散式的叢集,每個master都建議至少給一個slave,
3個master,3個slave

3臺虛擬機器去搭建6個redis例項的redis cluster

不同虛擬機器上建立目錄,比如在虛擬機器A上:

mkdir -p /etc/redis-cluster
mkdir -p /var/log/redis

mkdir -p /var/redis/7001

mkdir -p /var/redis/7002


每臺虛擬機器上都需要將配置檔案修改一下選項,比如在虛擬機器A上:
port 7001
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-7001.conf
cluster-node-timeout 15000
daemonize    yes                            
pidfile        /var/run/redis_7001.pid                         
dir         /var/redis/7001        
logfile /var/log/redis/7001.log
bind 192.168.0.101   
appendonly yes

將上面的配置檔案,在/etc/redis下放6個,
分別為: 7001.conf,7002.conf,7003.conf,7004.conf,7005.conf,7006.conf

2),準備生產環境的啟動指令碼

在/etc/init.d下,放6個啟動指令碼,分別為:
 redis_7001, redis_7002, redis_7003, redis_7004, redis_7005, redis_7006。每個啟動指令碼內,都修改對應的埠號

3),分別在3臺機器上,啟動6個redis例項。redis-trib.rb是Redis Cluster的一個常用工具。使用redis-trib.rb需要ruby環境,下面就是安裝ruby等環境

wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
tar -zxvf ruby-2.3.1.tar.gz
./configure -prefix=/usr/local/ruby
make && make install
cd ruby所在目錄
cp "ruby所在目錄"/bin/ruby /usr/bin
cp "ruby所在目錄"/bin/gem /usr/bin

wget http://rubygems.org/downloads/redis-3.3.0.gem
gem install -l ./redis-3.3.0.gem
gem list check redis gem

cp “redis安裝所在目錄”/src/redis-trib.rb /usr/bin

--replicas: 每個master有幾個slave

使用redis-tri.rb命令,
redis-trib.rb create --replicas 1 192.168.0.101:7001 192.168.0.101:7002 192.168.0.102:7003 192.168.0.102:7004 192.168.0.103:7005 192.168.0.103:7006

得到的結果如下:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.101:7001
192.168.0.102:7003
192.168.0.103:7005
Adding replica 192.168.0.102:7004 to 192.168.0.101:7001
Adding replica 192.168.0.101:7002 to 192.168.0.102:7003
Adding replica 192.168.0.103:7006 to 192.168.0.103:7005
M: 20df2234e622fcadcce1d135e7458dcfd1c16ca2 192.168.0.101:7001
   slots:0-5460 (5461 slots) master
S: 5ac6d025af59500553df2ba13a265d9519f8865a 192.168.0.101:7002
   replicates 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f
M: 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f 192.168.0.102:7003
   slots:5461-10922 (5462 slots) master
S: ec8d3bd658982ba4180df00fc5ae0d2b79b2fe5b 192.168.0.102:7004
   replicates 20df2234e622fcadcce1d135e7458dcfd1c16ca2
M: cf13c8086fcba76bb9b97a6bf0699069da419e1d 192.168.0.103:7005
   slots:10923-16383 (5461 slots) master
S: 05e3e313c07b31a136bd50191324ba52f90c58a7 192.168.0.103:7006
   replicates cf13c8086fcba76bb9b97a6bf0699069da419e1d
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.0.101:7001)
M: 20df2234e622fcadcce1d135e7458dcfd1c16ca2 192.168.0.101:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5ac6d025af59500553df2ba13a265d9519f8865a 192.168.0.101:7002
   slots: (0 slots) slave
   replicates 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f
S: 05e3e313c07b31a136bd50191324ba52f90c58a7 192.168.0.103:7006
   slots: (0 slots) slave
   replicates cf13c8086fcba76bb9b97a6bf0699069da419e1d
M: 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f 192.168.0.102:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: cf13c8086fcba76bb9b97a6bf0699069da419e1d 192.168.0.103:7005
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: ec8d3bd658982ba4180df00fc5ae0d2b79b2fe5b 192.168.0.102:7004
   slots: (0 slots) slave
   replicates 20df2234e622fcadcce1d135e7458dcfd1c16ca2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
從結果看得出,每個master虛擬機器大概平均被分配了5461個slots

嘗試 check 一下,輸入:
redis-trib.rb check 192.168.0.101:7001

>>> Performing Cluster Check (using node 192.168.0.101:7001)
M: 20df2234e622fcadcce1d135e7458dcfd1c16ca2 192.168.0.101:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5ac6d025af59500553df2ba13a265d9519f8865a 192.168.0.101:7002
   slots: (0 slots) slave
   replicates 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f
S: 05e3e313c07b31a136bd50191324ba52f90c58a7 192.168.0.103:7006
   slots: (0 slots) slave
   replicates cf13c8086fcba76bb9b97a6bf0699069da419e1d
M: 01d1bbd83ec5ec1586d831cfc0edbd0d2ac58b3f 192.168.0.102:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: cf13c8086fcba76bb9b97a6bf0699069da419e1d 192.168.0.103:7005
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: ec8d3bd658982ba4180df00fc5ae0d2b79b2fe5b 192.168.0.102:7004
   slots: (0 slots) slave
   replicates 20df2234e622fcadcce1d135e7458dcfd1c16ca2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

沒問題,16384個slots全被分配了。