# Waypoint 集成示例 本文档提供了将Waypoint与现有基础设施和工具集成的具体示例。 ## 1. 与Nomad集成 ### 1.1 基本Nomad部署配置 ```hcl app "api-service" { build { use "docker" { dockerfile = "Dockerfile" disable_entrypoint = true } } deploy { use "nomad" { // Nomad集群地址 address = "http://nomad-server:4646" // 部署配置 datacenter = "dc1" namespace = "default" // 资源配置 resources { cpu = 500 memory = 256 } // 服务配置 service_provider = "consul" { service_name = "api-service" tags = ["api", "v1"] check { type = "http" path = "/health" interval = "10s" timeout = "2s" } } } } } ``` ### 1.2 高级Nomad配置 ```hcl app "web-app" { deploy { use "nomad" { // 基本配置... // 存储卷配置 volume_mount { volume = "app-data" destination = "/data" read_only = false } // 网络配置 network { mode = "bridge" port "http" { static = 8080 to = 80 } } // 环境变量 env { NODE_ENV = "production" } // 健康检查 health_check { timeout = "5m" check { name = "http-check" route = "/health" method = "GET" code = 200 } } } } } ``` ## 2. 与Vault集成 ### 2.1 从Vault获取静态密钥 ```hcl app "database-service" { deploy { use "nomad" { // 基本配置... env { // 从Vault获取数据库凭据 DB_USERNAME = dynamic("vault", { path = "kv/data/database/creds" key = "username" }) DB_PASSWORD = dynamic("vault", { path = "kv/data/database/creds" key = "password" }) } } } } ``` ### 2.2 使用Vault动态密钥 ```hcl app "api-service" { deploy { use "nomad" { // 基本配置... template { destination = "secrets/db-creds.txt" data = < 0.01" } } } } ``` ## 7. 自定义插件示例 ### 7.1 自定义构建器插件 ```go // custom_builder.go package main import ( "context" sdk "github.com/hashicorp/waypoint-plugin-sdk" ) // CustomBuilder 实现自定义构建逻辑 type CustomBuilder struct { config BuildConfig } type BuildConfig struct { Command string `hcl:"command"` } // ConfigSet 设置配置 func (b *CustomBuilder) ConfigSet(config interface{}) error { c, ok := config.(*BuildConfig) if !ok { return fmt.Errorf("invalid configuration") } b.config = *c return nil } // BuildFunc 执行构建 func (b *CustomBuilder) BuildFunc() interface{} { return b.build } func (b *CustomBuilder) build(ctx context.Context, ui terminal.UI) (*Binary, error) { // 执行自定义构建命令 cmd := exec.CommandContext(ctx, "sh", "-c", b.config.Command) cmd.Stdout = ui.Output() cmd.Stderr = ui.Error() if err := cmd.Run(); err != nil { return nil, err } return &Binary{ Source: "custom", }, nil } // 注册插件 func main() { sdk.Main(sdk.WithComponents(&CustomBuilder{})) } ``` ### 7.2 使用自定义插件 ```hcl app "custom-app" { build { use "custom" { command = "make build" } } deploy { use "nomad" { // 部署配置... } } } ``` ## 8. 监控和可观测性集成 ### 8.1 Prometheus集成 ```hcl app "monitored-app" { deploy { use "nomad" { // 基本配置... // Prometheus注解 service_provider = "consul" { service_name = "monitored-app" meta { "prometheus.io/scrape" = "true" "prometheus.io/path" = "/metrics" "prometheus.io/port" = "8080" } } } } } ``` ### 8.2 与ELK堆栈集成 ```hcl app "logging-app" { deploy { use "nomad" { // 基本配置... // 日志配置 logging { type = "fluentd" config { fluentd_address = "fluentd.service.consul:24224" tag = "app.${nomad.namespace}.${app.name}" } } } } } ``` ## 9. 本地开发工作流 ### 9.1 本地开发配置 ```hcl app "dev-app" { build { use "docker" {} } deploy { use "docker" { service_port = 3000 // 开发环境特定配置 env { NODE_ENV = "development" DEBUG = "true" } // 挂载源代码目录 binds { source = abspath("./src") destination = "/app/src" } } } } ``` ### 9.2 本地与远程环境切换 ```hcl variable "environment" { type = string default = "local" } app "fullstack-app" { build { use "docker" {} } deploy { // 根据环境变量选择部署方式 use dynamic { value = var.environment // 本地开发 local { use "docker" { // 本地Docker配置... } } // 开发环境 dev { use "nomad" { // 开发环境Nomad配置... } } // 生产环境 prod { use "nomad" { // 生产环境Nomad配置... } } } } } ``` ## 10. 多应用协调 ### 10.1 依赖管理 ```hcl project = "microservices" app "database" { // 数据库服务配置... } app "backend" { // 后端API配置... // 声明依赖关系 depends_on = ["database"] } app "frontend" { // 前端配置... // 声明依赖关系 depends_on = ["backend"] } ``` ### 10.2 共享配置 ```hcl // 定义共享变量 variable "version" { type = string default = "1.0.0" } variable "environment" { type = string default = "development" } // 共享函数 function "service_name" { params = [name] result = "${var.environment}-${name}" } // 应用配置 app "api" { build { use "docker" { tag = "${var.version}" } } deploy { use "nomad" { service_provider = "consul" { service_name = service_name("api") } env { APP_VERSION = var.version ENVIRONMENT = var.environment } } } }