歡迎光臨
每天分享高質量文章

Redis 叢集部署及踩過的坑

(點選上方公眾號,可快速關註)


來源:碼上青雲

my.oschina.net/gaoenwei/blog/1623214


本文標的


要在單臺機器上搭建Redis叢集,方式是透過不同的TCP埠啟動多個實體,然後組成叢集,同時記錄在搭建過程中踩過的坑。


安裝準備


centos版本:6.7

redis版本:3.2.3

安裝方式:原始碼安裝

伺服器:1臺


操作步驟


此處預設已安裝好單臺redis

 

1、啟動Redis多個實體


我們在Redis安裝目錄下建立目錄cluster,並編寫7000.conf~7005.conf 6個配置檔案,這6個配置檔案用來啟動6個實體,後面將使用這6個實體組成叢集。


以7000.conf為例,配置檔案需要填寫如下幾項:

port  7000                                        //埠7000,7001,7002,7003,7004,7005        

bind 192.168.186.91                                     //預設ip為127.0.0.1 需要改為其他節點機器可訪問的ip 否則建立叢集時無法訪問對應的埠,無法建立叢集

daemonize    yes                               //redis後臺執行

pidfile  ./redis_7000.pid          //pidfile檔案對應7000,7001,7002,7003,7004,7005

clusterenabled  yes                           //開啟叢集  把註釋#去掉

clusterconfigfile  nodes_7000.conf   //叢集的配置  配置檔案首次啟動自動生成 7000,7001,7002,7003,7004,7005

clusternodetimeout  15000                //請求超時  預設15秒,可自行設定

appendonly  yes                           //aof日誌開啟  有需要就開啟,它會每次寫操作都記錄一條日誌

分別啟動6個實體

redisserver redis_cluster/7000/redis.conf

redisserver redis_cluster/7001/redis.conf

redisserver redis_cluster/7002/redis.conf

redisserver redis_cluster/7003/redis.conf

redisserver redis_cluster/7004/redis.conf

redisserver redis_cluster/7005/redis.conf

啟動成功後,看一下行程

# ps -ef | grep redis | grep cluster

idata    15711 22329  0 18:40 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7000 [cluster]

idata    15740 22329  0 18:40 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7001 [cluster]

idata    15810 22329  0 18:40 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7002 [cluster]

idata    17023 22329  0 18:42 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7003 [cluster]

idata    17030 22329  0 18:42 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7004 [cluster]

idata    17035 22329  0 18:42 pts/10   00:00:00 ./bin/redisserver 192.168.186.91:7005 [cluster]

至此,ip=192.168.186.91機器上建立了6個實體,埠號為port=7000~7005。

2、安裝ruby


1)yum安裝ruby和依賴的包。

[root@itfirst redis_cluster]# yum -y install ruby ruby-devel rubygems rpm-build

2)使用gem這個命令來安裝redis介面

[root@itfirst redis_cluster]# gem install redis

ERROR:  Error installing redis:

redis requires Ruby version >= 2.2.2.

這一步驟中出現了bug,度娘告訴我是Ruby版本太低,需要升級版本。

3)升級Ruby的版本

安裝rvm,我不知道這是個什麼東西,但是感覺像是Ruby的一個包管理器。

[root@itfirst redis_cluster]# curl -L get.rvm.io | bash -s stable

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 24090  100 24090    0     0  10919      0  0:00:02  0:00:02 –:–:– 91242

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

gpg: 20170911 星期一 045921 CST 建立的簽名,使用 RSA,鑰匙號 BF04FF17

gpg: 無法檢查簽名:No public key

Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

 

GPG signature verification failed for ‘/usr/local/rvm/archives/rvm-1.29.3.tgz’‘https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc’! Try to install GPG v2 and then fetch the public key:

 

    gpg2recvkeys 409B6B1796C275462A1703113804BB82D39DC0E3

 

or if it fails:

 

    command curlsSL https://rvm.io/mpapis.asc | gpg2 –import –

 

the key can be compared with:

 

    https://rvm.io/mpapis.asc

    https://keybase.io/mpapis

 

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

這一操作得到了:

gpg2 –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

然後利用該金鑰下載Ruby並升級。

[root@itfirst redis_cluster]# gpg –keyserver hkp://keys.gnupg.net –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

gpg: 下載金鑰‘D39DC0E3’,從 hkp 伺服器 keys.gnupg.net

gpg: /root/.gnupg/trustdb.gpg:建立了信任度資料庫

gpg: 金鑰 D39DC0E3:公鑰“Michal Papis (RVM signing) <mpapis@gmail.com>”已匯入

gpg: 沒有找到任何絕對信任的金鑰

gpg: 合計被處理的數量:1

gpg:           已匯入:1  (RSA: 1)

[root@itfirst redis_cluster]# curl -sSL https://get.rvm.io | bash -s stable

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc

gpg: 20170911 星期一 045921 CST 建立的簽名,使用 RSA,鑰匙號 BF04FF17

gpg: 完好的簽名,來自於“Michal Papis (RVM signing) <mpapis@gmail.com>

gpg:               亦即“Michal Papis <mpapis@gmail.com>

gpg:               亦即“[jpeg image of size 5015]

gpg: 警告:這把金鑰未經受信任的簽名認證!

gpg:       沒有證據表明這個簽名屬於它所聲稱的持有者。

主鑰指紋: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3

子鑰指紋: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17

GPG verified ‘/usr/local/rvm/archives/rvm-1.29.3.tgz’

Creating group ‘rvm’

 

Installing RVM to /usr/local/rvm/

Installation of RVM in /usr/local/rvm/ is almost complete:

 

  * First you need to add all users that will be using rvm to ‘rvm’ group,

    and logoutlogin again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

 

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`

    in all your open shell windows, in rare cases you need to reopen all shell windows.

接著,source環境,讓rvm可用。

[root@itfirst ~]# source /usr/local/rvm/scripts/rvm

檢視Ruby可用版本

[root@itfirst ~]# rvm list known

# MRI Rubies

[ruby]1.8.6[p420]

[ruby]1.8.7[head] # security released on head

[ruby]1.9.1[p431]

[ruby]1.9.2[p330]

[ruby]1.9.3[p551]

[ruby]2.0.0[p648]

[ruby]2.1[.10]

[ruby]2.2[.7]

[ruby]2.3[.4]

[ruby]2.4[.1]

rubyhead

 

# for forks use: rvm install ruby-head- –url https://github.com/github/ruby.git –branch 2.2

 

# JRuby

jruby1.6[.8]

jruby1.7[.27]

jruby[9.1.13.0]

jrubyhead

 

# Rubinius

rbx1[.4.3]

rbx2.3[.0]

rbx2.4[.1]

rbx2[.5.8]

rbx3[.84]

rbxhead

 

# Opal

opal

 

# Minimalistic ruby implementation – ISO 30170:2012

mruby1.0.0

mruby1.1.0

mruby1.2.0

mruby1[.3.0]

mruby[head]

 

# Ruby Enterprise Edition

ree1.8.6

ree[1.8.7][2012.02]

 

# Topaz

topaz

 

# MagLev

maglev[head]

maglev1.0.0

 

# Mac OS X Snow Leopard Or Newer

macruby0.10

macruby0.11

macruby[0.12]

macrubynightly

macrubyhead

 

# IronRuby

ironruby[1.1.3]

ironrubyhead

可以看到最新的版本是2.4.1,本文安裝2.3.0

至此,我們升級了Ruby的版本。

[root@itfirst ~]# rvm install 2.3.0

Searching for binary rubies, this might take some time.

Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/6/x86_64/ruby-2.3.0.tar.bz2

Checking requirements for centos.

Installing requirements for centos.

Installing required packages: autoconf, automake, bison, libffidevel, libtool, readlinedevel, sqlitedevel, libyamldevel……….

Requirements installation successful.

ruby2.3.0#configure

ruby2.3.0#download

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 21.9M  100 21.9M    0     0   266k      0  0:01:24  0:01:24 –:–:–  278k

No checksum for downloaded archive, recording checksum in user configuration.

ruby2.3.0#validate archive

ruby2.3.0#extract

ruby2.3.0#validate binary

ruby2.3.0#setup

ruby2.3.0#gemset created /usr/local/rvm/gems/ruby-2.3.0@global

ruby2.3.0#importing gemset /usr/local/rvm/gemsets/global.gems…………………………

ruby2.3.0#generating global wrappers……..

ruby2.3.0#gemset created /usr/local/rvm/gems/ruby-2.3.0

ruby2.3.0#importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list

ruby2.3.0#generating default wrappers……..

4)安裝gem redis介面

[root@itfirst ~]# rvm use 2.3.0

Using /usr/local/rvm/gems/ruby2.3.0

[root@itfirst ~]# rvm remove 1.8.7

ruby1.8.7head#already gone

Using /usr/local/rvm/gems/ruby2.3.0

[root@itfirst ~]#  ruby –version

ruby 2.3.0p0 (20151225 revision 53290) [x86_64linux]

[root@itfirst ~]# gem install redis

Fetching: redis4.0.1.gem (100%)

Successfully installed redis4.0.1

Parsing documentation for redis4.0.1

Installing ri documentation for redis4.0.1

Done installing documentation for redis after 0 seconds

1 gem installed

5)安裝rubygems

# yum install -y rubygems

到目前為止,我們的Ruby和執行redis-trib.rb需要的環境安裝完成了。

3、Redis叢集搭建


有了Ruby執行環境,可以開始將之前的6個實體組建成叢集了。

redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

有三個master,有三個是slave。 後面跟上6個實體就好了,形式就是ip:port

【此處有坑】

第一坑

[root@itfirst src]# redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

bash: redistrib.rb: command not found

[root@itfirst src]# cp redis-trib.rb /usr/local/bin

需要將redis-trib.rb複製到/usr/local/bin目錄下。

第二坑

[root@itfirst bin]# redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

>>> Creating cluster

[ERR] Node 192.168.186.91:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

踩完第一坑後,繼續執行,發現了第二坑,還好有度娘,但是網上各種說法都有(主要參照了《極客on之路》的部落格),發現錯誤的原因是redis資料庫沒有清除。

[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7001

192.168.186.91:7001> flushdb

OK

192.168.186.91:7001> quit

[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7002

192.168.186.91:7002> flushdb

OK

192.168.186.91:7002> quit

[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7003

192.168.186.91:7003> flushdb

OK

192.168.186.91:7003> quit

[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7004

192.168.186.91:7004> flushdb

OK

192.168.186.91:7004> quit

[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7005

192.168.186.91:7005> flushdb

OK

192.168.186.91:7005> quit

[root@itfirst src]# redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes

Using 3 masters:

192.168.186.91:7000

192.168.186.91:7001

192.168.186.91:7002

Adding replica 192.168.186.91:7003 to 192.168.186.91:7000

Adding replica 192.168.186.91:7004 to 192.168.186.91:7001

Adding replica 192.168.186.91:7005 to 192.168.186.91:7002

M: 61b2b608177374fd0382c872f996a2c25f62daac 192.168.186.91:7000

   slots:05460,9189 (5462 slots) master

M: 50e678c98c31baa4ece1cba096cc34b4545456f3 192.168.186.91:7001

   slots:546110922 (5462 slots) master

M: b8dc855a92d1c9a6e358380286a757011c40601d 192.168.186.91:7002

   slots:9189,1092316383 (5462 slots) master

S: 42392d8b4665500b3229b5c5b9dcebed311c9cdf 192.168.186.91:7003

   replicates 61b2b608177374fd0382c872f996a2c25f62daac

S: 4e8cd9bae1dc0ffa63a3b8315e3f92b0490e65f8 192.168.186.91:7004

   replicates 50e678c98c31baa4ece1cba096cc34b4545456f3

S: 3344981c3290c39b0d9f427842398c17de835293 192.168.186.91:7005

   replicates b8dc855a92d1c9a6e358380286a757011c40601d

Can I set the above configuration? (type ‘yes’ to accept): yes

/usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis/client.rb:119:in `call‘: ERR Slot 9189 is already busy (Redis::CommandError)

from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing’

from /usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis.rb:45:in `block in synchronize

from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize’

from /usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis.rb:45:in `synchronize

from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing’

from /usr/local/bin/redistrib.rb:212:in `flush_node_config

from /usr/local/bin/redis-trib.rb:776:in `block in flush_nodes_config’

from /usr/local/bin/redistrib.rb:775:in `each

from /usr/local/bin/redis-trib.rb:775:in `flush_nodes_config’

from /usr/local/bin/redistrib.rb:1296:in `create_cluster_cmd

from /usr/local/bin/redis-trib.rb:1696:in `

第三坑

[root@itfirst src]# redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes

Using 3 masters:

192.168.186.91:7000

192.168.186.91:7001

192.168.186.91:7002

Adding replica 192.168.186.91:7003 to 192.168.186.91:7000

Adding replica 192.168.186.91:7004 to 192.168.186.91:7001

Adding replica 192.168.186.91:7005 to 192.168.186.91:7002

M: 61b2b608177374fd0382c872f996a2c25f62daac 192.168.186.91:7000

   slots:05460,9189 (5462 slots) master

M: 50e678c98c31baa4ece1cba096cc34b4545456f3 192.168.186.91:7001

   slots:546110922 (5462 slots) master

M: b8dc855a92d1c9a6e358380286a757011c40601d 192.168.186.91:7002

   slots:9189,1092316383 (5462 slots) master

S: 42392d8b4665500b3229b5c5b9dcebed311c9cdf 192.168.186.91:7003

   replicates 61b2b608177374fd0382c872f996a2c25f62daac

S: 4e8cd9bae1dc0ffa63a3b8315e3f92b0490e65f8 192.168.186.91:7004

   replicates 50e678c98c31baa4ece1cba096cc34b4545456f3

S: 3344981c3290c39b0d9f427842398c17de835293 192.168.186.91:7005

   replicates b8dc855a92d1c9a6e358380286a757011c40601d

Can I set the above configuration? (type ‘yes’ to accept): yes

/usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis/client.rb:119:in `call‘: ERR Slot 9189 is already busy (Redis::CommandError)

from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing’

from /usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis.rb:45:in `block in synchronize

from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize’

from /usr/local/rvm/gems/ruby2.3.0/gems/redis4.0.1/lib/redis.rb:45:in `synchronize

from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing’

from /usr/local/bin/redistrib.rb:212:in `flush_node_config

from /usr/local/bin/redis-trib.rb:776:in `block in flush_nodes_config’

from /usr/local/bin/redistrib.rb:775:in `each

from /usr/local/bin/redis-trib.rb:775:in `flush_nodes_config’

from /usr/local/bin/redistrib.rb:1296:in `create_cluster_cmd

from /usr/local/bin/redis-trib.rb:1696:in `

還是度娘靠譜,在《redis 跨機器叢集啟動出錯》部落格中找到了答案。

這是由於之間建立叢集沒有成功,需要將nodes.conf和dir裡面的檔案全部刪除。

[root@itfirst 7000]# find / -name “nodes-7000.conf”

/usr/local/redis3.2.3/src/nodes7000.conf

[root@itfirst 7000]# cd ../../

[root@itfirst src]# rm -rf nodes-700*

然後重啟redis服務。

[root@itfirst src]# redis-trib.rb  create  –replicas  1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes

Using 3 masters:

192.168.186.91:7000

192.168.186.91:7001

192.168.186.91:7002

Adding replica 192.168.186.91:7003 to 192.168.186.91:7000

Adding replica 192.168.186.91:7004 to 192.168.186.91:7001

Adding replica 192.168.186.91:7005 to 192.168.186.91:7002

M: 319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000

   slots:05460 (5461 slots) master

M: 3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001

   slots:546110922 (5462 slots) master

M: 6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002

   slots:1092316383 (5461 slots) master

S: ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003

   replicates 319da27d8668a15d2d2d02afe433247694343459

S: 43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004

   replicates 3da756265e301ac0210760f13e990473f87a3017

S: 3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005

   replicates 6f336da48c892d8e0c541a864765978ebfbca6d5

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.186.91:7000)

M: 319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000

   slots:05460 (5461 slots) master

M: 3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001

   slots:546110922 (5462 slots) master

M: 6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002

   slots:1092316383 (5461 slots) master

M: ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003

   slots: (0 slots) master

   replicates 319da27d8668a15d2d2d02afe433247694343459

M: 43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004

   slots: (0 slots) master

   replicates 3da756265e301ac0210760f13e990473f87a3017

M: 3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005

   slots: (0 slots) master

   replicates 6f336da48c892d8e0c541a864765978ebfbca6d5

[OK] All nodes agree about slots configuration.

>>> Check for open slots

>>> Check slots coverage

[OK] All 16384 slots covered.

4、驗證叢集狀態


登入叢集客戶端,-c標識以叢集方式登入

[root@itfirst src]# redis-cli -h 192.168.186.91 -c -p 7002


檢視叢集狀態

192.168.186.91:7002> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:3

cluster_stats_messages_sent:124174

cluster_stats_messages_received:124174

192.168.186.91:7002> cluster nodes

319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000 master0 1519465974307 1 connected 05460

3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005 slave 6f336da48c892d8e0c541a864765978ebfbca6d5 0 1519465971278 6 connected

6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002 myself,master0 0 3 connected 1092316383

3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001 master0 1519465972288 2 connected 546110922

43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004 slave 3da756265e301ac0210760f13e990473f87a3017 0 1519465973298 5 connected

ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003 slave 319da27d8668a15d2d2d02afe433247694343459 0 1519465969258 4 connected

看完本文有收穫?請轉發分享給更多人

關註「資料分析與開發」,提升資料技能

贊(0)

分享創造快樂