Azure Pipelines应该生成我的Kubernetes清单文件吗?如果是,为什么我的不生成?

pn9klfpd  于 2022-12-22  发布在  Kubernetes
关注(0)|答案(2)|浏览(130)

我已经纠结了好几周了,所以我终于伸出了手。
据我所知,Azure DevOps管道能够生成一个从头到尾的YAML文件,该文件用于构建Docker文件并将其推送到Azure容器注册表中。然后使用Kubernetes生成清单文件作为工件,随后使用生成的清单文件将我们的多容器应用部署到Azure Kubernetes服务中。在使用管道之前,我需要自己编写清单文件吗?如果需要,是否有更好的方法来生成清单文件?目前我尝试手动逐行编写清单文件,但遇到了问题。
我已经把自动生成的YAML文件附加到这篇文章中--我已经检查了代码并隐藏了个人/私人细节。我已经能够让它毫无问题地完成第一阶段--合成/推送docker文件到ACR,但部署阶段每次都失败了。原因有很多--我猜是因为我的清单文件写得不正确。

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

resources:
- repo: self

variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'HIDDEN'
  imageRepository: 'dec7'
  containerRegistry: 'HIDDEN'
  dockerfilePath: '**/Dockerfile'
  buildContext: 1.x/trunk/src/
  tag: '$(Build.BuildId)'
  imagePullSecret: 'HIDDEN'

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

  # Name of the new namespace being created to deploy the PR changes.
  k8sNamespaceForPR: 'review-app-$(System.PullRequest.PullRequestId)'

stages: 
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: DockerCompose@0
      displayName: 'Build services'
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscription: HIDDEN
        azureContainerRegistry: 'HIDDEN'
        dockerComposeFile: '1.x/trunk/src/docker-compose.yml'
        dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
        action: 'Build services'
        additionalImageTags: '$(Build.BuildId)'

    - task: DockerCompose@0
      displayName: 'Push services'
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscription: HIDDEN
        azureContainerRegistry: 'HIDDEN'
        dockerComposeFile: '1.x/trunk/src/docker-compose.yml'
        dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
        action: 'Push services'
        additionalImageTags: '$(Build.BuildId)'

    - task: DockerCompose@0
      displayName: 'Lock services'
      inputs:
        containerregistrytype: 'Azure Container Registry'
        azureSubscription: HIDDEN
        azureContainerRegistry: 'HIDDEN'
        dockerComposeFile: '1.x/trunk/src/docker-compose.yml'
        dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
        action: 'Lock services'
        outputDockerComposeFile: '$(Build.StagingDirectory)/docker-compose.yml'

    - upload: manifests
      artifact: manifests

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

  jobs:
  - deployment: Deploy
    condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/pull/')))
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    environment: HIDDEN
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              action: 'createSecret'
              kubernetesServiceConnection: 'AKSServiceConnectionDec6'
              secretType: 'dockerRegistry'
              secretName: '$(imagePullSecret)'
              dockerRegistryEndpoint: '$(dockerRegistryServiceConnection)'

          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: 'deploy'
              kubernetesServiceConnection: 'AKSServiceConnectionDec6'
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.yml
                $(Pipeline.Workspace)/manifests/service.yml
              containers: '$(containerRegistry)/$(imageRepository):$(tag)'
              imagePullSecrets: '$(imagePullSecret)'

  - deployment: DeployPullRequest
    displayName: Deploy Pull request
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/pull/'))
    pool:
      vmImage: $(vmImageName)

    environment: 'HIDDEN$(k8sNamespaceForPR)'
    strategy:
      runOnce:
        deploy:
          steps:
          - reviewApp: HIDDEN

          - task: Kubernetes@1
            displayName: 'Create a new namespace for the pull request'
            inputs:
              connectionType: 'Kubernetes Service Connection'
              kubernetesServiceEndpoint: 'AKSServiceConnectionDec6'
              command: 'apply'
              useConfigurationFile: true
              secretType: 'dockerRegistry'
              containerRegistryType: 'Azure Container Registry'

          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              action: createSecret
              secretName: $(imagePullSecret)
              namespace: $(k8sNamespaceForPR)
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)

          - task: KubernetesManifest@0
            displayName: Deploy to the new namespace in the Kubernetes cluster
            inputs:
              action: 'deploy'
              kubernetesServiceConnection: 'AKSServiceConnectionDec6'
              namespace: '$(k8sNamespaceForPR)'
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.yml
                $(Pipeline.Workspace)/manifests/service.yml
              containers: '$(containerRegistry)/$(imageRepository):$(tag)'
              imagePullSecrets: '$(imagePullSecret)'

          - task: Kubernetes@1
            name: get
            displayName: 'Get services in the new namespace'
            continueOnError: true
            inputs:
              connectionType: 'Kubernetes Service Connection'
              kubernetesServiceEndpoint: 'AKSServiceConnectionDec6'
              namespace: '$(k8sNamespaceForPR)'
              command: 'get'
              arguments: 'svc'
              secretType: 'dockerRegistry'
              containerRegistryType: 'Azure Container Registry'
              outputFormat: 'jsonpath=''http://{.items[0].status.loadBalancer.ingress[0].ip}:{.items[0].spec.ports[0].port}'''

          # Getting the IP of the deployed service and writing it to a variable for posing comment
          - script: |
              url="$(get.KubectlOutput)"
              message="Your review app has been deployed"
              if [ ! -z "$url" -a "$url" != "http://:" ]
              then
                message="${message} and is available at $url.<br><br>[Learn More](https://aka.ms/testwithreviewapps) about how to test and provide feedback for the app."
              fi
              echo "##vso[task.setvariable variable=GITHUB_COMMENT]$message"

我尝试过使用经典编辑器和Microsoft提供的新编辑器从头开始生成新管道。我遇到了构建阶段无法找到工作目录的问题。我通过手动指定来修复此问题。但是,管道进入部署阶段后,我遇到了以下错误:

##[error]No manifest file(s) matching /home/vsts/work/1/manifests/deployment.yml,/home/vsts/work/1/manifests/service.yml was found.

这告诉我管道没有像我想象的那样生成清单文件。所以我自己编写了一个清单文件,可能是错误的,它运行了一次-但是超时了。现在我在使用更改的清单文件运行部署阶段后得到了以下错误:

error: deployment "v4deployment" exceeded its progress deadline
##[error]Error: error: deployment "v4deployment" exceeded its progress deadline
qybjjes1

qybjjes11#

这理解不好吗?
是的。您仍然需要创作您的部署清单。管道可以将清单应用到集群,但它不会为您生成任何内容。

ru9i0ody

ru9i0ody2#

    • 分配:您的群集是AKS**

这理解不好吗?
不,这是正确的理解。当你创建新管道并选择"部署到Azure Kubernetes服务"选项时,此选项将要求Azure订阅,而选择"全部"选项将在存储库根目录下的清单文件夹中生成管道yaml以及Kubernetes清单文件。你可以根据需要修改/更新这些清单文件。我已从快照中删除了一些个人详细信息。

[错误]找不到与/home/vsts/work/1/清单/部署. yml、/home/vsts/work/1/清单/服务. yml匹配的清单文件。

你必须检查repo中生成的清单文件的路径,并提供管道yaml文件中的绝对路径。例如,我们的案例清单文件位于root--〉manifests--〉framework--〉develop下,所以我的yaml是这样的。

- task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: deploy
              manifests: |
                $(Pipeline.Workspace)/manifests/framework/develop/deployment.yml
                $(Pipeline.Workspace)/manifests/framework/develop/service.yml
                imagePullSecrets: |
                $(imagePullSecret)
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

错误:部署"v4deployment"已超过其进度截止时间##[error]错误:错误:部署"v4deployment"已超过其进度截止时间
此错误表示您的部署已完成。现在部署正在等待所有应用程序Pod处于运行状态,但由于出现错误,Pod未准备就绪。要检查错误,您可以访问群集(kubeconfig或 Jmeter 板)并检查命名空间事件,也可以直接检查Pod事件/日志。这两个命令将为您提供Pod不正常的充分证据。

kubectl describe pod your-pod-name -n your-namespace
kubectl logs -f your-pod-name -n your-namespace

相关问题