使用docker部署redis集群

编写脚本

批量创建目录文件,编写配置文件

[root@localhost ~]# cat redis.sh
#/bin/bash
for port in $(seq 1 6); do
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >>/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

赋予权限并执行

[root@localhost ~]# chmod 777 redis.sh[root@localhost ~]# ./redis.sh#验证
[root@localhost ~]# cd /mydata/redis/
[root@localhost redis]# ll
total 0
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-1
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-2
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-3
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-4
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-5
drwxr-xr-x. 3 root root 18 Mar  8 19:43 node-6
[root@localhost redis]# cd node-1/conf/
[root@localhost conf]# cat redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.11
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes

创建6个redis节点

[root@localhost conf]# docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /mydata/redis/node-1/data:/data -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
Unable to find image 'redis:5.0.9-alpine3.11' locally
5.0.9-alpine3.11: Pulling from library/redis
cbdbe7a5bc2a: Pull complete
dc0373118a0d: Pull complete
cfd369fe6256: Pull complete
3e45770272d9: Pull complete
558de8ea3153: Pull complete
a2c652551612: Pull complete
Digest: sha256:83a3af36d5e57f2901b4783c313720e5fa3ecf0424ba86ad9775e06a9a5e35d0
Status: Downloaded newer image for redis:5.0.9-alpine3.11
4aa9af4a6fc623ac78c593d89e1e28877753c46f8c4ea4822c57760a457634f5[root@localhost conf]# docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v /mydata/redis/node-2/data:/data -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
8929cc4f724aa131433e151b8bfdb1eedf92fac369eb33386f666be0774248c8[root@localhost conf]# docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v /mydata/redis/node-3/data:/data -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
ab12911b3219a41cb4bc312ec0670341a70fcafd122af7e6d929b6eaa3a16668[root@localhost conf]# docker run -p 6374:6379 -p 16374:16379 --name redis-4 -v /mydata/redis/node-4/data:/data -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
d1b7a902129666731868004ee2c37d0c53e38eadd800da3d6aa15e399d90d517[root@localhost conf]# docker run -p 6375:6379 -p 16375:16379 --name redis-5 -v /mydata/redis/node-5/data:/data -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
21e026df6569c13e2c0b662918d4e185363f666d74ddf1bd0bcce7ef1afd3f01[root@localhost conf]# docker run -p 6376:6379 -p 16376:16379 --name redis-6 -v /mydata/redis/node-6/data:/data -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
1ed447b3c1493a54e2e98f561d530b6692436e0f2ba94b5233fcdd13f280ef46[root@localhost conf]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
1ed447b3c149   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   2 seconds ago        Up 2 seconds        0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp   redis-6
21e026df6569   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   21 seconds ago       Up 21 seconds       0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp   redis-5
d1b7a9021296   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   44 seconds ago       Up 43 seconds       0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp   redis-4
ab12911b3219   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp   redis-3
8929cc4f724a   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp   redis-2
4aa9af4a6fc6   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes        0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp   redis-1

进入redis-1节点,创建集群

[root@localhost conf]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof  nodes.conf#创建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.
38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379slots:[0-5460] (5461 slots) master
M: 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379slots:[5461-10922] (5462 slots) master
M: 22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379slots:[10923-16383] (5461 slots) master
S: 3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379replicates 22966010cbe3258f73b6eef88ed56ae43b607a96
S: 71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379replicates df7140c32a3ff89276d01318aebee214540e6207
S: 032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379replicates 3c2077ccf83262ec029ad6edbb10eb8411a0fba1
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> 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.38.0.11:6379)
M: df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
M: 22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: 032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379slots: (0 slots) slavereplicates 3c2077ccf83262ec029ad6edbb10eb8411a0fba1
S: 3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379slots: (0 slots) slavereplicates 22966010cbe3258f73b6eef88ed56ae43b607a96
S: 71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379slots: (0 slots) slavereplicates df7140c32a3ff89276d01318aebee214540e6207
M: 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

连接集群

/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:170
cluster_stats_messages_pong_sent:177
cluster_stats_messages_sent:347
cluster_stats_messages_ping_received:172
cluster_stats_messages_pong_received:170
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:347
127.0.0.1:6379>

查看集群信息

127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:170
cluster_stats_messages_pong_sent:177
cluster_stats_messages_sent:347
cluster_stats_messages_ping_received:172
cluster_stats_messages_pong_received:170
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:347

查看节点信息,三主三从

127.0.0.1:6379> cluster nodes
22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379@16379 master - 0 1709960670173 3 connected 10923-16383
032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379@16379 slave 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 0 1709960671000 6 connected
3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379@16379 slave 22966010cbe3258f73b6eef88ed56ae43b607a96 0 1709960669171 4 connected
71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379@16379 slave df7140c32a3ff89276d01318aebee214540e6207 0 1709960671178 5 connected
df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379@16379 myself,master - 0 1709960668000 1 connected 0-5460
3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379@16379 master - 0 1709960670000 2 connected 5461-10922

高可用测试

set a b两个值

127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
#发现当前处理的是172.38.0.13:6379,是第三个节点master

停止redis-3容器运行

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                                                                      NAMES
1ed447b3c149   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   12 minutes ago   Up 12 minutes   0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp   redis-6
21e026df6569   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   12 minutes ago   Up 12 minutes   0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp   redis-5
d1b7a9021296   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp   redis-4
ab12911b3219   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp   redis-3
8929cc4f724a   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes   0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp   redis-2
4aa9af4a6fc6   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes   0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp   redis-1[root@localhost ~]# docker stop redis-3
redis-3[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                                                                      NAMES
1ed447b3c149   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp   redis-6
21e026df6569   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp   redis-5
d1b7a9021296   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes   0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp   redis-4
8929cc4f724a   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes   0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp   redis-2
4aa9af4a6fc6   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   15 minutes ago   Up 15 minutes   0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp   redis-1

get a获取信息

发现是get到a了,虽然redis3已经停止运行了,但redis4上获取到了

127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> cluster nodes

 高可用实现

redis-3节点停掉之后,redis-4节点变成master节点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/735447.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

记录西门子:IO隔离SCL编程

在PLC变量中创建IO输入输出 在PLC类型中创建输入和输出&#xff0c;并将PLC变量的输入输出名称复制过来 创建一个FC块或者FB块 创建一个DB块 MAIN主程序中&#xff1a;

【UVM_phase objection_2024.03.08

phase 棕色&#xff1a;function phase 不消耗仿真时间 绿色&#xff1a;task phase 消耗仿真时间 run_phase与右边的phase并行执行&#xff0c;右边的phase&#xff08;run_time phase&#xff09;依次执行&#xff1a; List itemreset_phase对DUT进行复位&#xff0c;初始…

24 深度卷积神经网络 AlexNet【李沐动手学深度学习v2课程笔记】(备注:含AlexNet和LeNet对比)

目录 1. 深度学习机器学习的发展 1.1 核方法 1.2 几何学 1.3 特征工程 opencv 1.4 Hardware 2. AlexNet 3. 代码 1. 深度学习机器学习的发展 1.1 核方法 2001 Learning with Kernels 核方法 &#xff08;机器学习&#xff09; 特征提取、选择核函数来计算相似性、凸优…

总结:Spring创建Bean循环依赖问题与@Lazy注解使用详解

总结&#xff1a;Spring创建Bean循环依赖问题与Lazy注解使用详解 一前提知识储备&#xff1a;1.Spring Bean生命周期机制&#xff08;IOC&#xff09;2.Spring依赖注入机制&#xff08;DI&#xff09;&#xff08;1&#xff09;Autowired注解标注属性set方法注入&#xff08;2&…

步进电机的神秘世界:斩波与细分算法的探索之旅

欢迎来到步进电机的奇妙领域&#xff01;今天&#xff0c;我们将一同揭开斩波和细分算法的神秘面纱&#xff0c;并探讨它们的编程实现。准备好踏上这趟充满惊喜的探索之旅了吗&#xff1f;让我们一起出发吧&#xff01; 首先&#xff0c;让我们了解一下步进电机斩波的原理。为了…

爬虫与DataFrame对象小小结合

import pandas as pd import requests from lxml import etree #数据请求 url"https://www.maigoo.com/brand/list_1715.html" headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari…

windows安装ElasticSearch踩坑记

ElasticSearch是一个开源的分布式搜索和分析引擎。它提供实时分布式搜索功能&#xff0c;可以索引和搜索大量的结构化和非结构化数据。Elasticsearch以其速度、可伸缩性和处理复杂查询的能力而闻名。它常用于日志分析、全文搜索、文档搜索和数据分析等领域。使用ElasticSearch的…

稀碎从零算法笔记Day14-LeetCode:同构字符串

题型&#xff1a;字符串、哈希表 链接&#xff1a;205. 同构字符串 - 力扣&#xff08;LeetCode&#xff09; 来源&#xff1a;LeetCode 题目描述 给定两个字符串 s 和 t &#xff0c;判断它们是否是同构的。 如果 s 中的字符可以按某种映射关系替换得到 t &#xff0c;那…

AI相关的实用工具分享

AI实用工具大赏&#xff1a;赋能科研与生活&#xff0c;探索AI的无限可能 前言 在数字化浪潮汹涌而至的今天&#xff0c;人工智能&#xff08;AI&#xff09;已经渗透到我们生活的方方面面&#xff0c;无论是工作还是生活&#xff0c;都在悄然发生改变。AI的崛起不仅为我们带…

AHU 算法分析 实验四 动态规划

实验四&#xff1a;动态规划 实验目的 • 理解动态规划的基本思想&#xff0c;理解动态规划算法的两个基本要素最 优子结构性质和子问题的重叠性质。 • 熟练掌握典型的动态规划问题。 • 掌握动态规划思想分析问题的一般方法&#xff0c;对较简单的问题能正确 分析&#x…

网络故障基本判断方法

1&#xff09;电脑上使用winR键&#xff0c;打开运行窗口&#xff0c;输入CMD命令&#xff0c;点击回车键 2&#xff09;在弹出的CMD运行窗口中输入ipconfig命令 通过该命令可以查看本机的IP地址&#xff0c;子网掩码和网关等信息。确认电脑中所有网卡配置是否正确。 3&…

llc稳压基本思路2

这套控制思路实际上就是开关电源中常见的反馈系统 大致思路就是&#xff0c;如果由于其他参数波动引起了输出电压偏差&#xff0c;我们可以直接监测这个输出电压&#xff0c;然后根据输出电压与目标电压值的偏差信号传输给芯片&#xff0c;然后芯片来改变频率&#xff0c;从而改…

前端学习之行内和块级标签

行内标签 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>span</title> </head> <body><!-- 行内标签特点&#xff1a;1、不换行,一行可以放多个2、默认宽度内容撑开代表&#…

Googlenet网络架构

原文链接&#xff1a;[1409.4842v1] Going Deeper with Convolutions (arxiv.org) 图源&#xff1a;深入解读GoogLeNet网络结构&#xff08;附代码实现&#xff09;-CSDN博客 表截自原文 以下&#x1f4d2;来自博客深入解读GoogLeNet网络结构&#xff08;附代码实现&#xff0…

【顶刊|修正】多区域综合能源系统热网建模及系统运行优化【复现+延伸】

目录 主要内容 部分代码 结果一览 下载链接 主要内容 该程序复现《多区域综合能源系统热网建模及系统运行优化》模型并进一步延伸&#xff0c;基于传热学的基本原理建立了区域热网能量传输通用模型&#xff0c;对热网热损方程线性化实现热网能量流建模&#xff0…

使用docker-compose编排ruoyi项目

目录 一、开始部署 1.拉取ruoyi代码 2.拉取node镜像 3.拉取maven镜像 4.在/root/ruoyi/java下写一个Dockerfile用于后端Java环境 5.拉取MySQL&#xff0c;Redis&#xff0c;Nginx镜像 6.在/root/java目录下写一个nginx.conf 7.在/root/ruoyi目录下写docker-compose.yml文…

Idea导入Maven项目

方法一&#xff1a;使用Maven面板 方法二&#xff1a;在项目结构中设置&#xff0c;在最后一步中选择pom.xml。

前端学习之列表标签

目录 有序列表 结果 无序标签 结果 数据标签 结果 有序列表 &#xff08;注&#xff1a;注释是解释&#xff09; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Document</title> </…

【JavaEE初阶系列】——计算机是如何工作的

目录 &#x1f388;冯诺依曼体系 ❗外存和内存的概念 ❗CPU中央处理器—人类当今科技领域巅峰之作之一 &#x1f6a9;如何衡量cpu &#x1f6a9;指令&#xff08;Instruction&#xff09; &#x1f388;操作系统&#xff08;Operating System&#xff09; &#x1f388;…

关于GPU显卡的介绍

一.关于英伟达历代产品架构 显卡是一种计算机硬件设备,也被称为显示适配器或图形处理器。目前的硬件部分主要由主板、芯片、存储器、散热器&#xff08;散热片、风扇&#xff09;等部分。显卡的主要芯片是显卡的主要处理单元。显卡上也有和计算机存储器相似的存储器&#xff0…