Codis集群搭建摘要
來自: http://blog.jobbole.com/99246/
準備方案
Golang環境搭建
環境搭建很簡單,下載go1.4.2.linux-amd64.tar.gz安裝包,直接解壓并添加到環境變量就可以。 假設解壓到/usr/local/go下,這個目錄就是GOROOT,另外需要定義一個go開發目錄,假設為/workspace/golang。 go開發目錄未來會產生一些主要的子目錄:
1. src 存放源碼
2. pkg 編譯后生成的文件
3. bin 編譯后生產的可執行文件(比如godep命令在安裝后就會放在這個目錄下)
環境變量添加:
export GOROOT=/usr/local/go export GOPATH=/workspace/golang PATH=".:$PATH:$GOROOT/bin:$GOPATH/bin:$ZOOKEEPER/bin"
生效后就可以執行go version查看go的版本信息。 將$GOPATH/bin加入PATH是為了讓一些開源的或者自己安裝的go擴展命令(如godep)可以全局使用。
Codis下載和編譯
最新版的Codis位于[ https://github.com/CodisLabs/codis ] 通過下面命令獲得源碼
go get -u -d github.com/CodisLabs/codis
進入源碼路徑,執行make開始編譯。
Codis的編譯使用了godep,如果沒有安裝的話就會報godep command not found的錯誤。 godep是golang的一個包管理工具,通過go get github.com/tools/godep
安裝,成功后執行godep就會有對應的命令信息,如果沒有可以在$GOPATH/bin目錄下查找這個命令,然后加入環境變量。
如果你是用golang 1.5 beta3以上的版本進行編譯,還有可能出現的一個問題是
GOPATH=godep path godep restore Error: GO15VENDOREXPERIMENT is enabled and the vendor/ directory is not a valid Go workspace. godep: Error restore requires GOPATH but it is empty. make: *** [godep] Error 1
這是因為golang 1.5 beta3之后go添加了GO15VENDOREXPERIMENT這個特性,并在1.6默認開啟,你可以參照Codis issue715 里面的方案解決。 最簡單就是在編譯前
export GO15VENDOREXPERIMENT = 0
編譯后的文件
編譯后會產生一個bin目錄,下面有三個可執行文件和一個目錄
assets //dashboard的靜態文件目錄 codis-config //codis 命令式配置管理組件 codis-proxy //codis 代理也就是核心組件 codis-server //codis 基于redis2.8.21的定制版redis
這就是我們最終集群部署的時候需要用到的文件,當然跨平臺的話主要要交叉編譯或者在特定平臺下編譯,不然可能無法運行。
集群方案
這里使用三臺機器做一個小的集群。每臺機器部署一個zookeeper實例,兩個codis server實例(也就是redis server)。 其中兩臺機器部署codis proxy,另外一臺開啟codis dashboard。
host | ip | zookeeper port | redis server master/slave port | codis proxy | codis dashboard |
---|---|---|---|---|---|
slave1 | 192.168.4.124 | 2181 | 6379/6380 | Y | |
slave2 | 192.168.4.126 | 2181 | 6379/6380 | Y | |
slave3 | 192.168.4.128 | 2181 | 6379/6380 | Y |
集群搭建
zookeeper集群
zookeeper的搭建這里就不詳細介紹了,最終節點是
1. slave1:2181 2. slave2:2181 3. slave3:2181
先把zookeeper起起來
codis服務集群
創建codis服務目錄
[root@slave1]# mkdir -p /usr/local/codis/{log,redis_conf} [root@slave1]# cp -rf bin /usr/local/codis/ [root@slave1]# cp config.ini /usr/local/codis/conf/ [root@slave1]# cp extern/redis-test/6379.conf /usr/local/codis/redis_conf/
對每臺機器執行以上操作
根據注釋修改config.ini文件
##### 這些配置是為dashboard和proxies服務的 # zookeeper or etcd coordinator=zookeeper # Use comma "," for multiple instances. If you use etcd, you should also use this property. zk=slave1:2181,slave2:2181,slave3:2181 # 這個是標識zk下的命名空間,比如命名test在zookeeper會/zk/codis/db_test下 product=test # dashboard的地址和監聽端口 dashboard_addr=192.168.4.126:18087 password= ##### 下面是proxies的配置 # Proxy will ping-pong backend redis periodly to keep-alive backend_ping_period=5 # If there is no request from client for a long time, the connection will be droped. Set 0 to disable. session_max_timeout=1800 # Buffer size for each client connection. session_max_bufsize=131072 # Number of buffered requests for each client connection. # Make sure this is higher than the max number of requests for each pipeline request, or your client may be blocked. session_max_pipeline=1024 # If proxy don't send a heartbeat in timeout millisecond which is usually because proxy has high load or even no response, zk will mark this proxy offline. # A higher timeout will recude the possibility of "session expired" but clients will not know the proxy has no response in time if the proxy is down indeed. # So we highly recommend you not to change this default timeout and use Jodis(https://github.com/CodisLabs/jodis) # which watches the available proxies and will skip the offline proxy or add new online proxy automatically. # If you are not using Java in client, you can DIY a zk watcher accourding to Jodis source code. zk_session_timeout=30000 ##### 每個代理的id,不能相同 比如slave1 命名為proxy_1, slave2命名為proxy_2 proxy_id=proxy_1
啟動dashboard
在slave2上執行 bin/codis-config dashboard, 該命令會啟動 dashboard
初始化slots
在任一機器上執行 bin/codis-config slot init,該命令會在zookeeper上創建slot相關信息
啟動Codis Redis
和官方redis的參數一下,三臺機器上分別執行
[root@slave1]# /usr/local/codis/bin/codis-server /usr/local/codis/redis_conf/6379.conf & [root@slave1]# /usr/local/codis/bin/codis-server /usr/local/codis/redis_conf/6380.conf &
添加Redis Server Group
每一個 Server Group 作為一個 Redis 服務器組存在, 只允許有一個 master, 可以有多個 slave, group id 僅支持大于等于1的整數
$ bin/codis-config server -h usage: codis-config server list codis-config server add codis-config server remove codis-config server promote codis-config server add-group codis-config server remove-group
如: 添加三個 server group, 每個 group 有兩個 redis 實例,group的id分別為1、2和3, redis實例為一主一從。
添加一個group,group的id為1, 并添加一個redis master到該group
$ bin/codis-config server add 1 slave1:6379 master
添加一個redis slave到該group
$ bin/codis-config server add 1 slave1:6380 slave
類似的,再添加group,group的id為2
$ bin/codis-config server add 2 slave2:6379 master $ bin/codis-config server add 2 slave2:6380 slave
類似的,再添加group,group的id為3
$ bin/codis-config server add 3 slave3:6379 master $ bin/codis-config server add 3 slave3:6380 slave
設置 server group 服務的 slot 范圍
Codis 采用 Pre-sharding 的技術來實現數據的分片, 默認分成 1024 個 slots (0-1023), 對于每個key來說, 通過以下公式確定所屬的 Slot Id : SlotId = crc32(key) % 1024 每一個 slot 都會有一個且必須有一個特定的 server group id 來表示這個 slot 的數據由哪個 server group 來提供.
$ bin/codis-config slot -h usage: codis-config slot init codis-config slot info codis-config slot set codis-config slot range-set codis-config slot migrate [--delay=]
如:
設置編號為[0, 334]的 slot 由 server group 1 提供服務, 編號 [335, 669] 的 slot 由 server group 2 提供服務, 編號 [670, 1023] 的 slot 由 server group 3 提供服務,
$ bin/codis-config slot range-set 0 334 1 online $ bin/codis-config slot range-set 335 669 2 online $ bin/codis-config slot range-set 670 1023 3 online
啟動 codis-proxy
bin/codis-proxy -c config.ini -L ./log/proxy.log --cpu=8 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000
剛啟動的 codis-proxy 默認是處于 offline狀態的, 然后設置 proxy 為 online 狀態, 只有處于 online 狀態的 proxy 才會對外提供服務
bin/codis-config -c config.ini proxy online <---- proxy的id, 如 proxy_1
瀏覽器管理
訪問http://192.168.4.126:18087/admin ,現在可以在瀏覽器里面完成各種操作了。