(我是利用已經(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))