0
votes

We are using client-go to talk to our kubernetes cluster (the api version: batchv1/appv1/corev1), and we mainly use three types of resources: Job, Deployment and Service.

My question is how do we judge when A Job or deployment is ready and is of running status?

For A Job, we found that when batchV1.Spec.Active > 0, the pod controlled by this job may be in either of pending status or running status. So to check if one kubernetes Job's pods are all in running status, do we have to enumerate every pods of the Kubernetes Job and check they are all in running status, then the Job is ready and running? Is there simple way to do that?

And how about the kubernetes Deployment and Service? Is there simple way to check deployment is ready?

1

1 Answers

2
votes

To check the deployment status you need to check the pod status created by this deployment. example:

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

I hope that can help you to resolve your issue .