系统:centos 6.9
软件版本:redis-4.0.8,rubygems-2.7.7,gcc version 4.4.7 20120313,openssl-1.1.0h,zlib-1.2.11
yum install c++ gcc
1、安装redis
tar -xf redis-4.0.8.tar.gz
cd redis-4.0.8
make
make install PREFIX=/redis-cluster
2、安装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 install
安装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 install
安装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 install
进入到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 install
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.gem
3、创建redis-cluster 目录
mkdir -p /redis-cluster/{redis7001,redis7006}
mkdir -p /redis-cluster/{redis7002,redis7005}
mkdir -p /redis-cluster/{redis7003,redis7004}
4、复制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程序文件
5、创建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方式的数据持久化
6、运行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进程
7、执行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.
8、查看端口
[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
9、登陆到集群测试
[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)