新图像未部署到 AKS

New images are not being deployed to AKS

我已经将初始 azure-pipelines.yml 拆分出来以使用模板、迭代等...无论出于何种原因,尽管使用了 latest 标签 and/or imagePullPolicy: Always.

此外,我基本上有两个管道 PRRelease:

这是我的 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)

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

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

因为没有散列,只有标签 latest 引用部署说: “你想要最新的?我有最新的 运行!”。 重要的部分是“运行”。如果一开始就不需要拉动,拉动政策 always 就无济于事。

可能的解决方案:

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

For whatever reason, the new images are not being deployed despite using latest tag

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

Here is an example of one of my k8s deployment manifests (the pipeline says unchanged when these are applied)

是的,未更改,因为声明性禁用状态 更改。 Deployment-manifest 状态 what 应该部署,它不是命令。

建议的解决方案

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