前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Docker Compose 运行 Undermoon

使用 Docker Compose 运行 Undermoon

作者头像
为少
发布2022-03-31 19:21:18
5820
发布2022-03-31 19:21:18
举报
文章被收录于专栏:黑客下午茶黑客下午茶

项目地址:https://github.com/doyoubi/undermoon

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

要求:

  • docker-compose
  • redis-cli
  • jq
在 docker-compose 中运行集群

直接下载并运行集群:

代码语言:javascript
复制
$ make docker-mem-broker-example

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

代码语言:javascript
复制
$ make docker-build-test-image
$ make docker-mem-broker
注册代理

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

代码语言:javascript
复制
$ ./examples/mem-broker/init.sh

我们有 6 个可用的代理。

代码语言:javascript
复制
$ curl http://localhost:7799/api/v3/proxies/addresses
创建集群

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

代码语言:javascript
复制
$ curl -XPOST -H 'Content-Type: application/json' \
    http://localhost:7799/api/v3/clusters/meta/mycluster -d '{"node_number": 4}'

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

代码语言:javascript
复制
# /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

代码语言:javascript
复制
# 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)并连接到它。

代码语言:javascript
复制
# 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 节点。

代码语言:javascript
复制
# 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 个节点:

代码语言:javascript
复制
# 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 个服务器代理:

代码语言:javascript
复制
$ 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 个节点:

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
$ docker ps | grep server_proxy5 | awk '{print $1}' | xargs docker kill

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

代码语言:javascript
复制
# 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 了。

代码语言:javascript
复制
$ curl -XDELETE http://localhost:7799/api/v3/proxies/meta/server_proxy3:6003
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 黑客下午茶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在 docker-compose 中运行集群
  • 注册代理
  • 创建集群
  • 扩大规模
  • 故障转移
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档