Search code examples
kuberneteskubectl

Pipeline task to Check Kubectl deployment status and get startup log to publish as a artifact


I have a yaml CD pipeline which is deploying a docker image to Tanzu kubernetes cluster successfully.

Post-deployment verification, i need to get the deployment status and collect the pod startup log to upload as an artifact.

Deployment status: I tried with the command kubectl get pods -n test -o jsonpath='{.status.phase}' but its returns nothing, actually we have an active pod running.

To upload the log:

I can use the command kubectl log podname to get the logs, but we are redeploying on same service everytime, which means the existing pod will be replaced with a new one, in case of new deployments failed, it shows to 2 pods, old one with running status and new one with failed status.

How to correctly choose the latest deployed pod to collect the logs.


Solution

  • So to know deployment status of pod you can use the following command:

    kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.containerStatuses[*].state}'
    

    To get your latest pod you can use resposne from previous command, it should give you smth similar to this:

    {"running":{"startedAt":"2023-05-24T07:23:52Z"}}
    

    So then you can check "startetAt" of all pods and find necessary one.

    But maybe firstly you need to identify all pods for particular deployment. You can do it with the following command:

    kubectl get pods -n <namespace> -l <label key>=<label name>
    

    In my cluster there is a label app.kubernetes.io/instance that is highlighting deployment. In your cluster it can be different one.