Home > Blockchain >  configuration of readiness probe for my webapp
configuration of readiness probe for my webapp

Time:02-13

I am running a sample webapp python in Kubernetes. I am not able to figure out how I make use of probes here.

  1. I want app to recover from broken states by automatically restarting pods

  2. Route the traffic to the healthy pods only.

  3. Make sure the database is up and running before the application (which in my case is Redis).

I have understanding of probes but not sure how/what values exactly to look for

My definition file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
     env: production
     app: frontend
spec:
  selector:
    matchLabels:
      env: production
      app: frontend
  replicas: 1
  template:
    metadata:
      name: myapp-pod 
      labels:
        env: production
        app: frontend
    spec:
      containers:
      - name: myapp-container
        image: myimage:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5000

Now I am doing something like this

        readinessProbe:
          httpGet:
            path: /
            port: 5000
          initialDelaySeconds: 20
          periodSeconds: 5
        livenessProbe:
        httpGet:
          path: /
          port: 5000
        intialDelaySeconds: 10
        periodSeconds: 5

CodePudding user response:

In Kubernetes you have three kind of probes:

  • Liveness - is my application still running? If this fails the application is restarted
  • Readiness - is my application ready to serve traffic? If this fails the current pod does not get traffic (e.g. via a service).
  • Startup (since 1.18) - this probe type was introduces for applications that need a long time to start. The other two probe types start after the startup probe reports success the first time.

So, in your case your application should provide one probe that checks if it still running and another if it can server traffic (also checking Redis). Be aware the liveness probes may be dangerous - maye start only with a readiness probe.

If your app provides the health check (incl. Redis) under /healthz, this is how you would define your readiness probe.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
     env: production
     app: frontend
spec:
  selector:
    matchLabels:
      env: production
      app: frontend
  replicas: 1
  template:
    metadata:
      name: myapp-pod 
      labels:
        env: production
        app: frontend
    spec:
      containers:
      - name: myapp-container
        image: myimage:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
        readinessProbe:
          httpGet:
            path: /healthz
            port: 5000
          initialDelaySeconds: 3
          periodSeconds: 3

CodePudding user response:

You need to define both

  • readinessProbe: allowing to tell whether your Deployment is ready to serve traffic at any point of time during its lifecycle. This configuration item can support different command but in your case, this would be an httpGet matching and endpoint that you would implement in your web-app (most modern stack define the endpoints by default so check check the documentation of whatever framework you are using). Note that the endpoint handler would need to check the readiness of any needed dependency, in your case you would need to check if http[s]://redis-host:redis-port responds with success
  • livenessProbe: allowing for the control plane to check continuously your pods health and to decide on actions needed to get the cluster to the desired state rolling out any pods failing to report being alive. This probe support different definitions as well and as for the readinessProbe most modern framework offer endpoints responding to such request by default

Here down you can see a sample of both probes, for which you would have two respective HTTP endpoints within your web application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
     env: production
     app: frontend
spec:
  selector:
    matchLabels:
      env: production
      app: frontend
  replicas: 1
  template:
    metadata:
      name: myapp-pod 
      labels:
        env: production
        app: frontend
    spec:
      containers:
      - name: myapp-container
        image: myimage:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
      readinessProbe:
        httpGet:
          path: /health
          port: 5000
      livenessProbe:
        httpGet:
          path: /health
          port: 5000
  • Related