发布时间:2020-03-08 13:48:47编辑:admin阅读(2547)
redis集群对于很多人来说非常熟悉,在前些日子,我也有一位大兄弟也发布过一篇关于在阿里云(centOS7)上搭建redis 集群的文章,虽然集群搭建的文章在网上很多,我比较喜欢这篇文章的地方是他在搭建过程中,指出一些我们会遇到而别人没有指出的问题。
在这里,我同样带给大家一遍关于阿里云(centOS7)redis集群搭建的文章,但是这次搭建有所不同的是,它是在docker上搭建的redis集群。
采用单机部署
操作系统 | ip | docker版本 | redis版本 |
centos 7.6 | 192.168.31.150 | 19.03.5 | 4.0.10 |
docker pull redis:4.0.10docker pull ruby
说明:ruby用是来做加入集群的用的,仅一次使用。
mkdir -p /data/redis-cluster vi /data/redis-cluster/redis-cluster.tmpl
内容如下:
port ${PORT} protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 192.168.31.150 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly no
注意:ip地址修改为本机ip
配置文件解释
port ${PORT} ##节点端口 protected-mode no #关闭protected-mode模式,外部网络可以直接访问 cluster-enabled yes ##cluster集群模式 cluster-config-file nodes.conf ##集群配置名 cluster-node-timeout 5000 ##超时时间 cluster-announce-ip 192.168.31.150 ##实际为各节点网卡分配ip 先用上网关ip代替 cluster-announce-port ${PORT} ##节点映射端口 cluster-announce-bus-port 1${PORT} ##节点总线端 appendonly no ##持久化模式
备注:此模版文件为集群节点通用文件 其中${PORT} 将读取命令行变量
docker network create redis-net
生成conf和data目录,并生成配置信息
for port in `seq 7000 7005`; do \ mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; \ done
共生成6个文件夹,从7000到7005,每个文件夹下包含data和conf文件夹,同时conf里面有redis.conf配置文件
创建6个redis容器
for port in `seq 7000 7005`; do \ docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \ -v /data/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \ -v /data/redis-cluster/${port}/data:/data \ --restart always --name redis-${port} --net redis-net \ --sysctl net.core.somaxconn=1024 redis:4.0.10 redis-server /usr/local/etc/redis/redis.conf; \ done
至此,通过命令docker ps可查看刚刚生成的6个容器信息
通过启动ruby来实现集群
echo yes | docker run -i --rm --net redis-net ruby sh -c '\ gem install redis \ && wget http://download.redis.io/releases/redis-4.0.10.tar.gz \ && tar xvf redis-4.0.10.tar.gz && cd redis-4.0.10/src \ && ruby redis-trib.rb create --replicas 1 \ '"$(for port in `seq 7000 7005`; do \ echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port} ' ' ; \ done)"
它其实就是调用了命令:
ruby redis-trib.rb create --replicas 1 172.18.0.2:7000 172.18.0.3:7001 172.18.0.4:7002 172.18.0.5:7003 172.18.0.6:7004 172.18.0.7:7005
输出如下:
... >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.. >>> Performing Cluster Check (using node 172.18.0.2:7000) M: a779c88a19a2582620d0a1ad3f90431f92f91af5 172.18.0.2:7000 slots:0-5460 (5461 slots) master additional replica(s) M: 880a2b0671d0ff15192e758ff7204ccf15f52cd0 192.168.31.150:7002 slots:10923-16383 (5461 slots) master additional replica(s) S: 3b04d5b6bcd86efaaceb2002087729bcb9bb1c07 192.168.31.150:7005 slots: (0 slots) slave replicates 5e33b75081c76bfc677e1941fdc2577709280f77 M: 5e33b75081c76bfc677e1941fdc2577709280f77 192.168.31.150:7001 slots:5461-10922 (5462 slots) master additional replica(s) S: dad167c467186794e5095b58a0f43e443ff692fb 192.168.31.150:7003 slots: (0 slots) slave replicates 880a2b0671d0ff15192e758ff7204ccf15f52cd0 S: fa3775cbcdd0512de4fe2c208de1046b5369e0b3 192.168.31.150:7004 slots: (0 slots) slave replicates a779c88a19a2582620d0a1ad3f90431f92f91af5 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
能看到3主3从,说明集群成功了。
查看集群信息
# docker exec -it redis-7000 /bin/bash -c "redis-cli -p 7000 cluster nodes" 880a2b0671d0ff15192e758ff7204ccf15f52cd0 192.168.31.150:7002@17002 master - 0 1573806315290 3 connected 10923-16383 3b04d5b6bcd86efaaceb2002087729bcb9bb1c07 192.168.31.150:7005@17005 slave 5e33b75081c76bfc677e1941fdc2577709280f77 0 1573806316296 6 connected 5e33b75081c76bfc677e1941fdc2577709280f77 192.168.31.150:7001@17001 master - 0 1573806315000 2 connected 5461-10922 dad167c467186794e5095b58a0f43e443ff692fb 192.168.31.150:7003@17003 slave 880a2b0671d0ff15192e758ff7204ccf15f52cd0 0 1573806316800 4 connected fa3775cbcdd0512de4fe2c208de1046b5369e0b3 192.168.31.150:7004@17004 slave a779c88a19a2582620d0a1ad3f90431f92f91af5 0 1573806316000 5 connected a779c88a19a2582620d0a1ad3f90431f92f91af5 192.168.31.150:7000@17000 myself,master - 0 1573806315000 1 connected 0-5460
# docker exec -it redis-7000 /bin/bash 127.0.0.1:7000> set a 123 -> Redirected to slot [15495] located at 192.168.31.150:7002 OK 192.168.31.150:7002> get a “123” 192.168.31.150:7002>
本文参考链接:
https://www.cnblogs.com/lianggp/articles/8136222.html
https://blog.csdn.net/qq_22211217/article/details/80436996
注意:参考链接中的redis-trib.rb,不能使用了。已经更新了!
所以,必须得用redis源码中的redis-trib.rb
47570
45927
36869
34434
29043
25681
24531
19686
19206
17718
5542°
6120°
5654°
5708°
6663°
5450°
5454°
5961°
5935°
7266°