gpt4 book ai didi

kubernetes - 如何编写kubernetes部署以获取使用GCP cloudbuild构建的最新镜像

转载 作者:行者123 更新时间:2023-12-02 11:39:17 25 4
gpt4 key购买 nike

我正在尝试使用GCP cloudbuild进行CI / CD。

  • 我已经在GCP中准备好了一个k8s集群。检查下面的部署 list 。
  • 我已经准备好cloudbuild.yaml来构建新镜像并将其推送到注册表和命令以更改部署镜像。检查下面的cloudbuild yaml。

  • 以前,我曾经使用 TAG最新来推送图像,以用于docker镜像,并且在部署中使用了相同的标签,但是它没有提取最新的镜像,因此现在我将其更改为使用 TAG $ COMMIT_SHA 。现在,我不知道将基于commit_sha的带有TAG的新镜像传递给部署的方法。

    nginx-deployment.yaml
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    name: mynginx
    spec:
    replicas: 3
    minReadySeconds: 50
    strategy:
    type: RollingUpdate
    rollingUpdate:
    maxUnavailable: 1
    maxSurge: 1
    selector:
    matchLabels:
    app: nginx

    template:
    metadata:
    labels:
    app: nginx
    spec:
    containers:
    - image: gcr.io/foods-io/cloudbuildtest-image:latest
    name: nginx
    ports:
    - containerPort: 80

    cloudbuild.yaml
    steps:
    #step1
    - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/cloudbuildtest-image:$COMMIT_SHA', '.' ]
    #step 2
    - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/cloudbuildtest-image:$COMMIT_SHA']
    #STEP-3
    - name: 'gcr.io/cloud-builders/kubectl'
    args: ['set', 'image', 'deployment/mynginx', 'nginx=gcr.io/foods-io/cloudbuildtest-image:$COMMIT_SHA']
    env:
    - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
    - 'CLOUDSDK_CONTAINER_CLUSTER=cloudbuild-test'
    images:
    - 'gcr.io/$PROJECT_ID/cloudbuildtest-image'

    Note: I repeat previously I was using the latest tag to the image and as is the same in deployment I expected to pull the new image with my 3rd steps in cloudbuild but that didn't so I made the above changes in TAG but now wondering how do I make changes to deployment manifest. Is using the helm only solution here?

    最佳答案

    您需要执行一个步骤来替换Deployment.yaml中的标记,一种方法是使用环境变量并使用envsubst替换它。

    更改deployment.yaml:

        - image: gcr.io/foods-io/cloudbuildtest-image:$COMMIT_SHA

    使用一些 bash脚本替换变量(例如,使用 ubuntu step):

    envsubst '$COMMIT_SHA' < deployment.yaml > nginx-deployment.yaml

    使用 sed的替代方法:

    sed -e 's/$COMMIT_SHA/'"$COMMIT_SHA"'/g' deployment.yaml > /workspace/nginx-deployment.yaml

    关于kubernetes - 如何编写kubernetes部署以获取使用GCP cloudbuild构建的最新镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58406130/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com