Redis集羣搭建

1、操做說明node

須要在192.168.110.66機器上安裝redis環境以後再搭建集羣linux

2、安裝rubygit

一、yum安裝ruby和依賴的包。github

 

[root@localhost redis_cluster]# yum -y install ruby ruby-devel rubygems rpm-buildredis

 

二、使用gem這個命令來安裝redis接口sql

 

[root@localhost redis_cluster]# gem install redisshell

ERROR:  Error installing redis:數據庫

redis requires Ruby version >= 2.2.2.windows

 

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

 

三、升級Ruby的版本

 

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

 

[root@localhost 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: 於 2017年09月11日 星期一 04時59分21秒 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 publickey:

 

    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

 

or if it fails:

 

    command curl -sSL 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 downgradeor upgrade to newer version (if available) or use the second method described above.

 

這一操做獲得了:

 

gpg2 –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

 

而後利用該密鑰下載Ruby並升級。

 

[root@localhost 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@localhost 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: 於 2017年09月11日 星期一 04時59分21秒 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 logout - login 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@localhost ~]# source /usr/local/rvm/scripts/rvm

 

查看Ruby可用版本

 

[root@localhost ~]# 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]

ruby-head

 

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

 

# JRuby

jruby-1.6[.8]

jruby-1.7[.27]

jruby[-9.1.13.0]

jruby-head

 

# Rubinius

rbx-1[.4.3]

rbx-2.3[.0]

rbx-2.4[.1]

rbx-2[.5.8]

rbx-3[.84]

rbx-head

 

# Opal

opal

 

# Minimalistic ruby implementation - ISO 30170:2012

mruby-1.0.0

mruby-1.1.0

mruby-1.2.0

mruby-1[.3.0]

mruby[-head]

 

# Ruby Enterprise Edition

ree-1.8.6

ree[-1.8.7][-2012.02]

 

# Topaz

topaz

 

# MagLev

maglev[-head]

maglev-1.0.0

 

# Mac OS X Snow Leopard Or Newer

macruby-0.10

macruby-0.11

macruby[-0.12]

macruby-nightly

macruby-head

 

# IronRuby

ironruby[-1.1.3]

ironruby-head

 

能夠看到最新的版本是2.4.1,本文安裝2.3.0

 

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

 

[root@localhost ~]# 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, libffi-devel, libtool, readline-devel, sqlite-devel, libyaml-devel..........

Requirements installation successful.

ruby-2.3.0 - #configure

ruby-2.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.

ruby-2.3.0 - #validate archive

ruby-2.3.0 - #extract

ruby-2.3.0 - #validate binary

ruby-2.3.0 - #setup

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

ruby-2.3.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................

ruby-2.3.0 - #generating global wrappers........

ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0

ruby-2.3.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list

ruby-2.3.0 - #generating default wrappers........

 

四、安裝gem redis接口

 

[root@localhost ~]# rvm use 2.3.0

Using /usr/local/rvm/gems/ruby-2.3.0

[root@localhost ~]# rvm remove 1.8.7

ruby-1.8.7-head - #already gone

Using /usr/local/rvm/gems/ruby-2.3.0

[root@localhost ~]#  ruby --version

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

[root@localhost ~]# gem install redis

Fetching: redis-4.0.1.gem (100%)

Successfully installed redis-4.0.1

Parsing documentation for redis-4.0.1

Installing ri documentation for redis-4.0.1

Done installing documentation for redis after 0 seconds

1 gem installed

 

五、安裝rubygems

 

[root@localhost ~]# yum install -y rubygems

 

到目前爲止,咱們的Ruby和運行redis-trib.rb須要的環境安裝完成了。

六、將redis-trib.rb命令添加到系統可執行目錄

[root@localhost ~]# cd /usr/local/redis/src

[root@localhost ~]# cp redis-trib.rb /usr/local/bin

3、集羣部署

[root@localhost ~]# redis-trib.rb  create  --replicas 1 192.168.110.56:7000 192.168.110.56:7001 192.168.110.56:7002 192.168.110.56:7003 192.168.110.63:7004 192.168.110.63:7005 192.168.110.63:7006 192.168.110.63:7007

>>> Creating cluster

>>> Performing hash slots allocation on 8 nodes...

Using 4 masters:

192.168.110.56:7000

192.168.110.63:7004

192.168.110.56:7001

192.168.110.63:7005

Adding replica 192.168.110.63:7006 to 192.168.110.56:7000

Adding replica 192.168.110.56:7002 to 192.168.110.63:7004

Adding replica 192.168.110.63:7007 to 192.168.110.56:7001

Adding replica 192.168.110.56:7003 to 192.168.110.63:7005

M: 33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000

   slots:0-4095 (4096 slots) master

M: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

   slots:8192-12287 (4096 slots) master

S: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

   replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

S: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

   replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

   slots:4096-8191 (4096 slots) master

M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

   slots:12288-16383 (4096 slots) master

S: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

   replicates 33571cb37ef6b868b8413d570c2c212572694204

S: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

   replicates 949607fc0e1ff1f3ca6f6e037c17c655e225cc59

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

M: 33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000

   slots:0-4095 (4096 slots) master

M: 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001

   slots:8192-12287 (4096 slots) master

M: d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002

   slots: (0 slots) master

   replicates 153dca4555605c8a9ccbc5bdf968727c43d3e5a6

M: 8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003

   slots: (0 slots) master

   replicates 25fe0699abf9b5d713fde30af06889a3000bc45a

M: 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004

   slots:4096-8191 (4096 slots) master

M: 25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005

   slots:12288-16383 (4096 slots) master

M: 72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006

   slots: (0 slots) master

   replicates 33571cb37ef6b868b8413d570c2c212572694204

M: 740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007

   slots: (0 slots) master

   replicates 949607fc0e1ff1f3ca6f6e037c17c655e225cc59

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered

 

說明:

(1)上面的命令建立的集羣中有4個master爲:

主節點

192.168.110.56:7000

192.168.110.56:7001

192.168.110.63:7004

192.168.110.63:7005

(2)有4個replicas爲:

從節點

192.168.110.56:7002

192.168.110.56:7003

192.168.110.63:7006

192.168.110.63:7007

(3)cluster集羣中默認是平均分配主節點和從節點的,上面的結果中顯示兩臺redis服務器主從節點各半

4、驗證集羣狀態

一、CLUSTER INFO命令是查看集羣狀態的,咱們在192.168.110.66機器上任意登陸一個集羣中的節點進行驗證

[root@localhost ~]# redis-cli -h 192.168.110.56 -c -p 7000

192.168.110.56:7000> KEYS *

(empty list or set)

192.168.110.56:7000> CLUSTER INFO

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:8

cluster_size:4

cluster_current_epoch:8

cluster_my_epoch:1

cluster_stats_messages_sent:208

cluster_stats_messages_received:208

192.168.110.56:7000> CLUSTER NODES

25fe0699abf9b5d713fde30af06889a3000bc45a 192.168.110.63:7005 master - 0 1521013866398 6 connected 12288-16383

740e86d0ba25e231dad2f4a360dbd2eaf245dfcd 192.168.110.63:7007 slave 949607fc0e1ff1f3ca6f6e037c17c655e225cc59 0 1521013863374 8 connected

153dca4555605c8a9ccbc5bdf968727c43d3e5a6 192.168.110.63:7004 master - 0 1521013865389 5 connected 4096-8191

33571cb37ef6b868b8413d570c2c212572694204 192.168.110.56:7000 myself,master - 0 0 1 connected 0-4095

d1a11f47c26fea4df4f86a0e1fdde253246c2465 192.168.110.56:7002 slave 153dca4555605c8a9ccbc5bdf968727c43d3e5a6 0 1521013865893 5 connected

949607fc0e1ff1f3ca6f6e037c17c655e225cc59 192.168.110.56:7001 master - 0 1521013867406 2 connected 8192-12287

8c7a6ef1bd97b1f4f73ba379d30b1a3947bd049b 192.168.110.56:7003 slave 25fe0699abf9b5d713fde30af06889a3000bc45a 0 1521013862870 6 connected

72afb98122d923d3c4e16027947ec34c33df80b3 192.168.110.63:7006 slave 33571cb37ef6b868b8413d570c2c212572694204 0 1521013864382 7 connected

192.168.110.56:7000>

說明:

(1)咱們能夠看到在輸出的信息中主從節點的顯示

(2)在cluster中無論在哪一個節點進行操做只要在鏈接redis的時候加上-c參數,都會默認去尋找其中的一個主節點進行一樣的操做,而後將數據同步到其餘主從節點上

(3)在cluster中若是有從節點down掉以後,會自動在集羣中剔除,去鏈接其餘可用節點

(4)在cluster中如何主節點down掉以後,會按照默認規則將最後一個從節點升爲主節點

二、能夠測試鏈接從節點而後進行插入數據的操做,而後在其餘的從節點和主節點去get查看鍵值是否存在

     -c 參數:鏈接集羣結點時使用,此選項可防止moved和ask異常。

相關文章
相關標籤/搜索