redis集羣及redis集羣監控redis-stat搭建實錄

redis集羣搭建

節點規劃

準備了3臺機子,每臺機子設置兩個redis幾點,規劃如下
192.168.0.131:7001(master)
192.168.0.131:7002(slave)
192.168.0.132:7001(master)
192.168.0.132:7002(slave)
192.168.0.133:7001(master)
192.168.0.133:7002(slave)

安裝步驟

安裝ruby

1、下載與上傳

下載ruby安裝包https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
使用scp命令把下載好的文件上傳到3個主機中(安裝包我下載到/Users/demondevil/mydatas/softs/ruby/路徑中)

scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz [email protected]:/home/softs
scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz [email protected]:/home/softs
scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz [email protected]:/home/softs

2、安裝

登陸主機
ssh [email protected]
解壓

cd /home/softs/
tar -zxvf ruby-2.3.1.tar.gz

配置

cd ruby-2.3.1
./configure --prefix=/usr/local/ruby

編譯與安裝

make
make install 

設置環境變量

vim /etc/profile

在最後面添加一行

export PATH=/usr/local/ruby/bin:$PATH

使配置起作用

source /etc/profile

查看是否配置成功

echo $PATH

安裝redis-3.0.7.gem

方法一

下載安裝redis-3.0.7.gem,安裝目的是使得ruby可以與redis集羣通信,這樣子就可以使用 redis-trib.rb工具管理集羣了
下載

cd /home/softs/
wget --no-check-certificate https://rubygems.global.ssl.fastly.net/gems/redis-3.0.7.gem

安裝

gem install -l redis-3.0.7.gem

報錯

cannot load such file – zlib

下載安裝zlib

wget http://down1.chinaunix.net/distfiles/zlib-1.2.7.tar.gz
tar -zxvf zlib-1.2.7.tar.gz
cd zlib-1.2.7
./configure
make
make install

再次運行gem install -l redis-3.0.7.gem還是報相同的錯誤,解決方法
進入ruby源碼文件夾

cd /home/softs/ruby-2.3.1

安裝ruby自身提供的zlib包

cd ext/zlib
ruby ./extconf.rb
make
make install

回到redis-3.0.7.gem當前目錄
再次運行gem install -l redis-3.0.7.gem成功

方法二

gem install redis

執行報錯

Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources

解決方法

yum -y install openssl-devel
yum -y install openssl

進入ruby 源碼目錄下的ext/openssl 目錄

ruby extconf.rb

將ruby 源碼目錄下的include 目錄軟鏈接到 / 目錄下:

ln -s /home/softs/ruby-2.3.1/include/ /
make
make install

再次執行gem install redis成功

如果有報源的錯誤則進行gem源更換,方法如下
gem使用的是https://rubygems.org/,境外源,國內無法訪問
解決辦法:
更換gem源爲https://ruby.taobao.org/,淘寶源
查看當前使用源

gem source –l
結果顯示
 *** CURRENT SOURCES ***
https://rubygems.org/
gem source -a https://ruby.taobao.org/ #添加淘寶源
gem source -r https://rubygems.org/	#刪除境外源

192.168.0.131和192.168.0.133使用的是方法一
192.168.0.132使用的是方法二

安裝redis

安裝

下載安裝包
http://download.redis.io/releases/redis-3.0.7.tar.gz
上傳至主機

scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz [email protected]:/home/softs
scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz [email protected]:/home/softs
scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz [email protected]:/home/softs

編譯與安裝

cd /home/softs
tar -zxvf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make install

配置

對192.168.0.131主機進行如下操作

cd /home/softs/redis-3.0.7/src
cp redis-cli /usr/local/bin/
cp redis-server /usr/local/bin/
cp redis-trib.rb /usr/local/bin/
cd  /home/softs/
mkdir redis-cluster
cd redis-cluster
mkdir data
mkdir log

創建並編輯7001端口的redis命令腳本,用於啓動停止等

vim redis_7001

redis_7001文件內容如下

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/home/softs/redis-cluster/data/redis-7001.pid
CONF="/home/softs/redis-cluster/redis_7001.conf"
REDISPORT="7001"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_7001
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_7001
# Description: Redis daemon
### END INIT INFO


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
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

創建並編輯7001端口的redis配置文件,用於集羣

vim redis_7001.conf

redis_7001.conf配置文件內容如下

################################ GENERAL  #####################################

daemonize yes
pidfile /home/softs/redis-cluster/data/redis-7001.pid
port 7001

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
#需要修改somaxconn and tcp_max_syn_backlog
tcp-backlog 1500


#客戶端空閒多長時間,關閉鏈接。0表示不關閉;此處表示空閒20s後關閉連接
timeout 0

#心跳包,60s未收到心跳認爲其死亡
tcp-keepalive 60

loglevel notice

logfile "/home/softs/redis-cluster/log/redis-7001.log"

databases 16

################################ SNAPSHOTTING  ################################

dir /home/softs/redis-cluster/data

################################# REPLICATION #################################
# 當從庫同主機失去連接或者複製正在進行,從機庫有兩種運行方式:
#yes(默認設置),從庫會繼續相應客戶端的請求
#no除了INFO和SLAVOF命令之外的任何請求都會返回一個錯誤"SYNC with master in progress"
slave-serve-stale-data yes


# Since Redis 2.6 by default slaves are read-only.
slave-read-only yes


#slave會每隔repl-ping-slave-period(默認10秒)ping一次master,如果超過repl-timeout(默認 60秒)都沒有收到響應,就會認爲Master掛了。如果Master明明沒掛但被阻塞住了也會報MASTER time out: no data nor PING received…這個錯。
#slave每隔10s向主發送一次心跳
repl-ping-slave-period 10

#slave認爲複製超時時間
repl-timeout 120

#在slave和master同步後(發送psync/sync),後續的同步是否設置成TCP_NODELAY
#假如設置成yes,則redis會合並小的TCP包從而節省帶寬,但會增加同步延遲(40ms),造成master與slave數據不一致
#假如設置成no,則redis master會立即發送同步數據,沒有延遲
#前者關注性能,後者關注一致性
repl-disable-tcp-nodelay yes

#多slave競爭轉換爲master的權值
slave-priority 100


################################## SECURITY ###################################



################################### LIMITS ####################################

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
maxclients 20000

# Don't use more memory than the specified amount of bytes.
#外網40G的閥值,達到閥值開始清理數據
#maxmemory 40G
maxmemory 1G

maxmemory-policy allkeys-lru

############################## APPEND ONLY MODE ###############################
#不開啓aof功能,該項目下配置的所有參數都不會生效
appendonly no

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
appendfsync everysec
#重寫aof的期間新的寫操作不要fsync,暫時存在內存中,等rewrite完成後再寫入
no-appendfsync-on-rewrite yes

#達到上一份文件的80%或者文件至少達到1g時候纔開始rewrite aof
auto-aof-rewrite-percentage 80-100
auto-aof-rewrite-min-size 1G

#aof rewrite過程中,是否採取增量文件同步策略,默認爲“yes
aof-rewrite-incremental-fsync yes
aof-load-truncated yes

################################ LUA SCRIPTING  ###############################

lua-time-limit 5000

################################ REDIS CLUSTER  ###############################


cluster-enabled yes


cluster-config-file nodes-7001.conf

#節點超時時間20s認爲超時 
cluster-node-timeout 20000


cluster-migration-barrier 1


################################## SLOW LOG ###################################
#記錄查詢超過200毫秒的所有查詢
slowlog-log-slower-than 200000

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
#日誌超過128後舊的記錄被移除,以回收內存
slowlog-max-len 128

################################ LATENCY MONITOR ##############################

hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 512mb 300mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

把命令腳本文件拷貝到/usr/local/bin/目錄便於使用redis_7001命令

cp redis_7001 /usr/local/bin/

接着配置7002端口

cp redis_7001 redis_7002
vim redis_7002(把裏面的7001改爲7002)
cp redis_7002 /usr/local/bin/
cp redis_7001.conf redis_7002.conf
vim redis_7002.conf(把裏面的7001改爲7002)

添加執行權限

chmod u+x /usr/local/bin/redis_*

啓動redis

redis_7001 start
redis_7002 start

停止redis

redis_7001 stop
redis_7002 stop

啓動之後查看進程

ps -ef |grep redis
root     24381     1  0 08:09 ?        00:00:00 /usr/local/bin/redis-server *:7001 [cluster]                        
root     24418     1  4 08:17 ?        00:00:01 /usr/local/bin/redis-server *:7002 [cluster]                        
root     24425 24165  6 08:18 pts/0    00:00:00 grep redis

對192.168.0.133進行和192.168.0.132相同的操作

啓動集羣

通過redis-trib創建cluster

–replicas 則指定了爲Redis Cluster中的每個Master節點配備幾個Slave節點
要求至少6個節點、3個master節點

redis-trib.rb create --replicas 1 192.168.0.131:7001 192.168.0.132:7001 192.168.0.133:7001 192.168.0.131:7002 192.168.0.132:7002 192.168.0.133:7002

輸出結果

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.131:7001
192.168.0.132:7001
192.168.0.133:7001
Adding replica 192.168.0.132:7002 to 192.168.0.131:7001
Adding replica 192.168.0.131:7002 to 192.168.0.132:7001
Adding replica 192.168.0.133:7002 to 192.168.0.133:7001
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:0-5460 (5461 slots) master
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:5461-10922 (5462 slots) master
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:10923-16383 (5461 slots) master
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
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 192.168.0.131:7001)
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:0-5460 (5461 slots) master
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:5461-10922 (5462 slots) master
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:10923-16383 (5461 slots) master
M: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) master
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) master
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) master
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

根據信息可知master、slave對應節點
192.168.0.131:7001(master)—>192.168.0.132:7002(slave)
192.168.0.132:7001(master)—>192.168.0.131:7002(slave)
192.168.0.133:7001(master)—>192.168.0.133:7002(slave)

檢查集羣信息
redis-trib.rb check anyExistNodeIp:port

redis-trib.rb check 192.168.0.131:7001

即可查看集羣的相關信息

集羣模式登錄redis客戶端

redis-cli -c -p 7001

在線添加/刪除節點

先在192.168.0.133上添加兩個redis服務

cd /home/softs/redis-cluster/
cp redis_7001 redis_7003
cp redis_7001 redis_7004
cp redis_7001.conf redis_7003.conf
cp redis_7001.conf redis_7004.conf

修改這4個文件,把7001改爲對應的端口

cp redis_7003 /usr/local/bin/
cp redis_7004 /usr/local/bin/
chmod u+x /usr/local/bin/redis_700*

啓動redis

redis_7003 start
redis_7004 start

添加master節點

執行redis-trib.rbadd-nodenewNodeIp:portanyExistNodeIp:port將新節點加入到 集羣中

redis-trib.rb add-node 192.168.0.133:7003 192.168.0.133:7001

輸出下面信息

>>> Adding node 192.168.0.133:7003 to cluster 192.168.0.133:7001
>>> Performing Cluster Check (using node 192.168.0.133:7001)
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.0.133:7003 to make it join the cluster.
[OK] New node added correctly.

檢查集羣信息

redis-trib.rb check 192.168.0.131:7002

輸出信息

>>> Performing Cluster Check (using node 192.168.0.131:7002)
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: b04d24e465d199e9f1a4fe52298dcaa9b48c187d 192.168.0.133:7003
   slots: (0 slots) master
   0 additional replica(s)
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

分配slot

數據分片redis-trib.rb reshard anyExistNodeIp:port

redis-trib.rb reshard 192.168.0.133:7001

輸出信息

>>> Performing Cluster Check (using node 192.168.0.133:7003)
M: b04d24e465d199e9f1a4fe52298dcaa9b48c187d 192.168.0.133:7003
   slots: (0 slots) master
   0 additional replica(s)
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
--- 想要移動的slots的個數 
--- 想要移動的slots個數,如果是想移除某個節點,則該處的slots個數應該是此將要移除的節點上的slots個數
How many slots do you want to move (from 1 to 16384)? 2500
--- 哪個節點將會接收這些移動的slots,這裏就是新添加的節點的nodeID,即b04d24e465d199e9f1a4fe52298dcaa9b48c187d
What is the receiving node ID? b04d24e465d199e9f1a4fe52298dcaa9b48c187d
--- 選擇要移動的slots數據來源
Please enter all the source node IDs.
--- all表示均衡的從所有節點中獲取slot
  Type 'all' to use all the nodes as source nodes for the hash slots.
--- 輸入某個或者幾個節點上面的slot並且以done結束
  Type 'done' once you entered all the source nodes IDs.
Source node #1:4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes

— 後面的中文是對應的解釋,自己加上去。

添加對應的slave節點

redis-trib.rb add-node 192.168.0.133:7004 192.168.0.133:7001

連接到要添加的slave數據庫中,然後執行replicate操作,id爲該slave node應對的master nodeid即對應新添加的192.168.0.133:7003節點的nodeid

redis-cli -h 192.168.0.133 -p 7004
cluster replicate b04d24e465d199e9f1a4fe52298dcaa9b48c187d

再檢查集羣信息

redis-trib.rb check 192.168.0.131:7002

輸出信息

>>> Performing Cluster Check (using node 192.168.0.131:7002)
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: b04d24e465d199e9f1a4fe52298dcaa9b48c187d 192.168.0.133:7003
   slots:0-188,5461-7580,10923-11112 (2499 slots) master
   1 additional replica(s)
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:189-5460 (5272 slots) master
   1 additional replica(s)
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:7581-10922 (3342 slots) master
   1 additional replica(s)
S: eb5e0e1d3736f34137a6c60e50c3315add8c2355 192.168.0.133:7004
   slots: (0 slots) slave
   replicates b04d24e465d199e9f1a4fe52298dcaa9b48c187d
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:11113-16383 (5271 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

刪除節點

刪除192.168.0.133:7003(master),192.168.0.133:7004(slave)兩個節點
使用redis-trib.rb del-node willRemoveNodeIp:port willRemoveNodeID命令依次刪除從節 點和主節點
——— 先刪除slave節點

redis-trib.rb del-node 192.168.0.133:7004 eb5e0e1d3736f34137a6c60e50c3315add8c2355

輸出信息

>>> Removing node eb5e0e1d3736f34137a6c60e50c3315add8c2355 from cluster 192.168.0.133:7004
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

——— 刪除master節點
刪除master節點之前先把該節點上對應全部slot遷移到另外一個節點上,目前redis-trib.rb一次只能遷移到一個節點上
1:移除master節點上的全部slot,使用命令redis-trib.rb reshard anyExistNodeIp:port

redis-trib.rb reshard 192.168.0.133:7001

輸出信息

>>> Performing Cluster Check (using node 192.168.0.133:7001)
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:11113-16383 (5271 slots) master
   1 additional replica(s)
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:7581-10922 (3342 slots) master
   1 additional replica(s)
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: b04d24e465d199e9f1a4fe52298dcaa9b48c187d 192.168.0.133:7003
   slots:0-188,5461-7580,10923-11112 (2499 slots) master
   0 additional replica(s)
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:189-5460 (5272 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 2500
---- 這裏選擇192.168.0.132:7001節點接受將要移除的slot
What is the receiving node ID? fda3e4e217f67f72c2082a9d417c7001eff4cfc8
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
---- 這裏選擇要移除的192.168.0.133:7003節點nodeid
Source node #1:b04d24e465d199e9f1a4fe52298dcaa9b48c187d
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes

2:刪除master節點
刪除節點之前使用redis-trib.rb check existNodeIp:port命令檢查集羣信息,檢測該節點上面的slot是否全部移除乾淨

redis-trib.rb check 192.168.0.131:7002

輸出信息

>>> Performing Cluster Check (using node 192.168.0.131:7002)
S: 73c18b72a7543881feede89f0a249e83ac63b337 192.168.0.131:7002
   slots: (0 slots) slave
   replicates fda3e4e217f67f72c2082a9d417c7001eff4cfc8
M: b04d24e465d199e9f1a4fe52298dcaa9b48c187d 192.168.0.133:7003
   slots: (0 slots) master
   0 additional replica(s)
M: 51211a7220b37b53d826bc139e7780a06c39c6b0 192.168.0.131:7001
   slots:189-5460 (5272 slots) master
   1 additional replica(s)
S: 31a7a5c5b5dbaf0ab7f0b73eb58c08d13a6d02d2 192.168.0.132:7002
   slots: (0 slots) slave
   replicates 51211a7220b37b53d826bc139e7780a06c39c6b0
M: fda3e4e217f67f72c2082a9d417c7001eff4cfc8 192.168.0.132:7001
   slots:0-188,5461-11112 (5841 slots) master
   1 additional replica(s)
S: e7823da58afdf968948e39259ca80032bee28d6e 192.168.0.133:7002
   slots: (0 slots) slave
   replicates 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5
M: 4cfc061fa365a5f7e2a9ff07c0e3c08f12b7b2c5 192.168.0.133:7001
   slots:11113-16383 (5271 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可看到192.168.0.133:7003節點上的slot已經全部被移除
執行刪除

redis-trib.rb del-node 192.168.0.133:7003 b04d24e465d199e9f1a4fe52298dcaa9b48c187d

輸出信息

>>> Removing node b04d24e465d199e9f1a4fe52298dcaa9b48c187d from cluster 192.168.0.133:7003
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

再次檢查集羣信息會發現已經沒有這個節點了

重啓集羣
把全部redis服務停止
刪除data目錄下文件

rm -f /home/softs/redis-cluster/data/*

啓動redis服務
由於redis沒有配置持久化機制,所以之前的數據會被全部清空
重新創建集羣

redis-trib.rb create --replicas 1 192.168.0.131:7001 192.168.0.132:7001 192.168.0.133:7001 192.168.0.131:7002 192.168.0.132:7002 192.168.0.133:7002

redis集羣監控redis-stat

源碼地址
https://github.com/junegunn/redis-stat
參考博客
http://blog.csdn.net/21aspnet/article/details/50748719

規劃在192.168.0.130節點上搭建redis-stat
以下是我的搭建記錄

ssh [email protected]
cd /home/softs/
yum install -y ruby
yum install -y ruby-devel
yum install -y rubygems
gem source -a https://ruby.taobao.org/ #添加淘寶源
gem source -r http://rubygems.org/ #刪除境外源
[root@localhost softs]# gem install redis-stat
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
ERROR:  Error installing redis-stat:
redis-stat requires daemons (~> 1.1.9, runtime)
[root@localhost softs]# gem install redis-stat -v 0.4.12
Successfully installed daemons-1.1.9
Successfully installed redis-stat-0.4.12
2 gems installed
Installing ri documentation for daemons-1.1.9...
Installing ri documentation for redis-stat-0.4.12...
Installing RDoc documentation for daemons-1.1.9...
Installing RDoc documentation for redis-stat-0.4.12...
[root@localhost softs]# redis-stat 1
/usr/lib/ruby/site_ruby/1.8/rubygems.rb:233:in `activate': can't activate daemons (~> 1.1.9, runtime) for ["redis-stat-0.4.12"], already activated daemons-1.2.3 for ["thin-1.5.1", "redis-stat-0.4.12"] (Gem::LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:249:in `activate'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:248:in `each'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:248:in `activate'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:1082:in `gem'
from /usr/bin/redis-stat:18
[root@localhost softs]# gem uninstall daemons
Select gem to uninstall:
 1. daemons-1.1.9
 2. daemons-1.2.3
 3. All versions
> 2
Successfully uninstalled daemons-1.2.3
[root@localhost softs]# [root@localhost softs]# redis-stat 1
-bash: [root@localhost: command not found
[root@localhost softs]# gem install redis-stat -v 0.4.12
Successfully installed redis-stat-0.4.12
1 gem installed
Installing ri documentation for redis-stat-0.4.12...
Installing RDoc documentation for redis-stat-0.4.12...
[root@localhost softs]# redis-stat 1
Faraday: you may want to install system_timer for reliable timeouts
[root@localhost softs]# gem install SystemTimer
Building native extensions.  This could take a while...
Successfully installed SystemTimer-1.2.3
1 gem installed
Installing ri documentation for SystemTimer-1.2.3...
Installing RDoc documentation for SystemTimer-1.2.3...

命令行啓動redis-stat

redis-stat 192.168.0.131:7001 192.168.0.131:7002 192.168.0.132:7001 192.168.0.132:7002 192.168.0.133:7001 192.168.0.133:7002 

以webserver的形式啓動,指定端口

redis-stat 192.168.0.131:7001 192.168.0.131:7002 192.168.0.132:7001 192.168.0.132:7002 192.168.0.133:7001 192.168.0.133:7002 --server=8080 5 —daemon

瀏覽器訪問http://192.168.0.130:8080即可看到實時監控頁面

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章