Redis集群搭建

(我是利用已經(jīng)安裝過(guò)redis的主機(jī)上進(jìn)行集群搭建的)

1舷丹、要讓集群正常工作至少需要3個(gè)主節(jié)點(diǎn)涮毫,在這里我們要?jiǎng)?chuàng)建6個(gè)redis節(jié)點(diǎn),其中三個(gè)為主節(jié)點(diǎn)呢铆,三個(gè)為從節(jié)點(diǎn)

2晦鞋、創(chuàng)建相關(guān)目錄,主文件夾是/data棺克,在此文件夾下建立6個(gè)子文件夾鳖宾,名稱分別是:8000,8001,8002,8003,8004,8005:

[root@host-10-101-16-201 data]# pwd

/data

[root@host-10-101-16-201 data]# mkdir 8000 8001 8002 8003 8004 8005

[root@host-10-101-16-201 data]# ls

8000? 8001? 8002? 8003? 8004? 8005? redis-4.0.1? redis-4.0.1.tar.gz? tomcat

3、把Redis源文件里面包含的配置文件redis.conf拷貝一份逆航,存放在8000目錄下

[root@host-10-101-16-201 data]# cp redis-4.0.1/redis.conf 8000/

[root@host-10-101-16-201 data]# cd 8000

[root@host-10-101-16-201 8000]# ls

redis.conf

4、使用UE連到這臺(tái)服務(wù)器上去渔肩,對(duì)redis.conf文件進(jìn)行修改因俐,修改后的redis.conf文件內(nèi)容如下(注釋在下面一行):

bind 10.101.16.201

# 綁定服務(wù)器IP地址

port 8000

# 綁定端口號(hào),必須修改,以此來(lái)區(qū)分Redis實(shí)例

daemonize yes

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

pidfile /var/run/redis-8000.pid

# 修改pid進(jìn)程文件名抹剩,以端口號(hào)命名

logfile /data/8000/redis.log

# 修改日志文件名稱撑帖,以端口號(hào)為目錄來(lái)區(qū)分

dir /data/8000/

# 修改數(shù)據(jù)文件存放地址,以端口號(hào)為目錄名來(lái)區(qū)分

cluster-enabled yes

# 啟用集群

cluster-config-file nodes-8000.conf

# 配置每個(gè)節(jié)點(diǎn)的配置文件澳眷,同樣以端口號(hào)為名稱

cluster-node-timeout 15000

# 配置集群節(jié)點(diǎn)的超時(shí)時(shí)間胡嘿,可改可不改

appendonly yes

# 啟動(dòng)AOF增量持久化策略

appendfsync always

# 發(fā)生改變就記錄日志

5、把8000目錄下面的redis.conf復(fù)制到其他五個(gè)目錄下面钳踊,并且把文件里面的8000分別替換成各自的目錄名

[root@host-10-101-16-201 8000]# cp redis.conf ../8001

[root@host-10-101-16-201 8000]# cp redis.conf ../8002

[root@host-10-101-16-201 8000]# cp redis.conf ../8003

[root@host-10-101-16-201 8000]# cp redis.conf ../8004

[root@host-10-101-16-201 8000]# cp redis.conf ../8005

6衷敌、根據(jù)配置文件啟動(dòng)6個(gè)Redis實(shí)例:

[root@host-10-101-16-201 8005]# cd ../redis-4.0.1/src/

[root@host-10-101-16-201 src]# pwd

/data/redis-4.0.1/src

[root@host-10-101-16-201 src]# ./redis-server ../../8000/redis.conf

[root@host-10-101-16-201 src]# ./redis-server ../../8001/redis.conf

[root@host-10-101-16-201 src]# ./redis-server ../../8002/redis.conf

[root@host-10-101-16-201 src]# ./redis-server ../../8003/redis.conf

[root@host-10-101-16-201 src]# ./redis-server ../../8004/redis.conf

[root@host-10-101-16-201 src]# ./redis-server ../../8005/redis.conf

[root@host-10-101-16-201 src]# ps -ef |grep redis

root? ? 25515? ? 1? 0 10:40 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8000 [cluster]

root? ? 25641? ? 1? 0 10:40 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8001 [cluster]

root? ? 25724? ? 1? 0 10:41 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8002 [cluster]

root? ? 25747? ? 1? 0 10:41 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8003 [cluster]

root? ? 25774? ? 1? 0 10:41 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8004 [cluster]

root? ? 25800? ? 1? 0 10:41 ?? ? ? ? 00:00:00 ./redis-server 10.101.16.201:8005 [cluster]

7、創(chuàng)建集群拓瞪,執(zhí)行redis-trib.rb腳本:

[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005

-bash: ruby: command not found

(如果本機(jī)沒(méi)有按住ruby則會(huì)報(bào)錯(cuò))

8缴罗、安裝Ruby,下載地址:http://www.ruby-lang.org/en/downloads/祭埂,我們安裝最新版本:ruby-2.5.3面氓,下載ruby-2.5.3.tar.gz,上傳到服務(wù)器上

10蛆橡、解壓:tar -zxvf?ruby-2.5.3.tar.gz

[root@host-10-101-16-201 data]# ls

8000? 8001? 8002? 8003? 8004? 8005? redis-4.0.1? redis-4.0.1.tar.gz? ruby-2.5.3? ruby-2.5.3.tar.gz

11舌界、安裝ruby:

[root@host-10-101-16-201 ruby-2.5.3]# ./configure

checking for ruby... false

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking target system type... x86_64-pc-linux-gnu

checking for gcc... no

checking for cc... no

checking for cl.exe... no

configure: error: in `/data/ruby-2.5.3':

configure: error: no acceptable C compiler found in $PATH

See `config.log' for more details

(沒(méi)有安裝編譯器)

12、安裝gcc:

[root@host-10-101-16-201 ruby-2.5.3]# yum install gcc

13泰演、再次安裝ruby

[root@host-10-101-16-201 ruby-2.5.3]# ./configure

[root@host-10-101-16-201 ruby-2.5.3]# make && make install

[root@host-10-101-16-201 etc]# ruby -v

ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]

14呻拌、默認(rèn)ruby會(huì)安裝在/usr/local/bin路徑下面,配置一下環(huán)境變量

# cd /etc

# 打開(kāi) profile? 修改path的最后添加: export PATH=/usr/local/bin/ruby:$PATH

# source profile

# echo $PATH? 看下PATH變量中是否成功加入


15粥血、切到redis目錄柏锄,繼續(xù)執(zhí)行redis-trib.rb腳本:

[root@host-10-101-16-201 src]# pwd

/data/redis-4.0.1/src

[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005

Traceback (most recent call last):

? ? ? ? 2: from redis-trib.rb:25:in `<main>'

? ? ? ? 1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'

/usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)

16、安裝rubygems复亏,最新版本會(huì)自動(dòng)安裝:

[root@host-10-101-16-201 src]# yum install rubygems

17趾娃、繼續(xù)執(zhí)行redis-trib.rb腳本:

[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005

Traceback (most recent call last):

? ? ? ? 2: from redis-trib.rb:25:in `<main>'

? ? ? ? 1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'

/usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)

(貌似問(wèn)題沒(méi)有解決)

18、需要安裝redis庫(kù):gem install redis

[root@host-10-101-16-201 src]# gem install redis

ERROR:? Loading command: install (LoadError)

? ? ? ? cannot load such file -- zlib

ERROR:? While executing gem ... (NoMethodError)

? ? undefined method `invoke_with_build_args' for nil:NilClass

19缔御、安裝zlib-1.2.11:

??? 網(wǎng)址:http://www.zlib.net/

??? 下載文件:zlib-1.2.11.tar.gz

??? 通過(guò)FTP上傳zlib-1.2.11.tar.gz到 /usr/local目錄下抬闷,執(zhí)行如下命令安裝:

[root@host-10-101-16-201 data]# ls

8000? 8001? 8002? 8003? 8004? 8005? redis-4.0.1? redis-4.0.1.tar.gz? ruby-2.5.3? ruby-2.5.3.tar.gz? zlib-1.2.11.tar.gz

[root@host-10-101-16-201 data]# tar -zxvf zlib-1.2.11.tar.gz

[root@host-10-101-16-201 data]# cd zlib-1.2.11

[root@host-10-101-16-201 zlib-1.2.11]# ./configure

[root@host-10-101-16-201 zlib-1.2.11]# make && make install

20、繼續(xù)安裝redis庫(kù):gem install redis:

[root@host-10-101-16-201 zlib-1.2.11]# gem install redis

ERROR:? Loading command: install (LoadError)

? ? ? ? cannot load such file -- zlib

ERROR:? While executing gem ... (NoMethodError)

? ? undefined method `invoke_with_build_args' for nil:NilClass

(貌似問(wèn)題沒(méi)有解決)

21耕突、在ruby源文件中安裝zlib笤成,切換到ruby安裝包后解壓的目錄

[root@host-10-101-16-201 data]# cd ruby-2.5.3/ext/zlib/

[root@host-10-101-16-201 zlib]# pwd

/data/ruby-2.5.3/ext/zlib

[root@host-10-101-16-201 zlib]# ruby extconf.rb --with-zlib-include=/usr/local/include/ --with-zlib-lib=/usr/local/lib/

(/usr/local是zlib默認(rèn)的安裝目錄)

[root@host-10-101-16-201 zlib]# make && make install

make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'.? Stop.

22、解決上述報(bào)錯(cuò):

打開(kāi)ext/zlib/Makefile文件眷茁,搜索top_srcdir炕泳,找到下面這一行:

zlib.o: $(top_srcdir)/include/ruby.h

修改為:

zlib.o: ../../include/ruby.h


再進(jìn)行編譯安裝:

[root@host-10-101-16-201 zlib]# make && make install

23、繼續(xù)執(zhí)行命令:gem install redis?

[root@host-10-101-16-201 zlib]# gem install redis

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

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

24上祈、安裝openssl

下載地址:https://www.openssl.org/source/

[root@host-10-101-16-201 data]# ls

8000? 8001? 8002? 8003? 8004? 8005? openssl-1.1.1.tar.gz? redis-4.0.1? redis-4.0.1.tar.gz

[root@host-10-101-16-201 data]# tar -xzvf openssl-1.1.1.tar.gz

[root@host-10-101-16-201 data]# cd openssl-1.1.1

[root@host-10-101-16-201 openssl-1.1.1]# ./config -fPIC --prefix=/usr/local/openssl enable-shared

[root@host-10-101-16-201 openssl-1.1.1]# ./config -t5

[root@host-10-101-16-201 openssl-1.1.1]# make && make install

25培遵、繼續(xù)執(zhí)行命令:gem install redis?

[root@host-10-101-16-201 openssl-1.1.1]# gem install redis

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

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

(貌似問(wèn)題沒(méi)有解決)

26浙芙、在ruby源文件中安裝openssl

[root@host-10-101-16-201 openssl-1.1.1]# cd /data/ruby-2.5.3/ext/openssl/

[root@host-10-101-16-201 openssl]# ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib

備注:/usr/local/openssl是我的openssl安裝目錄

[root@host-10-101-16-201 openssl]# make && make install

compiling openssl_missing.c

make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'.? Stop.

27、解決上述報(bào)錯(cuò):

打開(kāi)Makefile文件籽腕,將$(top_srcdir)全部替換成:../..

再進(jìn)行編譯安裝:

[root@host-10-101-16-201 openssl]# make && make install

28嗡呼、繼續(xù)執(zhí)行命令:gem install redis

[root@host-10-101-16-201 openssl]# gem install redis

Fetching: redis-4.0.3.gem (100%)

Successfully installed redis-4.0.3

Parsing documentation for redis-4.0.3

Installing ri documentation for redis-4.0.3

Done installing documentation for redis after 1 seconds

1 gem installed

29、啟動(dòng)集群:

[root@host-10-101-16-201 src]# pwd

/data/redis-4.0.1/src

[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005

>>> Creating cluster

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

Using 3 masters:

10.101.16.201:8000

10.101.16.201:8001

10.101.16.201:8002

Adding replica 10.101.16.201:8003 to 10.101.16.201:8000

Adding replica 10.101.16.201:8004 to 10.101.16.201:8001

Adding replica 10.101.16.201:8005 to 10.101.16.201:8002

M: 5825f39dc55c7dee5fdb0c726bdef0904c5368e3 10.101.16.201:8000

? slots:0-5460 (5461 slots) master

M: 17a9ad055e50467a86445d94624a26f577f8747b 10.101.16.201:8001

? slots:5461-10922 (5462 slots) master

M: cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d 10.101.16.201:8002

? slots:10923-16383 (5461 slots) master

S: 90027cbadcb3fb47fd239fd1653fe65b39f594e4 10.101.16.201:8003

? replicates 5825f39dc55c7dee5fdb0c726bdef0904c5368e3

S: 8f0b9ecc407d7c9301c4bdab78570f2adb8812ac 10.101.16.201:8004

? replicates 17a9ad055e50467a86445d94624a26f577f8747b

S: bad062eb1e0240d77688a3e6277b0b52ea2c38a7 10.101.16.201:8005

? replicates cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d

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.101.16.201:8000)

M: 5825f39dc55c7dee5fdb0c726bdef0904c5368e3 10.101.16.201:8000

? slots:0-5460 (5461 slots) master

? 1 additional replica(s)

M: 17a9ad055e50467a86445d94624a26f577f8747b 10.101.16.201:8001

? slots:5461-10922 (5462 slots) master

? 1 additional replica(s)

S: 90027cbadcb3fb47fd239fd1653fe65b39f594e4 10.101.16.201:8003

? slots: (0 slots) slave

? replicates 5825f39dc55c7dee5fdb0c726bdef0904c5368e3

S: bad062eb1e0240d77688a3e6277b0b52ea2c38a7 10.101.16.201:8005

? slots: (0 slots) slave

? replicates cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d

M: cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d 10.101.16.201:8002

? slots:10923-16383 (5461 slots) master

? 1 additional replica(s)

S: 8f0b9ecc407d7c9301c4bdab78570f2adb8812ac 10.101.16.201:8004

? slots: (0 slots) slave

? replicates 17a9ad055e50467a86445d94624a26f577f8747b

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@host-10-101-16-201 src]#

30皇耗、到此為止南窗,集群終于搭建起來(lái)了,可以通過(guò)客戶端進(jìn)行訪問(wèn)了郎楼。

(歡迎打賞万伤,一分也是愛(ài))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市箭启,隨后出現(xiàn)的幾起案子壕翩,更是在濱河造成了極大的恐慌,老刑警劉巖傅寡,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件放妈,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡荐操,警方通過(guò)查閱死者的電腦和手機(jī)芜抒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)托启,“玉大人宅倒,你說(shuō)我怎么就攤上這事⊥退剩” “怎么了拐迁?”我有些...
    開(kāi)封第一講書人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)疗绣。 經(jīng)常有香客問(wèn)我线召,道長(zhǎng),這世上最難降的妖魔是什么多矮? 我笑而不...
    開(kāi)封第一講書人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任缓淹,我火速辦了婚禮,結(jié)果婚禮上塔逃,老公的妹妹穿的比我還像新娘讯壶。我一直安慰自己,他們只是感情好湾盗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布伏蚊。 她就那樣靜靜地躺著,像睡著了一般格粪。 火紅的嫁衣襯著肌膚如雪丙挽。 梳的紋絲不亂的頭發(fā)上肺孵,一...
    開(kāi)封第一講書人閱讀 51,610評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音颜阐,去河邊找鬼。 笑死吓肋,一個(gè)胖子當(dāng)著我的面吹牛凳怨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播是鬼,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼肤舞,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了均蜜?” 一聲冷哼從身側(cè)響起李剖,我...
    開(kāi)封第一講書人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎囤耳,沒(méi)想到半個(gè)月后篙顺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡充择,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年德玫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片椎麦。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡宰僧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出观挎,到底是詐尸還是另有隱情琴儿,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布嘁捷,位于F島的核電站造成,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏普气。R本人自食惡果不足惜谜疤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望现诀。 院中可真熱鬧夷磕,春花似錦、人聲如沸仔沿。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)封锉。三九已至绵跷,卻和暖如春膘螟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背碾局。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工荆残, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人净当。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓内斯,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親像啼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子俘闯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容