阿里云redis5.0.6集群搭建

一、引言:

去年买的阿里云上周到期了,昨晚看到双11新用户有优惠活动,赶紧偷摸的用媳妇手机,花800块买了3年阿里云(2核4G),好了,搭建新环境吧!

注:需要打开阿里云的安全组端口(https://oneinstack.com/docs/securitygroup/

二、redis集群搭建:

1.​​​​CentOs命令如下:​

wget http://download.redis.io/releases/redis-5.0.6.tar.gz

tar xzf redis-5.0.6.tar.gz

cd redis-5.0.6

yum install gcc

make MALLOC=libc

cd src && make install

cd ../utils/create-cluster/

ps -ef|grep redis

vim create-cluster

2.修改create-cluster配置文件:

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

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

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

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

                            加入redis密码                                       -a qiang123 

2.3.stop命令:

                            加入redis密码                                       -a qiang123 

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 qiang123 --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 138.156.224.5:$PORT"
    done
    ../../src/redis-cli -a qiang123 --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 -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    -- 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.6/utils/create-cluster/create-cluster start
#2.建立集群间联系
sh /root/javaxiaobang/redis-5.0.6/utils/create-cluster/create-cluster create
#3.停止集群
sh /root/javaxiaobang/redis-5.0.6/utils/create-cluster/create-cluster stop
#4.清除集群
sh /root/javaxiaobang/redis-5.0.6/utils/create-cluster/create-cluster clean

      4.连接redis

 redis-cli -h 127.0.0.1 -p 7001 -c -a 密码

 -h ip地址
 -p 端口号
 -a 密码
 -c 集群

 

你可能感兴趣的:(架构师肖邦,分布式缓存redis)