Redis 在 3.0 的时候,推出了一个集群解决方案: redis-cluster. 能让咱们经过官方的方式构建咱们的redis集群, 但在以前, 咱们 生产环境跑的基本是redis 2.x 版本的实例, 若是须要进行集群管理, 以 惟品会 为例,不少同事采用的方案都是twitter的Twemproxy。html
Twemproxy 自己是一个静态的分布式方案,进行扩容、缩容的时候对咱们devops的要求很高,并且很可贵作到平滑的扩容、缩容。并且没有用于集群管理的 Dashboard,这样十分不便。前端
为了解决这些痛点,豌豆荚开源了它们的 Codis, 很感谢做者们带来了这么好的工具,不论是使用或者学习CAP相关开发,codis都是很好的参考。linux
Codis 官方项目地址 :https://github.com/CodisLabs/...git
codis架构github
上图是codis的一个整体架构,咱们能够获得几个关键组合件:codis-server、codis-proxy、codis-dashboard、codis-admin、 codis-fe、 cois-ha等。golang
理解了上面的组件含义,对咱们部署和维护codis颇有帮组,因此在搭建环境以前,务必先认真理解上面的组件的意义。redis
codis主要是采用golang开发,并且依赖zk或etcd进行配置管理的(做为一名gopher,我主要是采用etcd),因此最起码,咱们先要把go的环境先搭建起来。json
建议参考官方的指引:https://golang.org/doc/install后端
安装后,咱们检查是否成功:数据结构
$ go version go version go1.7 linux/amd64
添加 到PATH,例如:
export GOROOT=/usr/local/go export GOPATH=/home/apps/devgo export GOBIN=/home/apps/devgo/bin export PATH=$PATH:$GOROOT/bin
$ go get -u github.com/tools/godep
若是输入which godep 报命令command not found的错误,能够这样解决:
$ sudo cp $GOPATH/bin/godep /usr/local/bin/
$ mkdir -p $GOPATH/src/github.com/CodisLabs $ cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.0
项目检出成功后,咱们能够看到codis完整目录;因为codis的extern包含了一个redis的基础工具。因此,咱们须要在 codis的主目录下执行make命令
$ cd $GOPATH/src/github.com/CodisLabs/codis $ make make -j -C extern/redis-2.8.21/ ... ... go build -i -o bin/codis-dashboard ./cmd/dashboard go build -i -o bin/codis-proxy ./cmd/proxy go build -i -o bin/codis-admin ./cmd/admin go build -i -o bin/codis-ha ./cmd/ha go build -i -o bin/codis-fe ./cmd/fe
$ ls bin/ 总用量 74528 drwxrwxr-x 3 apps apps 4096 10月 2 09:52 . drwxrwxr-x 12 apps apps 4096 10月 2 09:51 .. drwxrwxr-x 4 apps apps 4096 10月 2 09:52 assets -rwxrwxr-x 1 apps apps 15710271 10月 2 09:52 codis-admin -rwxrwxr-x 1 apps apps 16978383 10月 2 09:52 codis-dashboard -rwxrwxr-x 1 apps apps 15810040 10月 2 09:52 codis-fe -rwxrwxr-x 1 apps apps 8730288 10月 2 09:52 codis-ha -rwxrwxr-x 1 apps apps 10267404 10月 2 09:52 codis-proxy -rwxrwxr-x 1 apps apps 4308197 10月 2 09:52 codis-server -rwxrwxr-x 1 apps apps 2166709 10月 2 09:52 redis-benchmark -rwxrwxr-x 1 apps apps 2314614 10月 2 09:52 redis-cli -rw-rw-r-- 1 apps apps 146 10月 2 09:52 version ... ... $ cat bin/version version = 2016-09-29 13:58:59 +0800 @29199bb81e7b0b3cdb4bd7e005c96c9fd674a6ea compile = 2016-10-02 09:52:18 +0800 by go version go1.7 linux/amd64
接下来,咱们须要处理一下 bin/ 下的命令工具
sudo cp codis-* /usr/local/bin sudo cp redis-* /usr/local/bin
因为我使用etcd做为配置管理,因此须要进行etcd的安装配置,但若是你喜欢zookeeper,那么能够忽略本点,本身安装zk.
$ curl -L https://github.com/coreos/etcd/releases/download/v2.3.7/etcd-v2.3.7-linux-amd64.tar.gz $ tar xzvf etcd-v2.3.7-linux-amd64.tar.gz $ cd etcd-v2.3.7-linux-amd64 && sudo cp etcd /usr/local/bin/ && cp etcdctl /usr/local/bin/
检查etcd安装是否成功
$ etcd --version etcd Version: 2.3.7 Git SHA: fd17c91 Go Version: go1.6.2 Go OS/Arch: linux/amd64
源码中提供了可供本地测试使用的脚本 scripts/demo.sh,该脚本会生成一个本地集群。
etcd.pid=22387 codis-server-16379.pid=22388 codis-server-16380.pid=22389 codis-server-16381.pid=22393 codis-server-16382.pid=22394 codis-server-16383.pid=22395 codis-server-16384.pid=22396 codis-server-16385.pid=22397 codis-server-16386.pid=22398 proxy-11080x19000.pid=22400 proxy-11081x19001.pid=22402 proxy-11082x19002.pid=22405 proxy-11083x19003.pid=22410 dashboard.pid=22412 fe.pid=22428 migrate slot-[ 512, 767] to group-3 migrate slot-[ 768,1023] to group-4 migrate slot-[ 0, 255] to group-1 migrate slot-[ 256, 511] to group-2 done done .... .... ....
脚本会输出每个进程的 PID,并将每一个实例的日志会输出到 scripts/tmp 目录下;
启动后,能够经过 http://127.0.0.1:8080 来访问 codis-fe。
小结
上述咱们已经能够快速跑一个demo版的codis,但这还没够的,咱们还须要进一步去学习codis的开发和运维。
上一节, 咱们经过运行了一个名称为“codis-test”的codis的demo方案,本节,咱们尝试经过官方的相关命令组件,运行本身的自定义环境。
仍是跟上一节同样,我这边继续选用etcd做为个人配置管理工具。
注意:请按照顺序逐步完成操做。
咱们使用etcd的默认端口 2379
nohup etcd --name=codis-demo &>/home/apps/codis/log/etcd/etcd.log &
若是启动过程当中,出现被锁的异常,能够清理相关历史记录:
etcdctl rm --recursive /codis3
咱们先启动一个单节点的server codis-server 自己就是一个redis实例, 咱们开启一个端口为16379的server实例
nohup ./bin/codis-server --port 16379 &>/home/apps/codis/log/redis/redis-16379.log & [44886] 02 Oct 16:50:11.381 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. [44886] 02 Oct 16:50:11.381 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted. [44886] 02 Oct 16:50:11.381 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.21 (29199bb8/0) 64 bit .-`` .-```. ```/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 16379 | `-._ `._ / _.-' | PID: 44886 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [44886] 02 Oct 16:50:11.385 # Server started, Redis version 2.8.21 [44886] 02 Oct 16:50:11.385 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [44886] 02 Oct 16:50:11.385 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. [44886] 02 Oct 16:50:11.385 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. [44886] 02 Oct 16:50:11.385 * DB loaded from disk: 0.000 seconds [44886] 02 Oct 16:50:11.385 * The server is now ready to accept connections on port 16379
nohup ./bin/codis-dashboard --config=/home/apps/codis/dashboard.toml --log=/home/apps/codis/log/dashboard/dashboard.log --log-level=WARN &
这里咱们使用了自定义的dashboard.toml, 它的内容以下:
################################################## # # # Codis-Dashboard # # # ################################################## # Set Coordinator, only accept "zookeeper" & "etcd" coordinator_name = "etcd" coordinator_addr = "127.0.0.1:2379" # Set Codis Product {Name/Auth}. product_name = "codis-demo" product_auth = "" # Set bind address for admin(rpc), tcp only. admin_addr = "0.0.0.0:18080"
nohup ./bin/codis-proxy --config=/home/apps/codis/proxy.toml --log=/home/apps/codis/log/proxy/proxy.log --log-level=WARN &
proxy.toml的内容以下:
product_name = "codis-demo" product_auth = "" proto_type = "tcp4" admin_addr = "0.0.0.0:11080" proxy_addr = "0.0.0.0:19000"
nohup ./bin/codis-fe -d /home/apps/codis/codis.json --listen=0.0.0.0:8080 &
condis.json的内容以下:
[ { "name": "codis-demo", "dashboard": "127.0.0.1:18080" } ]
上面咱们已经启动了基本的组件,接下来须要咱们经过codis-admin把咱们的proxy进行分组管理。
增长proxy组
./bin/codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 1
添加分组
./bin/codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 1 -x 127.0.0.1:16379
建立proxy
./bin/codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11080
配置slot
./bin/codis-admin --dashboard=127.0.0.1:18080 --slot-action --interval=100 ./bin/codis-admin --dashboard=127.0.0.1:18080 --rebalance --confirm
登录管理面板,以下图:
做者:domac的菜园子
原文:http://lihaoquan.me/2016/10/2...