redis5.0.5集群

本文详细介绍如何在CentOs上从零开始搭建Redis集群,包括下载、编译、配置及启动步骤。同时,针对集群中IP变为内网的问题提供了解决方案,涵盖节点重启、配置修改等关键操作。

 参考https://blog.youkuaiyun.com/sunrisetg/article/details/102884488

正常搭建集群后,使用时发现ip变成内网。

解决方案:

1.杀死节点(不是停止集群,否则不生效)

2.找到所有节点的nodes.conf文件,把内网地址改成你的ip

3.重启节点 ./redis-server redis.conf

 

二、redis集群搭建:
1.​​​​CentOs命令如下:​

wget http://download.redis.io/releases/redis-5.0.5.tar.gz
 
tar xzf redis-5.0.5.tar.gz
 
cd redis-5.0.5
 
yum install gcc
 
make MALLOC=libc
 
cd src && make install
 
cd ../utils/create-cluster/
 
ps -ef|grep redis
 
vim create-cluster
2.修改create-cluster配置文件:

注意 create中的ip 不要用localhost,127.0.0.1,要用内网ip如 192.168.1.173或外网IP 38.200.125.10

注:(以下这几点必须弄,否则阿里云公网节点间切换不正常)

2.1. start命令:   加入redis密码        --requirepass 88888888    

                 关闭安全模式(公网可以连接到redis)    --protected-mode no

2.2.create命令: 将127.0.0.1修改为阿里云公网的ip地址 (123.456.200这是假ip)

                 加入redis密码  -a 88888888

2.3.stop命令:加入redis密码       -a 88888888 

2.4.打开阿里云端口

2.4.打开阿里云端口

 

#!/bin/bash

# Settings
PORT=7000
TIMEOUT=2000
NODES=6
REPLICAS=1

# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.

if [ -a config.sh ]
then
    source "config.sh"
fi

# Computed vars
ENDPORT=$((PORT+NODES))

if [ "$1" == "start" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"
        ../../src/redis-server --requirepass 88888888 --protected-mode no  --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
    done
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        HOSTS="$HOSTS 123.456.200:$PORT"
    done
    ../../src/redis-cli -a 88888888  --cluster create $HOSTS --cluster-replicas $REPLICAS
    exit 0
fi

if [ "$1" == "stop" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Stopping $PORT"
        ../../src/redis-cli -a 88888888  -p $PORT shutdown nosave
    done
    exit 0
fi

if [ "$1" == "watch" ]
then
    PORT=$((PORT+1))
    while [ 1 ]; do
        clear
        date
        ../../src/redis-cli -p $PORT cluster nodes | head -30
        sleep 1
    done
    exit 0
fi

if [ "$1" == "tail" ]
then
    INSTANCE=$2
    PORT=$((PORT+INSTANCE))
    tail -f ${PORT}.log
    exit 0
fi

if [ "$1" == "call" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        ../../src/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
    done
    exit 0
fi

if [ "$1" == "clean" ]
then
    rm -rf *.log
    rm -rf appendonly*.aof
    rm -rf dump*.rdb
    rm -rf nodes*.conf
    exit 0
fi

if [ "$1" == "clean-logs" ]
then
    rm -rf *.log
    exit 0
fi

echo "Usage: $0 [start|create|stop|watch|tail|clean]"
echo "start       -- Launch Redis Cluster instances."
echo "create      -- Create a cluster using redis-cli --cluster create."
echo "stop        -- Stop Redis Cluster instances."
echo "watch       -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id>   -- Run tail -f of instance at base port + ID."
echo "clean       -- Remove all instances data, logs, configs."
echo "clean-logs  -- Remove just instances logs."


3.启动集群

#1.启动集群
sh /root/javaxiaobang/redis-5.0.5/utils/create-cluster/create-cluster start
#2.建立集群间联系
sh /root/javaxiaobang/redis-5.0.5/utils/create-cluster/create-cluster create
#3.停止集群
sh /root/javaxiaobang/redis-5.0.5/utils/create-cluster/create-cluster stop
#4.清除集群
sh /root/javaxiaobang/redis-5.0.5/utils/create-cluster/create-cluster clean
      4.连接redis

 redis-cli -h 127.0.0.1 -p 7001 -c -a 密码
 
 -h ip地址
 -p 端口号
 -a 密码
 -c 集群
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值