背景說明:
? ? 項目初期使用AWS的ElastiCache?Redis作為系統(tǒng)的數(shù)據(jù)緩存藻三,近期由于數(shù)據(jù)量和QPS加大八匠,導(dǎo)致Redis存在壓力,進行了兩次的單點垂直升級趴酣,緩解了目前的壓力梨树。項目組進行了數(shù)據(jù)和費用的評估,考慮到日后升級岖寞,決定使用Redis Cluster集群方案抡四,使用三個Master節(jié)點搭建集群。(不使用3Master+3Slave)
前期準(zhǔn)備:
? ? 1仗谆、Redis 安裝包:本次使用Redis 4.0.8版本進行搭建
? ? 下載地址:http://download.redis.io/releases/redis-4.0.8.tar.gz?
? ? 2指巡、Ruby環(huán)境,本次使用yum安裝 :yum install -y ruby rubygems
? ? 3隶垮、下載Ruby Redis工具包藻雪,本次使用3.3.3版本(后續(xù)說明版本問題)
? ? ? ? 采用離線下載:wget https://rubygems.org/downloads/redis-3.3.3.gem
? ? 4、在AWS上啟動3個EC2實例狸吞,實例類型為:t2.medium(雙核4g勉耀,40g SSD存儲)
? ? ? ? ip分別為:172.31.3.71、172.31.3.72蹋偏、172.31.3.73
開始搭建:
? ? 1便斥、安裝Redis:指定安裝目錄到:/workspace/libs/redis
????????tar xvfz redis-4.0.8.tar.gz
? ? ????cd redis-4.0.8
? ? ????make && make install? PREFIX=/workspace/libs/redis
? ? 2、配置Redis:在/workspace/libs/redis下創(chuàng)建conf目錄威始,用于存放配置文件
? ? ????拷貝??redis-4.0.8目錄下的redis.conf到/workspace/libs/redis/conf下
? ? ? ? 修改配置如下:
? ? ? ? ?注意:不能設(shè)置密碼枢纠,否則集群啟動時會連接不上
????????????port? 7001? ? ? //配置端口? ? ? ? ? ? ? ? ? ? ? ?
????????????//根據(jù)本機所在的IP或hostname去配置 node1 node2 node3?
????????????bind 本機ip //172.31.3.71,172.31.3.72,172.31.3.73? ? ? ? ? ? ? ? ? ? ? ? ? ??
????????????daemonize? ? yes? ? ? ? ? ? ? ? ?//redis后臺運行? ? ?
????????????//pidfile文件對應(yīng)7000,7001,7002
????????????pidfile? /var/run/redis_7001.pid? ? ? ? ?
????????????cluster-enabled? yes? ? ? ? ? ? ? ? ? ??????????//開啟集群?
????????????//集群的配置? 配置文件首次啟動自動生成 7001
????????????cluster-config-file? /workspace/libs/redis/conf/nodes_7001.conf? ? ?
????????????//請求超時? 默認(rèn)15秒,可自行設(shè)置
????????????cluster-node-timeout? 15000? ? ? ? ? ? ?
????????????appendonly? yes? ? ? ?????????//aof日志開啟? 本次使用aof
????????????//禁用rdb 注釋掉save
????????????#save 900 1
????????????#save 300 10
????????????#save 60 10000
? ? 3黎棠、安裝Ruby Redis工具包:安裝:gem install -l ./redis-3.3.3.gem
? ? 4晋渺、啟動Redis:
????????cd?/workspace/libs/redis
????????/workspace/libs/redis/bin/redis-server /workspace/libs/redis/conf/redis.conf
? ? ? ? 查看當(dāng)前目下是否生成appendonly.aof 文件和 ./conf/下是否生成:nodes-7001.conf
? ? 5、啟動集群:
? ? ? ? 拷貝redis-4.0.8/src下的文件redis-trib.rb到/workspace/libs/redis/bin/目錄下
? ? ? ? cd?/workspace/libs/redis/bin/
????????./redis-trib.rb create 172.31.3.71:7001 172.31.3.72:7002 172.31.3.73:7003
? ?6脓斩、 檢查集群:
? ? ./redis-trib.rb info 172.31.3.71:7001?
? ??./redis-trib.rb check 172.31.3.71:7001
? ?7木西、 添加節(jié)點:添加172.31.3.74:7004節(jié)點
? ??????./redis-trib.rb add-node 172.31.3.74:7004 172.31.3.71:7001
? ? ? ? ??注意:如果172.31.3..74:7004以前添加到集群過,重新添加的話俭厚,會報錯: [ERR] Node 172.31.3..74:7004 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
? ? ? ? 需要先刪除/workspace/libs/redis目錄下的appendonly.aof和conf下的nodes-7004.conf文件户魏,再重新執(zhí)行
? ?8、刪除節(jié)點:刪除節(jié)點4d3e007b38bf89aede64d0facc15124d9d605511(masterId)
? ??????./redis-trib.rb del-node 172.31.3.71:7001 4d3e007b38bf89aede64d0facc15124d9d605511
? ?9挪挤、移動槽位:
? ??????./redis-trib.rb reshard 172.31.3.71:7001
? ? ? ? 關(guān)閉槽位:多個節(jié)點(7001叼丑,7002)的1234槽位被打開,需要手動依次關(guān)閉
? ??????./redis-cli -h 172.31.3.71 -p 7001 -c
????????172.31.3.71:7001> CLUSTER SETSLOT 1234 stable
? ???????./redis-cli -h 172.31.3.72 -p 7002 -c
? ? ? ? ?172.31.3.72:7002> CLUSTER SETSLOT 1234 stable
? 10扛门、數(shù)據(jù)導(dǎo)入:導(dǎo)入外部節(jié)點:172.31.3.70:6379
? ??????./redis-trib.rb import --copy --from 172.31.3.70:6379 172.31.3.71:7001
功能測試:
? ??????./redis-cli -h 172.31.3.71 -p 7001 -c
????????172.31.3.71:7001> set aaa aaa
??????172.31.3.71:7001> get aaa
遇到問題:
? ? 1鸠信、ruby-redis版本兼容問題:
? ? ? ? 之前使用redis-4.0.0.rc1.gem安裝,在移槽的時候论寨,發(fā)生異常星立,導(dǎo)致槽位打開爽茴,需要手動關(guān)閉。
? ? ? ? 解決辦法:安裝redis-3.3.3.gem版本
? ? 2绰垂、從ElastiCache?Redis導(dǎo)入到集群中室奏,報錯:Migrating ????????D342A51BFE16FC943B2B9EADEC9414C4D38114FE_131141 to 172.31.3.71:7001: ERR unknown ????????command 'migrate'
? ? ? ? 網(wǎng)上搜了一遍,有人提到AWS 的ElastiCache?Redis 不支持migrate方法劲装,目前只能通過代碼遷移
參考鏈接:
? ??https://redis.io/topics/cluster-tutorial
? ??https://www.cnblogs.com/feiyun126/p/7248989.html