這次來記錄一下架設 Redis Cluster 的經過。因為有公司GCP的帳號,所以這次環境是放在 GCP VM 上。想說 Cloud Launcher 不是點一點就幫你把 Cluster 架好了嗎…. 但是並沒有那麼簡單的事。首先我信心滿滿的 Launch 了這個:
但是後來RD跟我說他們想要UAT上的 Redis 是 Cluster,這樣才比較接近 Production 的設定。但這個 Redis HA 是幫你 Launch Master/Slave 的 Replication Set。
首先上網研究了一下 Redis Cluster 到底長什麼樣子,理解下來大概是這樣:
由成雙的 Master/Slave 組成的 Cluster,每一個 Node 可以透過Node 設定好的 Port 前綴加1 互相溝通 (i.e. 6379 -> 16379 )
官網建議最少 Cluster 是 6 輛
Note that the minimal cluster that works as expected requires to contain at least three master nodes.
由於時間緊迫,不得已只好自己開VM來架 Cluster。選擇了比較熟悉的 CentOS7,先將 Redis 裝起來
$ sudo yum -y update $ sudo yum install redis -y $ sudo vim /etc/redis.config
這邊需要設定 Redis 要作為 Cluster 的組態。因為 Redis Cluster 設定時會讓你選擇誰是 Master 誰是 Slave, 所以 `redis.conf` 裡不需要設定 `slaveof`
大致上的設定是這樣的:
port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes_6379.conf cluster-node-timeout 15000 appendonly yes
六台都一樣,然後要啟動了
$ sudo systemctl start redis.service
啟動後看看狀況
$ sudo systemctl status redis.service ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Fri 2018-04-27 06:12:30 UTC; 4s ago Main PID: 12254 (redis-server) CGroup: /system.slice/redis.service └─12254 /usr/bin/redis-server 127.0.0.1:6379
都啟動後就可以來準備架設 Cluster了
首先你需要 Ruby。Redis 的 Github source 有付可以幫你設定 Cluster 的 Script `./redis-trib.rb`;是用 Ruby 寫的。(需要注意的是,我在 GCP 上 create 的這個 CentOS7 Ruby 版本是 2.0.0, 但要執行 `./redis-trib.rb` 需要 2.2 以上的 Ruby。)
所以我需要先 Upgrade Ruby。
接下來就是到 Github Redis Repository去把 Source Clone 下來 (官網本來是說, `gem install redis` 會放在 Utils 資料夾,但找來找去找不到,可能後來的版本就不附了)。
找到 `./redis-trib.rb` 後,確認一下六台主機的 ip (因為我bind 0.0.0.0) 是否正確並且 Redis 有啟動後。用下列指令來設定 Cluster
$ ./redis-trib.rb create --replicas 1 10.140.0.10:6379 10.140.0.11:6379 10.140.0.12:6379 10.140.0.13:6379 10.140.0.14:6379 10.140.0.15:6379 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 10.140.0.10:6379 10.140.0.11:6379 10.140.0.12:6379 Adding replica 10.140.0.13:6379 to 10.140.0.12:6379 Adding replica 10.140.0.14:6379 to 10.140.0.10:6379 Adding replica 10.140.0.15:6379 to 10.140.0.11:6379</code> M: ac14f4b6c395cfd7e715f901a4c726ffa0198273 10.140.0.10:6379 slots:0-5460 (5461 slots) master M: 9bba60c982771ed8945caae0a4469f10615ddce4 10.140.0.11:6379 slots:5461-10922 (5462 slots) master M: 32bf455e14e41ef3eb94ca27d0180b0164334cea 10.140.0.12:6379 slots:10923-16383 (5461 slots) master S: b9eb6df267af707365282f641f66d36dcb1a25bc 10.140.0.13:6379 replicates 32bf455e14e41ef3eb94ca27d0180b0164334cea S: e263026d609aa02c7dc845c287fc26ca7f4650d1 10.140.0.14:6379 replicates ac14f4b6c395cfd7e715f901a4c726ffa0198273 S: aeaacfed42bf7754f545abde642cf01380604ca4 10.140.0.15:6379 replicates 9bba60c982771ed8945caae0a4469f10615ddce4 Can I set the above configuration? (type 'yes' to accept):
設定 Master/Slave 配對的結構會如上顯示給確認。如果沒什麼問題的話,輸入 `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 10.140.0.10:6379) ........ ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
醬就差不多設定好了。
其實好像沒那麼麻煩。麻煩的是準備六輛 VM…. 但對 GCP 其實也沒很熟,應該有更快生 VM 出來的方式。但這次就先處理到這裡好了。