redis集群方案與redis cluster集群實現
一:redis集群與高可用有多重方式可以實現,比如高可用可以使用哨兵或者redis主從+keepalived的方式實現,redis集群支持多重方式,比如客戶端分片、代理方式、redis cluster以及Coodis,每個實現方式都有自己的優缺點,具體方案及實現如下:
1.1:客戶端分片:
mysql、memcached以及redis等都可以通過客戶端分片實現,其中mysql還可以通過客戶端實現分庫分表,客戶端分片是在客戶端將key進行hash按照不同的值保存到不同的redis 服務器,讀取的話也是按照不同的位置進行讀取,優勢是比較靈活,不存在單點故障,缺點是添加節點需要重新配置分片算法,并且需要手動同步數據,在緩存場景客戶端分片最適用于使用memcached,因為緩存是可以丟失一部分數據的,但是memcached可以做集群進行數據同步。
1.2:Redis Cluster:
在3.0版本以后支持,無中心,在某種情況下會造成數據丟失,其也是通過算法將數據分片保存至某個redis服務器,即不再通過客戶端計算key保存的redis服務器,redis服務器需要提前設置好自己所負責的槽位,比如redis A負責處理0-5000的哈希槽位數據,redis B負責處理5001-10000的hash槽位數據,redis C負責處理10001-16384的hash槽位數據,redis cluster需要特定的客戶端,要求客戶端必須支持集群協議 ,但是目前還沒有比較好的客戶端。
這種將哈希槽分布到不同節點的做法使得用戶可以很容易地向集群中添加或者刪除節點。 比如說:
如果用戶將新節點 D 添加到集群中, 那么集群只需要將節點 A 、B 、 C 中的某些槽移動到節點 D 就可以了。
與此類似, 如果用戶要從集群中移除節點 A , 那么集群只需要將節點 A 中的所有哈希槽移動到節點 B 和節點 C , 然后再移除空白(不包含任何哈希槽)的節點 A 就可以了。
因為將一個哈希槽從一個節點移動到另一個節點不會造成節點阻塞, 所以無論是添加新節點還是移除已存在節點, 又或者改變某個節點包含的哈希槽數量, 都不會造成集群下線。
redis cluster需要專門的客戶端,比如python當中引入的redis模塊也不能使用了, 目前官方的客戶端也不是很多,需要自己開發,
1.3:代理:
例如Twemproxy,由proxy代替客戶端換和服務端實現分片,可以使用在緩存場景中允許數據丟失的場景,其還支持memcached,可以為proxy配置算法,缺點為twemproxy是瓶頸,不支持數據遷移,官方github地址https://github.com/twitter/twemproxy/
1.4:Codis:豌豆莢的開源方案,目前redis集群比較穩定的方案,豌豆莢gitlab地址https://github.com/pingcap:
豌豆莢codis項目官方github地址https://github.com/CodisLabs/codis
可以動態擴容和縮容
多業務完全透明,業務不知道運行的是codis
支持多核心CPU,twemproxy只能單核
codis是有中心基于proxy的設計,是客戶端像連接單機一樣操作proxy
有部分命令不能支持,比如keys *等
支持group劃分,組內可以設置一個主多個從,通過sentinel 監控redis主從,當主down了自動將從切換為主
設置的進程要最大等于CPU的核心,不能超過CPU的核心數
其基于zookeeper,里面保存的是key保存的redis主機位置,因此zookeeper要做高可用
監控可以使用接口和dashboard
tidb豌豆莢團隊實現的分布式mysql數據庫,github地址https://github.com/pingcap/tidb
二:實現redis cluster:
2.1:環境:
操作系統:Centos 7.2-1511
服務器數量:2 臺,啟動8個redis服務
redis 版本:3.2.6
2.2:部署redis cluster集群,并實現動態增加主機:
2.2.1:服務器分別下載并安裝redis:
# cd /opt
# wget http://download.redis.io/releases/redis-3.2.6.tar.gz
# tar xvf redis-3.2.6.tar.gz
# ln -sv /opt/redis-3.2.6 /usr/local/redis
# cd /usr/local/redis/
# make && make install
2.2.2:需要服務器啟動6個redis 服務做集群,實現3主3從,另外在準備2個服務做動態集群的主機添加,一個主一個從,一共是每個服務器啟動8個redis 服務,因此需要準備8個不通的redis 配置文件:
[root@redis1 redis]# pwd
/usr/local/redi
# mkdir conf.d && cd conf.d
# mkdir `seq 6381 6388`
# cp /usr/local/redis/redis.conf /usr/local/redis/conf.d/6381/
# vim /usr/local/redis/conf.d/6381/redis.conf #主要修改以下地方,將各服務的端口、PID、日志文件以及數據持久保存的路徑進行單獨保存:
bind 0.0.0.0
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
loglevel notice
logfile "/usr/local/redis/conf.d/6381/6381.log"
dir /usr/local/redis/conf.d/6381/
maxmemory 512M
appendonly yes
appendfilename "6381.aof"
appendfsync everysec
cluster-enabled yes #必須打開cluster功能,否則集群創建不成功
cluster-config-file 6381.conf #每個主機一個配置文件,有集群創建和管理,集群內的各主機不能重名
2.2.3:服務器批量生成redis配置文件:
# cp /usr/local/redis/conf.d/6381/redis.conf /opt/ #復制配置文件為模板,通過sed批量生成reids 配置文件
# sed 's/6381/6382/g' /opt/redis.conf >> /usr/local/redis/conf.d/6382/redis.conf
# sed 's/6381/6383/g' /opt/redis.conf >> /usr/local/redis/conf.d/6383/redis.conf
# sed 's/6381/6384/g' /opt/redis.conf >> /usr/local/redis/conf.d/6384/redis.conf
# sed 's/6381/6385/g' /opt/redis.conf >> /usr/local/redis/conf.d/6385/redis.conf
# sed 's/6381/6386/g' /opt/redis.conf >> /usr/local/redis/conf.d/6386/redis.conf
# sed 's/6381/6387/g' /opt/redis.conf >> /usr/local/redis/conf.d/6387/redis.conf
# sed 's/6381/6388/g' /opt/redis.conf >> /usr/local/redis/conf.d/6388/redis.conf
2.2.4:服務器批量啟動redis 服務并驗證端口存在:
# for i in `seq 6381 6388`;do /usr/local/redis/src/redis-server /usr/local/redis/conf.d/$i/redis.conf;done

2.2.5:服務器驗證redis 命令可以連接到redis服務:
# redis-cli -h 192.168.10.101 -p 6388
192.168.10.101:6388>
2.3:安裝ruby 管理工具:
2.3.1:配置ruby源并安裝redis管理工具:
# yum install ruby rubygems -y
# gem install redis
# gem sources -l #當前使用的源為國外,但是連接很慢而且經常連接不上,因此將其刪除并添加阿里的ruby源
*** CURRENT SOURCES ***
https://rubygems.org/
# gem source -r https://rubygems.org/ #刪除默認的國外ruby源
# gem sources --add https://ruby.taobao.org/ #添加淘寶的ruby源
# gem sources -l #驗證ruby源已經更換
*** CURRENT SOURCES ***
https://ruby.taobao.org/
# gem sources -u #更新緩存
# gem install redis #安裝redis 工具
Fetching: redis-3.3.2.gem (100%)
Successfully installed redis-3.3.2
Parsing documentation for redis-3.3.2
Installing ri documentation for redis-3.3.2
1 gem installed
2.3.2:復制ruby的管理腳本:
# cp /usr/local/redis/src/redis-trib.rb /usr/local/bin/redis-trib
2.3.3:redis-trib命令介紹:
# redis-trib help
Usage: redis-trib <command> <options> <arguments ...>
help (show this help)
del-node host:port node_id #刪除節點
reshard host:port #重新分片
--timeout <arg>
--pipeline <arg>
--slots <arg>
--to <arg>
--yes
--from <arg>
fix host:port
--timeout <arg>
create host1:port1 ... hostN:portN #創建集群
--replicas <arg>
rebalance host:port
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
--use-empty-masters
--auto-weights
--weight <arg>
call host:port command arg arg .. arg
add-node new_host:new_port existing_host:existing_port #添加節點
--slave
--master-id <arg>
check host:port #檢測節點
import host:port
--replace
--copy
--from <arg>
set-timeout host:port milliseconds
info host:port
2.4:在單機部署redis cluster集群:
2.4.1:創建redis cluster:
# redis-trib create --replicas 1 192.168.10.101:6381 192.168.10.101:6382 192.168.10.101:6383 192.168.10.101:6384 192.168.10.101:6385 192.168.10.101:6386
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.10.101:6381
192.168.10.101:6382
192.168.10.101:6383
Adding replica 192.168.10.101:6384 to 192.168.10.101:6381 #前三個節點主主,后三個節點是從
Adding replica 192.168.10.101:6385 to 192.168.10.101:6382
Adding replica 192.168.10.101:6386 to 192.168.10.101:6383
M: fc3b44c6d18abbf7191338a8a7fafdc516b6d758 192.168.10.101:6381 #主
slots:0-5460 (5461 slots) master #master的分片位置
M: b3f5ba5a3b1f53e358f438c923f9591055510b96 192.168.10.101:6382 #主
slots:5461-10922 (5462 slots) master #master 的分片位置
M: 8fe3c52b352a1209dfc3b9f2f9fa1be9bc2e69a5 192.168.10.101:6383 #主
slots:10923-16383 (5461 slots) master #master的分片位置
S: b786bd3a567634a7087aa289714063ed6e53bb47 192.168.10.101:6384 #從
replicates fc3b44c6d18abbf7191338a8a7fafdc516b6d758
S: 7d2da4da536003b2c25c8599526c1c937d071e1e 192.168.10.101:6385 #從
replicates b3f5ba5a3b1f53e358f438c923f9591055510b96
S: cc76054bf257f9bbfec868b86880e22f04fab96e 192.168.10.101:6386 #從,可見后三個節點是前三個節點的從
replicates 8fe3c52b352a1209dfc3b9f2f9fa1be9bc2e69a5
Can I set the above configuration? (type 'yes' to accept): yes #輸入yes繼續,no退出
>>> 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.10.101:6381)
M: fc3b44c6d18abbf7191338a8a7fafdc516b6d758 192.168.10.101:6381 #當前redis服務的ID即主機IP短褲呢
slots:0-5460