使用nomad && consul && fabio 创建简单的微服务系统
具体每个组件的功能就不详细说明了 nomad 一个调度工具,consul 一个服务发现,健康检查多数据中心支持的工具
fabio 一个基于consul的负载均衡&&动态路由工具,对于集成的话,很简单就是定义简单的service tag 即可,格式为:
urlprefix-/ 参考nomad 测试项目 https://github.com/rongfengliang/nomad-demo-template
nomad 安装
比较简单
https://releases.hashicorp.com/nomad/0.8.4/nomad_0.8.4_linux_amd64.zip
consul
比较简单
https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip
fabio
https://github.com/fabiolb/fabio/releases
下载,配置环境变量即可
启动服务
- consul
dev 模式
consul agent --dev
- nomad
dev 模式
nomad agent --dev
- fabio
- consul 配置
配置consul 地址,使用默认的localhost:8500
一个参考配置
# These two lines are example of running fabio with HTTPS certificates
#proxy.cs = cs=lb;type=file;cert=/opt/fabio/certs.d/mydomain_com.ca-bundle.crt;key=/opt/fabio/certs.d/mydomain_com.key
#proxy.addr = :443;cs=lb;tlsmin=tls11;tlsmax=tls12;tlsciphers="0xc02f,0x9f,0xc030,0xc028,0xc014,0x6b,0x39,0x009d,0x0035",# :80
proxy.addr = :9999
proxy.header.tls = Strict-Transport-Security
proxy.header.tls.value = "max-age=63072000; includeSubDomains"
ui.addr = 10.185.20.180:9998
ui.access = ro
runtime.gogc = 800
log.access.target = stdout
log.access.format = - - [] "" ".Referer" ".User-Agent" "" "" "" ""
log.access.level = INFO
registry.consul.addr = 10.185.20.180:8500
proxy.maxconn = 20000
- 部署简单nomad 服务
job "website" {
datacenters = ["dc1"]
type = "service"
update {
max_parallel = 1
min_healthy_time = "10s"
healthy_deadline = "3m"
progress_deadline = "10m"
auto_revert = false
canary = 0
}
migrate {
max_parallel = 1
health_check = "checks"
min_healthy_time = "10s"
healthy_deadline = "5m"
}
group "website" {
count = 1
restart {
attempts = 2
interval = "30m"
delay = "15s"
mode = "fail"
}
ephemeral_disk {
size = 300
}
task "graphql" {
driver = "docker"
config {
image = "dalongrong/mygraphql"
port_map {
website = 80
}
}
resources {
cpu = 500 # 500 MHz
memory = 256 # 256MB
network {
mbits = 10
port "website" {}
}
}
service {
name = "graphql-website"
tags = ["global", "website","graphql","urlprefix-/"]
port = "website"
check {
name = "alive"
type = "http"
interval = "10s"
path = "/"
timeout = "2s"
}
}
}
}
}
参考界面
- nomad
- consul
- fabio
- 进行实例缩放
group "website" {
# The "count" parameter specifies the number of the task groups that should
# be running under this group. This value must be non-negative and defaults
# to 1.
count = 3
效果
总结
三个工具集成起来,开发部署,缩放,版本管理,灰度,如果集成了openresty、 coredns、envoy 的话如更简单。
参考资料
https://www.nomadproject.io/docs/
https://fabiolb.net/quickstart/
https://www.consul.io/docs/index.html
https://github.com/rongfengliang/nomad-demo-template
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)