K8S Get deployment liveness probe status

10/8/2020

We have defined K8s liveness and readiness probes in our deployment resource (we have defied there liveness...), we need with client-go lib to access this liveness probe, how can we do that?

I've tried it with the client-go lib

https://github.com/kubernetes/client-go

as follows:

client.Discovery().RESTClient().Get()

I've also tried to play around with the go library but did not find any deployment property on client.CoreV1(). However I do find service pod etc. What am I missing here?

PodList, err := client.CoreV1().Pods("mynamespace").List(metav1.ListOptions{LabelSelector: "run=liveness-app"})

At the end I need to get the pod liveness status according to the liveness probe which is defined in the deployment. I mean live or dead

-- JJD
go
kubernetes

2 Answers

10/11/2020

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
-- Jonas
Source: StackOverflow

10/8/2020
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

-- Ruben
Source: StackOverflow