K8S Get deployment liveness probe status

pods, err := client.CoreV1().Pods("").List(meta_v1.ListOptions{})
if err != nil {
    log.Fatal(err)
}

for _, pod := range pods.Items {
    for _, container := range pod.Spec.Containers {
        fmt.Println(container.LivenessProbe)
        fmt.Println(container.ReadinessProbe)
    }
}

You can iterate over pod.Items to get the container LivenessProbe and ReadinessProbe


How to do this depends on what you want to do.

Deployment

The Deployment contains a PodTemplate, that is used for creating each replica.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - image: myimage
        name: myapp
        livenessProbe:
          # your desired liveness check

You can get the desired PodTemplate from deployments using client-go

For example:

clientset := kubernetes.NewForConfigOrDie(config)
deploymentClient := clientset.AppsV1().Deployments("mynamespace")
deployment, err := deploymentClient.Get("myapp", metav1.GetOptions{})

for _, container := range deployment.Spec.Template.Spec.Containers {
    container.LivenessProbe // add your logic
}

Note: The Deployment only contains the desired PodTemplate, so to look at any status, you have to look at the created Pods.

Pods

You can list the Pods created from the deployment by using the same labels as in the selector of the Deployment.

Example list of Pods:

pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{
    LabelSelector: "app=myapp",
})

// check the status for the pods - to see Probe status
for _, pod := range pods.Items {
    pod.Status.Conditions // use your custom logic here

    for _, container := range pod.Status.ContainerStatuses {
        container.RestartCount // use this number in your logic
    }
}

The Status part of a Pod contain conditions: with some Probe-information and containerStatuses: with restartCount:, also illustrated in the Go example above. Use your custom logic to use this information.

A Pod is restarted whenever the livenessProbe fails.

Example of a Pod Status

status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-09-15T07:17:25Z"
    status: "True"
    type: Initialized
  containerStatuses:
  - containerID: docker://25b28170c8cec18ca3af0e9c792620a3edaf36aed02849d08c56b78610dec31b
    image: myimage
    imageID: docker-pullable://myimage@sha256:a432251b2674d24858f72b1392033e0d7a79786425555714d8e9a656505fa08c
    name: myapp
    restartCount: 0

Tags:

Go

Kubernetes