使用 Azure Pipelines 更新 Kubernetes 清单文件中的 Docker 映像的正确方法

Pus*_*ots 7 kubernetes azure-devops azure-pipelines

我已使用 Azure Pipelines 使用 Docker 任务成功构建了 docker 映像并将其推送到 Docker Hub。

现在,我想获取该 Docker 映像并将其部署到 AKS 中的 Kubernetes 集群。

我需要使用 更新图像kubectl apply。我需要kubectl apply专门使用,而不是replace, edit, or patch

如何在更新spec.containers.image部署.yml 文件的同时实现这一目标?

我基本上想获取这个变量tag: '$(Build.BuildId)',替换spec.containers.imagedeploy.yml,然后运行kubectl apply -f deploy.yml。

这是我的deploy.yml 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myprojectname
  namespace: my-namespace
  labels:
    app: myprojectname
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      web: myprojectname
  template:
    metadata:
      labels:
        app: myprojectname
        web: myprojectname
        type: webapp
    spec:
      containers:
        - name: myprojectname
          image: mydockerhubaccount/myprojectname:25
          resources:
            requests:
              cpu: 20m
              memory: 100Mi
            limits:
              cpu: 500m
              memory: 130Mi
          ports:
            - containerPort: 80
Run Code Online (Sandbox Code Playgroud)

这是我的 azure-pipelines.yml 文件:

# Docker
# Build and push an image to Azure Container Registry
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'Docker Hub'
  imageRepository: 'mydockerhubaccount/myprojectname'
  dockerfilePath: '$(Build.SourcesDirectory)/my-project-name/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
        buildContext: '$(Build.SourcesDirectory)'
Run Code Online (Sandbox Code Playgroud)

Dez*_*ndo 1

This might be an old question but is still relevant today. The process is mostly the same for the "classic" visual DevOps pipeline builder as it is for the YAML step builder.

Generally speaking, the way to update the container image tag with the current build (without using "latest" to enable rollbacks) is as follows:

  1. In the release pipeline make note of your Artifact Source Alias. The artifact being the result from your build process. If your build process doesn't publish an artifact, add the default: Publish Pipeline Artifact step. You can call the source alias whatever you like as long as it is unique and spaces are not important.

  2. In your Release Pipeline's Stages for Deploy to Kubernetes select the correct manifest.

  3. Then update the Containers field to the name of your container repo and add $(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId) as the tag. It should looks something like: YOUR_REPO.azurecr.io/YOUR_CONTAINER_NAME:$(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId)

  4. When the pipeline deploys to K8 it should be using the latest tag id from your build process. You can confirm this on your cluster by describing your pod/whatever with something like: kubectl describe pod POD_NAME

Example YAML deployment step:

steps:
- task: KubernetesManifest@0
  displayName: deploy
  inputs:
    kubernetesServiceConnection: 'YOUR_CONNECTION'
    namespace: 'CLUSTER_NAMEPACE'
    manifests: '$(System.DefaultWorkingDirectory)/YOUR_SOURCE_ALIAS_HERE/Job1/s/whatever-deployment.yml'
    containers: 'YOUR_REPO.azurecr.io/YOUR_CONTAINER_NAME:$(Release.Artifacts.YOUR_SOURCE_ALIAS_HERE.BuildId)'
Run Code Online (Sandbox Code Playgroud)