溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何部署Redis 4.0集群環(huán)境

發(fā)布時(shí)間:2021-11-11 15:30:53 來源:億速云 閱讀:196 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫

本篇內(nèi)容主要講解“如何部署Redis 4.0集群環(huán)境”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何部署Redis 4.0集群環(huán)境”吧!

實(shí)驗(yàn)環(huán)境:

Redis版本:4.0.9

OS:redhat 6.4三臺(tái)

IP:10.10.1.129     Hostname:wjq1      Port:[7001/7002/7003]

IP:10.10.1.130     Hostname:wjq2      Port:[7004/7005/7006]

IP:10.10.1.131     Hostname:wjq3      Port:[7007/7008/7009]

一、安裝redis


詳細(xì)關(guān)于redis 4.0的安裝可參考:CentOS 7.4安裝redis 4.0詳細(xì)步驟【http://blog.itpub.net/31015730/viewspace-2155307/】

下載、解壓、編譯、安裝redis4.0.9

詳細(xì)的安裝過程可參考:

[root@wjq1 wjq-software]# tar -zxvf redis-4.0.9.tar.gz

[root@wjq1 redis-4.0.9]# make

cd src && make all

make[1]: Entering directory `/usr/local/redis/redis-4.0.9/src'

    CC adlist.o

In file included from adlist.c:34:

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory

zmalloc.h:55:2: error: #error "Newer version of jemalloc required"

make[1]: *** [adlist.o] Error 1

make[1]: Leaving directory `/usr/local/redis/redis-4.0.9/src'

make: *** [all] Error 2

在README.md中有這有一段話:

如何部署Redis 4.0集群環(huán)境


解釋一下:

Redis在安裝的時(shí)候關(guān)于內(nèi)存分配器allocator, 如果有MALLOC  這個(gè) 環(huán)境變量, 會(huì)有用這個(gè)環(huán)境變量的 去建立Redis。如果沒有,那么就是用默認(rèn)的分配器;redis2.4版本之后,默認(rèn)使用jemalloc來做內(nèi)存管理,因?yàn)閖emalloc被證明解決fragmentation problem(內(nèi)存碎片化問題)比libc更好,而且libc 并不是默認(rèn)的 分配器, 但是如果你又沒有jemalloc 而只有 libc 當(dāng)然 make 出錯(cuò)。 所以加這么一個(gè)參數(shù)。

解決方法:

[root@wjq1 src]# make MALLOC=libc

如果想用jemalloc的話,安裝jemalloc即可;如果使用yum安裝的話需要配置EPEL源

[root@wjq1 src]# make MALLOC=libc

[root@wjq1 redis-4.0.9]# make PREFIX=/usr/local/redis install

[root@wjq1 redis]# ll

total 8

drwxr-xr-x 2 root root 4096 Jun  5 11:32 bin

drwxr-xr-x 6 root root 4096 Jun  5 11:24 redis-4.0.9

[root@wjq1 redis-4.0.9]# cp redis.conf /etc/redis/

二、創(chuàng)建redis節(jié)點(diǎn)

 

1、首先在10.10.1.129主機(jī)上創(chuàng)建目錄/etc/redis/redis_cluster

[root@wjq1 ~]# mkdir /etc/redis/redis_cluster

2、在redis_cluster目錄下,創(chuàng)建名為7000、70017002的目錄,并將 redis.conf拷貝到這三個(gè)目錄中

[root@wjq1 ~]# mkdir /etc/redis/redis_cluster/{7001,7002,7003}

[root@wjq1 ~]# cp /etc/redis/redis.conf /etc/redis/redis_cluster/7001

[root@wjq1 ~]# cp /etc/redis/redis.conf /etc/redis/redis_cluster/7002

[root@wjq1 ~]# cp /etc/redis/redis.conf /etc/redis/redis_cluster/7003

3、分別修改這三個(gè)配置文件,修改的內(nèi)容如下所示:

[root@wjq1 ~]# cat /etc/redis/redis_cluster/7001/redis.conf |grep -v ^# |grep -v ^$

//默認(rèn)ip為127.0.0.1,需要改為其他節(jié)點(diǎn)機(jī)器可訪問的ip,否則創(chuàng)建集群時(shí)無法訪問對(duì)應(yīng)的端口,無法創(chuàng)建集群

bind 10.10.1.129

//端口7000,7002,7003

port 7001

//redis后臺(tái)運(yùn)行

daemonize yes

//pidfile文件對(duì)應(yīng)7000,7001,7002

pidfile /var/run/redis/redis_7001.pid

//logfile文件對(duì)應(yīng)7000,7001,7002

logfile /var/log/redis/redis_7001.log

//rdb文件對(duì)應(yīng)7000,7001,7002

dbfilename dump7001.rdb

//開啟集群,把注釋#去掉

cluster-enabled yes

//集群的配置,配置文件首次啟動(dòng)自動(dòng)生成 7000,7001,7002

cluster-config-file nodes-7001.conf

//請(qǐng)求超時(shí),默認(rèn)15秒,可自行設(shè)置

cluster-node-timeout 15000


關(guān)于redis配置文件中參數(shù)的說明可參考:redis配置文件中各參數(shù)詳解【http://blog.itpub.net/31015730/viewspace-2154818/】
 

接著在另外兩臺(tái)機(jī)器上
(10.10.1.130、10.10.1.131)重復(fù)以上三步,只是把目錄改為7003、7004、7005、7006、7007、7008對(duì)應(yīng)的配置文件也按照這個(gè)規(guī)則修改即可

三、啟動(dòng)各個(gè)節(jié)點(diǎn)的redis

節(jié)點(diǎn)一:

[root@wjq1 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7001/redis.conf

[root@wjq1 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7002/redis.conf  

[root@wjq1 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7003/redis.conf  

[root@wjq1 ~]#

[root@wjq1 ~]# ps -ef | grep redis

root      40414      1  0 12:36 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.129:7001 [cluster]              

root      40419      1  0 12:36 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.129:7002 [cluster]              

root      40424      1  0 12:36 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.129:7003 [cluster]              

root      40429  38753  0 12:36 pts/0    00:00:00 grep redis

[root@wjq1 ~]#

[root@wjq1 ~]# netstat -tuplan | grep redis

tcp        0      0 10.10.1.129:17003           0.0.0.0:*                   LISTEN      40424/redis-server  

tcp        0      0 10.10.1.129:7001            0.0.0.0:*                   LISTEN      40414/redis-server  

tcp        0      0 10.10.1.129:7002            0.0.0.0:*                   LISTEN      40419/redis-server  

tcp        0      0 10.10.1.129:7003            0.0.0.0:*                   LISTEN      40424/redis-server  

tcp        0      0 10.10.1.129:17001           0.0.0.0:*                   LISTEN      40414/redis-server  

tcp        0      0 10.10.1.129:17002           0.0.0.0:*                   LISTEN      40419/redis-server  

節(jié)點(diǎn)二:

[root@wjq2 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7004/redis.conf

[root@wjq2 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7005/redis.conf

[root@wjq2 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7006/redis.conf

[root@wjq2 ~]#

[root@wjq2 ~]# ps -ef | grep redis

root      39261      1  0 12:38 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.130:7004 [cluster]              

root      39266      1  0 12:38 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.130:7005 [cluster]              

root      39271      1  0 12:38 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.130:7006 [cluster]              

root      39276  38355  0 12:38 pts/0    00:00:00 grep redis

[root@wjq2 ~]#

[root@wjq2 ~]# netstat -tuplan | grep redis

tcp        0      0 10.10.1.130:17004           0.0.0.0:*                   LISTEN      39261/redis-server  

tcp        0      0 10.10.1.130:17005           0.0.0.0:*                   LISTEN      39266/redis-server  

tcp        0      0 10.10.1.130:17006           0.0.0.0:*                   LISTEN      39271/redis-server  

tcp        0      0 10.10.1.130:7004            0.0.0.0:*                   LISTEN      39261/redis-server  

tcp        0      0 10.10.1.130:7005            0.0.0.0:*                   LISTEN      39266/redis-server  

tcp        0      0 10.10.1.130:7006            0.0.0.0:*                   LISTEN      39271/redis-server  

節(jié)點(diǎn)三:

[root@wjq3 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7007/redis.conf

[root@wjq3 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7008/redis.conf

[root@wjq3 ~]# /usr/local/redis/bin/redis-server /etc/redis/redis_cluster/7009/redis.conf

[root@wjq3 ~]#

[root@wjq3 ~]# ps -ef | grep redis

root      24742      1  0 13:00 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.131:7007 [cluster]              

root      24758      1  0 13:01 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.131:7008 [cluster]              

root      24763      1  1 13:01 ?        00:00:00 /usr/local/redis/bin/redis-server 10.10.1.131:7009 [cluster]              

root      24768  23890  1 13:01 pts/0    00:00:00 grep redis

[root@wjq3 ~]#

[root@wjq3 ~]# netstat -tuplan | grep redis

tcp        0      0 10.10.1.131:17007           0.0.0.0:*                   LISTEN      24742/redis-server  

tcp        0      0 10.10.1.131:17008           0.0.0.0:*                   LISTEN      24758/redis-server  

tcp        0      0 10.10.1.131:17009           0.0.0.0:*                   LISTEN      24763/redis-server  

tcp        0      0 10.10.1.131:7007            0.0.0.0:*                   LISTEN      24742/redis-server  

tcp        0      0 10.10.1.131:7008            0.0.0.0:*                   LISTEN      24758/redis-server  

tcp        0      0 10.10.1.131:7009            0.0.0.0:*                   LISTEN      24763/redis-server

注:確保每個(gè)節(jié)點(diǎn)配置正確,并且能夠成功啟動(dòng)起來

隨便找個(gè)節(jié)點(diǎn)測(cè)試一下:

[root@wjq1 ~]# /usr/local/redis/bin/redis-cli -h 10.10.1.129 -p 7002

10.10.1.129:7002>

10.10.1.129:7002> keys *

(empty list or set)

10.10.1.129:7002>

10.10.1.129:7002> set name wjq

(error) CLUSTERDOWN Hash slot not served

連接成功了,但好像報(bào)錯(cuò)了???
(error) CLUSTERDOWN Hash slot not served(不提供集群的散列槽),這是什么鬼?
這是因?yàn)殡m然我們配置并啟動(dòng)了 Redis 集群服務(wù),但是他們暫時(shí)還并不在一個(gè)集群中,互相直接發(fā)現(xiàn)不了,而且還沒有可存儲(chǔ)的位置,就是所謂的slot(槽)

四、redis-trib創(chuàng)建集群

Redis的實(shí)例全部運(yùn)行之后,還需要redis-trib.rb工具來完成集群的創(chuàng)建,redis-trib.rb二進(jìn)制文件在Redis包主目錄下的src目錄中,運(yùn)行該工具依賴Ruby環(huán)境和gem,因此需要提前安裝。

1、安裝ruby【任意一個(gè)節(jié)點(diǎn)安裝即可】

[root@wjq1 ~]# yum install ruby

[root@wjq1 ~]# ruby -v

ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

[root@wjq1 ~]#

[root@wjq1 ~]# gem -v

-bash: gem: command not found

使用yum安裝ruby后,但是沒有g(shù)em命令并且ruby的版本是1.8.7并不滿足后續(xù)的要求;最新redis要求ruby版本在2.2以上,系統(tǒng)自帶ruby大部分都低于2.2,輸入以下命令,刪除舊版本:

[root@wjq1 ~]# yum remove ruby

下面是完成的安裝ruby2.4的步驟:

(1)首先下載ruby2.4或高版本

下載地址:https://www.ruby-lang.org/en/downloads/

(2)安裝ruby之前,首選安裝openssl,否則后續(xù)再安裝ruby的過程中遇到非常多的問題,詳細(xì)的錯(cuò)誤在文章后邊有相應(yīng)的解決方法

OpenSSL官網(wǎng):https://www.openssl.org/

下載OpenSSL版本1.0.2n源碼:

[root@wjq1~]# wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz

解壓并進(jìn)入openssl代碼根目錄:

[root@wjq1 ~]# tar zxvf openssl-1.0.2n.tar.gz  

[root@wjq1 ~]# cd openssl-1.0.2n  

配置、編譯、安裝:

[root@wjq1 openssl-1.0.2n]# ./config  

[root@wjq1 openssl-1.0.2n]# make  

[root@wjq1 openssl-1.0.2n]# make install 

安裝完成后,默認(rèn)的安裝位置為/usr/local/ssl

(3)解壓、編譯安裝ruby

[root@wjq2 ~]# mkdir -p /usr/local/ruby2.4

[root@wjq2 ~]# cd /wjq-software/

[root@wjq2 wjq-software]# ll

total 120240

drwxr-xr-x 20 root root      4096 Jun  5 15:57 openssl-1.0.2n

-rw-r--r--  1 root root   5375802 Jun  5 15:51 openssl-1.0.2n.tar.gz

-rw-r--r--  1 root root     91648 Jun  5 15:50 redis-4.0.1.gem

-rw-r--r--  1 root root  14225338 Jun  5 15:53 ruby-2.4.4.tar.gz

[root@wjq2 wjq-software]#

[root@wjq2 wjq-software]# tar -zxvf ruby-2.4.4.tar.gz

[root@wjq2 ruby-2.4.4]# ./configure -prefix=/usr/local/ruby2.4 --with-openssl-include=/usr/local/ssl/include/ --with-openssl-lib=/usr/local/ssl/lib

[root@wjq2 ruby-2.4.4]# make

[root@wjq2 ruby-2.4.4]# make install

[root@wjq2 ruby-2.4.4]# /usr/local/ruby2.4/bin/ruby -v

ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-linux]

[root@wjq2 ruby-2.4.4]#

[root@wjq2 ruby-2.4.4]# /usr/local/ruby2.4/bin/gem -v

2.6.14.1

[root@wjq2 ruby-2.4.4]# vim ~/.bash_profile

RUBY_PATH=/usr/local/ruby2.4/bin

PATH=$PATH:$HOME/bin:/usr/local/mongdb/bin:$RUBY_PATH

2、使用gem安裝redis安裝redis的接口

[root@wjq2 ~]# gem install redis

##由于源的原因,可能下載失敗,就手動(dòng)下載下來安裝

下載地址:https://rubygems.org/gems/redis/versions/

[root@wjq2 ~]# gem install /wjq-software/redis-4.0.1.gem

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

WARNING:  Unable to pull data from 'https://rubygems.org/': timed out (https://api.rubygems.org/specs.4.8.gz)

1 gem installed

3、開始創(chuàng)建集群,這才是真正的集群

Redis 官方提供了 redis-trib.rb 這個(gè)工具,就在解壓目錄的 src 目錄中

簡單解釋一下這個(gè)命令:調(diào)用 ruby 命令來進(jìn)行創(chuàng)建集群:

--replicas 1 表示主從復(fù)制比例為 1:1,即一個(gè)主節(jié)點(diǎn)對(duì)應(yīng)一個(gè)從節(jié)點(diǎn);然后,默認(rèn)給我們分配好了每個(gè)主節(jié)點(diǎn)和對(duì)應(yīng)從節(jié)點(diǎn)服務(wù),以及 solt 的大小,因?yàn)樵?Redis 集群中有且僅有 16383 個(gè) solt ,默認(rèn)情況會(huì)給我們平均分配,當(dāng)然你可以指定,后續(xù)的增減節(jié)點(diǎn)也可以重新分配。

[root@wjq2 ~]# /usr/local/redis/redis-4.0.9/src/redis-trib.rb create --replicas 1 \

> 10.10.1.129:7001 10.10.1.129:7002 10.10.1.129:7003 \

> 10.10.1.130:7004 10.10.1.130:7005 10.10.1.130:7006 \

> 10.10.1.131:7007 10.10.1.131:7008 10.10.1.131:7009

>>> Creating cluster

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

Using 4 masters:

10.10.1.129:7001

10.10.1.130:7004

10.10.1.131:7007

10.10.1.129:7002

Adding replica 10.10.1.131:7008 to 10.10.1.129:7001

Adding replica 10.10.1.129:7003 to 10.10.1.130:7004

Adding replica 10.10.1.130:7006 to 10.10.1.131:7007

Adding replica 10.10.1.131:7009 to 10.10.1.129:7002

Adding replica 10.10.1.130:7005 to 10.10.1.129:7001

M: 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 10.10.1.129:7001

   slots:0-4095 (4096 slots) master

M: 924d61969343b5cc2200bd3a2277e815dc76048c 10.10.1.129:7002

   slots:12288-16383 (4096 slots) master

S: b9ba251f575b5396da4bea307e25a98d85b3c504 10.10.1.129:7003

   replicates a4a5de0be9bb5704eec17cbe0223076eb38fc4a4

M: a4a5de0be9bb5704eec17cbe0223076eb38fc4a4 10.10.1.130:7004

   slots:4096-8191 (4096 slots) master

S: bdc2f6b254459a6a6d038d93e5a3d3a67fe3e936 10.10.1.130:7005

   replicates 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957

S: 4ae20400d02e57e274f9b9f29d4ba120aa2b574c 10.10.1.130:7006

   replicates 2b0f974e151cd798f474107ac68a47e188cc88a2

M: 2b0f974e151cd798f474107ac68a47e188cc88a2 10.10.1.131:7007

   slots:8192-12287 (4096 slots) master

S: b240d86fdf6abc73df059baf64b930387664da15 10.10.1.131:7008

   replicates 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957

S: 5b7989d5370aef41679e92a6bd34c30ac3be3581 10.10.1.131:7009

   replicates 924d61969343b5cc2200bd3a2277e815dc76048c

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 10.10.1.129:7001)

M: 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 10.10.1.129:7001

   slots:0-4095 (4096 slots) master

   2 additional replica(s)

S: 4ae20400d02e57e274f9b9f29d4ba120aa2b574c 10.10.1.130:7006

   slots: (0 slots) slave

   replicates 2b0f974e151cd798f474107ac68a47e188cc88a2

M: 924d61969343b5cc2200bd3a2277e815dc76048c 10.10.1.129:7002

   slots:12288-16383 (4096 slots) master

   1 additional replica(s)

M: 2b0f974e151cd798f474107ac68a47e188cc88a2 10.10.1.131:7007

   slots:8192-12287 (4096 slots) master

   1 additional replica(s)

S: 5b7989d5370aef41679e92a6bd34c30ac3be3581 10.10.1.131:7009

   slots: (0 slots) slave

   replicates 924d61969343b5cc2200bd3a2277e815dc76048c

S: b9ba251f575b5396da4bea307e25a98d85b3c504 10.10.1.129:7003

   slots: (0 slots) slave

   replicates a4a5de0be9bb5704eec17cbe0223076eb38fc4a4

S: b240d86fdf6abc73df059baf64b930387664da15 10.10.1.131:7008

   slots: (0 slots) slave

   replicates 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957

S: bdc2f6b254459a6a6d038d93e5a3d3a67fe3e936 10.10.1.130:7005

   slots: (0 slots) slave

   replicates 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957

M: a4a5de0be9bb5704eec17cbe0223076eb38fc4a4 10.10.1.130:7004

   slots:4096-8191 (4096 slots) master

   1 additional replica(s)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

出現(xiàn)上述的輸出則代表集群搭建成功啦!!!

說明:

M: 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 為主節(jié)點(diǎn)id

S: b240d86fdf6abc73df059baf64b930387664da15 為從節(jié)點(diǎn)的id

目前來看,7001、7002、7004、7007 為主節(jié)點(diǎn),7003、7005、7008、7009 為從節(jié)點(diǎn),并向你確認(rèn)是否同意這么配置。輸入 yes 后,會(huì)開始集群創(chuàng)建。

五、集群驗(yàn)證

1、隨意登錄一個(gè)節(jié)點(diǎn),查看集群的信息和節(jié)點(diǎn)的信息

加參數(shù) -C 可連接到集群,因?yàn)?redis.conf 將 bind 改為了ip地址,所以 -h 參數(shù)不可以省略,-p 參數(shù)為端口號(hào)

[root@wjq2 ~]# /usr/local/redis/bin/redis-cli -h 10.10.1.130 -p 7005 -c

10.10.1.130:7005>

10.10.1.130:7005> cluster info

cluster_state:ok                 #集群狀態(tài)

cluster_slots_assigned:16384     #被分配的槽位數(shù)

cluster_slots_ok:16384           #正確分配的槽位

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:9            #集群節(jié)點(diǎn)數(shù)

cluster_size:4

cluster_current_epoch:9

cluster_my_epoch:1

cluster_stats_messages_ping_sent:215

cluster_stats_messages_pong_sent:225

cluster_stats_messages_meet_sent:5

cluster_stats_messages_sent:445

cluster_stats_messages_ping_received:222

cluster_stats_messages_pong_received:220

cluster_stats_messages_meet_received:3

cluster_stats_messages_received:445

10.10.1.130:7005>

10.10.1.130:7005> cluster nodes

4ae20400d02e57e274f9b9f29d4ba120aa2b574c 10.10.1.130:7006@17006 slave 2b0f974e151cd798f474107ac68a47e188cc88a2 0 1528186867658 7 connected

924d61969343b5cc2200bd3a2277e815dc76048c 10.10.1.129:7002@17002 master - 0 1528186865000 2 connected 12288-16383

b240d86fdf6abc73df059baf64b930387664da15 10.10.1.131:7008@17008 slave 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 0 1528186865643 8 connected

7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 10.10.1.129:7001@17001 master - 0 1528186862612 1 connected 0-4095

bdc2f6b254459a6a6d038d93e5a3d3a67fe3e936 10.10.1.130:7005@17005 myself,slave 7a047cfaae70c30d0d7e1a5d9854eb7f11afe957 0 1528186863000 5 connected

b9ba251f575b5396da4bea307e25a98d85b3c504 10.10.1.129:7003@17003 slave a4a5de0be9bb5704eec17cbe0223076eb38fc4a4 0 1528186865000 4 connected

a4a5de0be9bb5704eec17cbe0223076eb38fc4a4 10.10.1.130:7004@17004 master - 0 1528186866650 4 connected 4096-8191

5b7989d5370aef41679e92a6bd34c30ac3be3581 10.10.1.131:7009@17009 slave 924d61969343b5cc2200bd3a2277e815dc76048c 0 1528186866000 9 connected

2b0f974e151cd798f474107ac68a47e188cc88a2 10.10.1.131:7007@17007 master - 0 1528186864631 7 connected 8192-12287

10.10.1.130:7005>

2、在其中的一個(gè)節(jié)點(diǎn)插入一個(gè)key

10.10.1.130:7005> keys *

(empty list or set)

10.10.1.130:7005>

10.10.1.130:7005> set name wjq

-> Redirected to slot [5798] located at 10.10.1.130:7004

OK

10.10.1.130:7004> get name

"wjq"

在其他的節(jié)點(diǎn)驗(yàn)證是否成功

[root@wjq3 ~]# /usr/local/redis/bin/redis-cli -h 10.10.1.131 -p 7009 -c

10.10.1.131:7009> get name

-> Redirected to slot [5798] located at 10.10.1.130:7004

"wjq"

10.10.1.130:7004>

六、小結(jié)

redis cluster在設(shè)計(jì)的時(shí)候,就考慮到了去中心化、去中間件,也就是說,集群中的每個(gè)節(jié)點(diǎn)都是平等關(guān)系,都是對(duì)等的,每個(gè)節(jié)點(diǎn)都保存各自的數(shù)據(jù)和整個(gè)集群的狀態(tài)。每個(gè)節(jié)點(diǎn)都和其他所有節(jié)點(diǎn)連接,而且這些連接保持活躍,這樣就保證了我們只需要連接集群中的任意一個(gè)節(jié)點(diǎn),就可以獲取到其他節(jié)點(diǎn)的數(shù)據(jù)。

Redis 集群沒有并使用傳統(tǒng)的一致性哈希來分配數(shù)據(jù),而是采用另外一種叫做哈希槽 (hash slot)的方式來分配的。redis cluster 默認(rèn)分配了 16384 個(gè) slot,當(dāng)我們 set 一個(gè) key 時(shí),會(huì)用CRC16算法來取模得到所屬的 slot,然后將這個(gè) key 分到哈希槽區(qū)間的節(jié)點(diǎn)上,具體算法就是:CRC16(key) % 16384。所以我們?cè)跍y(cè)試的時(shí)候看到 set 和 get 的時(shí)候,直接跳轉(zhuǎn)到了7000端口的節(jié)點(diǎn)。

Redis 集群會(huì)把數(shù)據(jù)存在一個(gè) master 節(jié)點(diǎn),然后在這個(gè) master 和其對(duì)應(yīng)的 salve 之間進(jìn)行數(shù)據(jù)同步。當(dāng)讀取數(shù)據(jù)時(shí),也根據(jù)一致性哈希算法到對(duì)應(yīng)的 master 節(jié)點(diǎn)獲取數(shù)據(jù)。只有當(dāng)一個(gè) master 掛掉之后,才會(huì)啟動(dòng)一個(gè)對(duì)應(yīng)的 salve 節(jié)點(diǎn),充當(dāng) master 。

需要注意的是:必須要3個(gè)或以上的主節(jié)點(diǎn),否則在創(chuàng)建集群時(shí)會(huì)失敗,并且當(dāng)存活的主節(jié)點(diǎn)數(shù)小于總節(jié)點(diǎn)數(shù)的一半時(shí),整個(gè)集群就無法提供服務(wù)了。

 

但是在搭建的過程中遇到一些問題,下面是問題的處理方法:

搭建過程中遇到的問題總結(jié):

[root@wjq1 ~]# gem install redis

#由于源的原因,可能下載失敗,就手動(dòng)下載下來安裝

[root@wjq2 ~]# gem install /wjq-software/redis-4.0.1.gem

ERROR:  While executing gem ... (Gem::Exception)

    Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources

原因
缺少openssl,需要安裝openssl包,但是之前我已經(jīng)安裝一個(gè)openssl-1.0.2n.tar.gz

解決方法:

[root@wjq1 ruby-2.4.4]# cd ./ext/openssl/

[root@wjq1 openssl]# ruby extconf.rb

checking for t_open() in -lnsl... no

checking for socket() in -lsocket... no

checking for openssl/ssl.h... no

提示沒有找到ssl.h, 因?yàn)槌霈F(xiàn)了錯(cuò)誤:openssl/ssl.h:沒有那個(gè)文件,接著按照下面的方法執(zhí)行

[root@wjq1 openssl]# ruby extconf.rb --with-openssl-include=/usr/local/ssl/include/ --with-openssl-lib=/usr/local/ssl/lib         

checking for t_open() in -lnsl... no

checking for socket() in -lsocket... no

checking for openssl/ssl.h... yes

checking for OpenSSL version is 0.9.8 or later... yes

.....

checking for SSL_CTX_set_min_proto_version in openssl/ssl.h... no

checking for SSL_CTX_get_security_level()... no

checking for X509_get0_notBefore()... no

checking for SSL_SESSION_get_protocol_version()... no

creating extconf.h

creating Makefile

接下來并且將ruby 源碼目錄下的include目錄軟鏈接到 / 目錄下:

[root@wjq1 openssl]# ln -s /wjq-software/ruby-2.4.4/include/ /        

[root@wjq1 openssl]# ll /

lrwxrwxrwx    1 root root    33 Jun  5 15:08 include -> /wjq-software/ruby-2.4.4/include/

接著再執(zhí)行make,如果在配置openssl時(shí)沒有-fPIC參數(shù)時(shí)就會(huì)出現(xiàn)如下錯(cuò)誤

[root@wjq1 openssl]#  make

compiling openssl_missing.c

compiling ossl.c

compiling ossl_asn1.c

.....

compiling ossl_x509revoked.c

compiling ossl_x509store.c

linking shared-object openssl.so

/usr/bin/ld: /usr/local/ssl/lib/libssl.a(s3_meth.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

/usr/local/ssl/lib/libssl.a: could not read symbols: Bad value

collect2: ld returned 1 exit status

make: *** [openssl.so] Error 1

提示重新試過編譯openssl時(shí)帶上-fPIC參數(shù) 。于是重新編譯openssl,當(dāng)出現(xiàn)該錯(cuò)誤的時(shí)候,就需要重新編譯openssl,在編譯的時(shí)候要加上-fPIC參數(shù),然后再次執(zhí)行即可

[root@wjq1 openssl]# make

compiling openssl_missing.c

compiling ossl.c

compiling ossl_asn1.c

.....

compiling ossl_x509store.c

linking shared-object openssl.so

[root@wjq1 openssl]# make install

/usr/bin/install -c -m 0755 openssl.so /usr/local/lib/ruby/site_ruby/2.4.0/x86_64-linux

installing default openssl libraries

安裝成功,回過頭來,這時(shí)候我們已經(jīng)把配置集群遇到的各種問題已經(jīng)解決好了,再次使用gem install 安裝 ruby redis 接口:

[root@wjq1 ~]# gem install /wjq-software/redis-4.0.1.gem

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

WARNING:  Unable to pull data from 'https://rubygems.org/': timed out (https://api.rubygems.org/specs.4.8.gz)

1 gem installed

到此,相信大家對(duì)“如何部署Redis 4.0集群環(huán)境”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI