Redis3 cluster

redis cluster  

安装依赖包

yum -y install wget vim openssl openssl-devel zlib zlib-devel gcc gcc-c++ epel-release jemalloc jemalloc-devel

创建用户

groupadd redis
useradd -gredis -s /sbin/nologin -M redis

开始安装redis =

wget http://download.redis.io/releases/redis-3.2.10.tar.gz
tar xzf redis-3.2.10.tar.gz
cd redis-3.2.10
make MALLOC=jemalloc
make PREFIX=/data/soft/redisc install
cp src/redis-trib.rb /data/soft/redisc/bin
ln -s /data/soft/redisc/bin/redis-cli /usr/bin/

配置为系统服务 =

vim /etc/init.d/redis_6380
加入以下内容
#!/bin/sh
#
# redis        init file for starting up the redis daemon
#
# chkconfig:   - 20 80
# description: Starts and stops the redis daemon.

# Source function library.
. /etc/rc.d/init.d/functions

name="redis-server"
exec="/data/soft/redisc/bin/$name"
pidfile="/var/run/redis/redis_6380.pid"
REDIS_CONFIG="/data/soft/redisc/6380.conf"

[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis_6380

start() {
    [ -f $REDIS_CONFIG ] || exit 6
    [ -x $exec ] || exit 5
    echo -n $"Starting $name: "
    daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name
    retval=$?
    echo
    [ $retval -eq 0 ] && unlink $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    false
}

rh_status() {
    status -p $pidfile $name
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
esac
exit $?

添加为系统服务

chmod u+x /etc/init.d/redis_6380
chkconfig --add redis_6380
chkconfig redis_6380 on

添加配置文件

cat > /data/soft/redisc/6380.conf << EOF
protected-mode no
port 6380
tcp-backlog 511
repl-backlog-size 64mb
maxclients 20000
maxmemory 30gb
timeout 0
tcp-keepalive 60
daemonize yes
supervised no
pidfile /var/run/redis/redis_6380.pid
loglevel notice
logfile /data/logs/redis/redis_6380.log
databases 16
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename cluster.rdb
dir /data/soft/redisc/data/6380
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 90
appendonly no
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
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 0 0 0
hz 10
aof-rewrite-incremental-fsync yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000

EOF

建立目录

mkdir -p /data/logs/redis
mkdir -p /data/soft/redisc/data/6380
mkdir -p /var/run/redis

chown -R redis.redis /data/logs/redis
chown -R redis.redis /data/soft/redisc
chown -R redis.redis /var/run/redis

修改系统配置

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local

chmod u+x /etc/rc.d/rc.local

启动redis

service redis_6380 start

部署集群

准备工作

  • 重置上边的步骤

  • 修改配置(6380改为6381等)

  • 建立一共6个redis(同一机器需要不同端口 不同机器可同一端口)

  • 组成三主三从的高可用redis集群

安装ruby、redis插件

  • 安装集群,在其中一台机器上执行即可
yum  -y install centos-release-scl-rh
yum  -y install rh-ruby23 rubygems
scl  enable  rh-ruby23 bash
ruby -v
gem install redis -v 3.3.3

拷贝redis-trib.rb

  • 在编译目录下复制创建集群命令
cp redis-3.2.10/src/redis-trib.rb  /data/soft/redisc/bin

打开防火墙端口

  • 防火墙打开相应的端口,如:6380和 16380(集群通讯用)

创建集群 ====

  • 开始创建集群,根据服务器IP和端口添加
./redis-trib.rb create --replicas 1 192.168.0.1:6380 192.168.0.2:6381 192.168.0.3:6380 192.168.0.4:6381 192.168.0.5:6380 192.168.0.6:6381

确认集群创建成功

redis-cli -c -p 6380
CLUSTER NODES
127.0.0.1:6380> CLUSTER NODES
e0899e108ad4bdda1ff4e73cc4170c632779ce02 192.168.0.6:6381 slave 727ea1c3d22e39d72ab9a3b26c73311666f61a61 0 1548123431966 3 connected
2d3f764f4aac76b9ecc2ec1c9fcdd97e214f9cba 192.168.0.2:6381 slave c054f91fe9ac917982959d27d689dfee7ed8b941 0 1548123432968 4 connected
727ea1c3d22e39d72ab9a3b26c73311666f61a61 192.168.0.3:6380 master - 0 1548123429963 3 connected 5461-10922
57e28960f806c9a586bab351265631ad750283cb 192.168.0.1:6380 master - 0 1548123426959 5 connected 10923-16383
751d444e0c4a9b354b50ea5d070e5d59383af201 192.168.0.4:6381 slave 57e28960f806c9a586bab351265631ad750283cb 0 1548123430963 6 connected
c054f91fe9ac917982959d27d689dfee7ed8b941 192.168.0.5:6380 myself,master - 0 0 1 connected 0-5460