【问题标题】:Pulumi on GCP - How to create a Managed Instance Group with Docker Container InstancesPulumi on GCP - 如何使用 Docker 容器实例创建托管实例组
【发布时间】:2022-02-28 09:20:37
【问题描述】:

我一直在尝试在 GCP 上创建一个托管实例组,该组由托管自定义 docker 映像的实例组成。但是,我正在努力弄清楚如何使用 Pulumi 做到这一点。

阅读Google's GCP documentation,可以通过instance templates 在托管实例组中部署托管 docker 容器的实例。

实际上在 gcloud 中看起来像这样:

gcloud compute instance-templates create-with-container TEMPLATE_NAME --container-image DOCKER_IMAGE

然而,阅读Pulumi's instance template documentation,尚不清楚如何创建一个实例模板来执行与上述命令相同的操作。

是否可以在 Pulumi 中创建托管实例组,其中实例托管自定义 Docker 映像,还是我必须手动创建实例模板,并在我的 Pulumi 脚本中引用它?

【问题讨论】:

  • 我不使用 Pulimi,但我使用 Terraform 和 REST API。我首先要弄清楚如何创建一个指定 COS 图像 cos-stable 的实例。然后弄清楚如何指定声明要运行的容器的 cloud-init 配置文件。弄清楚基础知识后,创建一个实例模板。您问题中的 CLI 命令实际上非常复杂,并且在单个命令中执行许多步骤。此链接可能会有所帮助:cloud.google.com/container-optimized-os/docs/how-to/…
  • 作为建议,您的问题过于宽泛,需要大量代码来重现 CLI 命令。创建不需要一周工作即可编写的较小问题。
  • 感谢您的洞察@JohnHanley。我不同意这个问题太宽泛——我想说它实际上非常具体,希望能清楚我想要什么。但是,如果自动化解决方案需要大量工作,那将是很有见地的。我希望 Pulumi 作为 Terraform 的包装器会使工作更容易,但也许事实并非如此。理想情况下,社区中的某个人已经解决了这个问题,并且已经有了解决方案,并且愿意分享他们是如何做到的。我不希望任何人为了回答我的问题而从头开始解决这个问题。
  • @JohnHanley 对于初学者,我已经包含了一个相关的 gcloud sn-p 来实现我所追求的。您假设解决方案很复杂,需要“一周的工作”来编写。如果问题不清楚,我很乐意澄清。但是,如果您的抱怨是解决方案包含大量代码,那么我认为这不违背堆栈溢出精神,让答案是“它有很多代码”,并希望至少提供一些高级步骤,其中解决问题。我很乐意稍后在我自己的答案中填写详细信息。
  • @JohnHanley - 我的问题本质上是关于在 Pulumi 中重现 gcloud 命令。没有示例 Pulumi 代码,因为不清楚如何做到这一点。我已阅读指南,但我不确定您如何或为什么觉得我没有遵循这些指南?我已经提供了足够的信息来解释我想要实现的目标,这是一个我相信很多其他人都会遇到的问题,但在这里你正试图关闭这个问题。在我看来,这对社区有害,因为它无疑会在未来多次出现在其他人身上。

标签: docker google-cloud-platform google-compute-engine pulumi


【解决方案1】:

这是一种同时使用gcloudPulumi 的混合方法。

高水平:

  1. 创建docker container并上传到Google Container Registry
  2. 使用gcloud 创建一个instance template
  3. 创建托管实例组,从 Pulumi 脚本中引用实例模板

#1 创建 Docker 容器

使用CloudBuild 检测 Git 存储库中的更改、创建 docker 容器并将其上传到 Google Container Registry。

在我的仓库中,我有一个 Dockerfile 文件,其中包含有关如何构建将用于我的实例的容器的说明。我使用Supervisord 来启动和监控我的应用程序。

它的外观如下:

# my-app-repo/Dockerfile

FROM ubuntu:22.04

RUN apt update
RUN apt -y install software-properties-common

RUN apt install -y supervisor
COPY supervisord.conf /etc/supervisord.conf
RUN chmod 0700 /etc/supervisord.conf

COPY ./my-app /home/my-app
RUN chmod u+x /home/my-app

EXPOSE 443/tcp # HTTPS
EXPOSE 9001/tcp # supervisord support

CMD ["supervisord", "-c", "/etc/supervisord.conf"]

第二部分是构建 docker 容器并上传到 Google Container Registry。我通过 CloudBuild 做到这一点。以下是对应的 Pulumi 代码(构建 Golang 应用):

注意:请确保您首先通过 GCP 网站的 CloudBuild section 连接了 repo

const myImageName = pulumi.interpolate`gcr.io/${project}/my-image-name`
const buildTrigger = new gcp.cloudbuild.Trigger("my-app-build-trigger", {
  name: "my-app",
  description: "Builds My App image",
  build: {
    steps: [
      {
        name: "golang",
        id: "build-server",
        entrypoint: "bash",
        timeout: "300s",
        args: ["-c", "go build"],
      },
      {
        name: "gcr.io/cloud-builders/docker",
        id: "build-docker-image",
        args: [
          "build",
          "-t", pulumi.interpolate`${myImageName}:$BRANCH_NAME-$REVISION_ID`,
          "-t", pulumi.interpolate`${myImageName}:latest`,
          '.',
        ],
      },
    ],
    images: [myImageName]
  },
  github: {
    name: "my-app-repo",
    owner: "MyGithubUsername",
    push: {
      branch: "^main$"
    }
  },
});

#2 创建实例模板

由于我无法弄清楚如何通过 Pulumi 轻松创建实例模板,我决定通过 gcloud 命令行工具使用 Google SDK。

gcloud compute instance-templates create-with-container my-template-name-01 \
--region us-central1 \
--container-image=gcr.io/my-project/my-image-name:main-e286d94217719c3be79aac1cbd39c0a629b84de3 \
--machine-type=e2-micro \
--network=my-network-name-59c9c08 \
--tags=my-tag-name \
--service-account=my-service-account@my-project.iam.gserviceaccount.com

上面的值(容器、网络名称等)我只需在 GCP website 上浏览我的项目即可获得。

#3 创建托管实例组

创建实例模板后,您现在可以在 Pulumi 脚本中引用该模板

const myHealthCheck = new gcp.compute.HealthCheck("my-app-health-check", {
  checkIntervalSec: 5,
  timeoutSec: 5,
  healthyThreshold: 2,
  unhealthyThreshold: 5,
  httpHealthCheck: {
    requestPath: "/health-check",
    port: 80,
  },
});

const instanceGroupManager = new gcp.compute.InstanceGroupManager("my-app-instance-group", {
  baseInstanceName: "my-app-name-prefix",
  zone: hostZone,
  targetSize: 2,
  versions: [
    {
      name: "my-app",
      instanceTemplate: "https://www.googleapis.com/compute/v1/projects/my-project/global/instanceTemplates/my-template-name-01",
    },
  ],
  autoHealingPolicies: {
    healthCheck: myHealthCheck.id,
    initialDelaySec: 300,
  },
});

为了完整起见,我还包含了我的 Pulumi 脚本的另一部分,它创建一个后端服务并通过 InstanceGroupManager 调用将其连接到上面创建的实例组。请注意,此示例中的负载均衡器使用 TCP 而不是 HTTPS(我的应用正在处理 SSL 连接,因此使用TCP Network Load Balancer)。

const backendService = new gcp.compute.RegionBackendService("my-app-backend-service", {
  region: hostRegion,
  enableCdn: false,
  protocol: "TCP",
  backends: [{
    group: instanceGroupManager.instanceGroup,
  }],
  healthChecks: defaultHttpHealthCheck.id,
  loadBalancingScheme: "EXTERNAL",
});

const myForwardingRule = new gcp.compute.ForwardingRule("my-app-forwarding-rule", {
  description: "HTTPS forwarding rule",
  region: hostRegion,
  ipAddress: myIPAddress.address,
  backendService: backendService.id,
  portRange: "443",
});

注意:理想情况下,第 2 步也可以使用 Pulumi 完成,但我还没有完成那部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-22
    • 2021-06-29
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2021-07-20
    • 2021-08-09
    • 2020-05-16
    相关资源
    最近更新 更多