Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K8S Get deployment liveness probe status

Tags:

go

kubernetes

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

like image 200
JJD Avatar asked Oct 08 '20 14:10

JJD


People also ask

How do you check readiness probe of a pod in Kubernetes?

To check the status of the pod, run the kubectl get pod command and check the STATUS column. As you can see, in this case all the pods are in running state. Also, the READY column states the pod is ready to accept user traffic.

How do I check my pod status in Kubernetes?

If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment. In the Conditions section, the Ready field should indicate "True".

Does liveness probe restart pod or container?

Giving up in case of liveness probe means restarting the container. In case of readiness probe the Pod will be marked Unready.


2 Answers

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
like image 82
Jonas Avatar answered Oct 29 '22 12:10

Jonas


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

like image 33
Ruben Avatar answered Oct 29 '22 14:10

Ruben