簡體   English   中英

如何使用 kube.netes 識別/失敗 Azure Devops 管道

[英]How to identify/Fail the Azure Devops pipeline with kubernetes

我正在使用 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

錯誤消息顯示您的代理沒有安裝 PowerShell Core。 要解決此問題:

方法 1 :在 Azure CLI 任務中,使用batch作為scriptType而不是pscore

方法二:在你的agent機器上安裝PowerShell Core。 您可以參考此文檔以獲取詳細信息。

使用 Microsoft 托管的 linux 代理也是一種選擇。 但我不推薦它,因為它可能會導致其他問題,例如無法找到托管在您機器上的文件等。

1:在您的情況下,最佳和最可行的解決方案是在您的 Kube.netes@1 任務中使用 wait arguments。 您可以在等待中添加條件,例如

wait --for=condition=ready pod -l app.netshoot.

更多詳細信息可以在這里找到。 https://kube.netes.io/docs/reference/generated/kubectl/kubectl-commands#apply

kubectl wait --for=condition=complete --timeout=30s

2.我一直在使用的第二個選項是使用Kube.netesManifest@0而不是Kube.netes@1 ,因為Kube.netesManifest@0默認包含等待參數並等待直到所有 pod 啟動並運行最終超時並在管道失敗時失敗不滿足條件(任何 pod 處於失敗/掛起狀態)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM