系統:centos 6.9
軟件版本:redis-4.0.8,rubygems-2.7.7,gcc version 4.4.7 20120313,openssl-1.1.0h,zlib-1.2.11node
yum install c++ gccc++
一、安裝redis
tar -xf redis-4.0.8.tar.gz
cd redis-4.0.8
make
make install PREFIX=/redis-clusterredis
二、安裝ruby和rubygems及相關組件(zlib和openssl)
安裝ruby
wget https://rubygems.org/rubygems/rubygems-2.7.7.zip
unzip rubygems-2.7.7.zip
cd rubygems-2.7.7
./configure --prefix=/usr/local/ruby
make
make installvim
安裝zlib
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make installcentos
安裝openssl(1.1是當前穩定版本)
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
tar -xf openssl-1.1.0h.tar.gz
cd openssl-1.1.0h
./config --prefix=/usr/local/openssl #這裏用的是config
# ./config -fPIC --prefix=/usr/local/openssl enable-shared
./config -t
make
make installruby
進入到ruby目錄下安裝zlib和openssl
cd /soft/ruby-2.4.4/ext/zlib #切換到ruby源碼包中
ruby extconf.rb --with-zlib-include=/usr/local/zlib/include/ --with-zlib-lib=/usr/local/zlib/lib
# 會生成一個Makefile文件
# /usr/local/zlib是zlib安裝目錄
# 必需要指定zlib的路徑
make
#出錯提示 make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop
#修改Makefile,把這行記錄 zlib.o: $(top_srcdir)/include/ruby.h 改爲:zlib.o: ../../include/ruby.h
make && make installbash
cd /soft/ruby-2.4.4/ext/openssl
ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib
make
#錯誤提示 make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'. Stop
#打開Makefile文件,將$(top_srcdir)所有改爲../..
make && make install
而後回到/soft/rubygems-2.7.7/目錄下
[root@localhost rubygems-2.7.7]# ruby setup.rb
Bundler 1.16.2 installed
RubyGems 2.7.7 installed
Regenerating binstubs
Parsing documentation for rubygems-2.7.7
Installing ri documentation for rubygems-2.7.7
#安裝
或者下載
gem install redis-3.3.0.gemapp
三、建立redis-cluster 目錄
mkdir -p /redis-cluster/{redis7001,redis7006}
mkdir -p /redis-cluster/{redis7002,redis7005}
mkdir -p /redis-cluster/{redis7003,redis7004} tcp
四、複製redis程序(可選)
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7001/
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7002/
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7003/
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7004/
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7005/
cp -rf /usr/local/redis3.0/* /redis-cluster/redis7006/
# 能夠獨立執行redis-server,也能夠公用一個redis-server bin程序文件測試
五、建立redis.conf腳本
在每一個redis700X目錄下建立redis.conf
port 7001
daemonize yes
pidfile "/redis-cluster/redis7001/redis7001.pid"
logfile "/redis-cluster/redis7001/redis7001.log"
dir "/redis-cluster/redis7001/data"
cluster-enabled yes
cluster-config-file "/redis-cluster/redis7001/7001.conf"
cluster-node-timeout 5000
appendonly yes
#bind 192.168.2.22 若是須要多臺主機通訊,加入這條
# cluster-config-file是集羣的信息文件名,默認與redis7001.conf在同一目錄,不須要指定路徑
# appendonly 表示開啓redis AOF方式的數據持久化
六、運行redis測試
使用腳本批量啓動
cd /redis-cluster
vim startall.sh
#!/bin/bash
cd /redis-cluster
redis-server redis7001/redis.conf
redis-server redis7002/redis.conf
redis-server redis7003/redis.conf
redis-server redis7004/redis.conf
redis-server redis7005/redis.conf
redis-server redis7006/redis.conf
chmod +x startall.sh #增長執行權限
./startall.sh #打開測試
ss -tnl #查看端口
killall redis-server #測試完畢結束全部redis-server進程
七、執行redis-trib.rb啓動集羣
從源碼包裏src目錄中複製redis-trib.rb到/redis-cluster下,按提示輸入yes
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: 63979c7c252db530f828a0b013796eb362107601 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 7b6e483a2060237ab770b192f3cad02448e063be 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 5f519d56955a80d89e4ab773c4e13a1abdfe0872 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: 0198263589b8d89f0179886cfc898294e0cab64f 127.0.0.1:7004
replicates 63979c7c252db530f828a0b013796eb362107601
S: d0db3c39a1ce7ef9148b9b5ca43394735a5d532d 127.0.0.1:7005
replicates 7b6e483a2060237ab770b192f3cad02448e063be
S: 02b3906a4cac9d566c14f58f8818384062242f2c 127.0.0.1:7006
replicates 5f519d56955a80d89e4ab773c4e13a1abdfe0872
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 127.0.0.1:7001)
M: 63979c7c252db530f828a0b013796eb362107601 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 7b6e483a2060237ab770b192f3cad02448e063be 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 5f519d56955a80d89e4ab773c4e13a1abdfe0872 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
M: 0198263589b8d89f0179886cfc898294e0cab64f 127.0.0.1:7004
slots: (0 slots) master
replicates 63979c7c252db530f828a0b013796eb362107601
M: d0db3c39a1ce7ef9148b9b5ca43394735a5d532d 127.0.0.1:7005
slots: (0 slots) master
replicates 7b6e483a2060237ab770b192f3cad02448e063be
M: 02b3906a4cac9d566c14f58f8818384062242f2c 127.0.0.1:7006
slots: (0 slots) master
replicates 5f519d56955a80d89e4ab773c4e13a1abdfe0872
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
八、查看端口
[root@localhost redis-cluster]# netstat -tunl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:17006 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7006 0.0.0.0:* LISTEN
九、登錄到集羣測試
[root@localhost redis-cluster]# redis-cli -h 127.0.0.1 -c -p 7001 #本地能夠不加-h
127.0.0.1:7001> keys *
(empty list or set)
127.0.0.1:7001> set name wajika
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set name ceshi
OK
127.0.0.1:7002> set wajika name
OK
127.0.0.1:7002> set wwwwwwwwwwaaa wajika
-> Redirected to slot [2810] located at 127.0.0.1:7001
OK
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:3976
cluster_stats_messages_received:3976
127.0.0.1:7001>
#集羣部署成功
#關閉集羣的方法[root@localhost redis7001]# redis-cli -c -h 127.0.0.1 -p 7001 shutdown #關閉集羣7001節點,若是沒有-h參數,默認鏈接127.0.0.1,若是沒有-p參數,默認鏈接6370端口#(全部若是用默認的,就沒有-h -p)