说明:记录一次Redis集群的搭建过程,以备后面使用。不论几台机器搭建集群,原理都是一样的。直接上干货,详细步骤省略!
1. 机器信息
2台华为云主机:xxx.xxx.xxx.219、xxx.xxx.xxx.181
redis版本:6.0.9
2. 在2台云主机上分别安装Redis
如果想看单例安装的详细步骤,参考这个:https://www.code-tool.com/post/49
cd /opt
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
tar -zxvf redis-6.0.9.tar.gz
cd redis-6.0.9
make
make install PREFIX=/usr/local/redis-cluster
3. 219主机安装步骤
(1)创建集群文件夹
mkdir -p /usr/local/redis-cluster/{219_6001, 219_6002, 219_6003}
(2)创建6001配置文件
cat >> 219_6001/redis.conf << EOF
(3)拷贝如下内容
bind 0.0.0.0
protected-mode no
port 6001
daemonize no
dir /usr/local/redis-cluster/219_6001
cluster-enabled yes
cluster-config-file /usr/local/redis-cluster/219_6001/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/redis-cluster/219_6001/redis.pid
logfile /usr/local/redis-cluster/219_6001/redis.log
EOF
(4)拷贝6002和6003配置文件
sed 's/6001/6002/g' 219_6001/redis.conf > 219_6002/redis.conf
sed 's/6001/6003/g' 219_6001/redis.conf > 219_6003/redis.conf
(5)创建启动文件
cd /usr/local/redis-cluster/
touch start-redis-cluster.sh
#!/bin/sh
REDIS_HOME=/usr/local/redis-cluster
./$REDIS_HOME/bin/redis-server $REDIS_HOME/219_6001/redis.conf
./$REDIS_HOME/bin/redis-server $REDIS_HOME/219_6002/redis.conf
./$REDIS_HOME/bin/redis-server $REDIS_HOME/219_6003/redis.conf
chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
(6)启动服务
如果启动不成功就一个一个启动吧。
./usr/local/redis-cluster/start-redis-cluster.sh
4. 181主机配置
(1)创建集群文件夹
mkdir -p /usr/local/redis-cluster/{181_6001,181_6002,181_6003}
(2)创建6001配置文件
cat >> 181_6001/redis.conf << EOF
(3)拷贝如下内容
bind 0.0.0.0
protected-mode no
port 6001
daemonize no
dir /usr/local/redis-cluster/181_6001
cluster-enabled yes
cluster-config-file /usr/local/redis-cluster/181_6001/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/redis-cluster/181_6001/redis.pid
logfile /usr/local/redis-cluster/181_6001/redis.log
EOF
(4)拷贝6002和6003配置文件
sed 's/6001/6002/g' 181_6001/redis.conf > 181_6002/redis.conf
sed 's/6001/6003/g' 181_6001/redis.conf > 181_6003/redis.conf
(5)创建启动文件
cd /usr/local/redis-cluster/
touch start-redis-cluster.sh
#!/bin/sh
REDIS_HOME=/usr/local/redis-cluster
./$REDIS_HOME/bin/redis-server $REDIS_HOME/181_6001/redis.conf
./$REDIS_HOME/bin/redis-server $REDIS_HOME/181_6002/redis.conf
./$REDIS_HOME/bin/redis-server $REDIS_HOME/181_6003/redis.conf
chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
(6)启动服务
如果启动不成功就一个一个启动吧。
./usr/local/redis-cluster/start-redis-cluster.sh
5. 添加集群配置
如果主机有内容IP,就采用内容IP,这样效率高。下面是我的内容IP,请自行修改!
cd /usr/local/redis-cluster/bin
./redis-cli --cluster create 192.168.0.90:6001 192.168.0.90:6002 192.168.0.90:6003 192.168.0.4:6001 192.168.0.4:6002 192.168.0.4:6003 --cluster-replicas 1 -c -a '123456'
6. 测试
redis-cli -c -p 6001
redis-cli -h 192.168.0.90 -p 6001 -c
7. 添加访问秘钥(2台同样操作)
config set masterauth 123456
config set requirepass 123456
auth 123456
config rewrite
8. 继续测试
redis-cli -h 192.168.0.90 -p 6001 -c -a '123456' --raw
注意:本文归作者所有,未经作者允许,不得转载