参考链接:
https://blog.youkuaiyun.com/u013515384/article/details/89283435
https://blog.youkuaiyun.com/chen_1129/article/details/104603887?
https://blog.youkuaiyun.com/h_big_tiger/article/details/103202732?
一、准备运行环境和安装包
1、电脑系统:Centos7 64位
虚拟机A:192.168.224.128 7000、7001、7002
虚拟机B:192.168.224.130 7003、7004、7005
2、Redis安装包:Redis5.08
redis-5.0.8.tar.gz
二、配置节点
1、配置虚拟机A的节点
(1).在虚拟机A的redis目录下创建redis_cluster目录,接着在redis_cluster目录下创建7000、7001、7002三个节点目录,然后复制redis-5.0.8目录下的配置文件redis.conf到7000、7002、7002三个节点目录下
mkdir redis_cluster
cd redis_cluster/
mkdir 7000 7001 7002
cp ../redis-5.0.8/redis.conf 7000
cp ../redis-5.0.8/redis.conf 7001
cp ../redis-5.0.8/redis.conf 7002
(2).接着分别修改虚拟机A三个节点目录下的redis.conf配置文件,如修改7000目录下redis.conf如下,其他两个同理
#7000/redis.conf
bind 192.168.224.128 # 绑定本机IP
port 7000 # 指定端口
daemonize yes # 后台运行
pidfile /home/redis/redis_cluster/7000/redis_7000.pid # pid文件
cluster-enabled yes # 去掉#号,开启集群
cluster-config-file nodes_7000.conf # 集群配置文件,集群启动后生成
appendonly yes # 开启日志
#7001/redis.conf
bind 192.168.224.128
port 7001
daemonize yes
pidfile /home/redis/redis_cluster/7001/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes_7001.conf
appendonly yes
#7002/redis.conf
bind 192.168.224.128
port 7002
daemonize yes
pidfile /home/redis/redis_cluster/7002/redis_7002.pid
cluster-enabled yes
cluster-config-file nodes_7002.conf
appendonly yes
2、配置虚拟机B的节点
(1).在虚拟机B的redis目录下创建redis_cluster目录,接着在redis_cluster目录下创建7003、7004、7005三个节点目录,然后复制redis-5.0.8目录下的配置文件redis.conf到7003、7004、7005三个节点目录下。
注:当然,拷贝一下,再修改相关的参数也可以的。
mkdir redis_cluster
cd redis_cluster/
mkdir 7003 7004 7005
cp ../redis-5.0.8/redis.conf 7003
cp ../redis-5.0.8/redis.conf 7004
cp ../redis-5.0.8/redis.conf 7005
(2).接着分别修改虚拟机B三个节点目录下的redis.conf配置文件,如修改7003目录下redis.conf如下,其他两个同理
#7003/redis.conf
bind 192.168.224.130
port 7003
daemonize yes
pidfile /home/redis/redis_cluster/7003/redis_7003.pid
cluster-enabled yes
cluster-config-file nodes_7003.conf
appendonly yes
#7004/redis.conf
bind 192.168.224.130
port 7004
daemonize yes
pidfile /home/redis/redis_cluster/7004/redis_7004.pid
cluster-enabled yes
cluster-config-file nodes_7004.conf
appendonly yes
#7005/redis.conf
bind 192.168.224.130
port 7005
daemonize yes
pidfile /home/redis/redis_cluster/7005/redis_7005.pid
cluster-enabled yes
cluster-config-file nodes_7005.conf
appendonly yes
三、启动
1、虚拟机A-编写启动/关闭redis的脚本
(1).在虚拟机A上的redis_cluster下新建且编写start-all.sh启动脚本,如下:
vim start-all.sh
###############start-all.sh###############
../redis-5.0.8/src/redis-server 7000/redis.conf
../redis-5.0.8/src/redis-server 7001/redis.conf
../redis-5.0.8/src/redis-server 7002/redis.conf
echo 'start'
###############start-all.sh###############
(2).新建且编写stop-all.sh停止脚本,如下:
vim stop-all.sh
###############stop-all.sh###############
#!/bin/bash
PORT=7000
ENDPORT=7003
while [ $((PORT < ENDPORT)) != "0" ]; do
echo "Stopping Redis $PORT"
../redis-5.0.8/src/redis-cli -h 192.168.224.128 -p $PORT shutdown
PORT=$((PORT+1))
done
echo "done"
exit 0
###############stop-all.sh###############
(3).为脚本添加可执行权限,并且启动
chmod +x *.sh
./start-all.sh
2、虚拟机B-编写启动/关闭redis的脚本
(1).在虚拟机B上的redis_cluster下新建且编写start-all.sh启动脚本,如下:
vim start-all.sh
###############start-all.sh###############
../redis-5.0.8/src/redis-server 7003/redis.conf
../redis-5.0.8/src/redis-server 7004/redis.conf
../redis-5.0.8/src/redis-server 7005/redis.conf
echo 'start'
###############start-all.sh###############
(2).新建且编写stop-all.sh停止脚本,如下:
# vim stop-all.sh
###############stop-all.sh###############
#!/bin/bash
PORT=7003
ENDPORT=7006
while [ $((PORT < ENDPORT)) != "0" ]; do
echo "Stopping Redis $PORT"
../redis-5.0.8/src/redis-cli -h 192.168.224.130 -p $PORT shutdown
PORT=$((PORT+1))
done
echo "done"
exit 0
###############stop-all.sh###############
(3).为脚本添加可执行权限,并且启动
chmod +x *.sh
./start-all.sh
四、创建集群命令
1、在…/redis-5.0.8/src目录下执行命令
./redis-cli --cluster create 192.168.224.128:7000 192.168.224.128:7001 192.168.224.128:7002 192.168.224.130:7003 192.168.224.130:7004 192.168.224.130:7005 --cluster-replicas 1
如果出现问题如下:
说明防火墙需要设置开启端口,详见附加问题一
五、验证集群是否安装成功
1、虚拟机A上用redis-cli连接192.168.224.130,7004端口
虚拟机A执行
redis-cli -c -h 192.168.1.104 -p 7004
2、虚拟机B上用redis-cli连接192.168.224.128,7004端口
虚拟机B执行
redis-cli -c -h 192.168.1.103 -p 7001
集群创建成功
六、附加
1、问题一:No route to host
注:按照官网解释,在开启7003、7004、7005的同时,redis同时会开启17003、17004、17005端口供集群选举通信使用(CLUSTER MEET),即使用端口的大小加上10000(端口是XXXX,那么通信窗口是1XXXX)
此时开启端口,如果没有设置firewall,可以不进行开启端口的操作
firewall-cmd --zone=public --add-port=17001/tcp --permanent
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=17002/tcp --permanent
firewall-cmd --zone=public --add-port=7002/tcp --permanent
firewall-cmd --zone=public --add-port=17003/tcp --permanent
firewall-cmd --zone=public --add-port=7003/tcp --permanent
firewall-cmd --reload
两台服务器都要开启相应端口的哟。开启之后,再去执行创建集群命令哟。