Kubernetes check readinessProbe at Service/Deployment level

10/15/2019

Is there a way to request the status of a readinessProbe by using a service name linked to a deployment ? In an initContainer for example ?

Imagine we have a deployment X, using a readinessProbe, a service linked to it so we can request for example http://service-X:8080. Now we create a deployment Y, in the initContainer we want to know if deployment X is ready. Is there a way to ask something likedeployment-X.ready or service-X.ready ?

I know that the correct way to handle dependencies is to let kubernetes do it for us, but i have a container which doesn't crash and I have no hand on it...

-- Borhink
kubernetes
kubernetes-deployment
kubernetes-service

3 Answers

10/17/2019

I finaly found a solution by following this link : https://blog.giantswarm.io/wait-for-it-using-readiness-probes-for-service-dependencies-in-kubernetes/

We first need to create a ServiceAccount in Kubernetes to allow listing endpoints from an initContainer. After this, we ask for the available endpoints, if there is at least one, dependency is ready (in my case).

-- Borhink
Source: StackOverflow

10/15/2019

You can add a ngnix proxy sidecar on deployment Y. Set the deploymentY.initContainer.readynessProbe to a port on nginx and that port is proxied to deploymentY.readynessProbe

-- Yang
Source: StackOverflow

10/17/2019

Instead of readinessProbe You can use just InitContainer.

You create a pod/deployment X, make service X, and create a initContainer which is searching for the service X.

If he find it -> he will make the pod.

If he won't find it -> he will keep looking until service X will be created.

Just a simple example, we create nginx deployment by using kubectl apply -f nginx.yaml.

nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

Then we create initContainer

initContainer.yaml

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup my-nginx; do echo waiting for myapp-pod2; sleep 2; done;']

initContainer will look for service my-nginx, until You create it ,it will be in Init:0/1 status.

NAME                        READY   STATUS     RESTARTS   AGE
myapp-pod                   0/1     Init:0/1   0          15m

After You add service for example by using kubectl expose deployment/my-nginx and initContainer will find my-nginx service, he will be created.

NAME                        READY   STATUS     RESTARTS   AGE
myapp-pod                   1/1     Running    0          35m

Result:

Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/myapp-pod to kubeadm2
  Normal  Pulled     20s        kubelet, kubeadm2  Container image "busybox:1.28" already present on machine
  Normal  Created    20s        kubelet, kubeadm2  Created container init-myservice
  Normal  Started    20s        kubelet, kubeadm2  Started container init-myservice
  Normal  Pulled     20s        kubelet, kubeadm2  Container image "busybox:1.28" already present on machine
  Normal  Created    20s        kubelet, kubeadm2  Created container myapp-container
  Normal  Started    20s        kubelet, kubeadm2  Started container myapp-container

Let me know if that answer your question.

-- jt97
Source: StackOverflow