您是否曾经希望容器编排能够比静态依赖链更灵活,但又比 kubernetes 更简单?满足 pnr(提示和响应) - 一种配置驱动的方法,利用 go 强大的平台抽象功能根据实际的就绪状态而不是简单的依赖关系来编排容器。
在深入探讨 pnr 之前,让我们先了解一下为什么 go 特别适合跨平台容器编排:
统一的 docker api 接口:go 的 docker 客户端库通过特定于平台的套接字连接提供跨 windows、linux 和 macos 的一致接口:
原生并发支持:go 的 goroutine 和通道可实现高效的容器监控:
跨平台网络处理:go 的 net 包抽象了特定于平台的网络详细信息:
pnr 通过三个关键组件来编排容器:
让我们看看典型的 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
}
安装 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 强大的平台抽象功能如何在不牺牲简单性或功能的情况下创建强大的跨平台容器编排工具。
如果您想查看更多示例或对特定于平台的实现有疑问,请在评论中告诉我!
以上就是PnR:具有 Go 平台抽象的配置意图驱动的容器编排的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号