github 操作 GKE 工作流程 - 部署说明

github actions GKE workflow - deployment clarification

我已经在 GitHub 上设置了一个 Google Cloud Platform kubernetes 集群(和 Container Registry),源代码在 GitHub 上。源代码被分成文件夹,每个微服务都有单独的 Dockerfile。

我想使用 GitHub 操作设置 CI/CD。

据我了解,default GKE workflow 将使用机密连接到 gcloud,构建映像并将它们推送到 Container Registry。然后执行更新。

我的问题

    - name: Build
      run: |-
        docker build -t "gcr.io/$PROJECT_ID/$IMAGE_1:$GITHUB_SHA" service1/.
        docker build -t "gcr.io/$PROJECT_ID/$IMAGE_2:$GITHUB_SHA" service2/.
        docker build -t "gcr.io/$PROJECT_ID/$IMAGE_3:$GITHUB_SHA" service3/.

    - name: Publish
      run: |-
        docker push "gcr.io/$PROJECT_ID/$IMAGE_1:$GITHUB_SHA"
        docker push "gcr.io/$PROJECT_ID/$IMAGE_2:$GITHUB_SHA"
        docker push "gcr.io/$PROJECT_ID/$IMAGE_3:$GITHUB_SHA"

这是来自 GKE 工作流程的部署片段:

    # Deploy the Docker image to the GKE cluster
    - name: Deploy
      run: |-
        ./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA
        ./kustomize build . | kubectl apply -f -
        kubectl rollout status deployment/$DEPLOYMENT_NAME
        kubectl get services -o wide

How is the deployment performed?

要了解如何部署或运行此工作流程,请参阅此documentation

What is kustomize for?

kustomize 是一个configuration mangement 用于应用程序配置

Do I have to configure on gcloud anything else than GKE key / token

除非您要添加额外的安全层来验证工作流程,否则您不必这样做。

Suppose I want to update multiple docker images. Will it suffice to build multiple images and push them? Like below (a little bit simplified for clarity), or do I have to also modify the Deploy job

我认为不需要修改部署作业。构建多个镜像并推送到 GCR

就足够了

我只是想在发布这个问题然后实施 GitHub 操作后分享我使用 GKE 的经验。

How is the deployment performed?

基本上,工作流通过 gcloud CLI 建立到 GKE 的连接(这也建立了 kubectl 上下文)。
建立连接并找到正确的集群后,您就可以随心所欲了。

Do I have to configure on gcloud anything else than GKE key / token

没有其他要求。请记住正确地对其进行哈希处理并将其秘密存储在 GitHub.

Suppose I want to update multiple docker images...

按照问题中的方式做是绝对有效且功能齐全的。

...or do I have to also modify the Deploy job

我决定稍微更改一下 Deploy。

# Deploy update to services
- name: Deploy
  run: |-
    kubectl set image deployment dep1 dep1="gcr.io/$PROJECT_ID/$IMAGE_1:$GITHUB_SHA"
    kubectl set image deployment dep2 dep2="gcr.io/$PROJECT_ID/$IMAGE_2:$GITHUB_SHA"

这样我就不用使用我不熟悉的Kustomize
如果你有 update strategy set to RollingUpdate - - which I believe is the default - - the change in image tag will trigger the rolling update (other strategies may work as well). But to use this approach you have to use the same image tag in building Docker images and deploying them using the code above. Using the $GITHUB_SHA 将为提交提供不同的哈希值,可用于区分 docker 图像。

这可能不是最优雅的解决方案,但我相信您可以想出更好的解决方案(例如获取发布标签),因为这只是一个变量。