目录
1 Codis
集群
Codis
是一个代理中间件,用的是 GO
语言开发的,如下图,Codis
在系统的位置是这样的。
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的节点数量有多少,偏多的话,可以设置槽多一些。
Codis
中key
的分配算法,先是把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
执行完上述脚本后,我们在页面中可以看到数据被分配到三个节点上了,且从实例和主实例上的数据是一样的。起到了复制的功能了。