Redis集群安装配置

1. 环境及目录准备

echo never > /sys/kernel/mm/transparent_hugepage/enabled
vi /etc/sysctl.conf

# 增加或修改 Redis 配置
net.core.somaxconn= 1024
vm.overcommit_memory = 1

sysctl -p
mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/conf
mkdir -p /usr/local/redis/logs
mkdir -p /usr/local/redis/data
mkdir -p /usr/local/redis/data/7000/
mkdir -p /usr/local/redis/data/7001/
mkdir -p /usr/local/redis/data/7002/
mkdir -p /usr/local/redis/data/7003/
mkdir -p /usr/local/redis/data/7004/
mkdir -p /usr/local/redis/data/7005/
本集群默认采用6节点,3主3从。

2. 编译安装

wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar xzf redis-5.0.4.tar.gz
cd redis-5.0.4
make
make install
mv /usr/local/bin/redis-* /usr/local/redis/bin/

3. 集群配置

3.1 节点1配置
vi /usr/local/redis/conf/redis_7000.conf

bind 127.0.0.1
protected-mode no
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
tcp-backlog 511
timeout 3000
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_7000.pid
loglevel notice
logfile /usr/local/redis/logs/redis_7000.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data/7000/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

3.2 节点2-6配置同节点1,并修改以下几个配置
vi /usr/local/redis/conf/redis_7001.conf

port 7001
pidfile /var/run/redis_7001.pid
logfile /usr/local/redis/logs/redis_7001.log
dir /usr/local/redis/data/7001/

vi /usr/local/redis/conf/redis_7002.conf

port 7002
pidfile /var/run/redis_7002.pid
logfile /usr/local/redis/logs/redis_7002.log
dir /usr/local/redis/data/7002/

vi /usr/local/redis/conf/redis_7003.conf

port 7003
pidfile /var/run/redis_7003.pid
logfile /usr/local/redis/logs/redis_7003.log
dir /usr/local/redis/data/7003/

vi /usr/local/redis/conf/redis_7004.conf

port 7004
pidfile /var/run/redis_7004.pid
logfile /usr/local/redis/logs/redis_7004.log
dir /usr/local/redis/data/7004/

vi /usr/local/redis/conf/redis_7005.conf

port 7005
pidfile /var/run/redis_7005.pid
logfile /usr/local/redis/logs/redis_7005.log
dir /usr/local/redis/data/7005/

4. 管理集群

4.1 编写管理脚本
vi /usr/local/redis/redis-manage

#!/bin/bash

# Settings
PORT=7000
TIMEOUT=2000
NODES=6
REPLICAS=1
REDIS_PATH=/usr/local/redis

# 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
        echo "Starting $PORT"
        ${REDIS_PATH}/bin/redis-server ${REDIS_PATH}/conf/redis_${PORT}.conf
        PORT=$((PORT+1))
    done
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        HOSTS="$HOSTS 127.0.0.1:$PORT"
        PORT=$((PORT+1))
    done
    ${REDIS_PATH}/bin/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS
    exit 0
fi

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

if [ "$1" == "watch" ]
then
    while [ 1 ]; do
        clear
        date
        ${REDIS_PATH}/bin/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 ${REDIS_PATH}/logs/redis_${PORT}.log
    exit 0
fi

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

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

if [ "$1" == "clean-logs" ]
then
    rm -rf ${REDIS_PATH}/logs/*.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."

4.2 脚本改为可执行权限
chmod a+x /usr/local/redis/redis-manage
4.3 启动Redis
/usr/local/redis/redis-manage start
4.4 创建集群
/usr/local/redis/redis-manage create
4.5 停止Redis
/usr/local/redis/redis-manage stop
4.6 监控集群状态
/usr/local/redis/redis-manage watch
4.7 查看集群节点日志
/usr/local/redis/redis-manage tail 0
/usr/local/redis/redis-manage tail 1
/usr/local/redis/redis-manage tail 2
/usr/local/redis/redis-manage tail 3
/usr/local/redis/redis-manage tail 4
/usr/local/redis/redis-manage tail 5
4.8 清除集群节点数据、日志、配置
/usr/local/redis/redis-manage clean
4.9 仅清除集群节点日志
/usr/local/redis/redis-manage clean-logs

你可能感兴趣的:(Redis集群安装配置)