1. 程式人生 > >Redis cluster 簡單搭建

Redis cluster 簡單搭建

Redis cluster

環境

兩臺虛擬機虛擬6個節點,每臺3個節點,創建出3 master,3 slave 環境

系統 IP Redis節點
CentOS 6 192.168.153.12 7000、7001、7002
CentOS 7 192.168.153.11 7003、7004、7005

Redis版本:

redis-3.0.6.tar.gz

安裝 Redis

Redis官方由於xx原因無法訪問,Redis的包需要自行下載。

tar zxvf redis-3.0.6.tar.gz && cd redis-3.0.6 && make && make install

創建 Redis 節點

在兩臺機器上分別創建一個 redis_cluster 文件夾,這裏放置在 /usr/local 目錄下。(路徑和文件夾名都是可以隨便定義的,你可以在 /tmp/ 下創建一個 xx 目錄,做為集群目錄也可以。)

mkdir -p /usr/local/redis_cluster/

然後在 192.168.153.12 下創建 7000、7001、7002 3個目錄。在 192.168.153.11 下創建7003、7004、7005 3個目錄。
在192.168.153.12執行

mkdir -p /usr/local/redis_cluster/700{0..2}

在192.168.153.11執行

mkdir -p /usr/local/redis_cluster/700{3..5}

並將 redis.conf 分別拷貝到700* 目錄裏面。redis.conf在前面解壓的源碼包裏面

cd redis-3.0.6 && cp redis.conf /usr/local/redis_cluster/7000
cp redis.conf /usr/local/redis_cluster/7001
... 以此類推

分別修改每個700*目錄下的 redis.conf

daemonize yes   #做為守護進程運行
port 7000       #這裏是7000目錄下,所以改為7000,如果是7001目錄下就修改為7001
bind 192.168.153.12 #本機ip
appendonly yes
cluster-enabled yes     #開啟集群
cluster-config-file nodes-7000.conf #集群配置文件,同 port 選項,在7001目錄下則修改為7001
cluster-node-timeout 5000

啟動節點

執行命令的時候不會有回顯。如果有回顯,說明啟動有問題。
192.168.153.12

redis-server /usr/local/redis_cluster/7000/redis.conf
redis-server /usr/local/redis_cluster/7001/redis.conf
redis-server /usr/local/redis_cluster/7002/redis.conf

192.168.153.11

redis-server /usr/local/redis_cluster/7003/redis.conf
redis-server /usr/local/redis_cluster/7004/redis.conf
redis-server /usr/local/redis_cluster/7005/redis.conf

檢查啟動情況

192.168.153.12

ps -ef|grep redis
root       4215      1  0 19:27 ?        00:00:13 redis-server 192.168.153.12:7000 [cluster]
root       4219      1  0 19:27 ?        00:00:13 redis-server 192.168.153.12:7001 [cluster]
root       4223      1  0 19:27 ?        00:00:13 redis-server 192.168.153.12:7002 [cluster]
root       5192   5133  0 21:32 pts/2    00:00:00 grep redis

192.168.153.11

ps -ef|grep redis
root       1138      1  0 14:19 ?        00:00:00 redis-server 192.168.153.11:7003 [cluster]
root       1142      1  0 14:19 ?        00:00:00 redis-server 192.168.153.11:7004 [cluster]
root       1146      1  0 14:19 ?        00:00:00 redis-server 192.168.153.11:7005 [cluster]

創建集群

官方提供一個創建集群的工具redis-trib,它位於 Redis 源碼的 src 文件夾中, 它是一個 Ruby 程序, 這個程序通過向實例發送特殊命令來完成創建新集群, 檢查集群, 或者對集群進行重新分片(reshared)等工作。
所以要安裝 Ruby 及 RubyGems。

yum install ruby rubygems -y

由於眾所周知的原因,Gems的源也是沒法用的,我們需要將它替換為國內源。

gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
gem sources -l
https://gems.ruby-china.org
# 確保只有 gems.ruby-china.org

用 Gems 安裝 Redis

gem install redis

上面這個命令會提示你
redis requires Ruby version >= 2.2.2,讓你升級Ruby
因為默認安裝的是最新版 Redis 4.x 版本。而我本次安裝的是 Redis 3.0.6 的,gem安裝的也要配套,所以要用下面這個命令

gem install redis -v 3.0.6

redis-trib.rb 這個工具在源碼包下的 src文件夾,現在將它復制到 /usr/local/bin/ 目錄,這樣就可以直接在命令行下使用該工具了。

cp /tmp/redis-3.0.6/src/redis-trib.rb /usr/local/bin/ 

運行 redis-trib.rb 工具創建集群

# redis-trib.rb  create  --replicas  1 192.168.153.12:7000 192.168.153.12:7001 192.168.153.12:7002 192.168.153.11:7003 192.168.153.11:7004 192.168.153.11:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.153.11:7003
192.168.153.12:7000
192.168.153.11:7004
Adding replica 192.168.153.12:7001 to 192.168.153.11:7003
Adding replica 192.168.153.11:7005 to 192.168.153.12:7000
Adding replica 192.168.153.12:7002 to 192.168.153.11:7004
M: 60aa133262cd817fa729cc2c1103a25e3a9b2311 192.168.153.12:7000
   slots:5461-10922 (5462 slots) master
S: 30570cd99b32ffbc5f39ef7df58059a53d51ea50 192.168.153.12:7001
   replicates b6454920091c772bb82f166683fd899fddffe35d
S: 560764d15cad022d0d83286d6a83f6aa880cd0e9 192.168.153.12:7002
   replicates fa0fcc6e09c5dca5824f23f0b012b1eb3efe247b
M: b6454920091c772bb82f166683fd899fddffe35d 192.168.153.11:7003
   slots:0-5460 (5461 slots) master
M: fa0fcc6e09c5dca5824f23f0b012b1eb3efe247b 192.168.153.11:7004
   slots:10923-16383 (5461 slots) master
S: 2dc77357e6fb97f15a0fa03ab43e796ed8fb2e2a 192.168.153.11:7005
   replicates 60aa133262cd817fa729cc2c1103a25e3a9b2311
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.153.12:7000)
M: 60aa133262cd817fa729cc2c1103a25e3a9b2311 192.168.153.12:7000
   slots:5461-10922 (5462 slots) master
M: 30570cd99b32ffbc5f39ef7df58059a53d51ea50 192.168.153.12:7001
   slots: (0 slots) master
   replicates b6454920091c772bb82f166683fd899fddffe35d
M: 560764d15cad022d0d83286d6a83f6aa880cd0e9 192.168.153.12:7002
   slots: (0 slots) master
   replicates fa0fcc6e09c5dca5824f23f0b012b1eb3efe247b
M: b6454920091c772bb82f166683fd899fddffe35d 192.168.153.11:7003
   slots:0-5460 (5461 slots) master
M: fa0fcc6e09c5dca5824f23f0b012b1eb3efe247b 192.168.153.11:7004
   slots:10923-16383 (5461 slots) master
M: 2dc77357e6fb97f15a0fa03ab43e796ed8fb2e2a 192.168.153.11:7005
   slots: (0 slots) master
   replicates 60aa133262cd817fa729cc2c1103a25e3a9b2311
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群認證

測試 Redis 集群比較簡單的辦法就是使用 redis-rb-cluster 或者 redis-cli 。註意端口變化

[root@cs6 ~]# redis-cli -h 192.168.153.12 -c -p 7000
192.168.153.12:7000> set foo bar
-> Redirected to slot [12182] located at 192.168.153.11:7004
OK
192.168.153.11:7004> set hello world
-> Redirected to slot [866] located at 192.168.153.11:7003
OK
192.168.153.11:7003> get foo
-> Redirected to slot [12182] located at 192.168.153.11:7004
"bar"
192.168.153.11:7004> get hello
-> Redirected to slot [866] located at 192.168.153.11:7003
"world"

Redis cluster 簡單搭建