本文基于网络上其他作者的文章,和官网的codis搭建文档,并结合自己的搭建过程和测试,若转载请注明出处!
Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表), 上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务。
CODIS百度百科
codis特点:
图形化操作和管理员工具
支持大部分redis命令,完全兼容Twemproxy
代理数据在zookeeper中,可以实现高可用。redis cluster也可以通过主从实现高可用。
中文使用文档:https://github.com/CodisLabs/codis/blob/release3.2/doc/tutorial_zh.md
Codis 3.x 由以下组件组成:
**安装目录: **
go:/usr/local/go
codis:/usr/local/codis
jdk:/usr/local/jdk
zk:/usr/local/zookeeper-3.4.14
wget https://dl.google.com/go/go1.12.13.linux-amd64.tar.gz
注:官方地址需要外网环境
[root@codis-master ~]# wget https://dl.google.com/go/go1.12.13.linux-amd64.tar.gz
--2019-11-22 16:37:00-- https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 216.58.200.48, 2404:6800:4008:801::2010
Connecting to storage.googleapis.com (storage.googleapis.com)|216.58.200.48|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84840658 (81M) [application/x-gzip]
Saving to: ‘go1.6.2.linux-amd64.tar.gz’
100%[===========================================================================================>] 84,840,658 340KB/s in 3m 22s
2019-11-22 16:40:33 (410 KB/s) - ‘go1.6.2.linux-amd64.tar.gz’ saved [84840658/84840658]
# 解压到/usr/local/目录下
[root@codis-master ~]# tar -zxf go1.12.13.linux-amd64.tar.gz -C /usr/local/
# 配置go环境,在/etc/profile文件中加入下面三行
[root@codis-master ~]# vim /etc/profile
export GOROOT=/usr/local/go #go的安装路径
export GOPATH=/usr/local/gopath #默认安装包的路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 重新加载环境变量,查看go能否全局调用
[root@codis-master ~]# source /etc/profile
[root@codis-master ~]# go version
go version go1.12.13 linux/amd64
# 测试go语言能否成功执行(体验go语言使用)
[root@codis-master ~]# vim hello.go
package main
import "fmt"
func main(){
fmt.Printf("hello,world\n")
}
[root@codis-master ~]# go run hello.go
hello,world
# 下载jdk包到本机中,方法不限,此处采用手动安装:
# 将jdk包发送到Linux环境中
[root@codis-master ~]# rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring jdk-8u191-linux-x64.tar.gz...
100% 187259 KB 15604 KB/sec 00:00:12 0 Errors
[root@codis-master ~]# ll
total 312236
-rw-r--r--. 1 root root 127968473 Nov 1 08:13 go1.12.13.linux-amd64.tar.gz
-rw-r--r--. 1 root root 191753373 Nov 22 18:55 jdk-8u191-linux-x64.tar.gz
[root@codis-master ~]# tar zxf jdk-8u191-linux-x64.tar.gz -C /usr/local/jdk
[root@codis-master ~]# cd /usr/local/jdk/jdk1.8.0_191/
[root@codis-master jdk1.8.0_191]# ll
total 25976
drwxr-xr-x. 2 10 143 4096 Oct 6 2018 bin
-r--r--r--. 1 10 143 3244 Oct 6 2018 COPYRIGHT
drwxr-xr-x. 3 10 143 132 Oct 6 2018 include
-rw-r--r--. 1 10 143 5207154 Sep 12 2018 javafx-src.zip
drwxr-xr-x. 5 10 143 185 Oct 6 2018 jre
drwxr-xr-x. 5 10 143 245 Oct 6 2018 lib
-r--r--r--. 1 10 143 40 Oct 6 2018 LICENSE
drwxr-xr-x. 4 10 143 47 Oct 6 2018 man
-r--r--r--. 1 10 143 159 Oct 6 2018 README.html
-rw-r--r--. 1 10 143 424 Oct 6 2018 release
-rw-r--r--. 1 10 143 21101479 Oct 6 2018 src.zip
-rw-r--r--. 1 10 143 108062 Sep 12 2018 THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r--. 1 10 143 155003 Oct 6 2018 THIRDPARTYLICENSEREADME.txt
#添加环境变量,添加如下三行
[root@codis-master jdk1.8.0_191]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_191/
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
# 测试java环境
[root@codis-master jdk1.8.0_191]# source /etc/profile
[root@codis-master jdk1.8.0_191]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
下载zookeeper安装包,并解压至/usr/local目录下
[root@codis-master ~]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
[root@codis-master ~]# tar zxf zookeeper-3.4.14 -C /usr/local/
修改配置文件,创建/tmp/zookeeper目录并新建一个myid文件,写入1
[root@codis-master ~]# cd /usr/local/zookeeper-3.4.14/conf/
[root@codis-master conf]# mv zoo_sample.cfg zoo.cfg
[root@codis-master conf]# vim zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-3.4.14/data # 这里自己设置一个路径
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.168.6.151:2888:3888
server.2=192.168.6.152:2888:3888
server.3=192.168.6.153:2888:3888
# 创建配置文件中的目录,并新建一个myid文件,向其中写入1,其他两台服务器按上面的配置文件,写入server后的数字
[root@codis-master apache-zookeeper-3.5.6]# mkdir /usr/local/zookeeper-3.4.14/data
[root@codis-master apache-zookeeper-3.5.6]# echo 1 > /usr/local/zookeeper-3.4.14/data/myid
1
配置防火墙,打开对应端口
[root@codis-master bin]# firewall-cmd --zone=public --add-port=2181/tcp
success
[root@codis-master bin]# firewall-cmd --zone=public --add-port=2888/tcp
success
[root@codis-master bin]# firewall-cmd --zone=public --add-port=3888/tcp
success
[root@codis-master bin]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT
[root@codis-master bin]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT
[root@codis-master bin]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3888 -j ACCEPT
启动zookeeper
[root@codis-master ~]# cd /usr/local/zookeeper-3.4.14/bin/
[root@codis-master bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED #启动成功
为了方便启动zookeeper,我们可以添加环境变量
[root@codis-master bin]# vim /etc/profile
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.14/
export PATH=$PATH:$ZOOKEEPER_HOME/bin
新建目录与下载,使用git命令下载,没有git命令时使用yum install -y git安装:
[root@codis-slave01 ~]# mkdir -p $GOPATH/src/github.com/CodisLabs # 目录结构一定要和官网一样,若不同则会报错
[root@codis-slave01 ~]# cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.2 #进入刚刚创建的目录并下载release
进入codis目录,编译安装:
[root@codis-master CodisLabs]# cd codis/
[root@codis-master codis]# make MALLOC=libc #此处添加了“MALLOC=libc”参数,原因如下图
如果编译报错:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
原因是jemalloc重载了Linux下的ANSI C的malloc和free函数。解决办法:make时添加参数:make MALLOC=libc
添加了参数后又发现报错:
此时安装autoconf即可解决
yum install -y autoconf
安装后继续使用make MALLOC=libc编译,这次编译完成
修改配置文件中admin_addr一项,改为自己的ip
[root@codis-master codis]# vim config/dashboard.toml
# Set bind address for admin(rpc), tcp only.
admin_addr = "192.168.6.151:18080"
使用codis-dashboard-admin.sh start启动dashboard
[root@codis-master codis]# ./admin/codis-dashboard-admin.sh start
/usr/local/gopath/src/github.com/CodisLabs/codis/admin/../config/dashboard.toml
starting codis-dashboard ...
查看dashboard日志确认启动是否有异常
[root@codis-master codis]# tail -100 ./log/codis-dashboard.log.2019-11-23 # 太多,我只保留了最后几行,后面步骤同理
/usr/local/gopath/src/github.com/CodisLabs/codis/bin/codis-dashboard.pid
2019/11/23 15:09:03 fsclient.go:195: [INFO] fsclient - create /codis3/codis-demo/topom OK
2019/11/23 15:09:03 topom_sentinel.go:169: [WARN] rewatch sentinels = []
2019/11/23 15:09:03 main.go:179: [WARN] [0xc000095320] dashboard is working ...
2019/11/23 15:09:03 topom.go:429: [WARN] admin start service on [::]:18080
使用codis-proxy-admin.sh start启动codis-proxy,并查看proxy日志是否有异常
[root@codis-master codis]# ./admin/codis-proxy-admin.sh start
/usr/local/gopath/src/github.com/CodisLabs/codis/admin/../config/proxy.toml
starting codis-proxy ...
[root@codis-master codis]# tail -100 ./log/codis-proxy.log.2019-11-23
2019/11/23 15:46:04 proxy.go:293: [WARN] [0xc0000a0b00] set sentinels = []
2019/11/23 15:46:04 main.go:343: [WARN] rpc online proxy seems OK
2019/11/23 15:46:05 main.go:233: [WARN] [0xc0000a0b00] proxy is working ...
使用codis-server-admin.sh start启动 codis-server,并查看redis日志是否有异常
[root@codis-master codis]# ./admin/codis-server-admin.sh start
/usr/local/gopath/src/github.com/CodisLabs/codis/admin/../config/redis.conf
starting codis-server ...
[root@codis-master codis]# tail -100 /tmp/redis_6379.log
53996:M 23 Nov 15:30:53.514 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.11 (de1ad026/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 53996
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
53996:M 23 Nov 15:30:53.517 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
53996:M 23 Nov 15:30:53.517 # Server started, Redis version 3.2.11
53996:M 23 Nov 15:30:53.517 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
53996:M 23 Nov 15:30:53.518 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
53996:M 23 Nov 15:30:53.518 * The server is now ready to accept connections on port 6379
redis.conf 配置中 pidfile、logfile 默认保存在 /tmp 目录,若启动失败,请检查当前用户是否有该目录的读写权限。
使用codis-fe-admin.sh start启动 codis-fe,并查看fe日志是否有异常
[root@codis-master codis]# ./admin/codis-fe-admin.sh start
starting codis-fe ...
[root@codis-master codis]# tail -100 ./log/codis-fe.log.2019-11-23
2019/11/23 15:31:28 main.go:101: [WARN] set ncpu = 1
2019/11/23 15:31:28 main.go:104: [WARN] set listen = 0.0.0.0:9090
2019/11/23 15:31:28 main.go:120: [WARN] set assets = /usr/local/gopath/src/github.com/CodisLabs/codis/bin/assets
2019/11/23 15:31:28 main.go:162: [WARN] set --filesystem = /tmp/codis
2019/11/23 15:31:28 main.go:216: [WARN] option --pidfile = /usr/local/gopath/src/github.com/CodisLabs/codis/bin/codis-fe.pid
打开iptables端口,关闭防火墙
[root@codis-master codis]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
[root@codis-master codis]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 18080 -j ACCEPT
[root@codis-master codis]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 18087 -j ACCEPT
[root@codis-master codis]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 11080 -j ACCEPT
[root@codis-master codis]# systemctl stop firewalld.service
其余两台机器同理,按上方步骤操作即可
通过web浏览器访问集群管理页面(fe地址:192.168.6.151: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 按钮来做,如下图所示,点击此按钮,我们即快速完成了一个集群的搭建。
搭建完成
在Group中输入1,点击New Group创建组
创建完成后在下方添加新的Server
当master server服务挂掉后,会发现slave自动升级为主库
再关掉从库的服务,出现问题
点击PROMOTE后,此时发现主服务变为master,再启动152的server服务后,集群又重新服务
选项 | 描述 | 默认值 |
---|---|---|
-h | 指定服务器主机名 | 127.0.0.1 |
-p | 指定服务器端口 | 6379 |
-s | 指定服务器 socket | |
-c | 指定并发连接数 | 50 |
-n | 指定请求数 | 10000 |
-d | 以字节的形式指定 SET/GET 值的数据大小 | 2 |
-k | 1=keep alive 0=reconnect | 1 |
-r | SET/GET/INCR 使用随机 key, SADD 使用随机值 | |
-P | 通过管道传输 请求 | 1 |
-q | 强制退出 redis。仅显示 query/sec 值 | |
–csv | 以 CSV 格式输出 | |
-l | 生成循环,永久执行测试 | |
-t | 仅运行以逗号分隔的测试命令列表。 | |
-I | Idle 模式。仅打开 N 个 idle 连接并等待。 |
我们使用redis-benchmark工具,用50个线程发送100000个请求
[root@codis-master bin]# ./redis-benchmark -h 192.168.6.151 -p 6379 -c 50 -n 100000 -t get
====== GET ======
100000 requests completed in 1.35 seconds
50 parallel clients
3 bytes payload
keep alive: 1
97.91% <= 1 milliseconds
99.90% <= 2 milliseconds
99.95% <= 11 milliseconds
99.99% <= 12 milliseconds
100.00% <= 12 milliseconds
74019.25 requests per second
[root@codis-master bin]# ./redis-benchmark -h 192.168.6.151 -p 6379 -c 50 -n 100000 -t set
====== SET ======
100000 requests completed in 1.99 seconds
50 parallel clients
3 bytes payload
keep alive: 1
89.11% <= 1 milliseconds
99.88% <= 2 milliseconds
100.00% <= 2 milliseconds
50352.47 requests per second