Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有显著区别 (不支持的命令列表), 上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务
在这里我是网上下载的go1.8.linux-amd64.tar.gz压缩包
[root@server11 ~]# tar zxf go1.8.linux-amd64.tar.gz -C /usr/local/
[root@server11 ~]# vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
export GOPATH=/home/user/go
[root@server11 ~]# source /etc/profile
[root@server11 ~]# go version
go version go1.8 linux/amd64
写一个简单的go测试程序
[root@server11 ~]# vim hello.go
package main
import "fmt"
func main(){
fmt.Print("hello!!!\n")
}
[root@server11 ~]# go run hello.go
hello!!!
Codis采用源码安装,使用的安装包为codis-release3.2.zip
[root@server11 ~]# mkdir -p $GOPATH/src/github.com/CodisLabs
[root@server11 ~]# unzip codis-release3.2.zip
[root@server11 ~]# mv codis-release3.2 /home/user/go/src/github.com/CodisLabs/codis
[root@server11 ~]# cd /home/user/go/src/github.com/CodisLabs/codis
[root@server11 codis]# ls
admin config Dockerfile Godeps pkg vendor
ansible deploy example Makefile README.md version
cmd doc extern MIT-LICENSE.txt scripts wandoujia_license.txt
在编译之前还需要安装相应的包用来解决依赖关系
[root@server11 codis]# yum install -y gcc-c++ git autoconf
[root@server11 codis]# make
make -j4 -C extern/redis-3.2.8/
cache-oblivious : 1
=========================================
go build -i -o bin/codis-dashboard ./cmd/dashboard
go build -i -tags "cgo_jemalloc" -o bin/codis-proxy ./cmd/proxy
go build -i -o bin/codis-admin ./cmd/admin
go build -i -o bin/codis-fe ./cmd/fe
[root@server11 codis]# ./admin/codis-dashboard-admin.sh start
[root@server11 codis]# tail -100 ./log/codis-dashboard.log.2017-10-19
[root@server11 codis]# ./admin/codis-server-admin.sh start
[root@server11 codis]# tail -100 /tmp/redis_6379.log.2017-10-19
[root@server11 codis]# ./admin/codis-proxy-admin.sh start
[root@server11 codis]# tail -100 ./log/codis-proxy.log.2017-10-19
[root@server11 codis]# ./admin/codis-fe-admin.sh start
[root@server11 codis]# tail -100 ./log/codis-fe.log.2017-10-19
查看一下日志中是否有报错信息,注意,这里日志的日期是自动补齐的
通过web浏览器访问集群管理页面(fe地址:127.0.0.1:9090) 选择我们刚搭建的集群 codis-demo
在 Proxy 栏可看到我们已经启动的 Proxy, 但是 Group 栏为空,因为我们启动的 codis-server 并未加入到集群 添加 NEW GROUP,NEW GROUP 行输入 1,再点击 NEW GROUP 即可 添加 Codis Server,Add Server 行输入我们刚刚启动的 codis-server 地址,添加到我们刚新建的 Group,然后再点击 Add Server 按钮即可
新增的集群 slot 状态是 offline,因此我们需要对它进行初始化(将 1024 个 slot 分配到各个 group),而初始化最快的方法可通过 fe 提供的 rebalance all slots 按钮来做
首先安装开发工具及openjdk,zookeeper是由Java语言开发的,所以需要openjdk环境。
[root@server11 ~]# yum groupinstall "Development tools" "Compatibility libraries" -y
[root@server11 ~]# yum install openssl-devel openssl -y
[root@server11 ~]# yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk -y
确定Java运行环境正常
[root@server11 ~] java -version
openjdk version "1.8.0_101"
OpenJDK Runtime Environment (build 1.8.0_101-b13)
OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)
开始装zookeeper
[root@server11 ~]# tar zxf zookeeper-3.4.6.tar.gz
[root@server11 ~]# ln -s /usr/local/zookeeper-3.4.6/ /usr/local/zookeeper
[root@server11 ~]# cd /usr/local/zookeeper/conf
[root@server11 ~]# cp zoo_sample.cfg zoo.cfg
编译zookeeper配置文件/usr/local/zookeeper/conf/zoo.cfg
maxClientCnxns=60
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/db
dataLogDir=/data/zookeeper/log
clientPort=2181
# cluster configure
server.1=172.25.30.11:2888:3888
server.2=172.25.30.12:2888:3888
server.3=172.25.30.13:2888:3888
[root@server11 ~]# mkdir /data/zookeeper/{db,log} -p
下面需要生成ID,这里需要注意,myid对应的zoo.cfg的server.ID,比如第二台zookeeper主机对应的myid应该是2,以此类推,三个主机分别为:
[root@server11 ~]# echo 1 > /data/zookeeper/db/myid
[root@server12 ~]# echo 2 > /data/zookeeper/db/myid
[root@server13 ~]# echo 3 > /data/zookeeper/db/myid
输出环境变量及启动zookeeper
Vim /etc/profile
Source /etc/profile
[root@server11 ~] zkServer.sh start
查看状态
[root@server11 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@server12 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@server13 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
客户端连接,可以查看相关信息
[root@server13 conf]# zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2017-10-19 17:01:29,598 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2017-10-19 17:01:29,605 [myid:] - INFO [main:Environment@100] - Client environment:host.name=
2017-10-19 17:01:29,605 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_65
......
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
主库为6379,从库为6380
[root@server11 conf]# mkdir -p /data/codis/redis/redis-6379
[root@server11 conf]# mkdir -p /data/codis/redis/redis-6380
配置主库的组件
主库:/data/codis/redis/redis-6379/redis.conf
daemonize yes
pidfile /data/codis/run/redis-6379.pid
port 6379
tcp-backlog 65535
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/codis/log/redis-6379.log"
databases 16
lua-time-limit 5000
maxclients 10000
slowlog-log-slower-than 10000
slowlog-max-len 128
maxmemory 3G
maxmemory-policy noeviction
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
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
latency-monitor-threshold 0
从库:/data/codis/redis/redis-6380/redis.conf
pidfile /data/codis/run/redis-6380.pid
port 6380
tcp-backlog 65535
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/codis/log/redis-6380.log"
databases 16
lua-time-limit 5000
maxclients 10000
slowlog-log-slower-than 10000
slowlog-max-len 128
maxmemory 3G
maxmemory-policy noeviction
no-appendfsync-on-rewrite yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync no
auto-aof-rewrite-min-size 512mb
auto-aof-rewrite-percentage 100
aof-load-truncated yes
aof-rewrite-incremental-fsync 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
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
latency-monitor-threshold 0
使用codis-server启动redis
[root@server11 ~]# codis-server /data/codis/redis/redis-6379/redis.conf
[root@server11 ~]# codis-server /data/codis/redis/redis-6380/redis.conf
首先生成默认的配置文件:
codis-dashboard --default-config | tee /data/codis/conf/dashboard.toml
启动
nohup codis-dashboard --ncpu=2 --config=/data/codis/conf/dashboard.toml --log=/data/codis/log/dashboard.log --log-level=WARN &
生成配置文件
[root@server11 ~]# codis-proxy --default-config | tee /data/codis/conf/proxy.toml
启动
nohupcodis-proxy--ncpu=2--config=/data/codis/conf/proxy.toml--log=/data/codis/log/proxy.log --log-level=WARN &
codis-admin --dashboard=172.25.30.11:18080 --create-proxy -x 172.25.30.11:11080
codis-admin --dashboard=172.25.30.12:18080 --create-proxy -x 172.25.30.12:11080
codis-admin --dashboard=172.25.30.13:18080 --create-proxy -x 172.25.30.12:11080