使用 Docker Compose 运行 Undermoon

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介: 使用 Docker Compose 运行 Undermoon

以下示例使用 docker 创建一个 undermoon 集群。

要求:


  • docker-compose
  • redis-cli
  • jq


在 docker-compose 中运行集群

直接下载并运行集群:


$ make docker-mem-broker-example


或者自己构建并运行 undermoon docker 镜像:


$ make docker-build-test-image
$ make docker-mem-broker


注册代理


一切就绪后,运行初始化脚本,通过 HTTP API 注册存储资源:


$ ./examples/mem-broker/init.sh


我们有 6 个可用的代理。


$ curl http://localhost:7799/api/v3/proxies/addresses


创建集群


由于每个代理都有 2 个对应的 Redis 节点,所以我们总共有 12 个节点。请注意,集群的数量只能是 4 的倍数。让我们创建一个有 4 个节点的集群。


$ curl -XPOST -H 'Content-Type: application/json' \
    http://localhost:7799/api/v3/clusters/meta/mycluster -d '{"node_number": 4}'


在连接到集群之前,您需要将这些主机添加到您的 /etc/hosts 中:


# /etc/hosts
127.0.0.1 server_proxy1
127.0.0.1 server_proxy2
127.0.0.1 server_proxy3
127.0.0.1 server_proxy4
127.0.0.1 server_proxy5
127.0.0.1 server_proxy6


让我们检查一下我们的集群。它是由一些随机选择的代理创建的。我们需要先找出它们。请注意,您需要安装 jq 命令以轻松解析以下命令的 json


# List the proxies of the our "mycluster`:
$ curl -s http://localhost:7799/api/v3/clusters/meta/mycluster | jq '.cluster.nodes[].proxy_address' | uniq
"server_proxy5:6005"
"server_proxy6:6006"


为集群 mycluster 选择上面的代理地址之一(在我的例子中是 server_proxy5:6005)并连接到它。


# Add `-c` to enable cluster mode:
$ redis-cli -h server_proxy5 -p 6005 -c
# List the proxies:
server_proxy5:6005> cluster nodes
mycluster___________d71bc00fbdddf89_____ server_proxy5:6005 myself,master - 0 0 7 connected 0-8191
mycluster___________8de73f9146386295____ server_proxy6:6006 master - 0 0 7 connected 8192-16383
# Send out some requests:
server_proxy5:6005> get a
-> Redirected to slot [15495] located at server_proxy6:6006
(nil)
server_proxy6:6006> get b
-> Redirected to slot [3300] located at server_proxy5:6005
(nil)


Great! 我们可以像使用官方的 Redis 集群一样使用我们创建的集群。


扩大规模


它实际上有 4Redis 节点。


# List the nodes of the our "mycluster`:
$ curl -s http://localhost:7799/api/v3/clusters/meta/mycluster | jq '.cluster.nodes[].address'
"redis9:6379"
"redis10:6379"
"redis11:6379"
"redis12:6379"


其中两个是 master,另外两个是 replica

让我们扩展到 8 个节点:


# Add 4 nodes
$ curl -XPATCH -H 'Content-Type: application/json' \
    http://localhost:7799/api/v3/clusters/nodes/mycluster -d '{"node_number": 4}'
# Start migrating the data
$ curl -XPOST http://localhost:7799/api/v3/clusters/migrations/expand/mycluster


现在我们有 4 个服务器代理:


$ redis-cli -h server_proxy5 -p 6005 -c
server_proxy5:6005> cluster nodes
mycluster___________d71bc00fbdddf89_____ server_proxy5:6005 myself,master - 0 0 12 connected 0-4095
mycluster___________8de73f9146386295____ server_proxy6:6006 master - 0 0 12 connected 8192-12287
mycluster___________be40fe317baf2cf7____ server_proxy2:6002 master - 0 0 12 connected 4096-8191
mycluster___________9434df4158f3c5a4____ server_proxy4:6004 master - 0 0 12 connected 12288-16383


8 个节点:


# List the nodes of the our "mycluster`:
$ curl -s http://localhost:7799/api/v3/clusters/meta/mycluster | jq '.cluster.nodes[].address'
"redis9:6379"
"redis10:6379"
"redis11:6379"
"redis12:6379"
"redis3:6379"
"redis4:6379"
"redis7:6379"
"redis8:6379"


故障转移


如果你关闭任何一个 proxyreplica 就会被提升为 master。并且只要整个 undermoon 集群有剩余的空闲 proxy,它可以自动替换失效的 proxy


# List the proxies of the our "mycluster`:
$ curl -s http://localhost:7799/api/v3/clusters/meta/mycluster | jq '.cluster.nodes[].proxy_address' | uniq
"server_proxy5:6005"
"server_proxy6:6006"
"server_proxy2:6002"
"server_proxy4:6004"


让我们在这里关闭其中一个代理,例如 server_proxy5:6005


$ docker ps | grep server_proxy5 | awk '{print $1}' | xargs docker kill


undermoon 会检测到故障,更换故障 proxy,提升新的 master,并将新的 replica 添加到新的 master


# server_proxy5 is replaced by server_proxy3
$ curl -s http://localhost:7799/api/v3/clusters/meta/mycluster | jq '.cluster.nodes[].proxy_address' | uniq
"server_proxy3:6003"
"server_proxy6:6006"
"server_proxy2:6002"
"server_proxy4:6004"


现在我们可以从 undermoon 集群中删除 server_proxy3 了。


$ curl -XDELETE http://localhost:7799/api/v3/proxies/meta/server_proxy3:6003
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
Kubernetes 监控 Docker
Docker Compose与Kubernetes的比较
【6月更文挑战第11天】本文探讨了Docker Compose与Kubernetes在容器编排中的角色。Docker Compose适合简单部署和开发环境,提供一键启动多容器的便利;而Kubernetes则适用于大规模生产环境,具备自动化运维、集群管理和负载均衡等功能。Python在容器编排中扮演重要角色,示例代码展示了如何使用Python的Docker SDK和Kubernetes客户端进行部署、扩展和日志管理。通过Python,开发者可以实现自定义监控、自动水平扩展和实时日志监控等高级功能,增强了容器编排的灵活性和自动化。
55 1
Docker Compose与Kubernetes的比较
|
4天前
|
弹性计算 运维 应用服务中间件
容器的优势,在Docker中运行Tomcat
摘要:了解Docker与虚拟机的区别:虚拟机使用Hypervisor创建完整操作系统,而容器通过namespace和cgroup实现轻量级隔离,共享主机内核。Docker启动快、资源利用率高,适合快速部署和跨平台移植。但安全性相对较低。示例介绍了如何通过Docker搜索、拉取官方Tomcat镜像并运行容器,最后验证Tomcat服务的正常运行。
|
12天前
|
运维 Kubernetes 负载均衡
docker、docker compose、k8s有什么区别?
Docker 这个东西所扮演的角色,容易理解,它是一个容器引擎,也就是说实际上我们的容器最终是由Docker创建,运行在Docker中,其他相关的容器技术都是以Docker为基础,它是我们使用其他容器技术的核心。
47 1
|
18天前
|
安全 关系型数据库 开发者
Docker Compose凭借其简单易用的特性,已经成为开发者在构建和管理多容器应用时不可或缺的工具。
Docker Compose是容器编排利器,简化多容器应用管理。通过YAML文件定义服务、网络和卷,一键启动应用环境。核心概念包括服务(组件集合)、网络(灵活通信)、卷(数据持久化)。实战中,编写docker-compose.yml,如设置Nginx和Postgres服务,用`docker-compose up -d`启动。高级特性涉及依赖、环境变量、健康检查和数据持久化。最佳实践涵盖环境隔离、CI/CD、资源管理和安全措施。案例分析展示如何构建微服务应用栈,实现一键部署。Docker Compose助力开发者高效驾驭复杂容器场景。
33 1
|
18天前
|
存储 监控 安全
Docker Compose:轻松实现容器编排的利器
【7月更文挑战第2天】 1. **基础与概念**:服务(多容器实例)、网络(灵活通信)、卷(数据持久化)和配置(安全管理)。 2. **实战指南**:安装Compose,编写`docker-compose.yml`文件,启动应用,并介绍依赖、环境变量、健康检查和数据持久化。 3. **最佳实践**:环境隔离、CI/CD集成、资源管理、日志监控、安全策略及案例分析,展示完整应用栈搭建。
38 1
|
28天前
|
NoSQL Redis Docker
使用 Docker Compose 接管现有容器的文档
使用 Docker Compose 接管现有容器的文档
28 2
|
15天前
|
关系型数据库 MySQL 数据安全/隐私保护
Docker01,相关介绍,是快速构建、运行、管理应用的工具
Docker01,相关介绍,是快速构建、运行、管理应用的工具
|
1月前
|
Docker 容器
docker 运行 elasticsearch + kibana + head 集群
docker 运行 elasticsearch + kibana + head 集群
|
22天前
|
消息中间件 监控 RocketMQ
Docker Compose 一键快速部署 RocketMQ
Docker Compose 一键快速部署 RocketMQ
34 0
|
2月前
|
前端开发 API 数据库
【Docker专栏】Docker Compose实战:编排多容器应用
【5月更文挑战第7天】Docker Compose是Docker的多容器管理工具,通过YAML文件简化多容器应用部署。它能一键启动、停止服务,保证开发、测试和生产环境的一致性。安装后,创建`docker-compose.yml`文件定义服务,如示例中的web和db服务。使用`docker-compose up -d`启动服务,通过`docker-compose ps`、`stop`、`down`和`logs`命令管理服务。
【Docker专栏】Docker Compose实战:编排多容器应用