這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
一、基本資料
1、Codis叢集架構
image
2、環境資訊
作業系統:macos 10.12.3
codis版本:3.1
go語言版本:1.8.1
etcd版本:3.2.0-rc.0
二、部署Codis
1、安裝go
下載地址:https://golang.org/dl
Mac:
選擇go1.8.1.darwin-amd64.pkg下載並安裝
Windows:
選擇go1.8.1.windows-amd64.msi下載並安裝
Linux:
選擇go1.8.1.linux-386.tar.gz或者go1.8.1.linux-amd64.tar.gz下載(根據作業系統位元選擇下載對應的版本)
解壓到/usr/local路徑,並設定環境變數
//解壓到/usr/local路徑tar -C /usr/local/ -xzf go1.8.1.linux-amd64.tar.gz
#設定環境變數export GOROOT=/usr/local/goexport PATH=$PATH:$GOROOT/binexport GOPATH=/usr/local/gopath
2、安裝godep
go env GOPATH $GOPATHgo get -u github.com/tools/godep && which godep $GOPATH/bin/godep
3、安裝etcd
mkdir -p $GOPATH/src/github.com/coreoscd $GOPATH/src/github.com/coreosgit clone https://github.com/coreos/etcd.gitcd etcd./build
#設定環境變數export PATH=$PATH:$GOPATH/src/github.com/coreos/etcd/bin
4、安裝codis
#下載Codis原始碼mkdir -p $GOPATH/src/github.com/CodisLabscd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.1
#編譯Codis原始碼cd $GOPATH/src/github.com/CodisLabs/codismake
5、啟動codis服務
注意:以下命令需要指定設定檔和log路徑,我已經配置好了,下載設定檔,具體設定檔說明請看官方文檔
1. 啟動etcd
#啟動的連接埠為2379etcd --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379' &
說明:為叢集狀態提供外部儲存
2. 啟動codis-dashboard
./bin/codis-dashboard --config=conf/dashboard-18080.toml --log=conf/dashboard-18080.log --log-level=WARN &
說明:叢集管理工具,支援codis-proxy、codis-server 的添加、刪除,以及據遷移等操作
參數說明:--config 指定設定檔路徑 --log 指定記錄檔路徑
設定檔說明:
| 參數 |
說明 |
例子 |
| coordinator_name |
外部儲存類型,接受 zookeeper/etcd |
"etcd" |
| coordinator_addr |
外部儲存地址 |
"127.0.0.1:2379" |
| product_name |
叢集名稱,滿足正則 \w[\w\.\-]* |
"demo-test" |
| product_auth |
叢集密碼,預設為空白 |
"123" |
| admin_addr |
RESTful API 連接埠 |
"0.0.0.0:18080" |
3. 啟動codis-fe
./bin/codis-fe --log=conf/fe.log --log-level=WARN --etcd=127.0.0.1:2379 --listen=0.0.0.0:8080 &
說明:叢集管理介面
參數說明:--log 指定記錄檔路徑 --etcd 指定外部儲存地址 --listen 指定管理介面訪問地址
4. 啟動codis-server
./bin/codis-server --include conf/redis-16379.conf &./bin/codis-server --include conf/redis-17379.conf &
說明:基於 redis-3.2.8 分支開發
參數說明:--include 指定設定檔路徑
設定檔說明:
| 參數 |
說明 |
例子 |
| port |
監聽連接埠 |
"16379" |
| save |
db檔案儲存路徑,為空白則是codis主目錄 |
"" |
| dbfilename |
db檔案名稱 |
"16379.rdb" |
| requirepass |
登入密碼 |
"123" |
5. 啟動codis-proxy
./bin/codis-proxy --config=conf/proxy-19000.toml --log=conf/proxy-19000.log --etcd 127.0.0.1:2379 --log-level=WARN &
說明:用戶端串連的 Redis 代理服務, 實現了 Redis 協議。 除部分命令不支援以外(不支援的命令列表),表現的和原生的 Redis 沒有區別
參數說明:--config 指定設定檔路徑 --log 指定記錄檔路徑 --etcd 指定外部儲存地址
設定檔說明:
| 參數 |
說明 |
例子 |
| product_name |
叢集名稱,參考 dashboard 參數說明 |
demo-test |
| product_auth |
叢集密碼,預設為空白 |
123 |
| proto_type |
Redis 連接埠類型,接受 tcp/tcp4/tcp6/unix/unixpacket |
tcp4 |
| admin_addr |
RESTful API 連接埠 |
0.0.0.0:11080 |
| proxy_addr |
Redis 連接埠地址或者路徑 |
0.0.0.0:19000 |
| proxy_datacenter |
資料中心地址,codis-server需要指定的地址 |
localhost |
6、高可用(HA)服務
說明:二選一,也可不選,該工具會在檢測到 master 掛掉的時候主動應用主從切換策略,提升單個 slave 成為新的 master
1. 啟動codis-ha(可選組件)
./bin/codis-ha --log=conf/ha.log --log-level=WARN --dashboard=127.0.0.1:18080 &
說明:codis內建的主從切換工具
參數說明:--log 指定記錄檔路徑 --dashboard 指定針對哪個dashboard服務
2. 啟動codis-sentinel(可選組件)
./bin/codis-server conf/sentinel-26379.conf --sentinel &./bin/codis-server conf/sentinel-26380.conf --sentinel &./bin/codis-server conf/sentinel-26381.conf --sentinel &
說明:具體的切換機制請參考Redis Sentinel機制與用法
參數說明:--sentinel 以哨兵模式啟動
三、Codis管理介面
瀏覽器訪問http://127.0.0.1:8080開啟管理介面
1、添加組
image
2、添加Redis伺服器
image
3、添加哨兵
image
4、初始化Slots
image
說明:新增的叢集 slot 狀態是 offline,因此我們需要對它進行初始化(將 1024 個 slot 分配到各個 group),而初始化最快的方法可通過 fe 提供的 rebalance all slots 按鈕來做,如所示,點擊此按鈕,我們即快速完成了一個叢集的搭建。