访问具有专用池的专用 JFrog Artifactory 中的资源


本页面演示了如何使用 Cloud Build 私有池从私有 Virtual Private Cloud 网络访问资源。

在本教程中,您将在专用 VPC 网络内托管的 Compute Engine 中创建 JFrog Artifactory,然后配置在专用池中运行的构建以访问该 Artifactory 中的数据。Jfrog Artifactory 是一个开源二进制代码库管理器。

目标

  • 在 Compute Engine 上设置 Jfrog Artifactory
  • 将文件上传到 Artifactory
  • 创建专用池
  • 将托管私有池的服务提供方网络与 Artifactory 的 Virtual Private Cloud 网络进行对等互连
  • 编写构建配置文件以访问 Artifactory 中的数据

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

  • Compute Engine
  • Cloud Build

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Compute Engine, Cloud Build, Service Networking API。

    启用 API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Compute Engine, Cloud Build, Service Networking API。

    启用 API

选项 A:使用 Cloud Shell

您可以使用 Cloud Shell 来执行本教程中所述的操作,该环境中预装了本教程中用到的 Google Cloud CLI。如果使用 Cloud Shell,则无需在工作站上安装这些命令行工具。

如需使用 Cloud Shell,请执行以下操作:

  1. 前往 Google Cloud 控制台

    Google Cloud 控制台

  2. 点击 Google Cloud 控制台窗口顶部的激活 Cloud Shell 激活 Shell 按钮 按钮。

    一个 Cloud Shell 会话随即会在 Google Cloud 控制台底部的新框内打开,并显示命令行提示符。

    Cloud Shell 会话

选项 B:在本地使用命令行工具

如果您希望在工作站上按照本教程中的说明操作,请按照以下步骤安装必要的工具。

  1. 安装 Google Cloud CLI

创建私有工件

  1. 从容器创建 Compute Engine 实例:

    gcloud compute instances create-with-container jfrog \
    --container-image docker.bintray.io/jfrog/artifactory-jcr:latest \
    --zone us-central1-a
    
  2. 通过 SSH 登录实例。容器可能需要几分钟才能完成初始化。

    gcloud compute ssh --zone us-central1-a jfrog
    
  3. 通过运行以下命令来测试连接。容器准备就绪后,它将以 200 HTTP 代码响应,后跟 HTML 页面。

    curl -i http://localhost:8081
    
  4. 如需在 Artifactory 中创建代码库,您必须签署 JFrog EULA(最终用户许可协议):

    curl -XPOST -vu admin:password http://localhost:8081/artifactory/ui/jcr/eula/accept
    

    您将看到如下所示的输出:

        *   Trying 127.0.0.1:8081...
        * Connected to localhost (127.0.0.1) port 8081 (#0)
        * Server auth using Basic with user 'admin'
        > POST /artifactory/ui/jcr/eula/accept HTTP/1.1
        > Host: localhost:8081
        > Authorization: Basic ….
        > User-Agent: curl/7.74.0
        > Accept: */*
        >
        * Mark bundle as not supporting multiuse
        < HTTP/1.1 200 OK
        < X-JFrog-Version: Artifactory/7.19.9 71909900
        < X-Artifactory-Id: ….
        < X-Artifactory-Node-Id: jfrog2
        < SessionValid: false
        < Content-Length: 0
        < Date: Fri, 25 Jun 2021 19:08:10 GMT
    
        * Connection #0 to host localhost left intact
    

将文件上传到 Artifactory

  1. 创建 txt 文件以上传到 Artifactory:

    echo "Hello world" >> helloworld.txt
    
  2. JFrog 附带默认示例代码库。使用默认凭据上传到代码库:

    curl -u admin:password -X PUT \
    "http://localhost:8081/artifactory/example-repo-local/helloworld.txt" \
    -T helloworld.txt
    

    此时应返回:

        {
        "repo" : "example-repo-local",
        "path" : "/helloworld.txt",
        "created" : "2021-06-25T19:08:24.176Z",
        "createdBy" : "admin",
        "downloadUri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt",
        "mimeType" : "text/plain",
        "size" : "12",
        "checksums" : {
          "sha1" : "...",
          "md5" : "...",
          "sha256" : "..."
        },
        "originalChecksums" : {
          "sha256" : "..."
        },
        "uri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt"
        }
    
  3. 输入 exit 以结束 SSH 会话。

  4. 移除外部 IP 地址,因此 Artifactory 只能从专用内部来源访问。

    gcloud compute instances delete-access-config --zone us-central1-a jfrog
    

尝试从 Artifactory 访问数据

  1. 设置环境变量以存储您的项目 ID 和项目编号:

    PROJECT_ID=$(gcloud config list --format='value(core.project)')
    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
    
  2. Compute Engine Viewer 角色授予用于构建的服务帐号,以便其能够查看 JFrog 实例的内部 IP 地址:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:SERVICE_ACCOUNT \
        --role=roles/compute.viewer
    

    其中 SERVICE_ACCOUNT 是服务帐号电子邮件。

  3. 创建一个名为 cloudbuild.yaml 的文件,其中包含从 Artifactory 读取的以下代码。这是构建配置文件。

    第一步是从您创建的 Artifactory 提取内部 IP 地址。第二步:向该地址发送请求,读取您创建的 .txt 文件。这些步骤是分开的,以便更轻松地隔离权限和网络错误。如果第一步失败,这是由于权限错误,您需要确保 Cloud Build 服务账号有权访问 Compute Engine 资源,如上所示。如果第二步失败,则原因可能是网络错误。本教程的其余部分介绍网络配置。

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
  4. 使用构建配置文件启动构建。

    默认情况下,当您在 Cloud Build 上运行构建时,该构建在可访问公共互联网的安全托管环境中运行。每个构建都在其自己的工作器上运行,并与其他工作负载隔离。默认池对环境的可自定义程度有限制,特别是对于专用网络访问权限。在此示例中,您正在尝试从公共工作器访问专用网络。

    使用以下命令运行 cloudbuild.yaml。应该会失败。

    gcloud builds submit --no-source
    

    输出结果如下所示:

    BUILD
    Starting Step #0 - "Get Private Artifactory Address"
    Step #0 - "Get Private Artifactory Address": Already have image (with digest): gcr.io/cloud-builders/gcloud
    Finished Step #0 - "Get Private Artifactory Address"
    Starting Step #1 - "Pull from Private Artifactory"
    Step #1 - "Pull from Private Artifactory": Already have image (with digest): gcr.io/cloud-builders/curl
    Step #1 - "Pull from Private Artifactory":   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    Step #1 - "Pull from Private Artifactory":                                  Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:--  0:02:09 --:--:--     0curl: (7) Failed to connect to 10.128.0.2 port 8081: Connection timed out
    Finished Step #1 - "Pull from Private Artifactory"
    ERROR
    ERROR: build step 1 "gcr.io/cloud-builders/curl" failed: step exited with non-zero status: 7
    

    您可以按连接超时看到 Cloud Build 无法访问内部 IP 地址。要访问此专用资源,您必须使用 Cloud Build 专用池。

在 Artifactory 的 VPC 网络与服务提供方网络之间创建专用连接

  1. 首先,确保您的 VPC 网络允许入站流量。创建防火墙规则,以允许入站内部流量进入具有 jfrog 实例的网络。范围 10.0.0.0/16 位于专用地址空间中,以下步骤将用于 Cloud Build 专用池。

    gcloud compute firewall-rules create allow-private-pools --direction=INGRESS \
    --priority=1000 --network=default --action=ALLOW --rules=all --source-ranges=10.0.0.0/16
    
  2. 为 Cloud Build 专用池创建预留范围,以便用于工作器。预留范围必须位于 Artifactory 所在的网络中。在本例中,计算为 default 计算网络。

    设置预留范围时,您有以下两种选项。 您可以通过提供 --addresses--prefix-length 明确指定范围,也可以允许 Google Cloud 根据提供的 prefix-length 预配可用范围。

    在以下示例中,您将地址明确设置为与您创建的防火墙规则匹配。专用池将使用此地址空间,并且入站流量不会被阻止。

    gcloud compute addresses create jfrog-ranges --global --purpose=VPC_PEERING \
    --addresses=10.0.0.0 --prefix-length=16 --network=default
    
  3. 将 VPC 网络与 Service Networking API 对等互连。

    Cloud Build 专用池使用 Service Networking API 运行工作器。这样您就可以在内部 IP 地址上提供托管式服务。这是通过将运行 Cloud Build 专用池工作器的 Google 管理的 VPC 与您自己的 VPC 对等互连来实现的。此操作可能需要几分钟时间才能完成。

    gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \
    --ranges=jfrog-ranges --network=default
    

创建专用池

  1. default VPC 网络现已可以与 Cloud Build 专用池搭配使用。创建专用池,并将其与 VPC 网络进行对等互连。

     gcloud builds worker-pools create jfrog-pool --region us-central1 \
     --peered-network=projects/${PROJECT_ID}/global/networks/default
    
  2. 如需使用新的专用池运行构建,您可以使用 gcloud 命令传入 --worker-pool 标志,或者更新 cloudbuild.yaml 配置以确保它始终使用专用池。在本教程中,请添加以下选项来更新 cloudbuild.yaml

    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  3. 完整的文件如下所示:

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
    
    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  4. 启动构建:

     gcloud builds submit --no-source
    
  5. 构建将使用与 VPC 网络对等互连的新的专用池,从而能够访问 Artifactory 的内部 IP 地址。输出将成功,Step #1 应输出“Hello world”。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

如果您为本教程创建了一个新项目,请删除项目。 如果您使用的是现有项目,希望保留此项目且不保留本教程中添加的任何更改,请删除为教程创建的资源

删除项目

若要避免产生费用,最简单的方法是删除您为本教程创建的项目。

如需删除项目,请执行以下操作:

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

删除教程资源

  1. 删除您在本教程中部署的 Compute Engine 服务:

     gcloud compute instances delete jfrog
    
  2. 删除防火墙规则:

     gcloud compute firewall-rules delete allow-private-pools --network=default
    
  3. 移除预留范围:

     gcloud compute addresses delete jfrog-ranges --global
    
  4. 删除 Cloud Build 专用池:

     gcloud builds worker-pools delete jfrog-pool
    

后续步骤