codis介绍
Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说,
连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (有一些命令不支持),
上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作,
所有后边的一切事情, 对于前面的客户端来说是透明的,
可以简单的认为后边连接的是一个内存无限大的 Redis 服务,当然,
前段时间redis官方的3.0出了稳定版,3.0支持集群功能,codis的实现原理和3.0的集群功能差不多
,我了解的现在美团、阿里已经用了3.0的集群功能了,我们这边的业务主要是php,
3.0集群的sdk目前貌似还没有支持php语言的,
大家谁的php应用上了3.0集群,请联系我,我去取经,
有关redis常见的集群技术,请移步到 @萧田国 萧老师的infoq专栏Redis集群技术及Codis实践
codis架构
10.105.67.119 ZooKeeper-node1
10.105.78.46 ZooKeeper-node2
10.237.236.73 ZooKeeper-node3
10.105.78.57 codis-config codis-proxy
10.105.33.226 codis-proxy
10.237.164.111 codis server
6379 6380 group1
6381 6382 group2
6383 6384 group3
zookeeper机器部署
ZooKeeper-node1:
vim /data/zookeeper/zookeeper-3.4.6/conf/zoo.cfg ##撰写zk的配置文件
maxClientCnxns=50 #这里我在实际测试的时候50 zookeeper会卡住,所以我调成了500
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
mkdir /data/zookeeper/ ##创建zk的datadir目录
echo "1" >/data/zookeeper/myid ##生成ID,这里需要注意, myid对应的zoo.cfg的server.ID,比如ZooKeeper-node1对应的myid应该是1
/data/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start ## 服务启动
ZooKeeper-node2:
vim /data/zookeeper/zookeeper-3.4.6/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
mkdir /data/zookeeper/
echo "2" >/data/zookeeper/myid
/data/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start
ZooKeeper-node3:
vim /data/zookeeper/zookeeper-3.4.6/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
mkdir /data/zookeeper/
echo "3" >/data/zookeeper/myid
/data/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start
codis-config codis-proxy codis-server所在机器部署
wget https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz --no-check-certificate
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
mkdir /data/go
export GOPATH=/data/go
yum -y install git
/usr/local/go/bin/go get github.com/wandoulabs/codis 这个会报错安装git 然后还会报错不用管
cd /data/go/src/github.com/wandoulabs/codis/
rm -rf *
wget -c https://github.com/CodisLabs/codis/archive/release1.9.zip
unzip release1.9.zip
mv codis-release1.9/* .
vim bootstrap.sh 加入
go get -u github.com/xiam/resp
然后运行
sh -x bootstrap.sh
make gotest
===========================以上步骤需要在codis所有节点都运行===================================
启动 dashboard(codis-config上操作)
cat /etc/codis/config_10.ini ##撰写配置文件
zk=ZooKeeper-node1:2181,ZooKeeper-node2:2181,ZooKeeper-node3:2181
product=zh_news
proxy_id=codis-proxy_10
net_timeout=5000
proto=tcp4
dashboard_addr=10.105.78.57:18087
coordinator=zookeeper
cd /data/go/src/github.com/wandoulabs/codis/ && ./bin/codis-config -c /etc/codis/config_10.ini dashboard &
配置codis-proxy集群10.105.78.57 操作
mkdir -p /etc/codis/
vim /etc/codis/config_10.ini
zk=10.105.67.119:2181,10.105.78.46:2181,10.237.236.73:2181
product=zh_news
proxy_id=codis-proxy_10
net_timeout=5000
proto=tcp4
dashboard_addr=10.105.78.57:18087
coordinator=zookeeper
初始化 slots (codis-config上操作)
cd /data/go/src/github.com/wandoulabs/codis/ && ./bin/codis-config -c /etc/codis/config_10.ini slot init
配置codis-proxy集群10.105.33.226 操作
mkdir -p /etc/codis/
vim /etc/codis/config_49.ini
zk=10.105.67.119:2181,10.105.78.46:2181,10.237.236.73:2181
product=zh_news
proxy_id=codis-proxy_49
net_timeout=5000
proto=tcp4
dashboard_addr=10.105.78.57:18087
coordinator=zookeeper
启动 Codis Redis Server
cd /data/go/src/github.com/wandoulabs/codis/
./bin/codis-server /data/codis_server/conf/6379.conf
./bin/codis-server /data/codis_server/conf/6380.conf
./bin/codis-server /data/codis_server/conf/6381.conf
./bin/codis-server /data/codis_server/conf/6382.conf
./bin/codis-server /data/codis_server/conf/6383.conf
./bin/codis-server /data/codis_server/conf/6384.conf
6379.conf
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile "/data/codis_server/logs/redis_6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename 6379.rdb
dir /data/codis_server/data
slave-serve-stale-data yes
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 10000
maxmemory 3gb
maxmemory-policy allkeys-lru
appendonly yes
appendfilename "6379_appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
在codis config机器中添加 Redis Server Group
./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.237.164.111:6379 master
./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.237.164.111:6380 slave
./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.237.164.111:6381 master
./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.237.164.111:6382 slave
./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.237.164.111:6383 master
./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.237.164.111:6384 slave
设置 server group 服务的 slot (codis config操作)
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
设置 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上操作)
启动 codis-proxy (codis-proxy上操作)
10.105.78.57操作:
cd /data/go/src/github.com/wandoulabs/codis/
./bin/codis-proxy -c /etc/codis/config_10.ini -L /data/logs/codis-proxy_10.log --cpu=4 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000 &
10.105.33.226操作
cd /data/go/src/github.com/wandoulabs/codis/
./bin/codis-proxy -c /etc/codis/config_49.ini -L /data/logs/codis-proxy_49.log --cpu=4 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000 &
codis-server的HA
codis-ha实现codis-server的主从切换,
codis-server主库挂了会提升一个从库为主库,从库挂了会设置这个从库从集群下线
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/
使用方法:
/data/go/src/github.com/wandoulabs/codis/bin/codis-ha --codis-config=10.105.78.57:18087 --productName=zh_news &
使用supervisord管理codis-ha进程
yum install python-setuptools
easy_install supervisor
或者yum -y install supervisord
/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.105.78.57:18087 --productName=zh_news
user = root
startretries = 3
autostart = True
exitcodes = 0,2
[supervisord]
/etc/init.d/supervisord start
chkconfig supervisord on
或者使用命令:
cd /root
cp /etc/supervisord.conf .
supervisord -c /etc/supervisord.conf