Centos7上创建Redis5集群

jokewinl 2019-04-15

   一. 基本安装

1.安装
cd /opt
wget wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar xzf redis-5.0.4.tar.gz
ln -s ./redis-5.0.4 ./redis
cd ./redis
make

2.测试
src/redis-server
src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

3.集群创建
mkdir cluster

mkdir 7000 7001 7002 7003 7004 7005
cp ../redis.conf ./7000/
cp ../redis.conf ./7001/
cp ../redis.conf ./7002/
cp ../redis.conf ./7003/
cp ../redis.conf ./7004/
cp ../redis.conf ./7005/

sed -i 's/port 6379/port 7000/g'  ./7000/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7000/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7000/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7000/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7000/redis.conf

sed -i 's/port 6379/port 7000/g'  ./7001/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7001/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7001/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7001/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7001/redis.conf

sed -i 's/port 6379/port 7000/g'  ./7002/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7002/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7002/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7002/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7002/redis.conf

sed -i 's/port 6379/port 7000/g'  ./7003/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7003/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7003/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7003/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7003/redis.conf

sed -i 's/port 6379/port 7000/g'  ./7004/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7004/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7004/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7004/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7004/redis.conf

sed -i 's/port 6379/port 7000/g'  ./7005/redis.conf
sed -i 's/# cluster-enabled/cluster-enabled/g'  ./7005/redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g'  ./7005/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'  ./7005/redis.conf
sed -i 's/appendonly no/appendonly yes/g'  ./7005/redis.conf

    二.集群   

    1.启动全部Node脚本   

    $vi start_all_node.sh

#!/bin/bash

redis_bin_dir="/opt/redis/src"
redis_cluster_dir="/opt/redis/cluster"

cd $redis_cluster_dir/7000
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

cd $redis_cluster_dir/7001
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

cd $redis_cluster_dir/7002
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

cd $redis_cluster_dir/7003
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

cd $redis_cluster_dir/7004
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

cd $redis_cluster_dir/7005
nohup  $redis_bin_dir/redis-server ./redis.conf > log.log 2>&1 &

    2.关闭全部Node脚本

    $vi stop_all_node.sh

#!/bin/bash

ps -wux | grep 'redis-server' |  grep -v 'grep'  |  awk '{print $2}' | xargs kill -9

    3. 启动集群

    

$chmod +x ./start_all_node.sh
#启动全部Node
./start_all_node.sh
#创建集群
/opt/redis/src/redis-cli --cluster create 192.168.0.180:7000 \
192.168.0.180:7001 192.168.0.180:7002 192.168.0.180:7003 192.168.0.180:7004 \
192.168.0.180:7005 --cluster-replicas 1

#查看集群
$/opt/redis/src/redis-cli -p 7000 cluster nodes

    关键配置

cluster-enabled yes     #(yes/no)是否可以作为集群的一个node
requirepass test123     #用户该节点时使用的密码
masterauth test123      #作为从节点时使用该密码连接主节点
daemonize no              #(yes/no)启动后是否作为服务在后台运行
protected-mode no       #(yes/no)是否运行在保护模式,保护模式不允许从主机外的地方连接
port 7000                    #监听端口
bind 0.0.0.0                 #监听IP 不配置和配置 0.0.0.0 一样

相关推荐