您是否曾经希望容器编排能够比静态依赖链更灵活,但又比 kubernetes 更简单?满足 pnr(提示和响应) - 一种配置驱动的方法,利用 go 强大的平台抽象功能根据实际的就绪状态而不是简单的依赖关系来编排容器。
go 平台抽象的力量
在深入探讨 pnr 之前,让我们先了解一下为什么 go 特别适合跨平台容器编排:
-
统一的 docker api 接口:go 的 docker 客户端库通过特定于平台的套接字连接提供跨 windows、linux 和 macos 的一致接口:
- unix系统使用/var/run/docker.sock
- windows 使用命名管道
- client.newclientwithopts() 函数会自动处理这些差异
-
原生并发支持:go 的 goroutine 和通道可实现高效的容器监控:
- 每个容器的健康检查同时运行
- 意图循环协调多个容器而不阻塞
- 互斥保护状态更新可防止竞争情况
-
跨平台网络处理:go 的 net 包抽象了特定于平台的网络详细信息:
- tcp 健康检查在不同操作系统上的工作方式相同
- http 客户端处理特定于平台的 dns 解析
- 无论平台如何,端口绑定都使用一致的语法
核心概念:配置胜于代码
pnr 通过三个关键组件来编排容器:
- 域配置(json)
- 与平台无关的健康检查
- 运行时状态管理
让我们看看典型的 web 堆栈的实际效果:mongodb、api 服务器和 web 客户端。
域配置结构
{
"name": "dev_stack",
"cpuxs": {
"stack_startup": {
"design_chunks": [
{
"name": "mongodb",
"gatekeeper": {
"system_ready": {
"prompt": "is system ready?",
"response": ["yes"],
"tv": "y"
}
},
"flowout": {
"mongodb_ready": {
"prompt": "is mongodb ready?",
"response": ["yes"],
"tv": "y"
}
},
"health_check": {
"type": "tcp",
"port_key": "27017",
"timeout_seconds": 2,
"status_mapping": {
"success": {
"key": "mongodb_ready",
"response": ["yes"],
"tv": "y"
},
"failure": {
"key": "mongodb_ready",
"response": ["no"],
"tv": "n"
}
}
},
"container": {
"name": "pnr_mongodb",
"image": "mongo:latest",
"ports": {
"27017": "27017"
}
}
}
]
}
}
}
与平台无关的容器管理
pnr 的核心是其与平台无关的容器管理。其工作原理如下:
func (il *containerintentionloop) execute() error {
// create platform-specific network
_, err := il.dockerclient.networkcreate(il.ctx, "pnr_network", types.networkcreate{})
if err != nil {
return fmt.errorf("failed to create network: %v", err)
}
for {
// update runtime state
if err := il.updatertstatefromruntime(); err != nil {
return err
}
allcompleted := true
anyexecuting := false
// process each container
for i := range il.cpux.designchunks {
chunk := &il.cpux.designchunks[i]
// container state machine
switch chunk.status {
case "completed":
continue
case "executing":
anyexecuting = true
allcompleted = false
if il.checkchunkcompletion(chunk) {
chunk.status = "completed"
}
case "", "ready":
allcompleted = false
if il.checkgatekeeper(chunk) {
if err := il.startcontainer(chunk); err != nil {
return err
}
chunk.status = "executing"
anyexecuting = true
}
}
}
// check termination conditions
if allcompleted {
return nil
}
if !anyexecuting && !allcompleted {
return fmt.errorf("no progress possible - execution stalled")
}
time.sleep(5 * time.second)
}
}
跨平台健康检查
pnr 使用 go 的标准库实现平台无关的健康检查:
func (il *containerintentionloop) checkchunkcompletion(chunk *designchunk) bool {
// platform-agnostic container status check
isrunning, err := il.iscontainerrunning(chunk.container.name)
if !isrunning {
il.updatechunkstatus(chunk, false)
return false
}
// health check based on configuration
status := false
switch chunk.healthcheck.type {
case "tcp":
addr := fmt.sprintf("localhost:%s", chunk.container.ports[chunk.healthcheck.portkey])
conn, err := net.dialtimeout("tcp", addr, timeout)
if err == nil {
conn.close()
status = true
}
case "http":
url := fmt.sprintf("http://localhost:%s%s",
chunk.container.ports[chunk.healthcheck.portkey],
chunk.healthcheck.path)
resp, err := client.get(url)
if err == nil {
status = (resp.statuscode == chunk.healthcheck.expectedcode)
}
}
il.updatechunkstatus(chunk, status)
return status
}
主要优点
- 真正的跨平台支持:在 windows、linux 和 macos 上的工作方式相同
- 配置驱动:domain.json中的所有编排逻辑
- 与容器无关:无需对 pnr 特定容器进行修改
- 灵活的健康检查:tcp、http,并可扩展至其他协议
- 状态可见性:通过运行时文件清除状态更新
- 并发执行:高效的并行容器管理
入门
完整代码可以在这里找到:github
先决条件
安装 go(1.19 或更高版本):
安装 docker
项目结构
pnr-orchestrator/ ├── main.go ├── containers.go ├── config/ │ └── domain.json └── runtime/ # created automatically
安装
# create project directory mkdir pnr-orchestrator cd pnr-orchestrator # initialize go module go mod init pnr-orchestrator # install dependencies go get github.com/docker/docker/client go get github.com/docker/docker/api/types go get github.com/docker/go-connections/nat
构建和运行
# option 1: direct run go run main.go containers.go # option 2: build and run separately go build ./pnr-orchestrator # unix/linux/mac pnr-orchestrator.exe # windows
超越简单的依赖关系
传统 docker 撰写:
api:
depends_on:
- mongodb
pnr 的智能编排:
"gatekeeper": {
"mongodb_ready": {
"prompt": "Is MongoDB ready?",
"response": ["yes"],
"tv": "Y"
}
}
主要区别? pnr 确保跨任何平台的实际服务准备就绪,而不仅仅是容器启动。
下一步
- 探索更复杂的编排模式
- 添加自定义健康检查类型
- 实施正常关闭和清理
- 创建特定于平台的优化提示
pnr 演示了 go 强大的平台抽象功能如何在不牺牲简单性或功能的情况下创建强大的跨平台容器编排工具。
如果您想查看更多示例或对特定于平台的实现有疑问,请在评论中告诉我!











