Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表), 上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务.

Codis 由四部分组成:

Codis Proxy (codis-proxy)
Codis Dashboard (codis-config)
Codis Redis (codis-server)
ZooKeeper/Etcd
codis-proxy 是客户端连接的 Redis 代理服务, codis-proxy 本身实现了 Redis 协议, 表现得和一个原生的 Redis 没什么区别 (就像 Twemproxy), 对于一个业务来说, 可以部署多个 codis-proxy, codis-proxy 本身是无状态的.

codis-config 是 Codis 的管理工具, 支持包括, 添加/删除 Redis 节点, 添加/删除 Proxy 节点, 发起数据迁移等操作. codis-config 本身还自带了一个 http server, 会启动一个 dashboard, 用户可以直接在浏览器上观察 Codis 集群的运行状态.

codis-server 是 Codis 项目维护的一个 Redis 分支, 基于 2.8.21 开发, 加入了 slot 的支持和原子的数据迁移指令. Codis 上层的 codis-proxy 和 codis-config 只能和这个版本的 Redis 交互才能正常运行.

Codis 依赖 ZooKeeper 来存放数据路由表和 codis-proxy 节点的元信息, codis-config 发起的命令都会通过 ZooKeeper 同步到各个存活的 codis-proxy.

Codis 支持按照 Namespace 区分不同的产品, 拥有不同的 product name 的产品, 各项配置都不会冲突.

————————————————————————————
(摘自:官方Github,https://github.com/CodisLabs/codis/blob/master/doc/tutorial_zh.md)

本文涉及到六台服务器,它们的IP规划与架构初览如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/hosts
 
Local
       localhost
 
Pub
 
Server
-server1
-server2
 
Ha
     codisha1
     codisha2
 
Zookeeper
     zookeeper1
     zookeeper2
~]#

 

零、所有的服务器

关闭防火墙:
chkconfig iptables off

关闭SELinux:
sed -i “/SELINUX/enforcing/disabled/” /etc/selinux/config

开启YUM缓存:
sed -i “/keepcache/s/0/1/” /etc/yum.conf

网卡添加外网DNS:
echo “DNS1=114.114.114.114” >> /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
ping baidu.com -c 3

YUM生成库缓存:

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
/yum.repos.d/
16
--r--. 1 root root 2593 Jun 26  2012 CentOS-Vault.repo
--r--. 1 root root  626 Jun 26  2012 CentOS-Media.repo
--r--. 1 root root  637 Jun 26  2012 CentOS-Debuginfo.repo
--r--. 1 root root 1926 Jun 26  2012 CentOS-Base.repo
~]#
makecache
security
hostfile
mirrors.163.com
mirrors.163.com
mirrors.163.com
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
Created
~]#

 

Java支持:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--color ^java | grep --color jdk
/6.3
    updates
    updates
    updates
    updates
    updates
     updates
     updates
     updates
     updates
     updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
       updates
~]#
-openjdk
security
hostfile
mirrors.163.com
mirrors.163.com
mirrors.163.com
Process
Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.101-3.b13.el6_8 will be installed
--> Processing Dependency: java-1.8.0-openjdk-headless = 1:1.8.0.101-3.b13.el6_8 for package: 1:java-1.8.0-openjdk-1.8.0.101-3.b13.el6_8.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.101-3.b13.el6_8.x86_64
--> Running transaction check
---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.101-3.b13.el6_8 will be installed
--> Processing Dependency: tzdata-java >= 2014f-1 for package: 1:java-1.8.0-openjdk-headless-1.8.0.101-3.b13.el6_8.x86_64
---> Package libjpeg.x86_64 0:6b-46.el6 will be obsoleted
---> Package libjpeg-turbo.x86_64 0:1.2.1-3.el6_5 will be obsoleting
--> Running transaction check
---> Package tzdata-java.noarch 0:2012c-1.el6 will be updated
---> Package tzdata-java.noarch 0:2016f-1.el6 will be an update
--> Finished Dependency Resolution
 
Resolved
 
=====================================================================================================================
Size
=====================================================================================================================
Installing:
k
k
-46.el6
dependencies:
M
dependencies:
k
 
Summary
=====================================================================================================================
Package(s)
Package(s)
 
M
y
Packages:
    
    
    
    
---------------------------------------------------------------------------------------------------------------------
    
NOKEY
-6
0xC105B9DE:
>
/6.3)
-6
y
rpm_check_debug
Test
Succeeded
Transaction
/6
/6
/6
/6
/6
/6
/6
/6
/6
/6
/6
/6
 
Installed:
              
 
Installed:
                                                        
 
Updated:
                                                                                  
 
Replaced:
                                                                                        
 
Complete!
~]#
-version
-b13)
mode)
~]#

 

一、部署:Zookeeper。

官方项目主页:
http://zookeeper.apache.org/

官方介质下载页:
http://zookeeper.apache.org/releases.html

这里我下载最新版本:3.5.2 alpha。
Download Link:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.2-alpha/zookeeper-3.5.2-alpha.tar.gz

在98 / 99,两个IP上部署ZOOKEEPER。

(这里,仅演示在其中一台上搭建ZK的过程,第二台的方式类似)
上传软件介质到服务器:

1
2
3
4
5
6
7
8
9
/software
~]#
/software
18012
--r-- 1 root root 18443679 Aug 19  2016 zookeeper-3.5.2-alpha.tar.gz
~]#
/software
     /software
~]#

 

解压:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/software
ls
-alpha.tar.gz
software]#
-alpha.tar.gz
software]#
ls
-alpha.tar.gz
-alpha
ls
-alpha.jar
asc
-alpha.jar.md5
-alpha.jar.sha1
-alpha]#

 

移动到zookeeper的安装目录【/opt/zookeeper】:

1
2
3
4
5
6
7
8
/zookeeper
/zookeeper/
/zookeeper/
-alpha.jar
asc
-alpha.jar.md5
-alpha.jar.sha1
-alpha]#

 

配置zookeeper配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pwd
/zookeeper
conf
conf
zookeeper]#
conf/
ls
  zoo_sample.cfg
conf]#
zoo.cfg
zoo.cfg
=2000
=10
=5
/data
log
=2181
=zookeeper1:2287:3387
=zookeeper2:2288:3388
conf]#

节点二【zookeeper2】的这个配置文件是:

1
2
3
4
5
6
7
8
9
10
zoo.cfg
=2000
=10
=5
/data
log
=2182
=zookeeper1:2287:3387
=zookeeper2:2288:3388
conf]#

 

创建上面配置文件中需要的目录结构:
mkdir -p /data/zookeeper/zk1/{data,log}

创建myid文件:
【zookeeper1】

1
2
3
4
/myid
/myid
1
conf]#

【zookeeper2】

1
2
3
4
/myid
/myid
2
conf]#

 

启动zookeeper:

Oracle PL/SQL
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
pwd
/zookeeper
zookeeper]#
ls
-alpha.jar
asc
-alpha.jar.md5
-alpha.jar.sha1
zookeeper]#
-ltr
48
zkServer.sh
-initialize.sh
zkServer.cmd
zkEnv.sh
zkEnv.cmd
zkCli.sh
zkCli.cmd
zkCleanup.sh
README.txt
zookeeper]#
start
/java
default
/zoo.cfg
STARTED
zookeeper]#
--color zookeeper
local]
/zoo.cfg
--color zookeeper
zookeeper]#
java
          
          
          
          
zookeeper]#

可以看到,启动成功。

测试zookeeper的客户端程序:

【zookeeper1】

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
zookeeper1:2181
/java
zookeeper1:2181
GMT
=zookeeper1
=1.8.0_101
Corporation
/jre
/bin/../conf:
/lib
environment:java.io.tmpdir=/tmp
>
=Linux
=amd64
-279.el6.x86_64
=root
user.home=/root
/zookeeper
=236MB
=247MB
=241MB
@51521cc1
ZooKeeper!
error)
enabled
/10.158.1.98:2181
30000
 
WATCHER::
 
null
 
0]
quit
0x100004492e80000
closed
zookeeper]#

【zookeeper2】

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
zookeeper2:2182
/java
zookeeper2:2182
GMT
=zookeeper1
=1.8.0_101
Corporation
/jre
/bin/../conf:
/lib
environment:java.io.tmpdir=/tmp
>
=Linux
=amd64
-279.el6.x86_64
=root
user.home=/root
/zookeeper
=236MB
=247MB
=241MB
@51521cc1
ZooKeeper!
error)
enabled
/10.158.1.99:2182
30000
 
WATCHER::
 
null
 
0]
quit
0x20000448b090000
closed
zookeeper]#

 

这样,ZOOKEEPER,就部署好了。

二、部署:Codis。
(和上面一样,这里仅演示在一台节点上的操作详情,另一台也是类似的。在我的环境中,一共有两台Codis Server。)

1. Go语言支持。

官方网站:https://golang.org/

当前最新版本:1.7。

官方下载链接:
Linux:https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz
源码包:https://storage.googleapis.com/golang/go1.7.src.tar.gz
MS Windows:https://storage.googleapis.com/golang/go1.7.windows-amd64.msi

上传介质到服务器:

1
2
3
4
5
6
7
8
9
10
/software
/software/
ls
-amd64.tar.gz
*
--r-- 1 root root 81573766 Aug 19 00:40 go1.7.linux-amd64.tar.gz
software]#
*
-amd64.tar.gz
software]#

 

将go安装到/opt/golang。

创建目录:

1
2
3
4
5
6
7
8
9
/opt
rh
/golang
software]#
/opt
8
rh
golang
software]#

将安装介质解压到安装目录:

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
pwd
/software
software]#
/golang/
software]#
ls
-amd64.tar.gz
software]#
-amd64.tar.gz
software]#
/golang/
4
go
go/
144
--r--  1 root root  1638 Aug 16 06:47 README.md
--r--  1 root root  1303 Aug 16 06:47 PATENTS
--r--  1 root root  1479 Aug 16 06:47 LICENSE
--r--  1 root root 40192 Aug 16 06:47 CONTRIBUTORS
--r--  1 root root  1168 Aug 16 06:47 CONTRIBUTING.md
--r--  1 root root 29041 Aug 16 06:47 AUTHORS
api
--r--  1 root root    26 Aug 16 06:47 robots.txt
lib
--r--  1 root root  1150 Aug 16 06:47 favicon.ico
doc
src
--r--  1 root root     5 Aug 16 06:48 VERSION
test
pkg
misc
blog
bin
software]#

 

配置GO的环境变量:

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
~/.bash_profile
.bash_profile
 
functions
then
~/.bashrc
fi
 
programs
 
/bin
 
PATH
 
Golang
go
/go_me
 
/bin
 
 
software]#
~/.bash_profile
software]#
--color GO
go
/go_me
--color PATH | grep --color go
/bin
/go_me
software]#

其中:
GOROOT,Go语言的软件根目录
GOPATH,Go语言的项目根目录

创建Go语言的项目目录:

1
2
3
4
5
6
7
data
/go_me
software]#
/data/
4
go_me
software]#

 

测试Go的可用性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version
/amd64
software]#
env
software]#

 

2. 安装:Codis。
Codis项目主页:
https://github.com/CodisLabs/codis

开源中国的介绍:
http://www.oschina.net/p/codis/?fromerr=PIDoyfcY

GIT支持:

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
--color ^git
  
  
  
  
  
  
  
  
  
~]#
git
security
hostfile
mirrors.neusoft.edu.cn
mirrors.neusoft.edu.cn
centos.ustc.edu.cn
Process
Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.7.1-4.el6_7.1 will be installed
--> Processing Dependency: perl-Git = 1.7.1-4.el6_7.1 for package: git-1.7.1-4.el6_7.1.x86_64
--> Processing Dependency: perl(Git) for package: git-1.7.1-4.el6_7.1.x86_64
--> Processing Dependency: perl(Error) for package: git-1.7.1-4.el6_7.1.x86_64
--> Processing Dependency: libz.so.1(ZLIB_1.2.0)(64bit) for package: git-1.7.1-4.el6_7.1.x86_64
--> Processing Dependency: libssl.so.10(libssl.so.10)(64bit) for package: git-1.7.1-4.el6_7.1.x86_64
--> Processing Dependency: libcrypto.so.10(libcrypto.so.10)(64bit) for package: git-1.7.1-4.el6_7.1.x86_64
--> Running transaction check
---> Package openssl.x86_64 0:1.0.0-20.el6_2.5 will be updated
---> Package openssl.x86_64 0:1.0.1e-48.el6_8.1 will be an update
---> Package perl-Error.noarch 1:0.17015-4.el6 will be installed
---> Package perl-Git.noarch 0:1.7.1-4.el6_7.1 will be installed
---> Package zlib.x86_64 0:1.2.3-27.el6 will be updated
---> Package zlib.x86_64 0:1.2.3-29.el6 will be an update
--> Finished Dependency Resolution
 
Resolved
 
=====================================================================================================================
Size
=====================================================================================================================
Installing:
M
dependencies:
k
k
dependencies:
M
k
 
Summary
=====================================================================================================================
Package(s)
Package(s)
 
M
Packages:
    
    
    
    
    
---------------------------------------------------------------------------------------------------------------------
    
rpm_check_debug
Test
Succeeded
Transaction
/7
/7
/7
/7
/7
/7
/7
/7
/7
/7
/7
/7
/7
/7
 
Installed:
                                                                                      
 
Installed:
                        
 
Updated:
                          
 
Complete!
~]#
--version
1.7.1
~]#

 

获得Codis代码(需要服务器有git软件包的支持):

1
2
3
4
5
6
7
8
9
$GOPATH
pwd
/go_me
-ltr
0
/codis
等待一段时间。)
/codis
go_me]#

官方建议通过“go get”来获取codis,该命令会下载master分支的最新版,而master分支则由codis作者维护。

这一步的执行过程可能会比较久,而命令行挂起,看不到进程。
你可以开启另一个会话,监控GOPATH目录的容量变化,如下:
watch -n .1 -d “du -sh /data/go_me”

这样会比较显而易见:
codis+redis 集群搭建管理
大概25M左右。

如果这一步没有git支持,则你会遇到如下错误:

1
2
3
4
/codis
/gogetcmd
PATH
go_me]#

 

最终下载成功后:

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
src/
     src/
go_me]#
-ltr
4
src
go_me]#
src/
4
github.com
go_me]#
/github.com/
4
CodisLabs
go_me]#
/CodisLabs/
4
codis
go_me]#
/codis/
64
--r-- 1 root root 1076 Aug 19 10:07 MIT-LICENSE.txt
Godeps
--r-- 1 root root  320 Aug 19 10:07 Dockerfile
--r-- 1 root root 2949 Aug 19 10:07 README.md
--r-- 1 root root  775 Aug 19 10:07 Makefile
--r-- 1 root root 1817 Aug 19 10:07 config.ini
cmd
docker
doc
extern
genver.sh
pkg
test
vendor
--r-- 1 root root 1081 Aug 19 10:07 wandoujia_licese.txt
--r-- 1 root root 1475 Aug 19 10:07 vitess_license
go_me]#

 

编译前,需要安装的软件包
yum install -y gcc
yum groupinstall “Development Tools” # 很重要!

安装:jemalloc支持

下载jemalloc:
http://www.canonware.com/download/jemalloc/jemalloc-4.2.1.tar.bz2

上传服务器,并安装:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/software
ls
-4.2.1.tar.bz2
software]#
*
-amd64.tar.gz
-4.2.1.tar.bz2
software]#
-4.2.1.tar.bz2
-ltr
80092
-4.2.1
--r-- 1 root root 81573766 Aug 19 00:40 go1.7.linux-amd64.tar.gz
--r-- 1 root root   431132 Aug 19 10:23 jemalloc-4.2.1.tar.bz2
-4.2.1
ls
   VERSION
  test
-4.2.1]#
/jemalloc
-4.2.1]#
--prefix=/opt/jemalloc
过多的输出。)
 
no
yes
no
yes
yes
yes
yes
yes
./config.status
Makefile
jemalloc.pc
/html.xsl
/manpages.xsl
/jemalloc.xml
/jemalloc_macros.h
/jemalloc_protos.h
/jemalloc_typedefs.h
/jemalloc_internal.h
/test.sh
/jemalloc_test.h
config.stamp
-config
/jemalloc.sh
/jeprof
/jemalloc_defs.h
/jemalloc_internal_defs.h
/jemalloc_test_defs.h
commands
commands
commands
commands
commands
commands
commands
commands
commands
commands
commands
===============================================================================
-g3de035335255d553bdb344c32ffdb603816195d8
2
 
--prefix=/opt/jemalloc
gcc
-loops
-D_REENTRANT
            :
      :
  -lpthread
        :
 
/xsltproc
            :
 
/jemalloc
/bin
share
/include
/lib
/man
 
            :
-4.2.1/
            :
-4.2.1/
 
    :
JEMALLOC_PRIVATE_NAMESPACE
je_
     :
        :
0
1
0
0
1
0
0
0
0
1
1
0
0
0
0
0
1
1
===============================================================================
-4.2.1]#
make
过多的输出。)
/tcache.c
/ticker.c
/tsd.c
/util.c
/witness.c
/witness.o
/witness.pic.o
-4.2.1]#
install
/bin
/bin
/bin
/bin
/jemalloc
/jemalloc
/lib
/lib
/libjemalloc.so
/lib
/lib
/lib
/pkgconfig
/pkgconfig
/jemalloc
/jemalloc
/man3
/man3
-4.2.1]#
/jemalloc/
16
bin
include
lib
share
-4.2.1]#
 
~/.bash_profile
.bash_profile
 
functions
then
~/.bashrc
fi
 
programs
 
/bin
 
PATH
 
Golang
go
/go_me
 
/bin
 
Jemalloc
/bin
-4.2.1]#
~/.bash_profile
-4.2.1]#
--color PATH | grep --color jemalloc
/bin
-4.2.1]#

 

如果没有jemalloc,则编译的时候会出错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
adlist.o
ae.o
anet.o
dict.o
adlist.c:34:
directory
1
jobs....
ae.c:44:
directory
dict.c:47:
directory
1
1

注意:如果依旧报错,编译时采用:
make MALLOC=libc

Codis,编译:make MALLOC=libc。

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
/codis/
pwd
/codis
codis]#
ls
    vitess_license
  wandoujia_licese.txt
codis]#
=libc
过多的输出。)
t_list.o
t_set.o
t_zset.o
t_hash.o
config.o
aof.o
pubsub.o
multi.o
debug.o
sort.o
intset.o
syncio.o
migrate.o
endianconv.o
slowlog.o
scripting.o
bio.o
rio.o
rand.o
memtest.o
crc64.o
crc32.o
bitops.o
sentinel.o
notify.o
setproctitle.o
hyperloglog.o
latency.o
sparkline.o
slots.o
-cli.o
-benchmark.o
dump.o
-aof.o
-benchmark
dump
-aof
-server
-sentinel
-cli
 
' ;)
 
make[2]: Leaving directory `/data/go_me/src/github.com/CodisLabs/codis/extern/redis-2.8.21/src'
-2.8.21'
codis]#
gotest
./cmd/...
   17.117s
    9.351s
      2.158s
     0.656s
files]
files]
files]
   0.003s
files]
files]
files]
files]
files]
codis]#

执行成功后,会在当前路径下的bin目录中生成可执行文件。
如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pwd
/codis
codis]#
bin
30104
assets
-config
-proxy
-server
codis]#
bin/*
directory
stripped
stripped
stripped
codis]#

生成的可执行文件:
codis-config
codis-proxy
codis-server

assents,存放了codis-config的Dashboard Http需要的资源,需要与codis-config放在同一个目录下。

二、部署:Codis集群。
(这里应该是对Codis Proxy的配置。)

确认zookeeper状态是否运行:
【98】

1
2
3
4
5
6
7
8
9
10
11
java
          
          
          
          
~]#
zookeeper
local]
zookeeper
/zoo.cfg
~]#

 

【99】

1
2
3
4
5
6
7
8
9
10
11
12
zookeeper
local]
zookeeper
/zoo.cfg
~]#
java
          
          
          
          
          
~]#

 

配置Codis Dashboard的配置文件:
【94】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pwd
/codis
codis]#
config
--r-- 1 root root 1894 Aug 19 13:54 config.ini
codis]#
strings
=zookeeper
=10.158.1.98:2181,10.158.1.99:2182
=test
=10.158.1.94:18087
=coids
=5
=1800
=131072
=1024
=30000
=proxy_1
codis]#

【95】的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
strings
=zookeeper
=10.158.1.98:2181,10.158.1.99:2182
=test
=10.158.1.95:18087
password=
=5
=1800
=131072
=1024
=30000
=proxy_1
codis]#

 

启动Codis dashboard:

1
2
3
4
5
6
7
dashboard
:18087
23586}
**********
me,
manually
*******************************

 

开另一个会话,你可以查看:

1
2
3
4
codis
-con
-con
~]#

 

访问Dashboard:
http://10.158.1.94:18087/admin/
codis+redis 集群搭建管理

初始化slots:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/codis/
help
usage:
[-f]
>
>
>
--delay=<delay_time_in_ms>]
--delay=<delay_time_in_ms>]
codis]#
init
{
0
}
codis]#

 

通过管理WEB页面,查看SLOT信息:
http://10.158.1.94:18087/slots
codis+redis 集群搭建管理
可以看到,当前,状态都是:OFF。

启动Codis Server。

配置redis配置文件:

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
pwd
/codis
codis]#
--color redis
--r-- 1 root root 36142 Aug 19 12:10 redis.conf
codis]#
strings
yes
/redis.pid
6379
511
0
0
notice
16
1
10
10000
yes
yes
yes
dump.rdb
./
yes
yes
no
5
no
100
no
everysec
no
100
64mb
yes
5000
10000
128
0
512
64
512
64
512
128
64
3000
yes
0
60
60
10
yes
codis]#

(以上配置为默认配置,并没有做出什么改动。)

启动Redis:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--color codis
local]
dashboard
codis]#
/redis.conf
codis]#
--color codis
local]
dashboard
                        
codis]#
/redis.pid
24552
codis]#
-se
-ser
-ser
codis]#

 

检查状态:

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
info
Server
redis_version:2.8.21
redis_git_sha1:1b40d080
redis_git_dirty:0
redis_build_id:bbb713f7faaaae10
redis_mode:standalone
x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:24552
run_id:774a1cfc80934036409d0a4705f2adb384b2a14a
tcp_port:6379
uptime_in_seconds:157
uptime_in_days:0
hz:10
lru_clock:11970711
/redis.conf
 
Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
 
Memory
used_memory:2723448
used_memory_human:2.60M
used_memory_rss:3993600
used_memory_peak:2723448
used_memory_peak_human:2.60M
used_memory_lua:36864
mem_fragmentation_ratio:1.47
mem_allocator:libc
 
Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1471588346
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
 
Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
 
Replication
master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 
CPU
used_cpu_sys:0.22
used_cpu_user:0.17
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
 
Keyspace
codis]#

 

添加Redis Server Group。

编辑配置文件:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/codis/
12
--r-- 1 root root 1175 Aug 20 16:02 redis_server_group_1.conf
--r-- 1 root root 1175 Aug 20 16:03 redis_server_group_3.conf
--r-- 1 root root 1175 Aug 20 16:03 redis_server_group_2.conf
codis]#
/redis_server_group_1.conf
yes
 
/redis_server_group_1.pid
6381
 
511
0
0
notice
16
1
10
10000
yes
yes
yes
 
dump_server_group_1.rdb
 
./
yes
yes
no
5
no
100
no
 
 
everysec
no
100
64mb
yes
5000
10000
128
0
512
64
512
64
512
128
64
3000
yes
0
60
60
10
yes
codis]#
/redis_server_group_2.conf
yes
 
/redis_server_group_2.pid
6382
 
511
0
0
notice
16
1
10
10000
yes
yes
yes
 
dump_server_group_2.rdb
 
./
yes
yes
no
5
no
100
no
 
 
everysec
no
100
64mb
yes
5000
10000
128
0
512
64
512
64
512
128
64
3000
yes
0
60
60
10
yes
codis]#
/redis_server_group_3.conf
yes
 
/redis_server_group_3.pid
6383
 
511
0
0
notice
16
1
10
10000
yes
yes
yes
 
dump_server_group_3.rdb
 
./
yes
yes
no
5
no
100
no
 
 
everysec
no
100
64mb
yes
5000
10000
128
0
512
64
512
64
512
128
64
3000
yes
0
60
60
10
yes
codis]#

 

启动多个服务:
/data/go_me/src/github.com/CodisLabs/codis/bin/codis-server /config/codis/redis_server_group_1.conf
/data/go_me/src/github.com/CodisLabs/codis/bin/codis-server /config/codis/redis_server_group_2.conf
/data/go_me/src/github.com/CodisLabs/codis/bin/codis-server /config/codis/redis_server_group_3.conf

运行后,服务状态:

1
2
3
4
5
6
7
8
9
--color codis
local]
--cpu=2 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000
dashboard
                        
                                
                                
                                
codis]#

【codis-server1】与【codis-server2】都需要如上配置。

添加到server group:

组一:
主 – codis-server1:6379
辅 – codis-server2:6381

组二:
主 – codis-server2:6379
辅 – codis-server2:6382 / codis-server1:6382

组三:
主 – codis-server1:6383
辅 – codis-server2:6383 / codis-server1:6381

开始配置:
配置前:

1
2
3
list
null
codis]#

 

组一:

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
1
codis]#
master
{
0
}
slave
{
0
}
codis]#
list
[
  {
1,
[
      {
1,
      },
      {
1,
      }
    ]
  }
]
codis]#

 

组二:

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
master
{
0
}
slave
{
0
}
slave
{
0
}
codis]#
list
[
  {
1,
[
      {
1,
      },
      {
1,
      }
    ]
  },
  {
2,
[
      {
2,
      },
      {
2,
      },
      {
2,
      }
    ]
  }
]
codis]#

 

组三:

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
master
{
0
}
slave
{
0
}
slave
{
0
}
codis]#
list
[
  {
1,
[
      {
1,
      },
      {
1,
      }
    ]
  },
  {
3,
[
      {
3,
      },
      {
3,
      },
      {
3,
      }
    ]
  },
  {
2,
[
      {
2,
      },
      {
2,
      },
      {
2,
      }
    ]
  }
]
codis]#

 

配置好后,查看状态:
【codis-server1】

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
replication
Replication
master
connected_slaves:2
=0
=0
master_repl_offset:1373
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1372
codis]#
codis]#
replication
Replication
role:slave
-server1
master_port:6383
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1471691816
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1834
codis]#
replication
Replication
role:slave
-server2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:393
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
codis]#
replication
Replication
role:slave
master_host:localhost
master_port:6381
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:197
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
codis]#
replication
refused
codis]#

【codis-server2】

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
replication
Replication
role:slave
master_host:10.158.1.94
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:1499
slave_priority:100
slave_read_only:1
connected_slaves:2
=1
=0
master_repl_offset:505
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:504
codis]#
replication
Replication
role:slave
-server1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1513
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
codis]#
replication
Replication
role:slave
-server2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:519
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
codis]#
replication
Replication
role:slave
-server1
master_port:6383
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1471692056
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
codis]#

 

在zookeeper端,也会有关于server group的配置信息:

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
127.0.0.1:2181
/java
127.0.0.1:2181
GMT
=zookeeper1
=1.8.0_101
Corporation
/jre
/bin/../conf:
/lib
environment:java.io.tmpdir=/tmp
>
=Linux
=amd64
-279.el6.x86_64
=root
user.home=/root
/zookeeper
=236MB
=247MB
=241MB
@51521cc1
ZooKeeper!
error)
enabled
/127.0.0.1:2181
30000
 
WATCHER::
 
null
 
/servers
group_3]
1]

 

通过Codis Dashboard查看:
codis+redis 集群搭建管理

codis+redis 集群搭建管理

设置Server Group的slot范围。

可以先初始化slot信息,如果以前做过初始化,则需要强制初始化:

1
2
3
4
5
6
                  
{
0
}
  

 

初始化完成后,就可以访问dashboard查看了:
http://10.158.1.94:18087/slots
codis+redis 集群搭建管理

codis+redis 集群搭建管理
注意上图中,红框高亮部分的编号信息。

slot默认初始化1024个,即:0~1023。(有时候也会有所不同。)

开始划分范围:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
online
{
0
}
online
{
0
}
online
{
0
}
codis]#

 

范围划分好后,dashboard的呈现有所变化:
codis+redis 集群搭建管理

如果选中其中某个范围,会在该页顶端显示其对应server group的详细信息:
codis+redis 集群搭建管理

启动:Codis Proxy。
命令:
bin/codis-proxy -c config.ini -L /var/log/codis-proxy.log –cpu=2 –addr=0.0.0.0:19000 –http-addr=0.0.0.0:11000 &

1
2
3
4
5
6
7
8
--cpu=2 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000
 
  _____
___/
  )
  /____/
 
等待输出。)

 

查看Proxy的状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/codis/
help
usage:
list
>
>
codis]#
list
[
  {
0,
5751,
  }
]
codis]#

 

这时候,通过dashboard也能够看到Proxy的状态:
http://10.158.1.94:18087/admin/
codis+redis 集群搭建管理

Proxy Debug:
http://10.158.1.94:11000/debug/vars
codis+redis 集群搭建管理

这样,Codis集群,就搭建完成了。

三、测试。

Oracle PL/SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
6379
ping
PONG
OK
name
>
quit
codis]#
codis]#
6381
name
>
quit
codis]#

可以看到,数据自动同步。

——————————
Done。

相关文章: