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

redis集群搭建

目錄 計算 sta filename tin aof global tsl policy

一、redis集群說明

集群采用一主三從,總共39從,總共12臺,每個機房中放置一主三從

技術分享

每個機房中放置其他機房的一從,達到高可用性

二、集群搭建

1.redis集群的搭建

1.1修改系統參數

1.使用root賬戶修改

  /etc/security/limits.conf

  添加

  * soft nofile 102400

  * hard nofile 102400

  修改 /etc/sysctl.

  添加net.core.somaxconn=32767

  sysctl -p 生效

  2.創建redis用戶

1.2 redis的安裝

1.下載並編譯

  主節點機器上創建文件

  mkdir -p /home/redis/redis/bin

  mkdir -p /home/redis/redis/bin

  mkdir -p /home/redis/redis/log

在主節點下

redis目錄下

wget http://download.redis.io/releases/redis-3.2.5.tar.gz

tar xzf redis-3.2.5.tar.gz

cd redis-3.2.5

make

cp src/redis-server ../redis/bin

cp src/redis-cli ../redis/bin

cp src/redis-trib.rb ../redis/bin

cp redis.conf ../redis/conf

touch /home/redis/redis/conf/cluster.conf

2.修改參數redis.conf,具體參數配置見後面附件

3.在其他機器上創建redis用戶

4.發送到其他機器上去

scp -r /home/redis/redis [email protected]:/home/redis/
5.登錄到各機器上,啟動redis

(1)修改cluster.conf配置

配置文件結構

redis.conf---

|---cluster.conf(master)

|---cluster.conf(slave1)

|---cluster.conf(slave2)

|---cluster.conf(slave3)

修改master,slave1,slave2,slave3節點中cluster.conf配置,在cluster.conf配置首行添加參數(各節點cluster.conf參數見附件)

include /home/redis/redis/conf/redis.conf

2)啟動redis

cd /home/redis/redis/

./bin/redis-server ./conf/cluster.conf

1.3集群操作

1.集群創建

1)添加redis server到cluster

1) 增加主節點

./bin/redis-trib.rb create master1,master2,master3

2)登錄redis客戶端

./bin/redis-cli -c -p 6379

3)查看master node id(類似這種:3eb099e326bc4c3d763223743d4dc90caa975cc8)

cluster nodes

4)添加從節點

./bin/redis-trib.rb add-node --salve -master-id nodeid /

slaveip:port ip:port(ip:port為任意一加入集群中的節點ip和port)

註意:安裝中可能遇到問題報:

/usr/bin/env: ruby: No such file or directory

則需要安裝ruby

yum install ruby

yum install rubygems

wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem

gem install -l ./redis-3.2.1.gem

(2)驗證集群搭建成功

./bin/redis-cli -c -p 6379

cluster info

查看cluster_state:ok則為成功

2.集群的節點增加

(1)master節點增加

1)啟動新增的節點

2)加入到集群中

./bin/redis-trib.rb add-node 新增節點ip:port 集群中已存在任意節點ip:prot

3)為新節點分配slot

./bin/redis-trib.rb reshard

顯示如下內容:

#根據提示選擇要遷移的slot數量

How many slots do you want to move (from 1 to 16384)? 500

#選擇要接受這些slotnode-id

What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf

#選擇slot來源:

#all表示從所有的master重新分配,

#或者數據要提取slotmaster節點id,最後用done結束

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.

Source node #1:all

#打印被移動的slot後,輸入yes開始移動slot以及對應的數據.

#Do you want to proceed with the proposed reshard plan (yes/no)? yes

#結束

(2)slave節點增加

./bin/redis-trib.rb add-node --salve -master-id nodeid /

slaveip:port ip:port(ip:port為任意一加入集群中的節點ip和port)

3.集群節點的重新負載

./bin/redis-trib.rb reshard 舊的masterip:port

#從該節點移動slot到其他master節點

How many slots do you want to move (from 1 to 16384)? 500

#接受slot的節點id

What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf

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.

#輸入all則分配給所有的master,done則為指定的master

Source node #1:done

4.節點的刪除

1master節點的刪除

1)清空master上的slot

#把要刪除的master節點slot和數據遷移到隨意一臺master

redis-trib.rb reshard 192.168.1.151:6379

#根據提示選擇要遷移的slot數量

How many slots do you want to move (from 1 to 16384)? 500(被刪除master的所有slot數量)

#選擇要接受這些slotnode-id(192.168.1.152:6379)

What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2

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.

Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被刪除masternode-id)

Source node #2:done

#打印被移動的slot後,輸入yes開始移動slot以及對應的數據.

#Do you want to proceed with the proposed reshard plan (yes/no)? yes

2)刪除節點

./bin/redis-trib.rb del-node 192.168.1.151:6379 \

‘f51e26b5d5ff74f85341f06f28f125b7254e61bf‘

(2)slave節點的刪除

./bin/redis-trib.rb del-node 192.168.1.151:6380 \

’5d664f56fb2e7763bfca89754271378a0ac3b657’

4.client中關於cluster的命令

集群

CLUSTER INFO 打印集群的信息

CLUSTER NODES 列出集群當前已知的所有節點(node),以及這些節點的相關信息。

節點

CLUSTER MEET <ip> <port> ip port 所指定的節點添加到集群當中,讓它成為集群的一份子。

CLUSTER FORGET <node_id> 從集群中移除 node_id 指定的節點。

CLUSTER REPLICATE <node_id> 將當前節點設置為 node_id 指定的節點的從節點。

CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。

(slot)

CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。

CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。

CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麽先讓另一個節點刪除該槽>,然後再進行指派。

CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。

CLUSTER SETSLOT <slot> IMPORTING <node_id> node_id 指定的節點中導入槽 slot 到本節點。

CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入(import)或者遷移(migrate)。

CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。

CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。

CLUSTER GETKEYSINSLOT <slot> <count> 返回 count slot 槽中的鍵。

5.

2.redis的運維

實時監控命令

watch -n 1 -d "/home/redis/redis/bin/redis-cli -c -p 6379 info | grep -e "connected_clients" -e "blocked_clients" -e "used_memory_human" -e "used_memory_peak_human" -e "rejected_connections" -e "evicted_keys" -e "instantaneous""

監控參數:

#連接數

connected_clients:1

#阻塞連接數

blocked_clients:0

#使用內存

used_memory_human:799.66K

#使用內存峰值

used_memory_peak_human:852.35K

#每秒執行命令個數

instantaneous_ops_per_sec:0

#每秒讀字節數

instantaneous_input_kbps:0.01

#每秒寫字節數

instantaneous_output_kbps:1.23

#拒絕連接數

rejected_connections:0

#因內存大小限制,被驅逐的鍵個數

evicted_keys:0

附件:

redis.conf配置文件

配置項

說明

cluster-enabled

yes

表示以集群方式運行,為no表示以非集群方式運行

cluster-node-timeout

3000

單位為毫秒:

repl-ping-slave-period+

(cluster-node-timeout*

cluster-slave-validity-factor)

cluster-slave-validity-factor

0

如果要最大的可用性,值設置為0

repl-ping-slave-period

1

slave ping master的時間間隔,單位為秒

repl-timeout

10

復制超時,單位為秒,須大於repl-ping-slave-period的值

slave-read-only

yes

slave是否只讀

slave-serve-stale-data

yes

當slave與master斷開連接,slave是否繼續提供服務

appendonly

yes

開啟aof

daemonize

yes

守護線程運行

protected-mode

no

是否限制遠程連接

tcp-backlog

32767

取值不能超過系統的/proc/sys/net/core/somaxconn

auto-aof-rewrite-percentage

100

設置自動rewite AOF文件(手工rewrite只需要調用命令BGREWRITEAOF)

auto-aof-rewrite-min-size

64mb

觸發rewrite的AOF文件大小,只有大於此大小時才會觸發rewrite

no-appendfsync-on-rewrite

yes

子進程在做rewrite時,主進程不調用fsync(由內核默認調度)

cluster-require-full-coverage

no

為no表示有slots不可服務時其它slots仍然繼續服務

repl-backlog-size

64M

默認1M,當寫入量很大時,backlog溢出會導致增量復制不成功

client-output-buffer-limit

normal 256mb 128mb 60

避免普通客戶端進行大批量數據查詢,如keys *,lrang等操作下的buffer不夠導致鏈接斷開

client-output-buffer-limit

slave 512mb 256mb 180

避免主從復制過程中,buffer過小導致復制鏈接斷開,rdb文件重傳或者循環的rdb操作

stop-writes-on-bgsave-error

no

避免redis後臺save失敗後,redis拒絕寫操作,前提是需要有監控軟件

maxmemory

8G

最大內存(可以不設置)

maxmemory-policy

volatile-lru

內存清理策略

volatile-lru 使用LRU算法來刪除過期的set

allkeys-lru 刪除任何遵循LRU算法的key

volatile-random 隨機地刪除過期set中的key

allkeys-random 隨機地刪除一個key

volatile-ttl 刪除最近即將過期的key(the nearest expire time (minor TTL))

noeviction 根本不過期,寫操作直接報錯

主節點cluster.conf配置文件

配置項

說明

port

6379

客戶端連接端口,並且總有一個剛好大於10000的端口,這個大的端口用於主從復制和集群內部通訊。

cluster-config-file

nodes-6379.conf

pidfile

/var/run/redis-6379.pid

只有當daemonize值為yes時,才有意義;並且這個要求對目錄/var/run有寫權限,否則可以考慮設置為/tmp/redis-6379.pid。

dir

/home/redis/redis/data/6379

dbfilename

dump-6379.rdb

位於dir指定的目錄下

appendfilename

"appendonly-6379.aof"

logfile

/home/redis/redis/log/redis-6379.log

日誌文件,包含目錄和文件名

從節點1 cluster.conf 配置文件

配置項

說明

port

6380

cluster-config-file

nodes-6380.conf

pidfile

/var/run/redis-6380.pid

dir

/home/redis/redis/data/6380

AOF和RDB文件存放目錄

dbfilename

dump-6380.rdb

RDB文件名

appendfilename

appendonly-6380.aof

AOF文件名

logfile

/home/redis/redis/log/redis-6380.log

從節點2 cluster.conf配置文件

配置項

說明

port

6381

cluster-config-file

nodes-6381.conf

pidfile

/var/run/redis-6381.pid

dir

/home/redis/redis/data/6381

AOF和RDB文件存放目錄

dbfilename

dump-6381.rdb

RDB文件名

appendfilename

appendonly-6381.aof

AOF文件名

logfile

/home/redis/redis/log/redis-6381.log

從節點3 cluster.conf 配置文件

配置項

說明

port

6382

cluster-config-file

nodes-6382.conf

pidfile

/var/run/redis-6382.pid

dir

/home/redis/redis/data/6382

AOF和RDB文件存放目錄

dbfilename

dump-6382.rdb

RDB文件名

appendfilename

appendonly-6382.aof

AOF文件名

logfile

/home/redis/redis/log/redis-6382.log

redis集群搭建