`

codis集群部署实战(redis集群代理)

阅读更多

二、架构

wKiom1U4wNbil8RQAAIeNjYHmcE992.jpg

三、角色分批

1
2
3
4
5
6
7
8
9
10
11
12
13
zookeeper集群:
10.10.0.47
10.10.0.48
10.10.1.76
codis-config、codis-ha:
10.10.32.10:18087
codis-proxy:
10.10.32.10:19000
10.10.32.49:19000
codis-server:
10.10.32.42:6379、10.10.32.43:6380(主、从)
10.10.32.43:6379、10.10.32.44:6380(主、从)
10.10.32.44:6379、10.10.32.42:6380(主、从)

四、部署

1、安装zookeeper

1
yum -y install zookeeper jdk  ##安装服务
1
2
3
4
vim /etc/hosts  ##添加host
10.10.0.47 ZooKeeper-node1
10.10.0.48 ZooKeeper-node2
10.10.1.76 ZooKeeper-node3
1
2
3
4
5
6
7
8
9
10
vim /etc/zookeeper/conf/zoo.cfg ##撰写zk的配置文件
maxClientCnxns=50
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/
clientPort=2181
server.1=ZooKeeper-node1:2888:3888
server.2=ZooKeeper-node2:2888:3888
server.3=ZooKeeper-node3:2888:3888
1
2
3
mkdir /data/zookeeper/ ##创建zk的datadir目录
echo "2" >/data/zookeeper/myid  ##生成ID,这里需要注意, myid对应的zoo.cfg的server.ID,比如ZooKeeper-node2对应的myid应该是2
/usr/lib/zookeeper/bin/zkServer.sh start  ## 服务启动

2、go安装(codis是go语言写的,所以那些机器需要安装你懂得)

1
2
3
4
5
6
7
8
9
10
11
12
wget https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz
tar -zxvf go1.4.1.linux-amd64.tar.gz
mv go /usr/local/
cd /usr/local/go/src/
bash all.bash
cat >> ~/.bashrc << _bashrc_export
export GOROOT=/usr/local/go
export PATH=\$PATH:\$GOROOT/bin
export GOARCH=amd64
export GOOS=linux
_bashrc_export
source ~/.bashrc

3、下载并编译codis(codis-config、codis-proxy、codis-server所在的机器)

1
2
3
4
5
6
mkdir /data/go
export GOPATH=/data/go
/usr/local/go/bin/go get github.com/wandoulabs/codis
cd  /data/go/src/github.com/wandoulabs/codis/
./bootstrap.sh
make gotest

五、服务启动及初始化集群

1、启动 dashboard(codis-config上操作)

1
2
3
4
5
6
7
cat /etc/codis/config_10.ini ##撰写配置文件
zk=10.10.0.47:2181,10.10.0.48:2181,10.10.1.76:2181
product=zh_news
proxy_id=codis-proxy_10
net_timeout=5000
proto=tcp4
dashboard_addr=10.10.32.10:18087
1
cd /data/go/src/github.com/wandoulabs/codis/ &&  ./bin/codis-config -c /etc/codis/config_10.ini  dashboard &

2、初始化 slots (codis-config上操作)

1
cd /data/go/src/github.com/wandoulabs/codis/ &&  ./bin/codis-config -c /etc/codis/config_10.ini slot init

3、启动 Codis Redis , 和官方的Redis Server参数一样(codis-server上操作)

1
cd /data/go/src/github.com/wandoulabs/codis/ && ./bin/codis-server /etc/redis_6379.conf &

4、添加 Redis Server Group , 每一个 Server Group 作为一个 Redis 服务器组存在, 只允许有一个 master, 可以有多个 slave, group id 仅支持大于等于1的整数(codis-config上操作)

1
2
3
4
5
6
7
cd /data/go/src/github.com/wandoulabs/codis/
./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.10.32.42:6379 master
./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.10.32.43:6380 slave
./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.10.32.43:6379 master
./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.10.32.44:6380 slave
./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.10.32.44:6379 master
./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.10.32.42:6380 slave

5、设置 server group 服务的 slot 范围 Codis 采用 Pre-sharding 的技术来实现数据的分片, 默认分成 1024 个 slots (0-1023), 对于每个key来说, 通过以下公式确定所属的 Slot Id : SlotId = crc32(key) % 1024 每一个 slot 都会有一个特定的 server group id 来表示这个 slot 的数据由哪个 server group 来提供.(codis-config上操作)

1
2
3
4
cd /data/go/src/github.com/wandoulabs/codis/
./bin/codis-config -c /etc/codis/config_10.ini slot range-set 0 300 1 online
./bin/codis-config -c /etc/codis/config_10.ini slot range-set 301 700 2 online
./bin/codis-config -c /etc/codis/config_10.ini slot range-set 701 1023 3 online

6、启动 codis-proxy  (codis-proxy上操作)

1
2
3
4
5
6
7
cat /etc/codis/config_10.ini ##撰写配置文件
zk=10.10.0.47:2181,10.10.0.48:2181,10.10.1.76:2181
product=zh_news
proxy_id=codis-proxy_10  ##10.10.32.49上改成codis-proxy_49,多个proxy,proxy_id 需要唯一
net_timeout=5000
proto=tcp4
dashboard_addr=10.10.32.10:18087
1
2
cd /data/go/src/github.com/wandoulabs/codis/ &&  ./bin/codis-proxy  -c /etc/codis/config_10.ini -L /data/log/codis-proxy_10.log  --cpu=4 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000 &
cd /data/go/src/github.com/wandoulabs/codis/ &&  ./bin/codis-proxy  -c /etc/codis/config_49.ini -L /data/log/codis-proxy_49.log  --cpu=4 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000 &

OK,整个集群已经搭建成功了,截图给你们show show

wKiom1U4woPj8_kiAAIq1SRxwxo029.jpg

六、codis-server的HA

codis-ha实现codis-server的主从切换,codis-server主库挂了会提升一个从库为主库,从库挂了会设置这个从库从集群下线

1、安装

1
2
3
4
5
6
7
export GOPATH=/data/go
/usr/local/go/bin/go get github.com/ngaut/codis-ha
cd  /data/go/src/github.com/ngaut/codis-ha
go build
cp codis-ha /data/go/src/github.com/wandoulabs/codis/bin/
使用方法:
codis-ha --codis-config=dashboard地址:18087 --productName=集群项目名称

2、使用supervisord管理codis-ha进程

1
yum -y install supervisord
1
2
3
4
5
6
7
8
9
10
11
/etc/supervisord.conf中添加如下内容:
[program:codis-ha]
autorestart = True
stopwaitsecs = 10
startsecs = 1
stopsignal = QUIT
command /data/go/src/github.com/wandoulabs/codis/bin/codis-ha --codis-config=10.10.32.17:18087 --productName=dh_tianqi
user = root
startretries = 3
autostart = True
exitcodes = 0,2

3、启动supervisord服务

1
2
/etc/init.d/supervisord start
chkconfig supervisord  on

此时,ps -ef |grep codis-ha 你回发现codis-ha进程已经启动,这个时候你去停掉一个codis-server的master,看看slave会不会提升为master呢

 

七、关于监控

关于整个codis集群的监控,我们这边用的是zabbix,监控的指标比较简单,所以这块大家有什么好的建议多给我提提哈

zookeeper:监控各个节点的端口联通性(以后想着把进程也监控上)

codis-proxy:监控了端口的联通性,这个监控远远不够呀

codis-server:监控了内存使用率、连接数、联通性

codis-ha:监控进程

dashboard:监控端口联通性

 

八、使用过程中遇到的问题

1、codis-proxy的日志切割,codis-proxy的默认日志级别是info,日志量很大,我们这边每天产生50多G日志,目前codis-proxy还不支持热重启,想修改启动参数还是比较麻烦的,日志切割推荐用logrotate

2、codis-proxy的监听地址默认没有具体ipv4,也就是codis-proxy启动之后没有0.0.0.0:19000这样的监听,这样会导致的问题就是前端lvs没有办法负载均衡codis-proxy,不能转发请求过,这个问题已联系作者处理了,在codis-proxy启动的配置文件中加上proto=tcp4这个参数就支持监听ipv4了

3、添加 Redis Server Group的时候,非codis-server(原生的redis)竟然也能加入到codis集群里面,在redis和codis-server共存在一个物理机上的清楚,很容易加错,希望能有个验证,非codis-server不能加入到codis集群

4、codis集群内部通讯是通过主机名的,如果主机名没有做域名解析那dashboard是通过主机名访问不到proxy的http-addr地址的,这会导致从web界面上看不到 OP/s的数据,至于还有没有其他问题,目前我这边还没有发现,建议内部通讯直接用内网IP

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics