本篇内容介绍了“Docker-Compose如何搭建Redis集群”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
3主+3从
由于仅用于测试,故我这里只用1台服务器进行模拟
redis列表
在server上创建一个目录用于存放redis集群部署文件。这里我放的路径为/root/redis-cluster
在/opt/docker/redis-cluster目录下创建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夹
mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6} #创建持久化目录 mkdir -p /opt/docker/redis-cluster/redis-1/data mkdir -p /opt/docker/redis-cluster/redis-2/data mkdir -p /opt/docker/redis-cluster/redis-3/data mkdir -p /opt/docker/redis-cluster/redis-4/data mkdir -p /opt/docker/redis-cluster/redis-5/data mkdir -p /opt/docker/redis-cluster/redis-6/data
cluster-enabled yes # 开启集群 cluster-config-file nodes.conf # 集群配置文件 cluster-node-timeout 5000 # 集群节点多少时间未响应视为该节点丢失 appendonly yes port 6379 # redis监听端口 masterauth passwd123 #设置master节点密码 requirepass passwd123 #设置密码
注意:port值不能都为6379,根据上面redis列表设置的端口号,依次给redis-1 ~ redis-6设置6379~6384端口号
这里直接使用daocloud提供的redis镜像地址daocloud.io/library/redis:6.0.4
在/root/redis-cluster文件夹下创建docker-compose.yml文件。
docker-compose.yml文件内容如下:
version: '3.1' services: # redis1配置 redis1: image: daocloud.io/library/redis:6.0.4 container_name: redis-1 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-1/data:/data - /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis2配置 redis2: image: daocloud.io/library/redis:6.0.4 container_name: redis-2 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-2/data:/data - /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis3配置 redis3: image: daocloud.io/library/redis:6.0.4 container_name: redis-3 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-3/data:/data - /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis4配置 redis4: image: daocloud.io/library/redis:6.0.4 container_name: redis-4 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-4/data:/data - /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis5配置 redis5: image: daocloud.io/library/redis:6.0.4 container_name: redis-5 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-5/data:/data - /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis6配置 redis6: image: daocloud.io/library/redis:6.0.4 container_name: redis-6 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-6/data:/data - /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
启动容器,执行命令:
#启动容器 docker-compose -f xxx.yaml up -d
随便找一个容器进入,这里我选择redis-1进入。 在进入容器后,输入如下命令开启集群:
redis-cli --cluster create 192.168.66.180:6379 \ 192.168.66.180:6380 \ 192.168.66.180:6381 \ 192.168.66.180:6382 \ 192.168.66.180:6383 \ 192.168.66.180:6384 \ --cluster-replicas 1
出现下图即为成功。
使用redis-cli命令,连接集群中任意节点。(随便找一台能ping通集群所在IP的电脑,这里我用的本机进行测试)
redis-cli -c -h 192.168.66.180 -p 6379 192.168.66.180:6379> cluster info
显示为如下图,即为集群健康状态
cluster nodes
注意看图中的slave,master,myself等关键字。
192.168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380>
注意:这里根据切片自动切换到了该数据分片所在的节点上,所以下面可以看到连接的节点变为了192.168.1.4:6380
get test
168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380> get test "hello world" 192.168.66.180:6380>
这里因为就是在该数据所在分片上进行的查询,所以连接节点没有改变。
#为redis.conf文件添加如下配置。这里设置密码为123456 masterauth 123456 requirepass 123456
#为docker-compose.yml中每个容器添加如下配置: environment: - REDISCLI_AUTH=123456
redis-cli -c -h 192.168.66.180 -p 6379 -a 123456
“Docker-Compose如何搭建Redis集群”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。