详解Codis安装与部署

Codis github上的介绍安装,里面很全,并且也有中/英文的,只不过按照github的步骤安装,会有一些坑,因此有了这么一篇文章。html

在上一篇文章《Redis实用监控工具一览》中,介绍了Redis经常使用的监控工具,codis是带有图形化的面板和管理工具的。因而才有了这么一篇文章。桌面系统:Centos7。linux

v简介

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

  • Codis Proxy (codis-proxy)
  • Codis Manager (codis-config)
  • Codis Redis (codis-server)
  • ZooKeeper

codis-proxy 是客户端链接的 Redis 代理服务, codis-proxy 自己实现了 Redis 协议, 表现得和一个原生的 Redis 没什么区别 (就像 Twemproxy), 对于一个业务来讲, 能够部署多个 codis-proxy, codis-proxy 自己是无状态的.github

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

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

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

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

Codis架构图apache

详解Codis安装与部署

图片来源于网络,侵删。vim

v特性

  • 自动平衡
  • 使用很是简单
  • 图形化的面板和管理工具
  • 支持绝大多数 Redis 命令,彻底兼容twemproxy
  • 支持 Redis 原生客户端
  • 安全并且透明的数据移植,可根据须要轻松添加和删除节点
  • 提供命令行接口
  • RESTful APIs

注意,为了不重复造轮子。以上“简介”部分来自百度百科的摘录。

v安装步骤

1.1 安装Go

1.1.1 下载Go压缩包

习惯系在最新的环境,目前最新版的go是1.12.5的,这里咱们就用最新版的。最新版更新能够在这里看。https://golang.org/dl/

wget https://storage.googleapis.com/golang/go1.12.5.linux-amd64.tar.gz

1.1.2 解压Go压缩包

tar -zxvf go1.12.5.linux-amd64.tar.gz

1.1.3 设置环境变量

详解Codis安装与部署

如图,go的安装目录是 /usr/local

vim /etc/profile

详解Codis安装与部署

export GOROOT=/usr/local/go #设置为go安装的路径 export GOPATH=/usr/local/gopath #默认安装包的路径 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

更新/etc/profile, wq 保存,使用 source /etc/profile 命令可使新创建的环境变量马上生效而不用从新启动系统。

详解Codis安装与部署

1.1.4 helloword go go go

新建一个go文件,而后运行。

详解Codis安装与部署

package main import "fmt" func main(){ fmt.Printf("hello,world\n") }

1.2 安装JDK

没有安装Java JDK的朋友能够直接看这里。《CentOS安装Java JDK》

1.3 安装ZooKeeper

1.3.1 下载ZooKeeper压缩包

wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz

1.3.2 解压ZooKeeper压缩包

tar -zxvf zookeeper-3.4.13.tar.gz

1.3.3 删除ZooKeeper压缩包

rm -f zookeeper-3.4.13.tar.gz

1.3.4 拷贝配置文件

cd /usr/local/zookeeper-3.4.13/conf

cp zoo_sample.cfg zoo.cfg

vim zoo.cfg

详解Codis安装与部署

这个zoo.cfg是zookeeper的配置文件,这里我搭的是单机版,若是想搭集群版也是经过修改配置文件便可。

# 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.13/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=cnblogs01:8888:9888 #这里修改成本身的主机名或者IP server.2=cnblogs02:8888:9888 server.3=cnblogs03:8888:9888

建立/usr/local/zookeeper-3.4.13/data文件夹,新建一个myid,写入1

mkdir data

vim myid

1.3.5 启动ZooKeeper

/usr/local/zookeeper-3.4.13/bin/zkServer.sh start

详解Codis安装与部署

1.4 安装Codis

1.4.1 安装Codis

Codis 源代码须要下载到 $GOPATH/src/github.com/CodisLabs/codis

mkdir -p $GOPATH/src/github.com/CodisLabs

cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.2

1.4.2 编译Codis

cd $GOPATH/src/github.com/CodisLabs/codis

make

直接经过 make 进行编译,这里报了个错 ./autogen.sh:行5: autoconf: 未找到命令

详解Codis安装与部署

安装autoconf

yum install autoconf

安装autoconf以后,继续经过 make 编译。

详解Codis安装与部署

dashboard,proxy,admin,ha,fe这些codis的组件编译完成了。

注意的是,目录结构必定要是,gopath的本身的定义,单后后面的目录须要新建,必须和官网同样,不同,要报错。 $GOPATH/src/github.com/CodisLabs/

1.4.3 启动codis-dashboard

使用 codis-dashboard-admin.sh 脚本启动 dashboard,并查看 dashboard 日志确认启动是否有异常。

./admin/codis-dashboard-admin.sh start

tail -100 ./log/codis-dashboard.log.2019-05-26 注意:2019-05-26为当前日期。

详解Codis安装与部署

详解Codis安装与部署

1.4.3 启动codis-proxy

使用 codis-proxy-admin.sh 脚本启动 codis-proxy,并查看 proxy 日志确认启动是否有异常。

./admin/codis-proxy-admin.sh start

tail -100 ./log/codis-proxy.log.2019-05-26

详解Codis安装与部署

1.4.4 启动codis-server

使用 codis-server-admin.sh 脚本启动 codis-server,并查看 redis 日志确认启动是否有异常。

./admin/codis-server-admin.sh start

tail -100 /tmp/redis_6379.log

查看日志以下图所示,启动codis时报错提示:

21875:M 26 May 20:51:24.154 * Increased maximum number of open files to 10032 (it was originally set to 1024). 21875:M 26 May 20:51:24.154 # Creating Server TCP listening socket 127.0.0.1:6379: bind: Address already in use

详解Codis安装与部署

由于Redis默认端口号就是6379,因为以前(centos安装Redis)设置了本机默认开机启动Redis,因此6379已被占用。

解决方案

以下图所示,修改 /usr/local/gopath/src/github.com/CodisLabs/codis/config/redis.conf 配置文件的端口号。改成6380

详解Codis安装与部署

详解Codis安装与部署

修改端口号之后再经过 ./admin/codis-server-admin.sh start 启动codis-server。

tail -100 /tmp/redis_6379.log 查看日志以下:

详解Codis安装与部署

注意:若是redis.conf中对应的logfile也改为6380的话,查看日志就得用 tail -100 /tmp/redis_6380.log

1.4.5 启动codis-fe

使用 codis-fe-admin.sh 脚本启动 codis-fe,并查看 fe 日志确认启动是否有异常。

./admin/codis-fe-admin.sh start

tail -100 ./log/codis-fe.log.2019-05-26

1.5 经过fe添加group

经过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 按钮便可,以下图所示:

详解Codis安装与部署

1.6 经过fe初始化slot

新增的集群 slot 状态是 offline,所以咱们须要对它进行初始化(将 1024 个 slot 分配到各个 group),而初始化最快的方法可经过 fe 提供的 rebalance all slots 按钮来作,以下图所示,点击此按钮,咱们即快速完成了一个集群的搭建。

详解Codis安装与部署

1.7 经过 ansible 快速部署集群

使用 ansible 可快速在单机、多机部署多套 codis 集群。 ansible 文件夹包含了部署 codis 集群的 playbook,根据本身部署环境修改 groups_var/all 文件里参数,修改 hosts 文件添加部署的环境 IP 便可。 ansible 安装也及其简单,各部署机器无需安装任何额外的 agent,彼此之间经过 ssh 通讯。

git clone https://github.com/ansible/ansible.git -b stable-2.3 cd ./ansible source ./hacking/env-setup cd $codis_dir/ansible ansible-playbook -i hosts site.yml

v集群配置

2.1 添加Redis实例

这里再分别添加638一、6382两个Redis实例。

cp config/redis.conf config/redis6381.conf

cp config/redis.conf config/redis6382.conf

分别更新638一、6382.conf的port、pidfile和logfile

vim config/redis6381.conf

更新以后,启动新增的两个Redis实例。

./bin/codis-server ./config/redis6381.conf

./bin/codis-server ./config/redis6382.conf

详解Codis安装与部署

按照上面add server的方法(以下图)添加两个实例,注意,若未执行 ./bin/codis-server ./config/redis6381.conf ,添加时会报错。

详解Codis安装与部署

刚添加进来的默认状态是NO:ONE,以下图,点击小扳手(SLAVEOF 127.0.0.1:6380)。

详解Codis安装与部署

测试效果:

详解Codis安装与部署

注意:如上图,开启集群管理以后,6380可读可写,81和82只有只读权限。

2.2 建立新的分组

按照上面步骤继续建立7380和7381.

详解Codis安装与部署

在Codis • Dashboard(http://ip:9090/#codis-demo)中添加新的分组和server成员。

详解Codis安装与部署

v博客总结

本篇文章只聊了codis,不聊集群,若是对集群感兴趣的能够看看以前的一篇文章。《详解Redis Cluster集群》

本文"安装步骤"段落,大面积采用的是Codis github上的介绍,该段落也能够去github看看。固然本文中也加入了一些github上没有说的太清楚或者不太好理解的细节。

原本打算在本文中直接介绍springboot引入codis的,可是这篇文章已经很长,排版起来实在不方便。因此关于springboot能够在这里看。《SpringBoot进阶教程(五十九)整合Codis》

其余参考资料:


做  者:请叫我头头哥
出  处:http://www.cnblogs.com/toutou/
关于做者:专一于基础平台的项目开发。若有问题或建议,请多多赐教!
版权声明:本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文连接。
特此声明:全部评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
声援博主:若是您以为文章对您有帮助,能够点击文章右下角推荐一下。您的鼓励是做者坚持原创和持续写做的最大动力!

相关文章
相关标签/搜索