2017年12月24日 星期日

Redis 4.0.6 Cluster on CentOS 7

Redis Cluster

建立 vm instances

建立 2 個 instances: rdc-01(10.128.0.6) 與 rdc-02(10.128.0.12), 設定如下:
由於 rdc-01 與 rdc-02 為 cluster, 以下步驟請重複 rdc-01 與 rdc-02
  • Boot disk
    • CentOS 7
  • Firewall
    • Allow HTTP traffic
    • Allow HTTPS traffic

更新套件管理包

SSH 進入 GCE, 更新套件管理包
# sudo su
# yum update -y

安裝相依套件

# yum -y install ruby ruby-devel rubygems rpm-build gcc tcl wget

抓取 redis 安裝包

# cd /home/lucars
# wget http://download.redis.io/releases/redis-4.0.6.tar.gz

解壓縮

# tar zxvf redis-4.0.6.tar.gz

編譯 Redis

# cd redis-4.0.6
# make

測試(可忽略)

# make test

安裝

# make install

建立目錄

# mkdir /etc/redis
# cd /etc/redis
# mkdir 7000 7001 7002

複製 config 檔案

以下步驟, 重複處理 7000 / 7001 / 7002
# cp /home/lucars/redis-4.0.6/redis.conf /etc/redis/7000/redis.conf

修改 config 檔案

以下步驟, 重複處理 7000 / 7001 / 7002
# vi /etc/redis/7000/redis.conf
修改內容如下
port 依序修改成 7000 / 7001 / 7002
bind 的 ip 請以內部 ip 為主
port 7000
bind 10.128.0.6
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000

啟動服務

# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7000/redis.conf
# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7001/redis.conf
# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7002/redis.conf

建立 Cluster

當 rdc-01 與 rdc-02 的 redis services 都啟動之後, 在 rdc-01 上啟動 cluster
以下步驟只需在 rdc-01 下執行
# gem install redis
# /home/lucars/redis-4.0.6/src/redis-trib.rb create --replicas 1 10.128.0.6:7000 10.128.0.6:7001 10.128.0.6:7002 10.128.0.12:7000 10.128.0.12:7001 10.128.0.12:7002
輸出內容如下
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.128.0.6:7000
10.128.0.12:7000
10.128.0.6:7001
Adding replica 10.128.0.12:7001 to 10.128.0.6:7000
Adding replica 10.128.0.6:7002 to 10.128.0.12:7000
Adding replica 10.128.0.12:7002 to 10.128.0.6:7001
M: 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2 10.128.0.6:7000
   slots:0-5460 (5461 slots) master
M: 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665 10.128.0.6:7001
   slots:10923-16383 (5461 slots) master
S: b5eb0947604ae617d32c542dc702ec1e623a7790 10.128.0.6:7002
   replicates dfa016eae87db5cf7a3347a700a4852888ce6f58
M: dfa016eae87db5cf7a3347a700a4852888ce6f58 10.128.0.12:7000
   slots:5461-10922 (5462 slots) master
S: 3bb319b44ff4934bcb9b8a1a4fc05f26cf57b5a5 10.128.0.12:7001
   replicates 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2
S: b560f9dab350d42f516e14db1141ad359607e628 10.128.0.12:7002
   replicates 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665
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 10.128.0.6:7000)
M: 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2 10.128.0.6:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: b560f9dab350d42f516e14db1141ad359607e628 10.128.0.12:7002
   slots: (0 slots) slave
   replicates 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665
M: 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665 10.128.0.6:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3bb319b44ff4934bcb9b8a1a4fc05f26cf57b5a5 10.128.0.12:7001
   slots: (0 slots) slave
   replicates 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2
M: dfa016eae87db5cf7a3347a700a4852888ce6f58 10.128.0.12:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b5eb0947604ae617d32c542dc702ec1e623a7790 10.128.0.6:7002
   slots: (0 slots) slave
   replicates dfa016eae87db5cf7a3347a700a4852888ce6f58
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

測試連線到 cluster

# /home/lucars/redis-4.0.6/src/redis-cli -h 10.128.0.6 -p 7000 -c

額外補充

redis-trib.rb 執行失敗: 找不到 redis

失敗原因
usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/lucars/redis-4.0.6/src/redis-trib.rb:25:in `<main>'
處理方式
# gem install redis

gem install redis 執行失敗: Ruby 版本低於 2.2.2

失敗原因
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.2.2.
更新到 ruby 到 2.2.4
# yum install gcc-c++ patch readline readline-devel zlib zlib-devel
# yum install libyaml-devel libffi-devel openssl-devel make
# yum install bzip2 autoconf automake libtool bison iconv-devel sqlite-devel
# curl -sSL https://rvm.io/mpapis.asc | gpg --import -
# curl -L get.rvm.io | bash -s stable
# source /etc/profile.d/rvm.sh
# rvm reload
# rvm requirements run
# rvm install 2.2.4
# rvm use 2.2.4 --default
# ruby --version