一、版本資訊
CentOS:release 6.5 (Final)
GCC:4.4.7
Redis:5.0.9
二、叢集搭建
2.1叢集規劃
主節點:11.11.11.160:6379,11.11.11.161:6379,11.11.11.162:6379
從節點:11.11.11.163:6379,11.11.11.164:6379,11.11.11.165:6379
2.2叢集安裝
首先建立/data/redis-cluster檔案夾,下載下傳 Redis 壓縮檔案到該檔案夾下,然後編譯安裝:
mkdir /data/redis-cluster
cd /data/redis-cluster
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
tar -zxvf redis-5.0.9.tar.gz
cd redis-5.0.9
make
修改/data/redis-cluster/redis-5.0.9檔案夾下的 redis.conf 檔案,修改如下部分:
port 6379
#bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6379.conf
protected-mode no
daemonize yes
appendonly yes
requirepass [email protected]
masterauth [email protected]
logfile "/data/logs/redis/redis.log"
pidfile /var/run/redis_6379.pid
配置說明
port:修改 Redis 端口
#bind 127.0.0.1:注釋掉後外網就可以連接配接 Redis 了
cluster-enabled:設定為 yes 表示開啟叢集
cluster-config-file: 表示叢集節點的配置檔案
protected-mode:由于配置了密碼登入,這裡将 protected-mode 設定為 no 關閉保護模式。
daemonize:設定為 yes 表示允許 Redis 在背景啟動。
requirepass:表示登入該 Redis 執行個體所需的密碼
masterauth:由于每個節點都開啟了密碼認證,是以又增加 masterauth 配置,使得從機可以登入到主機上。
appendonly:開啟aof檔案
其他配置項,更加詳細的說明:Redis配置檔案詳解
執行如下指令啟動 Redis 執行個體:
/data/redis-cluster/redis-5.0.9/src/redis-server /data/redis-cluster/redis-5.0.9/redis.conf
用redis-cli建立redis叢集
/data/redis-cluster/redis-5.0.9/src/redis-cli -a [email protected] --cluster create 11.11.11.160:6379 11.11.11.161:6379 11.11.11.162:6379 11.11.11.163:6379 11.11.11.164:6379 11.11.11.165:6379 --cluster-replicas 1
建立日志:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 11.11.11.164:6379 to 11.11.11.160:6379
Adding replica 11.11.11.165:6379 to 11.11.11.161:6379
Adding replica 11.11.11.163:6379 to 11.11.11.162:6379
M: f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:6379
slots:[0-5460] (5461 slots) master
M: 64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:6379
slots:[5461-10922] (5462 slots) master
M: 98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:6379
slots:[10923-16383] (5461 slots) master
S: a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:6379
replicates 98ca7df6298a84c58f73412f95a398a050ca302e
S: c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:6379
replicates f6bade412603174b67db6b1a470fd673f9a5c314
S: 5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:6379
replicates 64026cdbf85335a43d16ccfb9f5e0e335faa2722
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 11.11.11.160:6379)
M: f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:6379
slots: (0 slots) slave
replicates f6bade412603174b67db6b1a470fd673f9a5c314
M: 64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:6379
slots: (0 slots) slave
replicates 64026cdbf85335a43d16ccfb9f5e0e335faa2722
S: a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:6379
slots: (0 slots) slave
replicates 98ca7df6298a84c58f73412f95a398a050ca302e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2.3叢集驗證
2.3.1設定環境變量
vim /etc/profile
#添加
export REDIS_HOME=/data/redis-cluster/redis-5.0.9/src
export PATH=${JAVA_HOME}/bin:$PATH:$REDIS_HOME
source /etc/profile
2.3.2連接配接用戶端
redis-cli -c -a [email protected] -h 11.11.11.160 -p 6379
提示:-a通路服務端密碼,-c表示叢集模式,指定ip位址和端口号
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
11.11.11.160:6379>
11.11.11.160:6379>
11.11.11.160:6379> keys *
(empty list or set)
11.11.11.160:6379> set name my name is lihy
-> Redirected to slot [5798] located at 11.11.11.161:6379
(error) ERR syntax error
11.11.11.161:6379> keys *
(empty list or set)
11.11.11.161:6379> set name 'my name is lihy'
OK
11.11.11.161:6379> keys *
1) "name"
11.11.11.161:6379> get name
"my name is lihy"
2.3.3檢視叢集資訊
11.11.11.161:6379>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:2
cluster_stats_messages_ping_sent:1545
cluster_stats_messages_pong_sent:1615
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:3162
cluster_stats_messages_ping_received:1612
cluster_stats_messages_pong_received:1547
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:3162
2.3.4檢視節點清單
11.11.11.161:6379> cluster nodes
98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:[email protected] master - 0 1600765329213 3 connected 10923-16383
a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:[email protected] slave 98ca7df6298a84c58f73412f95a398a050ca302e 0 1600765330215 3 connected
64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:[email protected] myself,master - 0 1600765331000 2 connected 5461-10922
f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:[email protected] master - 0 1600765331217 1 connected 0-5460
5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:[email protected] slave 64026cdbf85335a43d16ccfb9f5e0e335faa2722 0 1600765331000 6 connected
c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:[email protected] slave f6bade412603174b67db6b1a470fd673f9a5c314 0 1600765332221 1 connected
2.3.5關閉叢集
關閉叢集則需要逐個進行關閉,使用指令:
redis-cli -a [email protected] -c -h 11.11.11.160 -p 6379 shutdown
2.3.6手動管理redis叢集
3、将 ip 和 port 所指定的節點添加到叢集中
CLUSTER MEET <ip> <port>
4、從叢集中移除 node_id 指定的節點
CLUSTER FORGET <node_id>
5、将目前節點設定為 node_id 指定的節點的從節點
CLUSTER REPLICATE <node_id>
6、将節點的配置檔案儲存到硬碟裡面
CLUSTER SAVECONFIG
7、将一個或多個槽(slot)指派(assign)給目前節點
CLUSTER ADDSLOTS <slot> [slot ...]
8、移除一個或多個槽對目前節點的指派
CLUSTER DELSLOTS <slot> [slot ...]
9、 移除指派給目前節點的所有槽,讓目前節點變成一個沒有指派任何槽的節點
CLUSTER FLUSHSLOTS
10、将槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點删除該槽>,然後再進行指派
CLUSTER SETSLOT <slot> NODE <node_id>
11、将本節點的槽 slot 遷移到 node_id 指定的節點中
CLUSTER SETSLOT <slot> MIGRATING <node_id>
12、從 node_id 指定的節點中導入槽 slot 到本節點
CLUSTER SETSLOT <slot> IMPORTING <node_id>
13、取消對槽 slot 的導入(import)或者遷移(migrate)
CLUSTER SETSLOT <slot> STABLE
14、計算鍵 key 應該被放置在哪個槽上
CLUSTER KEYSLOT <key>
15、傳回槽 slot 目前包含的鍵值對數量
CLUSTER COUNTKEYSINSLOT <slot>
16、傳回 count 個 slot 槽中的鍵
CLUSTER GETKEYSINSLOT <slot> <count>