【问题标题】:New images are not being deployed to AKS未将新映像部署到 AKS
【发布时间】:2021-06-13 22:47:51
【问题描述】:

我已将初始的 azure-pipelines.yml 拆分为使用模板、迭代等...无论出于何种原因,尽管使用了 latest 标记和/或 imagePullPolicy: Always,但并未部署新图像。

另外,我基本上有两条管道PRRelease

  • PR 在提交 PR 请求以合并到 production 时触发。它会自动触发此管道以运行单元测试、构建 Docker 映像、进行集成测试等,然后如果一切顺利,则将映像推送到 ACR。
  • PR流水线通过,PR被批准后,合并到production,然后触发Release流水线。

这是我的k8s 部署清单之一的示例(应用这些清单时管道显示unchanged):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: admin-v2-deployment-prod
  namespace: prod
spec:
  replicas: 3
  selector:
    matchLabels:
      component: admin-v2
  template:
    metadata:
      labels:
        component: admin-v2
    spec:
      containers:
        - name: admin-v2
          imagePullPolicy: Always
          image: appacr.azurecr.io/app-admin-v2:latest
          ports:
            - containerPort: 4001
---
apiVersion: v1
kind: Service
metadata:
  name: admin-v2-cluster-ip-service-prod
  namespace: prod
spec:
  type: ClusterIP
  selector:
    component: admin-v2
  ports:
    - port: 4001
      targetPort: 4001

以下是我一直在拆分的与.yamls 相关的各种管道:

PR 和发布:

# templates/variables.yaml
variables:
  dockerRegistryServiceConnection: '<GUID>'
  imageRepository: 'app'
  containerRegistry: 'appacr.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)'
  tag: '$(Build.BuildId)'
  imagePullSecret: 'appacr1c5a-auth'

  vmImageName: 'ubuntu-latest'

公关:

# pr.yaml
trigger: none

resources:
- repo: self

pool:
  vmIMage: $(vmImageName)

variables:
- template: templates/variables.yaml

stages:
- template: templates/changed.yaml
- template: templates/unitTests.yaml
- template: templates/build.yaml
  parameters: 
    services:
    - api
    - admin
    - admin-v2
    - client
- template: templates/integrationTests.yaml
# templates/build.yaml
parameters:
- name: services
  type: object
  default: []

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    steps:
    - ${{ each service in parameters.services }}:
      - task: Docker@2
        displayName: Build and push an ${{ service }} image to container registry
        inputs:
          command: buildAndPush
          repository: $(imageRepository)-${{ service }}
          dockerfile: $(dockerfilePath)/${{ service }}/Dockerfile
          containerRegistry: $(dockerRegistryServiceConnection)
          tags: |
            $(tag)

发布:

# release.yaml
trigger: 
  branches: 
    include:
    - production

resources:
- repo: self

variables:
- template: templates/variables.yaml

stages:
- template: templates/publish.yaml
- template: templates/deploy.yaml
  parameters: 
    services:
    - api
    - admin
    - admin-v2
    - client
# templates/deploy.yaml
parameters:
- name: services
  type: object
  default: []

stages:
- stage: Deploy
  displayName: Deploy stage
  dependsOn: Publish
  jobs:
  - deployment: Deploy
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    environment: 'App Production AKS'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              action: createSecret
              secretName: $(imagePullSecret)
              kubernetesServiceConnection: 'App Production AKS'
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)
          - ${{ each service in parameters.services }}:
            - task: KubernetesManifest@0
              displayName: Deploy to ${{ service }} Kubernetes cluster
              inputs:
                action: deploy
                kubernetesServiceConnection: 'App Production AKS'
                manifests: |
                  $(Pipeline.Workspace)/k8s/aks/${{ service }}.yaml
                imagePullSecrets: |
                  $(imagePullSecret)
                containers: |
                  $(containerRegistry)/$(imageRepository)-${{ service }}:$(tag)
  • PRRelease 均通过...
  • 新图像在 ACR 中...
  • 我已提取图像以验证它们是否具有最新更改...
  • 他们只是没有部署到 AKS。

对我在这里做错了什么有什么建议吗?

【问题讨论】:

    标签: azure kubernetes azure-devops azure-pipelines azure-aks


    【解决方案1】:

    在你的部署中你拉 image: appacr.azurecr.io/app-admin-v2:latest

    由于没有散列,而只是标记 latest 引用了部署,所以说: “你想要最新的?我有最新的跑步!”。 重要的部分是“运行”。如果一开始就不需要拉,那么拉策略 always 就没有帮助。

    可能的解决方案:

    • 更改部署中的某些内容会导致重新部署 pod。然后它实际上会再次拉取图像。
    • 更清洁的解决方案:不要使用最新的!使用一些语义版本控制或日期或任何与您的方法相匹配的策略。在这种情况下,标签将始终更改,并且始终拉取该图像。

    【讨论】:

      【解决方案2】:

      无论出于何种原因,尽管使用了 latest 标签,但新镜像并未部署

      Kubernetes 应该如何知道有一个新镜像? Kubernetes 配置是声明性的。 Kubernetes 已经在运行曾经是“最新”的镜像。

      这是我的 k8s 部署清单之一的示例(应用这些清单时管道显示不变)

      是的,它没有改变,因为声明性disered状态已经没有改变了。 Deployment-manifest 声明 什么 应该被部署,它不是一个命令。

      建议的解决方案

      每当您构建映像时,请始终为其指定一个唯一的名称。并且无论何时你想部署某些东西,总是设置一个唯一的名称 what 应该运行 - 然后 Kubernetes 将使用滚动部署以一种优雅的零停机方式管理它 - 除非你将它配置为不同的行为。

      【讨论】:

      • 好吧,不带标签的镜像名称与使用latest 相同 - 您应该为每个构建的镜像创建一个新标签。
      • 您需要部署与您构建的相同的“BuildId”。但是,为 PR 构建一个版本,为 Release 构建另一个版本(合并后)听起来很合理。
      • 您好像没有在发布管道上构建?如果那是在 git-merge 之后,您需要再次构建应用程序,然后部署。
      • 我现在读到 PR 和 Release 都在你的合并之后?然后,您只需要在 buildId 和 Release 管道中使用相同的编号。
      • 如果合并,则需要重新构建。因为我认为两个分支可以同时合并,对吧?这取决于您的分支策略。然后建议再次运行所有测试。
      猜你喜欢
      • 1970-01-01
      • 2019-11-11
      • 2016-10-27
      • 2018-12-19
      • 2019-03-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多