如何使用Kubernetes识别Azure Devops管道/使其失败

dfddblmv  于 2022-12-03  发布在  Kubernetes
关注(0)|答案(2)|浏览(158)

我正在使用Azure devops将服务部署到AKS。即使pod没有启动(处于崩溃状态),管道仍显示为成功的示例很少。
我添加此任务以验证kubectl pod:

- task: Kubernetes@1
            displayName: 'Deploy ${{ parameters.project_display_name }} to ${{ parameters.cluster }}'
            inputs:
              connectionType: Kubernetes Service Connection
              kubernetesServiceEndpoint: ${{ parameters.cluster }}-${{ parameters.namespace }}
              command: apply
              arguments: -f $(System.ArtifactsDirectory)/k8_manifest/manifest.yml -n ${{ parameters.namespace }}
              outputFormat: 'yaml'

          - task: AzureCLI@2
            displayName: 'Validate Deployment of ${{ parameters.project_display_name }} to ${{ parameters.cluster }}'
            inputs:
              scriptType: 'pscore'
              scriptLocation: 'inlineScript'
              inlineScript: |
                containerStatuses=$(kubectl get deployment loss-limit-api --namespace betting -o=jsonpath='{$.status.conditions}')
                for row in $(echo "${containerStatuses}" | jq -r '.[] | @base64'); do
                    _jq() {
                    echo ${row} | base64 --decode | jq -r ${1}
                    }
                    if [ $(_jq '.status') != "True" ]; then
                        echo "Inactive Pod erron on the deployment"
                        exit 1
                    fi
                done

它返回错误:

Starting: Validate Deployment of Loss Limit API to core-dev00
==============================================================================
Task         : Azure CLI
Description  : Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
Version      : 2.208.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
##[error]Script failed with error: Error: Unable to locate executable file: 'pwsh'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
Finishing: Validate Deployment of Loss Limit API to core-dev00
u5rb5r59

u5rb5r591#

该错误消息显示您的代理未安装PowerShell Core。要解决此问题,请执行以下操作:

方法1:在Azure CLI任务中,将batch用作scriptType,而不是pscore
方法2:在代理计算机上安装PowerShell Core。有关详细信息,请参阅this document

使用微软托管的linux代理也是一种选择,但我不推荐,因为它可能会导致其他问题,比如找不到你机器上的文件等等。

bnl4lu3b

bnl4lu3b2#

**1:**在您的情况下,最佳和最可行的解决方案是在Kubernetes@1任务中使用等待参数。您可以在等待中添加条件,例如

wait --for=condition=ready pod -l app=netshoot.
更多详细信息请访问https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply
kubectl wait --for=condition=complete --timeout=30s

2.我使用的第二个选项是使用KubernetesManifest@0,而不是Kubernetes@1,因为KubernetesManifest@0默认情况下包括wait参数,并等待直到所有pod启动并运行,如果不满足条件(任何pod处于失败/挂起状态),则最终超时并使管道失败。

相关问题