1. 程式人生 > >redis 集群搭建

redis 集群搭建

redis


#

#redis 官網: https://redis.io

#redis 文檔:https://redis.io/documentation

#redis 集群:https://redis.io/topics/cluster-tutorial

#


#軟件下載

[[email protected] ~]#  wget http://download.redis.io/releases/redis-3.2.9.tar.gz


#編譯

[[email protected] ~]# mkdir /usr/local/redis
[[email protected] ~]# mkdir /usr/local/redis/{data,conf,bin}
[[email protected]
/* */ ~]# tar -xf redis-3.2.9.tar.gz [[email protected] ~]# cd redis-3.2.9/ [[email protected] redis-3.2.9]# make


#拷貝編譯後的文件

[[email protected] redis-3.2.9]# cd src/
[[email protected] src]# \cp -p mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis/bin/
[[email protected]
/* */ src]# cd .. [[email protected] redis-3.2.9]# cp -p redis.conf /usr/local/redis/conf/


#創建數據目錄

[[email protected] redis-3.2.9]# cd /usr/local/redis/data/
[[email protected] data]# mkdir {7000..7005}
[[email protected] data]# ls
7000  7001  7002  7003  7004  7005
[[email protected] data]#


#修改相應端口(大致如下)

[[email protected] conf]# grep ‘^[a-Z]‘ redis.conf 
bind 0.0.0.0
dir /usr/local/redis/data/7000
pidfile /var/run/redis_7000.pid
appendfilename "appendonly-7000.aof"
port 7000
cluster-config-file nodes-7000.conf 
cluster-enabled yes
cluster-node-timeout 15000
protected-mode no
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
loglevel warning
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum no
dbfilename dump.rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
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 100000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
latency-monitor-threshold 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 256mb 64mb 60
client-output-buffer-limit pubsub 64mb 16mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxmemory-policy allkeys-lru
maxmemory-samples 5
maxclients 10000


#拷貝配置文件

[[email protected] conf]# for i in {7000..7005};do cp redis.conf $i.conf;done
[[email protected] conf]# ls
7000.conf  7001.conf  7002.conf  7003.conf  7004.conf  7005.conf  redis.conf
[[email protected] conf]# rm -f redis.conf
#批量修改
[[email protected] conf]# for i in {7000..7005};do sed -i "s/7000/$i/g" "$i".conf;done


#修改腳本(十分粗糙的腳本,不建議采納,望讀者自行編寫

[[email protected] ~]# cp -p  redis-3.2.9/utils/redis_init_script /etc/init.d/redis
[[email protected] ~]# vim /etc/init.d/redis 
[[email protected] ~]# cat /etc/init.d/redis 
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#REDISPORT=$2
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
CONFDIR="/usr/local/redis/conf/"
#PIDFILE=/var/run/redis_${REDISPORT}.pid
#CONF="$CONFDIR${REDISPORT}.conf"
function main(){
REDISPORT=$2
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="$CONFDIR${REDISPORT}.conf"
  case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first parameter,port or all as second parameter."
        ;;
  esac
}
if [ "$2" == "all" ]
then
    for i in `ls $CONFDIR | grep [0-9] | cut -c 1-4` ;do
main $1 $i
    done
else
    main $1 $2
fi



#啟動服務

[[email protected] ~]# service redis start all
[[email protected] ~]# netstat -lntup | grep redis
tcp        0      0 0.0.0.0:17000           0.0.0.0:*               LISTEN      6735/redis-server 0 
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      6737/redis-server 0 
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      6743/redis-server 0 
tcp        0      0 0.0.0.0:17003           0.0.0.0:*               LISTEN      6747/redis-server 0 
tcp        0      0 0.0.0.0:17004           0.0.0.0:*               LISTEN      6749/redis-server 0 
tcp        0      0 0.0.0.0:17005           0.0.0.0:*               LISTEN      6755/redis-server 0 
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      6735/redis-server 0 
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      6737/redis-server 0 
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      6743/redis-server 0 
tcp        0      0 0.0.0.0:7003            0.0.0.0:*               LISTEN      6747/redis-server 0 
tcp        0      0 0.0.0.0:7004            0.0.0.0:*               LISTEN      6749/redis-server 0 
tcp        0      0 0.0.0.0:7005            0.0.0.0:*               LISTEN      6755/redis-server 0 
[[email protected] ~]#


#創建集群

[[email protected] conf]# yum install ruby ruby-devel rubygems rpm-build
[[email protected] conf]# gem install redis        #如果沒有反應,可以更換源
#更換源 參考:http://gems.ruby-china.org/
# gem update --system # 這裏請翻墻一下
#gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
#
[[email protected] conf]# cd /usr/local/redis/bin/
[[email protected] bin]# ls
mkreleasehdr.sh  redis-check-aof  redis-cli       redis-server
redis-benchmark  redis-check-rdb  redis-sentinel  redis-trib.rb
[[email protected] bin]# ./redis-trib.rb create --replicas 1 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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003
   replicates de6c4b00953b0e354ea375106f29bd627ed3ab2e
S: 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004
   replicates fce2cebafefeb540592c624c68b741d465ce095b
S: 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005
   replicates 83474177e39b72145adaad60cad1fe4523a0ee79
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: de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003
   slots: (0 slots) slave
   replicates de6c4b00953b0e354ea375106f29bd627ed3ab2e
M: 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004
   slots: (0 slots) slave
   replicates fce2cebafefeb540592c624c68b741d465ce095b
S: 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 83474177e39b72145adaad60cad1fe4523a0ee79
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.



#登陸集群

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -h 127.0.0.1 -p 7000
127.0.0.1:7000> cluster nodes
fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 master - 0 1495358027725 2 connected 5461-10922
de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 slave de6c4b00953b0e354ea375106f29bd627ed3ab2e 0 1495358029772 4 connected
83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 master - 0 1495358025682 3 connected 10923-16383
64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 slave fce2cebafefeb540592c624c68b741d465ce095b 0 1495358028748 5 connected
3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 slave 83474177e39b72145adaad60cad1fe4523a0ee79 0 1495358026706 6 connected
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:77
cluster_stats_messages_received:77
127.0.0.1:7000> cluster nodes
fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 master - 0 1495358038300 2 connected 5461-10922
de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 slave de6c4b00953b0e354ea375106f29bd627ed3ab2e 0 1495358039395 4 connected
83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 master - 0 1495358036106 3 connected 10923-16383
64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 slave fce2cebafefeb540592c624c68b741d465ce095b 0 1495358033918 5 connected
3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 slave 83474177e39b72145adaad60cad1fe4523a0ee79 0 1495358040489 6 connected
127.0.0.1:7000>


#redis集群搭建成功



本文出自 “Chauncey” 博客,請務必保留此出處http://cqwujiang.blog.51cto.com/10808946/1927918

redis 集群搭建