CacheCloud環境需求
- Java 7
- Maven 3
- MySQL
- Redis 3
具體用法可參考:https://cachecloud.github.io
1.下載CacheCloud
官網https://github.com/sohutv/cachecloud
下載cachecloud-master.zip
解壓並移動cachecloud-master到/usr/local目錄
2.在MySQL中創建庫並授權
導入初始數據
3.編輯/usr/local/cachecloud-master/cachecloud-open-web/src/main/swap/online.properties文件
3.1在cachecloud根目錄下運行
mvn clean compile install -Ponline
3.2創建/opt/cachecloud-web目錄,並拷貝相關文件
mkdir -p /opt/cachecloud-web
cp cachecloud-open-web/target/cachecloud-open-web-1.0-SNAPSHOT.war /opt/cachecloud-web/
cp cachecloud-open-web/src/main/resources/cachecloud-web.conf /opt/cachecloud-web/
ln -s /opt/cachecloud-web/cachecloud-open-web-1.0-SNAPSHOT.war /etc/init.d/cachecloudweb
4.啟動cachecloud-web並通過http://IP:8585訪問,用戶名和密碼admin
/etc/init.d/cachecloudweb start
5.使用cachecloud-init.sh安裝redis和初始化,可根據需求修改腳本中redis的版本
6.在服務器上創建cachecloud SSH連接賬號,和系統配置管理頁面里的用戶名和密碼保持一致即可
useradd cachecloud
passwd cachecloud
6.1點擊右上角管理后台—>機器管理,添加機器
6.2點擊右上角導入應用
7.redis cluster安裝(要讓集群正常工作至少需要3個主節點,在這里我們要創建6個redis節點,其中三個為主節點,三個為從節點)
172.17.20.127 | redis-6379/6380 |
172.17.20.128 | redis-6381/6382 |
172.17.20.129 | redis-6383/6384 |
Redis 集群使用數據分片(sharding)而非一致性哈希(consistency hashing)來實現: 一個 Redis 集群包含 16384 個哈希槽(hash slot), 數據庫中的每個鍵都屬於這 16384 個哈希槽的其中一個, 集群使用公式 CRC16(key) % 16384 來計算鍵 key 屬於哪個槽, 其中 CRC16(key) 語句用於計算鍵 key 的 CRC16 校驗和 。集群中的每個節點負責處理一部分哈希槽。
下載編譯安裝redis
cp -pr redis redis-6380 #其他類似按照端口號復制
修改redis.conf
daemonize yes
port 6379 #其他配置改為6380/6381/6382/6383/6384pidfile /var/run/redis_6379.pid #其他配置按端口號修改
cluster-enabled yes
cluster-config-file nodes-6379.conf # 其他配置按端口號修改
cluster-node-timeout 15000
appendonly yes
創建配置文件軟連接並啟動redis
ln -s /opt/cachecloud/redis-6379/redis.conf /etc/redis-6379.conf #其他按照類似配置
分別啟動集群redis
8.安裝Redis 集群需要的Ruby工具
yum install ruby rubygems
gem install redis
進入reids目錄下面運行
cd /opt/cachecloud/redis/src/
- redis-trib.rb的create子命令構建
- --replicas 1則指定了為Redis Cluster中的每個Master節點配備1個Slave節點
- 節點角色由順序決定,先是master之后是slave
9.常用命令
查看集群情況
redis-trib.rb check ip:port #檢查集群狀態
redis-cli -c -h ip -p port #使用-c進入集群命令模式
redis-trib.rb rebalance ip:port --auto-weights #重新分配權重
增加、刪除集群節點
redis-trib.rb add-node ip:port(新增節點) ip:port(現有效節點)
redis-trib.rb del-node ip:port id(目標節點的id) #刪除master節點之前首先要使用reshard移除master的全部slot
重新划分slot
redis-trib.rb reshard ip:port
10.將master轉換為salve
cluster replicate master-id #轉換前6380端必須沒有slots
11.模擬故障切換
殺死一個redis進程,觀察FailOver情況
Redis集群通過選舉方式進行容錯,保證一台Server掛了還能跑,這個選舉是全部集群超過半數以上的Master發現其他Master掛了后,會將其他對應的Slave節點升級成Master.
12.把redis cluster加入cachecloud