Codis介绍
Codis是一个分布式Redis解决方案,对于上层的应用来说,连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别(有一些命令不支持),上层应用可以像使用单机的Redis一样使用,Codis底层会处理请求转发,不停机的数据迁移等工作,对于客户端是透明的。
Codis架构
Codis引入Group的概念,每个Group包括1个Redis Master及至少一个Redis Slave。如当前Master有问题,运维人员可以通过Dashboard切换到Slave。
支持热数据迁移(Auto Rebalance),修改了Redis Server 源码,称之为Codis Server。
Codis 采用预先分片(Pre-Sharding)机制,事先规定好了,分成1024个slots(最多支持后端1024个Codis Server),这些路由保存在zookeeper中。
Zookeeper还维护Codis Server Group信息,并提供分布式锁等服务。
Codis搭建
主机资源
192.168.10.2
192.168.10.3
192.168.10.4
Zookeeper
192.168.10.2
192.168.10.3
192.168.10.4
Zookeeper安装
/etc/hosts
###zookeeper for codis
192.168.10.2 zookeeper-node1
192.168.10.3 zookeeper-node2
192.168.10.4 zookeeper-node3
Zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data/
clientPort=2181
maxClientCnxns=50
server.1=zookeeper-node1:2888:3888 #2888为程序监控端口,3888选举通信端口
server.2=zookeeper-node2:2888:3888
server.3=zookeeper-node3:2888:3888
Mkdir zookeeper/data/
Echo “1” > zookeeper/data/myid #生成ID,myid对用zoo.cfg中的server.ID
#启动
#/usr/local/zookeeper/bin/zkServer.sh start
Go语言安装
Codis使用最新版本,支持新功能,查看
https://github.com/CodisLabs/codis/blob/release3.2/doc/tutorial_zh.md
需要go版本1.7.3
下载地址:https://studygolang.com/dl/golang/go1.7.3.linux-amd64.tar.gz
#tar -zxf go1.7.3.linux-amd64.tar.gz -C /usr/local/
#vi /etc/profile
#####go path for codis
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
#source /etc/profile
# go version
go version go1.7.3 linux/amd64
Codis安装
Codis使用最新3.2版本
下载目录为 $GOPATH/src/github.com/CodisLabs/codis
# mkdir -p /usr/local/go/src/github.com/CodisLabs
#cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.2
#cd codis
#make ##直接通过make编译
# ls bin/
assets codis-admin codis-dashboard codis-fe codis-ha codis-proxy codis-server redis-benchmark redis-cli redis-sentinel version
#####bin内生成可执行文件,bin/assets文件夹是codis-dashboard http服务需要的前端资源,需要和codis-dashboard放置在同一文件夹下
###创建codis目录
#mkdir /usr/local/codis
#mkdir -p /usr/local/codis/{logs,conf,scripts,db,run}
#mkdir -p /usr/local/codis/db/{redis_data_6380,redis_data_6381}
###copy codis bin目录
#cp -rf /usr/local/go/src/github.com/CodisLabs/codis/bin/ /usr/local/codis/
###codis本身只有codis-server,没有redis-cli,需要把redis-3.2.11安装包中的redis-cli copy 到/usr/local/codis/bin/下面(此步骤如果有redis-cli可以省略)
#cd /usr/local/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11/src/
#cp redis-cli /usr/local/codis/bin/
Codis redis配置
#cd /usr/local/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11
#cp redis.conf /usr/local/codis/conf/redis6380.conf
####Redis.conf 关闭了rdb aof
daemonize yes
pidfile /usr/local/codis/run/redis-6380.pid
port 6380
tcp-backlog 511
timeout 86400
tcp-keepalive 60
loglevel notice
logfile "/usr/local/codis/logs/redis-6380.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6380.rdb
dir /usr/local/codis/db/redis_data_6380
masterauth "123456"
slave-serve-stale-data yes
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"
maxmemory 10gb
maxmemory-policy volatile-lru
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
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
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 0 0 0
hz 10
aof-rewrite-incremental-fsync yes
#启动redis
/usr/local/codis/bin/codis-server /usr/local/codis/conf/redis-6380.conf &
/usr/local/codis/bin/codis-server /usr/local/codis/conf/redis-6381.conf &
Codis dashboard
Codis dashboard仅在需要启动管理截面的codis 的服务器上操作
Codis 3.0的dashboard,作为集群管理工具,支持Codis-proxy codis-server的添加、删除以及数据迁移等操作。在集群状态发生改变时,codis-dashboard维护集群下所有codis-proxy的状态一致性。
对于同一业务集群,同一时刻codis-dashboard只能有0或者1个。
所有对集群的修改都必须通过codis-dashboard完成。
####################################
生成配置文件
/usr/local/codis/bin/codis-dashboard --default-config | tee /usr/local/codis/conf/dashboard.conf
##Dashboard.conf
coordinator_name = "zookeeper"
coordinator_addr = "192.168.10.2:2181,192.168.10.3:2181,192.168.10.4:2181"
product_name = "codis-demo"
product_auth = "123456"
admin_addr = "0.0.0.0:18080"
migration_method = "semi-async"
migration_parallel_slots = 100
migration_async_maxbulks = 200
migration_async_maxbytes = "32mb"
migration_async_numkeys = 500
migration_timeout = "30s"
sentinel_client_timeout = "10s"
sentinel_quorum = 2
sentinel_parallel_syncs = 1
sentinel_down_after = "30s"
sentinel_failover_timeout = "5m"
sentinel_notification_script = ""
sentinel_client_reconfig_script = "
coordinator_name 外部存储类型,接受zookeeper/etcd
coordinator_addr 外部存储地址
product_name 集群名称,满足正则\w[\w\.\-]*
product_auth 集群密码,默认为空
admin_addr RESTful API端口
###########
启动 codis-dashboard
#nohup /usr/local/codis/bin/codis-dashboard --ncpu=4 --config=/usr/local/codis/conf/dashboard.conf --log=/usr/local/codis/logs/dashboard.log --log-level=WARN &
--说明:
--ncpu=N 最大使用cpu个数
-c CONF,--conf=CONF 指定启动配置文件
-l FILE , --log=FILE 设置log输出文件
--log-level=LEVEL 设置log输出等级:INFO,WARN,DEBUG,ERROR;默认INFO,推荐WARM
正常关闭dashboard bin/codis-admin --dashboard=*.*.*.*:18080 --shutdown
Codis proxy
生成proxy配置文件
# /usr/local/codis/bin/codis-proxy --default-config | tee /usr/local/codis/conf/proxy.conf
# grep -vE '^$|^#' conf/proxy.conf
product_name = "codis-demo"
product_auth = "123456"
session_auth = ""
admin_addr = "0.0.0.0:11080"
proto_type = "tcp4"
proxy_addr = "0.0.0.0:19000"
jodis_name = "zookeeper"
jodis_addr = "zookeeper-ip"
jodis_auth = ""
jodis_timeout = "20s"
jodis_compatible = false
proxy_datacenter = ""
proxy_max_clients = 1000
proxy_max_offheap_size = "1024mb"
proxy_heap_placeholder = "256mb"
backend_ping_period = "5s"
backend_recv_bufsize = "128kb"
backend_recv_timeout = "30s"
backend_send_bufsize = "128kb"
backend_send_timeout = "30s"
backend_max_pipeline = 20480
backend_primary_only = false
backend_primary_parallel = 1
backend_replica_parallel = 1
backend_keepalive_period = "75s"
backend_number_databases = 16
session_recv_bufsize = "128kb"
session_recv_timeout = "30m"
session_send_bufsize = "64kb"
session_send_timeout = "30s"
session_max_pipeline = 10000
session_keepalive_period = "75s"
session_break_on_failure = false
metrics_report_server = ""
metrics_report_period = "1s"
metrics_report_influxdb_server = ""
metrics_report_influxdb_period = "1s"
metrics_report_influxdb_username = ""
metrics_report_influxdb_password = ""
metrics_report_influxdb_database = ""
metrics_report_statsd_server = ""
metrics_report_statsd_period = "1s"
metrics_report_statsd_prefix = ""
--说明:
product_name 集群名称,参考dashboard参数说明
product_auth 集群密码,默认为空,此处密码为访问dashboard codis proxy codis server
admin_addr RESTful API 端口
proto_type redis端口类型,接受tcp/tcp4/tcp6/unix/unixpacket
proxy_addr redis端口地址或者路径
jodis_addr 注册zookeeper地址
jodis_timeout 注册session timeouot时间,单位second
backend_ping_period codis-server探活周期,单位second,0表示禁止
Session_max_timeout client 连接最大读超时,单位second,0表示禁止
Session_max_bufsize client联系读写缓冲区大小,单位Byte
session_max_pipeline client连接最大popeline大小
session_keepalive_period client的tcp keepalive 周期,仅tcp有效,0表示禁止
启动
#nohup /usr/local/codis/bin/codis-proxy --ncpu=4 --config=/usr/local/codis/conf/proxy.conf --log=/usr/local/codis/logs/proxy.log --log-level=WARN &
Codis-proxy启动后,处于waiting状态,监控proxy_add地址,但是不会accept连续,添加到集群并完成集群状态的同步,才能改变状态为online.
添加proxy的方法
1、通过codis-fe添加:通过add proxy按钮,将admin_addr加入到集群
2、通过codis-admin命令行添加
#/usr/local/codis/bin/codis-admin --dashboard=*.*.*.*:18080 --create-proxy -x *.*.*.*:11080
18080为dashboard的admin_addr地址
11080为proxy的admin_addr地址
添加过程,dashboard会完成如下一系列动作:
@获取proxy信息,对集群name以及auth进行验证,并将其信息写入到外部存储中;
@同步slots状态
@标记proxy状态为online,此后proxy开始accept连接并开始提供服务
关闭proxy
#/usr/local/codis/bin/codis-admin --proxy=*.*.*.*:11080 --auth=”” --shutdown
Codis-fe
配置启动codis FE集群管理界面(仅需要启动dashiboard的codis服务器上操作)
生成配置文件
#/usr/local/codis/bin/codis-admin --dashboard-list --zookeeper=192.168.10.3 | tee /usr/local/codis/conf/codis.json
注意:此处生成的dashboard配置为获取的主机名,写成IP方式。
启动codis-fe
nohup /usr/local/codis/bin/codis-fe --ncpu=2 --log=/usr/local/codis/logs/fe.log --log-level=WARN --dashboard-list=/usr/local/codis/conf/codis.json --listen=192.168.10.2:18090 &
添加proxy
添加group
添加server
每个group包含一主、多从server
Slots
按照group进行slots
------------------参考
http://navyaijm.blog.51cto.com/4647068/1637688
http://blog.csdn.net/shmiluwei/article/details/51958359
https://www.cnblogs.com/xmzncc/p/6218694.html
https://github.com/CodisLabs/codis/blob/release3.2/doc/tutorial_zh.md
http://www.cnblogs.com/reblue520/p/6874925.html
Sentinels
Sentinel.conf
protected-mode no
port 26379
logfile "/usr/local/codis/logs/sentinel.log"
dir “/tmp”
/usr/local/codis/bin/redis-sentinel /usr/local/codis/conf/sentinel.conf &
---参考
http://www.bubuko.com/infodetail-2316020.html