Redis搭建cluster集群(基于Redis5.07版本)

说点废话

  • 本文是基于Redis官方文档来写的。官方文档
  • Redis官方推荐六台(三主三从)Redis来做cluster集群,所有在下这里也是六台。
  • 穷逼买不起真是服务器,所有只能用虚拟机。
  • 官方文档说,cluster不支持NAT的网络环境。所有我这里是一台服务器开多个端口,在下也试过开六台服务器,每台运行一个redis,但是没有成功。
  • 综上所诉,在下会在一台服务器里面开六个端口来运行六个redis做cluster集群。

开干

  • 现在redis的根目录下面创建一个文件夹,用来保存配置文件。名字随意,我这里叫做 cluster。
    然后再 cluster 目录下面创建 六个文件夹,名字分别为:7000,7001,7002,7003,7004,7005。
    然后每个文件夹下面都创建一个文件叫做 redis.conf。最后结果应该是这样的:
[root@localhost cluster]# pwd
/root/redis-5.0.7/cluster
[root@localhost cluster]# tree -a
.
├── 7000
│   └── redis.conf
├── 7001
│   └── redis.conf
├── 7002
│   └── redis.conf
├── 7003
│   └── redis.conf
├── 7004
│   └── redis.conf
└── 7005
    └── redis.conf

6 directories, 6 files
[root@localhost cluster]# 

  • 看看redis.conf里面都写啥
[root@localhost cluster]# cat ./7000/redis.conf 
#定义Redis启动暂用的端口号,文件夹7000下面写7000,文件夹7001下面写7001
port 7000 
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
[root@localhost cluster]# 

  • 切换目录配置文件所在的文件夹启动redis
[root@localhost 7000]# pwd
/root/redis-5.0.7/cluster/7000
[root@localhost 7000]# ../../src/redis-server ./redis.conf 
[root@localhost 7001]# pwd
/root/redis-5.0.7/cluster/7001
[root@localhost 7001]# ../../src/redis-server ./redis.conf 
[root@localhost 7002]# pwd
/root/redis-5.0.7/cluster/7002
[root@localhost 7002]# ../../src/redis-server ./redis.conf 
[root@localhost 7003]# pwd
/root/redis-5.0.7/cluster/7003
[root@localhost 7003]# ../../src/redis-server ./redis.conf 
[root@localhost 7004]# pwd
/root/redis-5.0.7/cluster/7004
[root@localhost 7004]# ../../src/redis-server ./redis.conf 
[root@localhost 7005]# pwd
/root/redis-5.0.7/cluster/7005
[root@localhost 7005]# ../../src/redis-server ./redis.conf
  • 启动完成之后会生成配置文件
[root@localhost cluster]# pwd
/root/redis-5.0.7/cluster
[root@localhost cluster]# tree -a
.
├── 7000
│   ├── appendonly.aof
│   ├── nodes.conf
│   └── redis.conf
├── 7001
│   ├── appendonly.aof
│   ├── nodes.conf
│   └── redis.conf
├── 7002
│   ├── appendonly.aof
│   ├── nodes.conf
│   └── redis.conf
├── 7003
│   ├── appendonly.aof
│   ├── nodes.conf
│   └── redis.conf
├── 7004
│   ├── appendonly.aof
│   ├── nodes.conf
│   └── redis.conf
└── 7005
    ├── appendonly.aof
    ├── nodes.conf
    └── redis.conf

6 directories, 18 files
[root@localhost cluster]# 
  • 创建集群(三主三从)
[root@localhost redis-5.0.7]# pwd
/root/redis-5.0.7
[root@localhost redis-5.0.7]# ./src/redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 307769f425caeabfde6afde3a645806de3a57f51 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: fb2c769474b9b90362694cc2c3c38998061e9a63 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 6a04475846e7840952f2a5211ec599559056ef0d 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 787857c615439cba5c5dc8b1944ad5e06fa578e8 127.0.0.1:7003
   replicates fb2c769474b9b90362694cc2c3c38998061e9a63
S: e6892c2dbdefeddbad343cf8d2497fbf96be8593 127.0.0.1:7004
   replicates 6a04475846e7840952f2a5211ec599559056ef0d
S: 572dfb20e31e72f2345ccfd2d39ae26123801217 127.0.0.1:7005
   replicates 307769f425caeabfde6afde3a645806de3a57f51
Can I set the above configuration? (type 'yes' to accept): yes     
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 307769f425caeabfde6afde3a645806de3a57f51 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 6a04475846e7840952f2a5211ec599559056ef0d 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 572dfb20e31e72f2345ccfd2d39ae26123801217 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 307769f425caeabfde6afde3a645806de3a57f51
S: 787857c615439cba5c5dc8b1944ad5e06fa578e8 127.0.0.1:7003
   slots: (0 slots) slave
   replicates fb2c769474b9b90362694cc2c3c38998061e9a63
M: fb2c769474b9b90362694cc2c3c38998061e9a63 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e6892c2dbdefeddbad343cf8d2497fbf96be8593 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 6a04475846e7840952f2a5211ec599559056ef0d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-5.0.7]# 

  • 客户端连接集群,我这里连接的是7000端口的,执行set 命令直接跳转到了7001,说明cluster已经成功。
[root@localhost redis-5.0.7]# pwd
/root/redis-5.0.7
[root@localhost redis-5.0.7]# ./src/redis-cli -c -p 7000
127.0.0.1:7000> set name shadow
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> 

继续往下看

  • 发现新大陆,redis给了一个脚本直接创建cluster集群 utils/create-cluster,简单看一下,还真有!!!
[root@localhost create-cluster]# pwd
/root/redis-5.0.7/utils/create-cluster
[root@localhost create-cluster]# ls
create-cluster  README
[root@localhost create-cluster]# 
  • 简单执行一下命令,好像就已经启动好了6个redis
[root@localhost create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
[root@localhost create-cluster]# 

  • 再简单执行一下命令,好像集群就创建好了
[root@localhost create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0cb2605a74bcac85d39492bb5c6405963d8d780b 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 48eca9e59c746a889b45d7a582bfde6925641f79 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 0476d141ac6954dded86a438c544abf39e60a886 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: 9e2f700cdde7f44313cf2e68dfa3ab241abe7501 127.0.0.1:30004
   replicates 48eca9e59c746a889b45d7a582bfde6925641f79
S: 386f786946d91e3d655f67f5c19fb6182361e8bc 127.0.0.1:30005
   replicates 0476d141ac6954dded86a438c544abf39e60a886
S: f5a504fc8f0c6698e307c4e6835561616722e3c1 127.0.0.1:30006
   replicates 0cb2605a74bcac85d39492bb5c6405963d8d780b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 0cb2605a74bcac85d39492bb5c6405963d8d780b 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 0476d141ac6954dded86a438c544abf39e60a886 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f5a504fc8f0c6698e307c4e6835561616722e3c1 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 0cb2605a74bcac85d39492bb5c6405963d8d780b
S: 9e2f700cdde7f44313cf2e68dfa3ab241abe7501 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 48eca9e59c746a889b45d7a582bfde6925641f79
M: 48eca9e59c746a889b45d7a582bfde6925641f79 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 386f786946d91e3d655f67f5c19fb6182361e8bc 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 0476d141ac6954dded86a438c544abf39e60a886
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost create-cluster]# 

  • 此时发现当前目录生成了一堆配置文件
[root@localhost create-cluster]# pwd
/root/redis-5.0.7/utils/create-cluster
[root@localhost create-cluster]# tree -a
.
├── 30001.log
├── 30002.log
├── 30003.log
├── 30004.log
├── 30005.log
├── 30006.log
├── appendonly-30001.aof
├── appendonly-30002.aof
├── appendonly-30003.aof
├── appendonly-30004.aof
├── appendonly-30005.aof
├── appendonly-30006.aof
├── create-cluster
├── dump-30001.rdb
├── dump-30002.rdb
├── dump-30003.rdb
├── dump-30004.rdb
├── dump-30005.rdb
├── dump-30006.rdb
├── .gitignore
├── nodes-30001.conf
├── nodes-30002.conf
├── nodes-30003.conf
├── nodes-30004.conf
├── nodes-30005.conf
├── nodes-30006.conf
└── README

0 directories, 27 files
[root@localhost create-cluster]# 
  • 再次用客户端连接一下,居然成功了!!!
[root@localhost redis-5.0.7]# pwd
/root/redis-5.0.7
[root@localhost redis-5.0.7]# ./src/redis-cli -c -p 30001
127.0.0.1:30001> set name shadow
-> Redirected to slot [5798] located at 127.0.0.1:30002
OK
127.0.0.1:30002> 

  • 查看一下这个神奇的脚本文件,就大概知道这东西怎么用了(在下没有学过shell编程,但是依然看得懂)。
#!/bin/bash

# Settings
PORT=30000
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 --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 127.0.0.1:$PORT"
    done
    ../../src/redis-cli --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."

  • 这篇文章应该写完了。我选择直接使用脚本来创建!!!

你可能感兴趣的:(Redis搭建cluster集群(基于Redis5.07版本))