Redis Cluster(集群)的搭建

Redis Cluster(集群)的搭建

一、Redis的下載、安裝、啟動(dòng)(單實(shí)例)

我們統(tǒng)一將Redis安裝在/opt目錄下,執(zhí)行命令如下:

$ cd /opt
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar zxvf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make

安裝完成,下面我們啟動(dòng)Redis服務(wù):

$ cd /opt/redis-4.0.9
$ ./src/redis-server

Redis啟動(dòng)成功,將采用Redis的默認(rèn)配置。也可修改Redis根目錄下的redis.conf文件,并在Redis啟動(dòng)時(shí),指定配置文件,如下:

$ ./src/redis-server redis.conf

注意,由于Redis的保護(hù)模式,只綁定了本機(jī)的127.0.0.1,從其他機(jī)器是不能訪問的。所以我們需要添加本機(jī)的ip
192.168.xxx.xxx。

二、Redis Cluster(集群)的搭建

由于我們的機(jī)器有限,我們將采用一臺(tái)機(jī)器多個(gè)端口的方式搭建我們的Redis集群。

首先我們創(chuàng)建Redis的配置文件目錄,如下:

$ cd /opt
$ mkdir redis-cluster

并在redis-cluster目錄下創(chuàng)建6個(gè)節(jié)點(diǎn)的配置文件。分別為:

  • redis-7000.conf
  • redis-7001.conf
  • redis-7002.conf
  • redis-7003.conf
  • redis-7004.conf
  • redis-7005.conf

后面的7000,7001等是redis啟動(dòng)的端口號(hào)。接下來編輯文件的內(nèi)容:

#該集群階段的端口
port 7000
#為每一個(gè)集群節(jié)點(diǎn)指定一個(gè)pid_file
pidfile /var/run/redis_7000.pid
#在bind指令后添加本機(jī)的ip
bind 127.0.0.1 149.28.37.147
#找到Cluster配置的代碼段,使得Redis支持集群
cluster-enabled yes
#每一個(gè)集群節(jié)點(diǎn)都有一個(gè)配置文件,這個(gè)文件是不能手動(dòng)編輯的。確保每一個(gè)集群節(jié)點(diǎn)的配置文件不通
cluster-config-file nodes-7000.conf
#集群節(jié)點(diǎn)的超時(shí)時(shí)間,單位:ms,超時(shí)后集群會(huì)認(rèn)為該節(jié)點(diǎn)失敗
cluster-node-timeout 5000
#最后將appendonly改成yes
appendonly yes

這樣一個(gè)節(jié)點(diǎn)的配置就完成,其他的5個(gè)節(jié)點(diǎn)也做同樣的配置。并將6個(gè)節(jié)點(diǎn)的Redis實(shí)例啟動(dòng):

$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7000.conf &
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7001.conf &
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7002.conf &
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7003.conf &
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7004.conf &
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7005.conf &

使用這6個(gè)節(jié)點(diǎn)創(chuàng)建集群:

$ /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 149.28.37.147:7000 149.28.37.147:7001 149.28.37.147:7002 149.28.37.147:7003 149.28.37.147:7004 149.28.37.147:7005

--replicas 1 表示我們希望為集群中的每個(gè)主節(jié)點(diǎn)創(chuàng)建一個(gè)從節(jié)點(diǎn)。

執(zhí)行命令后會(huì)顯示:

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
149.28.37.147:7000
149.28.37.147:7001
149.28.37.147:7002
Adding replica 149.28.37.147:7004 to 149.28.37.147:7000
Adding replica 149.28.37.147:7005 to 149.28.37.147:7001
Adding replica 149.28.37.147:7003 to 149.28.37.147:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000
   slots:0-5460 (5461 slots) master
M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001
   slots:5461-10922 (5462 slots) master
M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002
   slots:10923-16383 (5461 slots) master
S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003
   replicates b977680e24f23f8fec96876d9014803ca752e2e2
S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004
   replicates 65625091304b0fa2dd75a00f62b6aceac1701094
S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005
   replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900
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 149.28.37.147:7000)
M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005
   slots: (0 slots) slave
   replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900
S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004
   slots: (0 slots) slave
   replicates 65625091304b0fa2dd75a00f62b6aceac1701094
M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003
   slots: (0 slots) slave
   replicates b977680e24f23f8fec96876d9014803ca752e2e2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群搭建完畢。我們可以使用Spring-Boot非常方便的去訪問Redis集群了。

三、Ruby版本過低問題

在使用redis-trib.rb時(shí),需要先安裝ruby:

$ yum -y install ruby ruby-devel rubygems rpm-build
$ gem install redis

這是出現(xiàn)redis requires Ruby version >= 2.2.2的報(bào)錯(cuò),我們先安裝rvm:

$ curl -L get.rvm.io | bash -s stable
$ source /usr/local/rvm/scripts/rvm

查看版本

rvm list known

安裝2.4.1版本

rvm install 2.4.1

使用2.4.1版本

rvm use 2.4.1

移除2.0.0版本

rvm remove 2.0.0

查看當(dāng)前ruby版本

ruby --version

再安裝redis,就可以了

gem install redis
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容