codis代理redis集群

目录

1 Codis 集群

2 go语言的部署

2.1 go语言安装包获取

2.2 解压配置环境变量

2.3 验证go语言是否安装正确

3 codis的部署

3.1 codis编译

3.2 启动服务

3.3 页面中操作

3.4 验证集群


1 Codis 集群

Codis 是一个代理中间件,用的是 GO 语言开发的,如下图,Codis 在系统的位置是这样的。

image

Codis分为四个部分,分别是Codis Proxy (codis-proxy)Codis Dashboard (codis-config)Codis Redis (codis-server)ZooKeeper/Etcd.

Codis就是起着一个中间代理的作用,能够把所有的Redis实例当成一个来使用,在客户端操作着SDK的时候和操作Redis的时候是一样的,没有差别。

因为Codis是一个无状态的,所以可以增加多个Codis来提升QPS,同时也可以起着容灾的作用。

Codis 分片原理

Codis中,Codis会把所有的key分成1024个槽,这1024个槽对应着的就是Redis的集群,这个在Codis中是会在内存中维护着这1024个槽与Redis实例的映射关系。这个槽是可以配置,可以设置成 2048 或者是4096个。看你的Redis的节点数量有多少,偏多的话,可以设置槽多一些。

Codiskey的分配算法,先是把key进行CRC32 后,得到一个32位的数字,然后再hash%1024后得到一个余数,这个值就是这个key对应着的槽,这槽后面对应着的就是redis的实例。

2 go语言的部署

2.1 go语言安装包获取

wget https://dl.google.com/go/go1.12.6.linux-amd64.tar.gz

2.2 解压配置环境变量

$ tar -xf go1.12.6.linux-amd64.tar.gz -C /opt

vim /etc/profile

#添加以下内容
export GO_HOME=/opt/go
export PATH=${GO_HOME}/bin:$PATH


# 立即生效
source /etc/profile

2.3 验证go语言是否安装正确

编辑hello.go程序,内容如下:

[root@wyl01 opt]#  vim hello.go
# 添加以下内容
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}


[root@wyl01 opt]# go run hello.go 
Hello, World!

如果出现上述结果,表示go语言安装正确。

3 codis的部署

3.1 codis编译

创建目录,然后解压codis-release3.2.zip,最后编译。

[root@wyl01 opt]# mkdir -p $GO_HOME/src/github.com/CodisLabs/codis
# 把解压后的所有文件和目录,拷贝到上面的目录中
[root@wyl01 opt]# unzip codis-release3.2.zip
[root@wyl01 opt]# cp -r codis-release3.2/* $GO_HMOE/src/github.com/CodisLabs/codis
[root@wyl01 codis]# cd $GO_HOME/src/github.com/CodisLabs/codis
# 注意编译的路径
[root@wyl01 codis]# make   

make -j4 -C extern/redis-3.2.11/
make[1]: Entering directory `/opt/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11'
cd src && make all
make[2]: Entering directory `/opt/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11/src'

Hint: It's a good idea to run 'make test' ;)

make[2]: Leaving directory `/opt/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11/src'
make[1]: Leaving directory `/opt/go/src/github.com/CodisLabs/codis/extern/redis-3.2.11'
fatal: Not a git repository (or any of the parent directories): .git
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-ha ./cmd/ha
go build -i -o bin/codis-fe ./cmd/fe

复制目录bin、admin、config到 /opt/codis目录下

[root@wyl01 codis]# cp -r bin/ admin/ config/  /opt/codis
[root@wyl01 codis]# cd bin/
[root@wyl01 bin]# ll
total 144292
drwxr-xr-x. 4 root root      117 Jun 27 13:25 assets
-rwxr-xr-x. 1 root root 26130144 Jun 27 13:25 codis-admin
-rwxr-xr-x. 1 root root 27535168 Jun 27 13:25 codis-dashboard
-rwxr-xr-x. 1 root root 25040240 Jun 27 13:25 codis-fe
-rwxr-xr-x. 1 root root 23585760 Jun 27 13:25 codis-ha
-rwxr-xr-x. 1 root root 29686760 Jun 27 13:25 codis-proxy
-rwxr-xr-x. 1 root root  5365896 Jun 27 13:25 codis-server
-rwxr-xr-x. 1 root root  2432104 Jun 27 13:25 redis-benchmark
-rwxr-xr-x. 1 root root  2586184 Jun 27 13:25 redis-cli
-rwxr-xr-x. 1 root root  5365896 Jun 27 13:25 redis-sentinel
-rw-r--r--. 1 root root       97 Jun 27 13:25 version

3.2 启动服务

启动dashboard服务

[root@wyl01 codis]# ./admin/codis-dashboard-admin.sh start

# 查看进程或者日志
[root@wyl01 codis]# ps -ef|grep codis-dashboard
root       3380      1  0 14:23 pts/1    00:00:56 /opt/codis/admin/../bin/codis-dashboard --config=/opt/codis/admin/../config/dashboard.toml --log=/opt/codis/admin/../log/codis-dashboard.log --log-level=INFO --pidfile=/opt/codis/admin/../bin/codis-dashboard.pid
root       5734   2698  0 16:56 pts/1    00:00:00 grep --color=auto codis-dashboard

 启动proxy服务

[root@wyl01 codis]# ./admin/codis-proxy-admin.sh start

# 查看进程或者日志
[root@wyl01 codis]# ps -ef|grep proxy
root       3401      1  0 14:23 pts/1    00:00:35 /opt/codis/admin/../bin/codis-proxy --config=/opt/codis/admin/../config/proxy.toml --dashboard=127.0.0.1:18080 --log=/opt/codis/admin/../log/codis-proxy.log --log-level=INFO --ncpu=4 --pidfile=/opt/codis/admin/../bin/codis-proxy.pid
root       5784   2698  0 16:59 pts/1    00:00:00 grep --color=auto proxy

 启动fe页面服务

[root@wyl01 codis]# ./admin/codis-fe-admin.sh start

# 查看进程或者日志

[root@wyl01 codis]# ps -ef|grep fe
root       3417      1  0 14:23 pts/1    00:00:02 /opt/codis/admin/../bin/codis-fe --assets-dir=/opt/codis/admin/../bin/assets --filesystem=/tmp/codis --log=/opt/codis/admin/../log/codis-fe.log --pidfile=/opt/codis/admin/../bin/codis-fe.pid --log-level=INFO --listen=0.0.0.0:9090
root       5839   2698  0 17:02 pts/1    00:00:00 grep --color=auto fe

 打开页面可以看到codis的dashboard页面

 启动codis-server服务

[root@wyl01 codis]# cp redis.conf  redis6379.conf
[root@wyl01 codis]# cp redis.conf  redis6380.conf
# 查看redis6379.confw文件内容
[root@wyl01 codis]# sed -n '/^[^#]/p' config/redis6379.conf 
bind 192.168.52.128  # 修改
protected-mode yes
port 6379            # 修改
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /tmp/redis_6379.pid    # 修改
loglevel notice
logfile "/tmp/redis_6379.log"  # 修改
databases 16
save 900 1
save 300 10
save 60 10000
...
hz 10
aof-rewrite-incremental-fsync yes

#启动服务
[root@wyl01 codis]# ./bin/codis-server config/redis6379.conf
[root@wyl01 codis]# ./bin/codis-server config/redis6380.conf

3.3 页面中操作

 创建一个group,操作如下,在输入框中输入1,点击左侧New Group按钮,则会在下方出现一行数据。

添加codis-server实例,在输入框中输入codis-server实例选择加入的一个组中,加入后才可以在Slots模块中划分槽。

我们再把从实例添加进去,然后和第一个实例形成主从关系.

 我们将codis目录拷贝到其他两台机器上,然后修改配置文件后,启动服务。添加codis-server实例,这里为了方便对比,我们的主从是在一台机器上,实际坏境中不要这么去做。重复上面的操作,可以看到一共三个组,每个组里都有一个主从。

此时,所有的slots都处在offline状态,需要进行分配slots。上面截图中已经有数据,分配过slots了。那我们来看看如何去分配slots,我们把从0到400分配给1组,401到800给2组,800到1023给3组。如下图所示:

3.4 验证集群

[root@wyl01 codis]# cat redis.sh 
#!/bin/bash

for (( i=0;i<=500;i++ ))
do  
/opt/codis/bin/redis-cli  -p 19000 <<EOF 
   set wyl$i '$i'
EOF
done

 执行完上述脚本后,我们在页面中可以看到数据被分配到三个节点上了,且从实例和主实例上的数据是一样的。起到了复制的功能了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值