一、环境准备:

系统OS: CentOS6.8
集群环境:三台主机9个节点
软件版本:redis-4.0.11.tar.gz

redis cluster节点信息:
redis01
172.16.8.13:7000
172.16.8.13:7001
172.16.8.13:7002
redis02
172.16.8.14:7003
172.16.8.14:7004
172.16.8.14:7005
redis03
172.16.8.15:7006
172.16.8.15:7007
172.16.8.15:7008

二、redis安装及配置
安装所需的包
#yum install -y gcc g++ make gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip
yum -y install zlib zlib-devel openssl openssl-devel gcc gcc-c++

百度云 链接:https://pan.baidu.com/s/1L9KdpWieYlr5K1qPRMuK_w 密码:t8ru
离线安装ruby
下载ruby-2.4.4并上传至服务器
https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
把包放到改目录下
tar -xvf ruby-2.4.4.tar.gz 
cd ruby-2.4.4
./configure --prefix=/usr/local/ruby
echo $?
make && make install
echo "PATH=$PATH:/usr/local/ruby/bin" >> /etc/profile
source /etc/profile
which ruby
/usr/bin/ruby
rm -rf /usr/bin/ruby
ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby

[root@localhost ruby-2.4.4]# ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-linux]

离线安装rubygems
下载rubygems-2.7.6.tgz并上传至服务器
https://rubygems.org/rubygems/rubygems-2.7.6.tgz
tar -xvf rubygems-2.7.6.tgz
chown -R root.root rubygems-2.7.6
mv rubygems-2.7.6 /usr/local/
cd /usr/local/rubygems-2.7.6/
ruby setup.rb 
echo "PATH=$PATH:/usr/local/rubygems-2.7.6" >> /etc/profile
source /etc/profile
查看当前版本

[root@localhost tools]# gem -v
2.7.6
-------------------------------------------------------------------------------------------------------------------

离线配置rubygems的redisapi
下载redis的gem并上传至服务器
https://rubygems.org/downloads/redis-3.3.0.gem
下载新的版本
https://rubygems.org/downloads/redis-4.0.2.gem

gem install -l redis-3.3.0.gem

[root@localhost tools]# gem list redis

*** LOCAL GEMS ***

redis (3.3.0)
如果安装是旧的版本
卸载
gem uninstal redis

安装

gem install -l redis-4.0.2.gem
[root@localhost tools]# gem list redis

*** LOCAL GEMS ***

redis (4.0.2)

离线安装tcl

下载tcl8并上传至服务器
https://sourceforge.net/projects/tcl/files/Tcl/8.6.0/
unzip tcl868-src.zip 
cd tcl8.6.8/unix/
./configure --prefix=/usr/local/tcl
echo $?
make
echo $?
make install
echo $?
make install-private-headers
ln -v -sf tclsh8.6 /usr/local/tcl/bin/tclsh
chmod -v 755 /usr/local/tcl/lib/libtcl8.6.so
echo "PATH=$PATH:/usr/local/tcl/bin" >> /etc/profile
source /etc/profile

创建用户

useradd qasuser
echo devops | passwd --stdin qasuser

下载redis安装包
http://download.redis.io/releases/redis-4.0.11.tar.gz

安装及配置

tar -xvf redis-4.0.11.tar.gz -C /app
chown -R qasuser.qasuser /app/redis-4.0.11
cd /app/redis-4.0.11
------------------------------------------------------------------------------------------------------------
如果因为上次编译失败,有残留的文件,做法如下:
[qasuser@localhost redis-4.0.11]$make distclean
------------------------------------------------------------------------------------------------------------
[qasuser@localhost redis-4.0.11]$ make

查看是否编译是否成功

[qasuser@localhost redis-4.0.11]$ echo $?
0

节点1上执行(172.16.8.13)

su - qasuser
mkdir /app/redis-4.0.11/redis-cluster
cd /app/redis-4.0.11/redis-cluster
mkdir {7000,7001,7002}

cat <<EOF>./7000/redis.conf
port 7000
bind 172.16.8.13
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7001/redis.conf
port 7001
bind 172.16.8.13
daemonize yes
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7002/redis.conf
port 7002
bind 172.16.8.13
daemonize yes
pidfile /var/run/redis_7002.pid
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes
EOF
for((i=0;i<=2;i++)); do /app/redis-4.0.11/src/redis-server /app/redis-4.0.11/redis-cluster/700$i/redis.conf; done

qasuser@localhost redis-cluster]$ ps -ef | grep redis-server
qasuser    9742      1  0 Sep04 ?        00:02:51 /app/redis-4.0.11/src/redis-server 172.16.8.13:7000 [cluster]                     
qasuser    9747      1  0 Sep04 ?        00:02:51 /app/redis-4.0.11/src/redis-server 172.16.8.13:7001 [cluster]                     
qasuser    9749      1  0 Sep04 ?        00:02:51 /app/redis-4.0.11/src/redis-server 172.16.8.13:7002 [cluster]                     
qasuser  104976 104464  0 13:54 pts/1    00:00:00 grep redis-server

节点2上执行(172.16.8.14)

su - qasuser
mkdir /app/redis-4.0.11/redis-cluster
cd /app/redis-4.0.11/redis-cluster
mkdir {7003,7004,7005}

cat <<EOF>./7003/redis.conf
port 7003
bind 172.16.8.14
daemonize yes
pidfile /var/run/redis_7003.pid
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7004/redis.conf
port 7004
bind 172.16.8.14
daemonize yes
pidfile /var/run/redis_7004.pid
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7005/redis.conf
port 7005
bind 172.16.8.14
daemonize yes
pidfile /var/run/redis_7005.pid
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes
EOF
for((i=3;i<=5;i++)); do /app/redis-4.0.11/src/redis-server /app/redis-4.0.11/redis-cluster/700$i/redis.conf; done

[qasuser@localhost redis-cluster]$ ps -ef | grep redis-server
qasuser   48130      1  0 11:20 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.14:7003 [cluster]                     
qasuser   48132      1  0 11:20 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.14:7004 [cluster]                     
qasuser   48134      1  0 11:20 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.14:7005 [cluster]                     
qasuser   48768  48593  0 13:56 pts/0    00:00:00 grep redis-server

节点3上执行(172.16.8.15)

su - qasuser
mkdir /app/redis-4.0.11/redis-cluster
cd /app/redis-4.0.11/redis-cluster
mkdir {7006,7007,7008}
cat <<EOF>./7006/redis.conf
port 7006
bind 172.16.8.15
daemonize yes
pidfile /var/run/redis_7006.pid
cluster-enabled yes
cluster-config-file nodes_7006.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7007/redis.conf
port 7007
bind 172.16.8.15
daemonize yes
pidfile /var/run/redis_7007.pid
cluster-enabled yes
cluster-config-file nodes_7007.conf
cluster-node-timeout 10100
appendonly yes
EOF

cat <<EOF>./7008/redis.conf
port 7008
bind 172.16.8.15
daemonize yes
pidfile /var/run/redis_7008.pid
cluster-enabled yes
cluster-config-file nodes_7008.conf
cluster-node-timeout 10100
appendonly yes
EOF
for((i=6;i<=8;i++)); do /app/redis-4.0.11/src/redis-server /app/redis-4.0.11/redis-cluster/700$i/redis.conf; done

[qasuser@localhost redis-cluster]$ ps -ef | grep redis-server
qasuser   48101      1  0 11:24 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.15:7006 [cluster]                     
qasuser   48103      1  0 11:24 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.15:7007 [cluster]                     
qasuser   48105      1  0 11:24 ?        00:00:18 /app/redis-4.0.11/src/redis-server 172.16.8.15:7008 [cluster]                     
qasuser   48548  48361  0 13:57 pts/0    00:00:00 grep redis-server

redis.conf的配置说明:
port 7000                            配置集群的端口,分别第一组7000、7001、7002,第一组7003、7004、7005,第一组7006、7007、7008
bind 本机的IP                        这里的默认配置是127.0.0.1改为内网ip。
daemonsize yes                       允许redis在后台运行
pidfile  /var/run/redis_7000.pid     改成和端口一致
cluster-enabled  yes                 开启集群 把注释去掉
cluster-config-file node_7000.conf   集群的配置,和端口一致
cluster-node-timeout  15000          请求超时,默认为15秒
appendonly  yes                      aof日志开启,有需要就开启,每一次写操作都会记录一条日志。

创建集群


/app/redis-4.0.11/src/redis-trib.rb create --replicas 1 172.16.8.13:7000 172.16.8.13:7001 172.16.8.13:7002 172.16.8.14:7003 172.16.8.14:7004 172.16.8.14:7005 172.16.8.15:7006 172.16.8.15:7007 172.16.8.15:7008

[qasuser@localhost redis-cluster]$ /app/redis-4.0.11/src/redis-trib.rb create --replicas 1 172.16.8.13:7000 172.16.8.13:7001 172.16.8.13:7002 172.16.8.14:7003 172.16.8.14:7004 172.16.8.14:7005 172.16.8.15:7006 172.16.8.15:7007 172.16.8.15:7008
>>> Creating cluster
/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
172.16.8.13:7000
172.16.8.14:7003
172.16.8.15:7006
172.16.8.13:7001
Adding replica 172.16.8.15:7007 to 172.16.8.13:7000
Adding replica 172.16.8.13:7002 to 172.16.8.14:7003
Adding replica 172.16.8.14:7005 to 172.16.8.15:7006
Adding replica 172.16.8.15:7008 to 172.16.8.13:7001
Adding replica 172.16.8.14:7004 to 172.16.8.13:7000
M: 6a4175a51250ed6a88b6caaf1667d055c6f226cd 172.16.8.13:7000
   slots:0-4095 (4096 slots) master
M: 3eeeb535acd80c239a09797cad611cb51ef76845 172.16.8.13:7001
   slots:12288-16383 (4096 slots) master
S: a63a2751d14ff5ba19bdb2ace3eab3a2617f0b87 172.16.8.13:7002
   replicates ecd312c581df995c352c67fc89f8a72993109df4
M: ecd312c581df995c352c67fc89f8a72993109df4 172.16.8.14:7003
   slots:4096-8191 (4096 slots) master
S: b7e4c7cf7da64114967a8a69acaa3aa4f809173f 172.16.8.14:7004
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
S: 9a5700fd232689579cb48f810162ed0a27c28948 172.16.8.14:7005
   replicates 3f9239d43ad592947c7c842de7cce1814ab159d5
M: 3f9239d43ad592947c7c842de7cce1814ab159d5 172.16.8.15:7006
   slots:8192-12287 (4096 slots) master
S: a5ec006d814acce1bf18b77a767291f407fbb7fe 172.16.8.15:7007
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
S: 95016481a4df1c9fbbc3c34e1e434627b266d144 172.16.8.15:7008
   replicates 3eeeb535acd80c239a09797cad611cb51ef76845
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 172.16.8.13:7000)
M: 6a4175a51250ed6a88b6caaf1667d055c6f226cd 172.16.8.13:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
M: 3f9239d43ad592947c7c842de7cce1814ab159d5 172.16.8.15:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
S: a63a2751d14ff5ba19bdb2ace3eab3a2617f0b87 172.16.8.13:7002
   slots: (0 slots) slave
   replicates ecd312c581df995c352c67fc89f8a72993109df4
M: 3eeeb535acd80c239a09797cad611cb51ef76845 172.16.8.13:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 95016481a4df1c9fbbc3c34e1e434627b266d144 172.16.8.15:7008
   slots: (0 slots) slave
   replicates 3eeeb535acd80c239a09797cad611cb51ef76845
S: 9a5700fd232689579cb48f810162ed0a27c28948 172.16.8.14:7005
   slots: (0 slots) slave
   replicates 3f9239d43ad592947c7c842de7cce1814ab159d5
S: b7e4c7cf7da64114967a8a69acaa3aa4f809173f 172.16.8.14:7004
   slots: (0 slots) slave
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
M: ecd312c581df995c352c67fc89f8a72993109df4 172.16.8.14:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
S: a5ec006d814acce1bf18b77a767291f407fbb7fe 172.16.8.15:7007
   slots: (0 slots) slave
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群验证

/app/redis-4.0.11/src/redis-cli -h 172.16.8.13 -c -p 7000
[qasuser@localhost ~]$ /app/redis-4.0.11/src/redis-cli -h 172.16.8.13 -c -p 7000
172.16.8.14:7003> set name qas 
OK
172.16.8.14:7003> get name
"qas"

/app/redis-4.0.11/src/redis-cli -h 172.16.8.14 -c -p 7004
[qasuser@localhost redis-cluster]$ /app/redis-4.0.11/src/redis-cli -h 172.16.8.14 -c -p 7004
172.16.8.14:7004> get name
-> Redirected to slot [5798] located at 172.16.8.14:7003
"qas"
/app/redis-4.0.11/src/redis-cli -h 172.16.8.15 -c -p 7008

[qasuser@localhost redis-cluster]$ /app/redis-4.0.11/src/redis-cli -h 172.16.8.15 -c -p 7008
172.16.8.15:7008> get name
-> Redirected to slot [5798] located at 172.16.8.14:7003
"qas"
----------------------------------------------------------------------------------------------------------------------------
/app/redis-4.0.11/src/redis-trib.rb check 172.16.8.13:7000
[qasuser@localhost ~]$ /app/redis-4.0.11/src/redis-trib.rb check 172.16.8.13:7000
/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Performing Cluster Check (using node 172.16.8.13:7000)
M: 6a4175a51250ed6a88b6caaf1667d055c6f226cd 172.16.8.13:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
M: 3f9239d43ad592947c7c842de7cce1814ab159d5 172.16.8.15:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
S: a63a2751d14ff5ba19bdb2ace3eab3a2617f0b87 172.16.8.13:7002
   slots: (0 slots) slave
   replicates ecd312c581df995c352c67fc89f8a72993109df4
M: 3eeeb535acd80c239a09797cad611cb51ef76845 172.16.8.13:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 95016481a4df1c9fbbc3c34e1e434627b266d144 172.16.8.15:7008
   slots: (0 slots) slave
   replicates 3eeeb535acd80c239a09797cad611cb51ef76845
S: 9a5700fd232689579cb48f810162ed0a27c28948 172.16.8.14:7005
   slots: (0 slots) slave
   replicates 3f9239d43ad592947c7c842de7cce1814ab159d5
S: b7e4c7cf7da64114967a8a69acaa3aa4f809173f 172.16.8.14:7004
   slots: (0 slots) slave
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
M: ecd312c581df995c352c67fc89f8a72993109df4 172.16.8.14:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
S: a5ec006d814acce1bf18b77a767291f407fbb7fe 172.16.8.15:7007
   slots: (0 slots) slave
   replicates 6a4175a51250ed6a88b6caaf1667d055c6f226cd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

列出集群节点

/app/redis-4.0.11/src/redis-cli -h 172.16.8.13 -c -p 7000
[qasuser@localhost ~]$ /app/redis-4.0.11/src/redis-cli -h 172.16.8.13 -c -p 7000
172.16.8.13:7000> cluster nodes
3f9239d43ad592947c7c842de7cce1814ab159d5 172.16.8.15:7006@17006 master - 0 1536205999000 7 connected 8192-12287
a63a2751d14ff5ba19bdb2ace3eab3a2617f0b87 172.16.8.13:7002@17002 slave ecd312c581df995c352c67fc89f8a72993109df4 0 1536205998105 4 connected
3eeeb535acd80c239a09797cad611cb51ef76845 172.16.8.13:7001@17001 master - 0 1536205999000 2 connected 12288-16383
95016481a4df1c9fbbc3c34e1e434627b266d144 172.16.8.15:7008@17008 slave 3eeeb535acd80c239a09797cad611cb51ef76845 0 1536205999000 9 connected
9a5700fd232689579cb48f810162ed0a27c28948 172.16.8.14:7005@17005 slave 3f9239d43ad592947c7c842de7cce1814ab159d5 0 1536206000136 7 connected
6a4175a51250ed6a88b6caaf1667d055c6f226cd 172.16.8.13:7000@17000 myself,master - 0 1536205997000 1 connected 0-4095
b7e4c7cf7da64114967a8a69acaa3aa4f809173f 172.16.8.14:7004@17004 slave 6a4175a51250ed6a88b6caaf1667d055c6f226cd 0 1536205999121 5 connected
ecd312c581df995c352c67fc89f8a72993109df4 172.16.8.14:7003@17003 master - 0 1536205997000 4 connected 4096-8191
a5ec006d814acce1bf18b77a767291f407fbb7fe 172.16.8.15:7007@17007 slave 6a4175a51250ed6a88b6caaf1667d055c6f226cd 0 1536206001139 8 connected

集群信息

172.16.8.13:7000>  cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:4
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1443
cluster_stats_messages_pong_sent:1494
cluster_stats_messages_sent:2937
cluster_stats_messages_ping_received:1486
cluster_stats_messages_pong_received:1443
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:2937

关闭集群
推荐做法:

[qasuser@localhost redis-cluster]$ pkill redis
[qasuser@localhost redis-cluster]$ pkill redis
[qasuser@localhost redis-cluster]$ pkill redis

  
或者循环节点逐个关闭

[qasuser@localhost redis-cluster]$ for((i=0;i<=2;i++)); do /app/redis-4.0.11/src/redis-cli -c -h 172.16.8.13 -p 700$i shutdown; done
[qasuser@localhost redis-cluster]$ for((i=3;i<=5;i++)); do /app/redis-4.0.11/src/redis-cli -c -h 172.16.8.14 -p 700$i shutdown; done
[qasuser@localhost redis-cluster]$ for((i=6;i<=8;i++)); do /app/redis-4.0.11/src/redis-cli -c -h 172.16.8.15 -p 700$i shutdown; done

问题解决:

/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
是因为 版本过低导致  gem 升级redis-4.0.2.gem
步骤如下:
gem uninstal redis
gem install -l redis-4.0.2.gem