kubernetes - 更新图像标签以通过CloudBuilder提交SHA的最佳方法是什么?

标签 kubernetes google-cloud-platform google-kubernetes-engine google-container-registry google-container-builder

我有一个 deployment.yaml ,其中包含3个容器的部署+ LB服务,以及 cloudbuild.yaml ,其中包含在每次对Bitbucket git repo上的某个分支进行新提交时生成容器镜像的步骤。

一切工作正常,但事实是,只要有新的镜像版本(我在部署中使用:latest标记)就不会更新我的部门,并且要更改此设置,我了解我的部署镜像应使用除:latest之外的其他独特名称,例如作为git commit SHA。

问题:
我不确定如何在GCB CI过程中执行图像声明更新以包含新的提交SHA。

YAML:https://paste.ee/p/CsETr

最佳答案

通过在部署过程中使用图像标签或URI变量并在构建期间用sed替换它们,找到了一个解决方案。

deplyment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: dev
  name: app
  labels:
    app: app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      initContainers:
      - name: init
        image: INIT_IMAGE_NAME
        imagePullPolicy: Always
        command: ['sh', '-c', 'cp -r /app /srv; chown -R 82:82 /srv/app']
        volumeMounts:
        - name: code
          mountPath: /srv
      containers:
      - name: nginx
        image: NGINX_IMAGE_NAME
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        volumeMounts:
        - name: code
          mountPath: /srv
        - name: php-socket
          mountPath: /var/run
        livenessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
        readinessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
      - name: php
        image: PHP_IMAGE_NAME
        imagePullPolicy: Always
        volumeMounts:
        - name: code
          mountPath: /srv
        - name: php-socket
          mountPath: /var/run
        livenessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
        readinessProbe:
          httpGet:
            path: /health.html
            port: 80
            httpHeaders:
            - name: X-Healthcheck
              value: Checked
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 15
      volumes:
        - name: code
          emptyDir: {}
        - name: php-socket
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  namespace: dev
  name: app-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: app

cloudbuild.yaml
steps:

# Build Images
- id: Building Init Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA', '-f', 'init.dockerfile', '.']

- id: Building Nginx Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA', '-f', 'nginx.dockerfile', '.']
  waitFor: ['-']

- id: Building PHP-FPM Image
  name: gcr.io/cloud-builders/docker
  args: ['build','-t', 'eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA', '-f', 'php.dockerfile', '.']
  waitFor: ['-']


# Push Images
- id: Pushing Init Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA']

- id: Pushing Nginx Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA']

- id: Pushing PHP-FPM Image
  name: gcr.io/cloud-builders/docker
  args: ['push','eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA']


# Update Image Tags
- id: 'Setting Init Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,INIT_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA," deployment.yaml']

- id: 'Setting Nginx Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,NGINX_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA," deployment.yaml']

- id: 'Setting PHP Image Tag'
  name: ubuntu
  args: ['bash','-c','sed -i "s,PHP_IMAGE_NAME,eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA," deployment.yaml']


# Update Deployment
- id: Updating Deployment
  name: gcr.io/cloud-builders/kubectl
  args: ['apply','-f','deployment.yaml']

  env:
    - CLOUDSDK_COMPUTE_ZONE=europe-west2-b
    - CLOUDSDK_CONTAINER_CLUSTER=clusterx

# Images
images:
  - eu.gcr.io/$PROJECT_ID/init:$SHORT_SHA
  - eu.gcr.io/$PROJECT_ID/nginx:$SHORT_SHA
  - eu.gcr.io/$PROJECT_ID/php:$SHORT_SHA

# Tags
tags:
  - master
  - dev
  - init

关于kubernetes - 更新图像标签以通过CloudBuilder提交SHA的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51479556/

相关文章:

Azure AKS - 群集处于故障状态,没有任何故障

google-cloud-platform - 如果 pip 不支持,如何在 Datalab 上安装额外的 python 包?

google-cloud-platform - Compute Engine 虚拟机创建通知

php - 如何计算 Kubernetes 集群上的 php-fpm 子进程

kubernetes - 在 stackdriver 上运行的 Pod 没有 CPU 指标

kubernetes - Kubernetes 中的自定义负载均衡

sockets - 无法在Kubernetes上启动Alluxio

docker - 无法将GCR图像作为同一存储库中的另一个图像名称推送

mongodb - 副本集配置无效或者我们不是它的成员,在 kubernetes 中运行

kubernetes - Kubernetes externalName服务在仪表板上保持待处理状态