以三台服务器为例192.168.100.17 192.168.100.18 192.168.100.19
1、下载redis4.0.9 安装在每台服务器上
cd /opt
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar -zxvf redis-4.0.9.tar.gz
cd redis-4.0.9
make & make install
2、创建目录
#在192.168.100.17创建目录
mkdir -p /opt/redis-4.0.9/redis-cluster/nodes-7000 /opt/redis-4.0.9/redis-cluster/nodes-7001
vim /opt/redis-4.0.9/redis-cluster/nodes-7000/redis.conf
------------------------------------------------
port 7000 #节点端口,在不同的服务器和nodes-xx中
bind 192.168.100.17 #绑定ip
daemonize yes #后台运行
pidfile /var/run/redis_7000.pid #运行pid
dir /data/redis_data/7000 #数据存储地址
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
-------------------------------------------------
mkmdir -p /data/redis_data/7000 /data/redis_data/7001
chmod -R 777 /data/redis_data
#在192.168.100.18创建目录
mkdir -p /opt/redis-4.0.9/redis-cluster/nodes-7002 /opt/redis-4.0.9/redis-cluster/nodes-7003
#在192.168.100.19创建目录
mkdir -p /opt/redis-4.0.9/redis-cluster/nodes-7004 /opt/redis-4.0.9/redis-cluster/nodes-7005
这边只举例17这台服务器,其对应的做修改 ,192.168.18新建nodes-7002、nodes-7003,192.168.100.19新建nodes-7004、nodes-7005
3、ruby安装
redis集群依赖ruby运行
apt install ruby
ruby -v
gem install redis # 安装与redis相关的包
cp /usr/local/src/redis-4.0.1/src/redis-trib.rb /usr/bin/ # 拷贝可执行文件(命令),先看一下是否可以用这个命令,不是必须步骤
4、启动各个节点
#192.168.100.17
nohup src/redis-server redis-cluster/nodes-7000/redis.conf > redis-cluster/nodes-7000/redis-server.out 2>&1 &
nohup src/redis-server redis-cluster/nodes-7001/redis.conf > redis-cluster/nodes-7001/redis-server.out 2>&1 &
#192.168.100.18
nohup src/redis-server redis-cluster/nodes-7002/redis.conf > redis-cluster/nodes-7002/redis-server.out 2>&1 &
nohup src/redis-server redis-cluster/nodes-7003/redis.conf > redis-cluster/nodes-7003/redis-server.out 2>&1 &
#192.168.100.19
nohup src/redis-server redis-cluster/nodes-7004/redis.conf > redis-cluster/nodes-7004/redis-server.out 2>&1 &
nohup src/redis-server redis-cluster/nodes-7005/redis.conf > redis-cluster/nodes-7005/redis-server.out 2>&1 &
5、创建replicas
src/redis-trib.rb create --replicas 1 192.168.100.17:7000 192.168.100.17:7001 192.168.100.18:7002 192.168.100.18:7003 192.168.100.19:7004 192.168.100.19:7005
----------------------------------------------------------------
Using 3 masters:
192.168.100.17:7000
192.168.100.18:7002
192.168.100.19:7004
Adding replica 192.168.100.18:7003 to 192.168.100.17:7000
Adding replica 192.168.100.19:7005 to 192.168.100.18:7002
Adding replica 192.168.100.17:7001 to 192.168.100.19:7004
M: f30e87cc90a89c62847e14488fe7156d8d14cb49 192.168.100.17:7000
slots:0-5460 (5461 slots) master
S: 5b27be99cb89b85d272f13a7b1c20b15b47cb0e3 192.168.100.17:7001
replicates c1f9ae86a742ade6216b77edc39fda69adede343
M: ea6e147d2e2b0923504cdaa99976129c5da9cee2 192.168.100.18:7002
slots:5461-10922 (5462 slots) master
S: b68693d3db1e8690990f5cde193c08a0fe7b0790 192.168.100.18:7003
replicates f30e87cc90a89c62847e14488fe7156d8d14cb49
M: c1f9ae86a742ade6216b77edc39fda69adede343 192.168.100.19:7004
slots:10923-16383 (5461 slots) master
S: 0922c44803c7c0fd1d08aa608a5ca0612d269956 192.168.100.19:7005
replicates ea6e147d2e2b0923504cdaa99976129c5da9cee2
----------------------------------------------------------------
6、登录验证
redis-cli -c -h 192.168.100.17 -p 7000 # 加上-c选项表示以集群的方式登录
--------------------------
192.168.100.17:7000 > cluster nodes #cluster nodes命令可以列出节点
cluster info #cluster info命令可以查看集群信息
--------------------------
redis-trib.rb check 192.168.100.17:7000 # 检测集群状态