Codis github上的介紹安裝,裏面很全,並且也有中/英文的,只不過按照github的步驟安裝,會有一些坑,因此有了這麼一篇文章。html
在上一篇文章《Redis實用監控工具一覽》中,介紹了Redis經常使用的監控工具,codis是帶有圖形化的面板和管理工具的。因而纔有了這麼一篇文章。桌面系統:Centos7。linux
Codis 是一個分佈式 Redis 解決方案, 對於上層的應用來講, 鏈接到 Codis Proxy 和鏈接原生的 Redis Server 沒有明顯的區別 (不支持的命令列表), 上層應用能夠像使用單機的 Redis 同樣使用, Codis 底層會處理請求的轉發, 不停機的數據遷移等工做, 全部後邊的一切事情, 對於前面的客戶端來講是透明的, 能夠簡單的認爲後邊鏈接的是一個內存無限大的 Redis 服務.git
codis-proxy 是客戶端鏈接的 Redis 代理服務, codis-proxy 自己實現了 Redis 協議, 表現得和一個原生的 Redis 沒什麼區別 (就像 Twemproxy), 對於一個業務來講, 能夠部署多個 codis-proxy, codis-proxy 自己是無狀態的.github
codis-config 是 Codis 的管理工具, 支持包括, 添加/刪除 Redis 節點, 添加/刪除 Proxy 節點, 發起數據遷移等操做. codis-config 自己還自帶了一個 http server, 會啓動一個 dashboard, 用戶能夠直接在瀏覽器上觀察 Codis 集羣的運行狀態.golang
codis-server 是 Codis 項目維護的一個 Redis 分支, 基於 2.8.13 開發, 加入了 slot 的支持和原子的數據遷移指令. Codis 上層的 codis-proxy 和 codis-config 只能和這個版本的 Redis 交互才能正常運行.web
Codis 依賴 ZooKeeper 來存放數據路由表和 codis-proxy 節點的元信息, codis-config 發起的命令都會經過 ZooKeeper 同步到各個存活的 codis-proxy.redis
Codis 支持按照 Namespace 區分不一樣的產品, 擁有不一樣的 product name 的產品, 各項配置都不會衝突.spring
Codis架構圖apache
圖片來源於網絡,侵刪。vim
注意,爲了不重複造輪子。以上「簡介」部分來自百度百科的摘錄。
1.1 安裝Go
1.1.1 下載Go壓縮包
習慣系在最新的環境,目前最新版的go是1.12.5的,這裏咱們就用最新版的。最新版更新能夠在這裏看。https://golang.org/dl/
wget https://storage.googleapis.com/golang/go1.12.5.linux-amd64.tar.gz
1.1.2 解壓Go壓縮包
tar -zxvf go1.12.5.linux-amd64.tar.gz
1.1.3 設置環境變量
如圖,go的安裝目錄是 /usr/local
vim /etc/profile
export GOROOT=/usr/local/go #設置爲go安裝的路徑 export GOPATH=/usr/local/gopath #默認安裝包的路徑 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
更新/etc/profile, wq
保存,使用 source /etc/profile
命令可使新創建的環境變量馬上生效而不用從新啓動系統。
1.1.4 helloword go go go
新建一個go文件,而後運行。
package main import "fmt" func main(){ fmt.Printf("hello,world\n") }
1.2 安裝JDK
沒有安裝Java JDK的朋友能夠直接看這裏。《CentOS安裝Java JDK》
1.3 安裝ZooKeeper
1.3.1 下載ZooKeeper壓縮包
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
1.3.2 解壓ZooKeeper壓縮包
tar -zxvf zookeeper-3.4.13.tar.gz
1.3.3 刪除ZooKeeper壓縮包
rm -f zookeeper-3.4.13.tar.gz
1.3.4 拷貝配置文件
cd /usr/local/zookeeper-3.4.13/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
這個zoo.cfg是zookeeper的配置文件,這裏我搭的是單機版,若是想搭集羣版也是經過修改配置文件便可。
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/zookeeper-3.4.13/data #這裏最好本身設置 # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 server.1=cnblogs01:8888:9888 #這裏修改成本身的主機名或者IP server.2=cnblogs02:8888:9888 server.3=cnblogs03:8888:9888
建立/usr/local/zookeeper-3.4.13/data文件夾,新建一個myid,寫入1
mkdir data
vim myid
1.3.5 啓動ZooKeeper
/usr/local/zookeeper-3.4.13/bin/zkServer.sh start
1.4 安裝Codis
1.4.1 安裝Codis
Codis 源代碼須要下載到 $GOPATH/src/github.com/CodisLabs/codis
mkdir -p $GOPATH/src/github.com/CodisLabs
cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.2
1.4.2 編譯Codis
cd $GOPATH/src/github.com/CodisLabs/codis
make
直接經過 make 進行編譯,這裏報了個錯 ./autogen.sh:行5: autoconf: 未找到命令
安裝autoconf
yum install autoconf
安裝autoconf以後,繼續經過 make
編譯。
dashboard,proxy,admin,ha,fe這些codis的組件編譯完成了。
注意的是,目錄結構必定要是,gopath的本身的定義,單後後面的目錄須要新建,必須和官網同樣,不同,要報錯。 $GOPATH/src/github.com/CodisLabs/
1.4.3 啓動codis-dashboard
使用 codis-dashboard-admin.sh
腳本啓動 dashboard,並查看 dashboard 日誌確認啓動是否有異常。
./admin/codis-dashboard-admin.sh start
tail -100 ./log/codis-dashboard.log.2019-05-26
注意:2019-05-26爲當前日期。
1.4.3 啓動codis-proxy
使用 codis-proxy-admin.sh
腳本啓動 codis-proxy,並查看 proxy 日誌確認啓動是否有異常。
./admin/codis-proxy-admin.sh start
tail -100 ./log/codis-proxy.log.2019-05-26
1.4.4 啓動codis-server
使用 codis-server-admin.sh
腳本啓動 codis-server,並查看 redis 日誌確認啓動是否有異常。
./admin/codis-server-admin.sh start
tail -100 /tmp/redis_6379.log
查看日誌以下圖所示,啓動codis時報錯提示:
21875:M 26 May 20:51:24.154 * Increased maximum number of open files to 10032 (it was originally set to 1024). 21875:M 26 May 20:51:24.154 # Creating Server TCP listening socket 127.0.0.1:6379: bind: Address already in use
由於Redis默認端口號就是6379,因爲以前(centos安裝Redis)設置了本機默認開機啓動Redis,因此6379已被佔用。
解決方案
以下圖所示,修改 /usr/local/gopath/src/github.com/CodisLabs/codis/config/redis.conf
配置文件的端口號。改成6380
修改端口號之後再經過 ./admin/codis-server-admin.sh start
啓動codis-server。
tail -100 /tmp/redis_6379.log
查看日誌以下:
注意:若是redis.conf中對應的logfile也改爲6380的話,查看日誌就得用 tail -100 /tmp/redis_6380.log
1.4.5 啓動codis-fe
使用 codis-fe-admin.sh
腳本啓動 codis-fe,並查看 fe 日誌確認啓動是否有異常。
./admin/codis-fe-admin.sh start
tail -100 ./log/codis-fe.log.2019-05-26
1.5 經過fe添加group
經過web瀏覽器訪問集羣管理頁面(fe地址:127.0.0.1:9090) 選擇咱們剛搭建的集羣 codis-demo,在 Proxy 欄可看到咱們已經啓動的 Proxy, 可是 Group 欄爲空,由於咱們啓動的 codis-server 並未加入到集羣 添加 NEW GROUP,NEW GROUP 行輸入 1,再點擊 NEW GROUP 便可 添加 Codis Server,Add Server 行輸入咱們剛剛啓動的 codis-server 地址,添加到咱們剛新建的 Group,而後再點擊 Add Server 按鈕便可,以下圖所示:
1.6 經過fe初始化slot
新增的集羣 slot 狀態是 offline,所以咱們須要對它進行初始化(將 1024 個 slot 分配到各個 group),而初始化最快的方法可經過 fe 提供的 rebalance all slots
按鈕來作,以下圖所示,點擊此按鈕,咱們即快速完成了一個集羣的搭建。
1.7 經過 ansible 快速部署集羣
使用 ansible 可快速在單機、多機部署多套 codis 集羣。 ansible 文件夾包含了部署 codis 集羣的 playbook,根據本身部署環境修改 groups_var/all
文件裏參數,修改 hosts 文件添加部署的環境 IP 便可。 ansible 安裝也及其簡單,各部署機器無需安裝任何額外的 agent,彼此之間經過 ssh 通訊。
git clone https://github.com/ansible/ansible.git -b stable-2.3 cd ./ansible source ./hacking/env-setup cd $codis_dir/ansible ansible-playbook -i hosts site.yml
2.1 添加Redis實例
這裏再分別添加638一、6382兩個Redis實例。
cp config/redis.conf config/redis6381.conf
cp config/redis.conf config/redis6382.conf
分別更新638一、6382.conf的port、pidfile和logfile
vim config/redis6381.conf
更新以後,啓動新增的兩個Redis實例。
./bin/codis-server ./config/redis6381.conf
./bin/codis-server ./config/redis6382.conf
按照上面add server的方法(以下圖)添加兩個實例,注意,若未執行 ./bin/codis-server ./config/redis6381.conf
,添加時會報錯。
剛添加進來的默認狀態是NO:ONE,以下圖,點擊小扳手(SLAVEOF 127.0.0.1:6380)。
測試效果:
注意:如上圖,開啓集羣管理以後,6380可讀可寫,81和82只有只讀權限。
2.2 建立新的分組
按照上面步驟繼續建立7380和7381.
在Codis • Dashboard(http://ip:9090/#codis-demo)中添加新的分組和server成員。
本篇文章只聊了codis,不聊集羣,若是對集羣感興趣的能夠看看以前的一篇文章。《詳解Redis Cluster集羣》
本文"安裝步驟"段落,大面積採用的是Codis github上的介紹,該段落也能夠去github看看。固然本文中也加入了一些github上沒有說的太清楚或者不太好理解的細節。
原本打算在本文中直接介紹springboot引入codis的,可是這篇文章已經很長,排版起來實在不方便。因此關於springboot能夠在這裏看。《SpringBoot進階教程(五十九)整合Codis》。
其餘參考資料:
做 者:請叫我頭頭哥
出 處:http://www.cnblogs.com/toutou/
關於做者:專一於基礎平臺的項目開發。若有問題或建議,請多多賜教!
版權聲明:本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文連接。
特此聲明:全部評論和私信都會在第一時間回覆。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我
聲援博主:若是您以爲文章對您有幫助,能夠點擊文章右下角【推薦】一下。您的鼓勵是做者堅持原創和持續寫做的最大動力!