Winse Blog

走走停停都是风景, 熙熙攘攘都向最好, 忙忙碌碌都为明朝, 何畏之.

Codis使用进阶

去年年中的时刻有安装过codis。当时因为任务紧就使用jedis的ShardedJedisPool功能粗略的解决,由于是自己手动路由和管理,维护起来太难,特别是当初设置的实例数不够用时,相当麻烦。

年初项目各种测试,于是有些闲暇的时间,重新弄一弄redis cluster。算是搭建一个环境来测试:

版本:

  • codis-3.2
  • centos6

测试环境编译安装

现在的版本已经有了全部的依赖,直接编译即可。(centos6和官网提供的编译版本不兼容)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
tar zxvf go1.6.2.linux-amd64.tar.gz 

/etc/profile
export GOROOT=/opt/go
export GOPATH=/opt/gopath
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

-

[root@cu2 CodisLabs]# pwd
/opt/go/src/github.com/CodisLabs

# @2017-06-05
# 如果下载的是tar.gz,直接在CodisLabs目录下解压,然后做个软链接
# cd $GOPATH ; mkdir -p src/github.com/CodisLabs/
# cd src/github.com/CodisLabs/; ln -s codis-3.2-rc2 codis
[root@cu2 CodisLabs]# git clone --branch release3.2  https://github.com/CodisLabs/codis.git 

[root@cu2 CodisLabs]# cd codis/

# 安装一些依赖
# # ./autogen.sh: line 5: autoconf: command not found
# yum install autoconf 
[root@cu2 codis]# make 

[root@cu2 codis]# ll bin/
total 101292
drwxr-xr-x 4 root root     4096 Mar 15 12:58 assets
-rwxr-xr-x 1 root root 21036930 Mar 15 12:58 codis-admin
-rwxr-xr-x 1 root root 22343059 Mar 15 12:58 codis-dashboard
-rwxr-xr-x 1 root root 18378506 Mar 15 12:58 codis-fe
-rwxr-xr-x 1 root root 22675153 Mar 15 12:58 codis-proxy
-rwxr-xr-x 1 root root  7982967 Mar 15 12:58 codis-server
-rwxr-xr-x 1 root root  5580431 Mar 15 12:58 redis-benchmark
-rwxr-xr-x 1 root root  5712419 Mar 15 12:58 redis-cli
-rw-r--r-- 1 root root      170 Mar 15 12:58 version
[root@cu2 codis]# cat bin/version 
version = 2017-03-15 00:40:41 +0800 @be9ee25c63a64396b5fb0076447be560497b909d @3.2-beta-10-gbe9ee25
compile = 2017-03-15 12:58:23 +0800 by go version go1.6.2 linux/amd64

# 生成默认配置
[root@cu2 codis]# bin/codis-dashboard --default-config | tee dashboard.toml
[root@cu2 codis]# bin/codis-proxy --default-config | tee proxy.toml

生产部署

把测试环境的GOPATH和GOROOT全部拷贝到生产即可。这里上面已经生成了dashboard和proxy的配置了哦!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
[ud@cu-ud6 opt]$ ll
drwxrwxr-x.  2 ud   ud   4096 3月  18 00:10 bin
drwxr-xr-x. 11 ud   ud   4096 4月  20 2016 go
drwxr-xr-x.  4 ud   ud   4096 3月  15 12:58 gopath
drwxr-xr-x.  8 ud   ud   4096 3月  17 20:13 jdk1.8.0_92
drwxr-xr-x. 10 ud   ud   4096 2月  20 2014 zookeeper-3.4.6

[ud@cu-ud6 opt]$ ll bin
总用量 24
-rw-rw-r--. 1 ud ud 234 3月  17 20:36 codis.profile
lrwxrwxrwx. 1 ud ud  54 3月  17 20:34 redis-cli -> ../gopath/src/github.com/CodisLabs/codis/bin/redis-cli
-rwxrwxr-x. 1 ud ud 487 3月  17 20:54 start-codis-dashboard.sh
-rwxrwxr-x. 1 ud ud 310 3月  18 00:10 start-codis-proxy.sh
-rwxrwxr-x. 1 ud ud 335 3月  17 21:17 start-redis.sh
-rwxrwxr-x. 1 ud ud 323 3月  17 20:55 start-zoo.sh

[ud@cu-ud6 opt]$ for f in $( find bin -type f ) ; do echo " =============== $f ================= "; cat "$f" ; done
 =============== bin/codis.profile ================= 
#!/bin/sh

export GOROOT=/opt/go
export GOPATH=/opt/gopath
export CODIS_HOME=$GOPATH/src/github.com/CodisLabs/codis/
export LOG_DIR=/var/log

export JAVA_HOME=/opt/jdk1.8.0_92

export PATH=$JAVA_HOME/bin:$GOPATH/bin:$GOROOT/bin:$PATH

 =============== bin/start-zoo.sh ================= 
#!/bin/sh

CODIS_BIN="${BASH_SOURCE-$0}"
CODIS_BIN="$(dirname "${CODIS_BIN}")"
CODIS_BINDIR="$(cd "${CODIS_BIN}"; pwd)"

source $CODIS_BINDIR/codis.profile

export ZOO_LOG_DIR=$LOG_DIR

cd /opt/zookeeper-3.4.6
sed 's@dataDir=/tmp/zookeeper@dataDir=/data/zookeeper@' conf/zoo_sample.cfg >conf/zoo.cfg

bin/zkServer.sh start

 =============== bin/start-codis-dashboard.sh ================= 
#!/bin/sh

CODIS_BIN="${BASH_SOURCE-$0}"
CODIS_BIN="$(dirname "${CODIS_BIN}")"
CODIS_BINDIR="$(cd "${CODIS_BIN}"; pwd)"

source $CODIS_BINDIR/codis.profile

cd $CODIS_HOME
nohup bin/codis-dashboard \
  --ncpu=4 \
  --config=dashboard.toml \
  --log=$LOG_DIR/codis_dashboard.log \
  --log-level=INFO \
  >/dev/null 2>&1 &

nohup bin/codis-fe \
  --ncpu=4 \
  --zookeeper=127.0.0.1:2181 \
  --listen=0.0.0.0:28080 \
  --log=$LOG_DIR/codis_fe.log \
  --log-level=INFO \
  >/dev/null 2>&1 &

 =============== bin/start-codis-proxy.sh ================= 
#!/bin/sh

CODIS_BIN="${BASH_SOURCE-$0}"
CODIS_BIN="$(dirname "${CODIS_BIN}")"
CODIS_BINDIR="$(cd "${CODIS_BIN}"; pwd)"

source $CODIS_BINDIR/codis.profile

cd $CODIS_HOME
nohup bin/codis-proxy \
  --ncpu=24 \
  --config=proxy.toml \
  --log=$LOG_DIR/codis_proxy.log \
  --log-level=INFO \
  >/dev/null 2>&1 &

 =============== bin/start-redis.sh ================= 
#!/bin/sh

CODIS_BIN="${BASH_SOURCE-$0}"
CODIS_BIN="$(dirname "${CODIS_BIN}")"
CODIS_BINDIR="$(cd "${CODIS_BIN}"; pwd)"

source $CODIS_BINDIR/codis.profile

PORT=${1:-6379}

cd $CODIS_HOME
bin/codis-server --daemonize yes --port $PORT --pidfile /var/run/redis_$PORT.pid --logfile $LOG_DIR/redis_$PORT.log --save "" --bind $(hostname) 

环境:

  • zookeeper: cu-ud6
  • dashboard: cu-ud6
  • fa: cu-ud6
  • proxy: cu-ud6/7/8
  • redis: cu-ud6/7/8:6378/6379
  • nginx代理: cu-ud9

web界面添加步骤:

  • 界面上添加proxy : cu6/7/8:11080
  • 再添加group,填数字: ½/¾/5/6
  • 然后添加server : cu-ud6/7/8:6378/6379
  • 最后分配slots

nginx1.11新版本已经支持tcp的代理,可以实现proxy的负载均衡:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 编译Nginx
./configure --with-stream --with-http_ssl_module --with-pcre=src/pcre --with-zlib=src/zlib --prefix=/usr/local/nginx
make && make install

[ud@cu-ud9 nginx]$ cat conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
error_log  /var/log/nginx_error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

stream {
  upstream proxy {
    hash   $remote_addr;
    server cu-ud6:19000;
    server cu-ud7:19000;
    server cu-ud8:19000;
  }
  
  server {
    listen cu-ud9:19000;
    proxy_timeout 600s;
    proxy_pass proxy;
  }
}

# 测试获取数据
[ud@cu-ud6 opt]$ bin/redis-cli -h cu-ud6 -p 6379 scan 0 # 样本Key
[ud@cu-ud6 opt]$ bin/redis-cli -h cu-ud9 -p 19000
> get XXX

重置统计量:

1
[ud@cu-ud6 codis]$ bin/codis-admin --proxy=cu-ud6:11080 --reset-stats

问题

pipeline量太大,修改proxy的 backend_max_pipeline/session_max_pipeline 。同时在客户端代码里面执行一定量的pipe后执行sync。

1
2
3
2017/03/18 00:01:23 session.go:79: [INFO] session [0xc839888d80] create: {"ops":0,"create":1489766483,"remote":"192.168.32.182:57029"}
2017/03/18 00:01:24 session.go:86: [INFO] session [0xc834a06d80] closed: {"ops":39601,"create":1489766483,"lastop":1489766484,"remote":"192.168.32.182:57028"}, error: too many pipelined r
equests

sync还是会超时,修改nginx的proxy_timeout以及客户端初始化的timeout参数。

1
new JedisPool(new GenericObjectPoolConfig(), "cu-ud9", 19000, 10 * 60 * 1000)

W:感觉proxy还是会有停顿,sync后有时会出现几分钟时间没响应。

–END

Comments