最近在讀 《Redis開發與運維》這本書,發現搜狐視頻出的cachecloud這個平臺很是不錯,下面是本身的學習筆記。不過這個是用java語言寫的,如今開發運維通常都使用python,後期看看能不能用python模仿作一個這樣的運維管理平臺,提高本身開發能力。php
目錄結構:前端
-平臺簡介java
-平臺安裝node
- 安裝包python
- 數據庫配置mysql
- 應用配置文件git
- 編譯應用github
- 啓動應用web
- 登陸測試redis
-redis安裝
- 建立cachecloud用戶
- 初始化redis環境
- 添加機器
- 實際案例
- Redis sentinel羣集應用
- Redis Cluster羣集應用
一 平臺簡介
CacheCloud是搜狐視頻提供的一個開源Redis雲管理平臺:
實現多種類型(Redis Standalone、Redis Sentinel、Redis Cluster)自動部署、解決Redis實例碎片化現象、提供完善統計、監控、運維功能、減小開發人員的運維成本和誤操做,提升機器的利用率,提供靈活的伸縮性,提供方便的接入客戶端。 它主要功能有:
二 平臺安裝
2.1 下載安裝包
從官方網站上https://github.com/sohutv/cachecloud,下載下來後,解壓到/opt 下,確保環境已經安裝了。
2.2 數據庫配置
提早安裝好數據庫mysql數據庫,而後建立cachecloud數據庫,導入cachecloud數據庫腳本,具體命令以下:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.24-patch-1.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> create database cachecloud;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> grant all on cachecloud.* to 'admin'@'localhost' identified by 'admin123';
Query OK, 0 rows affected (0.01 sec)
MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)]> quit
[root@localhost opt]# mysql -uroot cachecloud < /opt/cachecloud-master/script/cachecloud.sql
2.3 修改應用配置文件
這裏須要說明的是,設置好數據庫地址,帳號密碼等。
[root@localhost opt]# vi /opt/cachecloud-master/cachecloud-open-web/src/main/swap/online.properties
cachecloud.db.url = jdbc:mysql://127.0.0.1:3306/cachecloud
cachecloud.db.user = admin
cachecloud.db.password = admin123
cachecloud.maxPoolSize = 20
2.4 編譯cachecloud
因爲源碼沒有編譯,須要使用mvn編譯應用,而後生成實際的war包。
#mvn clean compile install-Ponline
[root@localhost cachecloud-master]# mkdir -p /opt/cachecloud
[root@localhost cachecloud-master]# cp cachecloud-open-web/target/cachecloud-open-web-1.0-SNAPSHOT.war /opt/cachecloud/
[root@localhost cachecloud-master]# cp cachecloud-open-web/src/main/resources/cachecloud-web.conf /opt/cachecloud/
[root@localhost cachecloud-master]# ln -s /opt/cachecloud /etc/init.d/cachecloud
[root@localhost cachecloud-master]#
2.5 啓動cachecloud應用
[root@localhost cachecloud-master]# /etc/init.d/cachecloud start
which: no start-stop-daemon in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/pycharm-4.5.4/bin:/usr/local/php/bin:/usr/local/mysql/bin/:/root/bin)
Started [4551]
[root@localhost cachecloud-master]#
2.6 登陸測試
打開IE,輸入登陸地址http://192.168.100.53:8585,輸入用戶和密碼:admin/admin,便可登陸。
三 redis安裝
3.1 在客戶機上建立cachecloud用戶
這裏建立的用戶和密碼,要和cachecloud配置裏面一致,以下圖:
3.2 初始化redis環境
對於新安裝的機器,須要使用下面腳本安裝redis,和初始化環境
/opt/cachecloud-master/script/cachecloud-init.sh redis
3.3 添加機器
用管理員登陸後,到後臺管理,添加機器,能夠看到應用已經添加進來
四 實際案例
4.1 redis sentinel
Redis-Sentinel是Redis官方推薦的高可用性(HA)解決方案,redis-sentinel又叫哨兵,它去監控主從數據庫運行狀態,當用Redis作Master-slave的高可用方案時,假如master宕機了,Redis自己(包括它的不少客戶端)都沒有實現自動進行主備切換,而Redis-sentinel自己也是一個獨立運行的進程,它能監控多個master-slave集羣,發現master宕機後能進行自懂切換,從而實現高可用。哨兵能夠配置多個, 若是配置了多個,當主發生故障的時候,sentinel須要投票選舉是否切換。
服務器地址以下,須要說明的是,添加到cachecloud上後,須要執行下面腳本,初始化redis環境,安裝編譯redis
/opt/cachecloud-master/script/cachecloud-init.sh
192.168.199.202 master sentinel1
192.168.199.203 slave sentinel2
192.168.199.204 sentinel3
格式不能錯處,下面ip必須是已經添加到cachecloud的機器IP
192.168.199.202:512:192.168.199.203
192.168.199.202
192.168.199.203
192.168.199.204
點擊開始部署後,能夠查看後臺服務器
或者登錄服務器上查看
[root@autoserver ~]# ps -ef | grep redis
cachecl+ 8999 1 0 11:31 ? 00:00:00 redis-server *:6386
cachecl+ 9085 1 0 11:32 ? 00:00:00 redis-server *:6387 [sentinel]
[root@node01 ~]# redis-cli -h 192.168.199.202 -p 6386 info | grep role
role:master
[root@node01 ~]# redis-cli -h 192.168.199.203 -p 6386 info | grep role
role:slave
先設置一個數據值
[root@node01 ~]# redis-cli -h 192.168.199.202 -p 6386 set cw test01
OK
[root@node01 ~]# redis-cli -h 192.168.199.203 -p 6386 get cw
"test01"
[root@node01 ~]#
關閉master節點
[root@node01 ~]# redis-cli -h 192.168.199.202 -p 6386 shutdown
檢查進程確實沒有了
[root@autoserver ~]# ps -ef | grep redis
cachecl+ 9085 1 0 11:32 ? 00:00:03 redis-server *:6387 [sentinel]
查看日誌:
17092:X 23 Aug 11:46:16.995 # +new-epoch 3
17092:X 23 Aug 11:46:16.997 # +vote-for-leader 1c076f3969391012eb246c4723c40f593244462f 3
17092:X 23 Aug 11:46:17.027 # Next failover delay: I will not start a failover before Wed Aug 23 11:52:17 2017
17092:X 23 Aug 11:46:18.041 # +config-update-from sentinel 192.168.199.202:6387 192.168.199.202 6387 @ sentinel-192.168.199.202-6386 192.168.199.202 6386
17092:X 23 Aug 11:46:18.041 # +switch-master sentinel-192.168.199.202-6386 192.168.199.202 6386 192.168.199.203 6386
17092:X 23 Aug 11:46:18.042 * +slave slave 192.168.199.202:6386 192.168.199.202 6386 @ sentinel-192.168.199.202-6386 192.168.199.203 6386
17092:X 23 Aug 11:46:38.127 # +sdown slave 192.168.199.202:6386 192.168.199.202 6386 @ sentinel-192.168.199.202-6386 192.168.199.203 6386
獲取redis值
[root@node01 conf]# redis-cli -h 192.168.199.203 -p 6386 info | grep role
role:master
[root@node01 conf]# redis-cli -h 192.168.199.203 -p 6386 get cw
"test01"
[root@node01 conf]#
啓動192.168.199.202上的redis後,檢查發現,已經變爲從應用了。
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6386 info | grep role
role:slave
4.2 Redis Cluster
redis cluster羣集,是經過主歷來實現分片高可用,官方解釋:
下面主從分配不能在一臺機器上,3個交叉部署
192.168.199.202:200:192.168.199.203
192.168.199.203:200:192.168.199.204
192.168.199.204:200:192.168.199.202
同redis sentinel同樣,須要先點擊格式檢查,而後點擊開始部署
[root@node01 conf]# ps -ef | grep redis
cachecl+ 17230 1 0 11:31 ? 00:00:10 redis-server *:6386
cachecl+ 17316 1 0 11:32 ? 00:00:19 redis-server *:6387 [sentinel]
cachecl+ 18847 1 0 12:04 ? 00:00:00 redis-server *:6388 [cluster]
cachecl+ 18948 1 0 12:04 ? 00:00:00 redis-server *:6389 [cluster]
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6388 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:4
cluster_my_epoch:1
cluster_stats_messages_sent:420
cluster_stats_messages_received:417
[root@node01 conf]#
經過前端監控頁面,也能夠發現已經成功了。
首先須要生成數據
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6388 -c set cw0001 chenwei0001
OK
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6388 -c get cw0001
"chenwei0001"
[root@node01 conf]#
檢查羣集節點信息,能夠找到202:6388對應的從節點是203:6388,
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6388 cluster nodes
a53f3d619aebc611b79650f8dd8b37f00076fd4d 192.168.199.204:6380 slave d5523f7cdebe8ee551fa86b66a63689457fa7ab7 0 1503486623834 2 connected
301cbb1c365cc6a60f50cb6f9f0d295f54c80912 192.168.199.203:6388 slave 7e01a9ac4087ef10d28fa7412f4bd9907f97e868 0 1503486624844 3 connected
c7b89ca5a92d605730c78f93fd83d6a22fa9eafc 192.168.199.202:6389 slave 4468e49f7ec3049a9d3452199123d686bb2f1df0 0 1503486623331 4 connected
7e01a9ac4087ef10d28fa7412f4bd9907f97e868 192.168.199.202:6388 myself,master - 0 0 1 connected 0-5461
4468e49f7ec3049a9d3452199123d686bb2f1df0 192.168.199.204:6381 master - 0 1503486625855 3 connected 10924-16383
d5523f7cdebe8ee551fa86b66a63689457fa7ab7 192.168.199.203:6389 master - 0 1503486622827 0 connected 5462-10923
[root@node01 conf]#
[root@node01 conf]# redis-cli -h 192.168.199.203 -p 6388 info | grep role
role:slave
關閉主節點192.168.199.202:6388應用
[root@node01 conf]# redis-cli -h 192.168.199.202 -p 6388 shutdown
[root@node01 conf]# redis-cli -h 192.168.199.203 -p 6388 cluster nodes
a53f3d619aebc611b79650f8dd8b37f00076fd4d 192.168.199.204:6380 slave d5523f7cdebe8ee551fa86b66a63689457fa7ab7 0 1503486875906 2 connected
301cbb1c365cc6a60f50cb6f9f0d295f54c80912 192.168.199.203:6388 myself,master - 0 0 5 connected 0-5461
d5523f7cdebe8ee551fa86b66a63689457fa7ab7 192.168.199.203:6389 master - 0 1503486874894 0 connected 5462-10923
c7b89ca5a92d605730c78f93fd83d6a22fa9eafc 192.168.199.202:6389 slave 4468e49f7ec3049a9d3452199123d686bb2f1df0 0 1503486876916 4 connected
7e01a9ac4087ef10d28fa7412f4bd9907f97e868 192.168.199.202:6388 master,fail - 1503486840733 1503486839525 1 disconnected
4468e49f7ec3049a9d3452199123d686bb2f1df0 192.168.199.204:6381 master - 0 1503486877924 3 connected 10924-16383
驗證數據是否能夠讀取,經過其它主應用,發現能夠讀取到數據
[root@node01 conf]# redis-cli -h 192.168.199.204 -p 6381 -c get cw0001
"chenwei0001"
[root@node01 conf]#