How to set up a Kubernetes Readiness and Liveness Probes

11/24/2019

I am trying to set a readinessprobe and a livenessprobe in kubernetes using yaml and minikube

My app is exposed and working perfectly, but when I try <ip>:<port>/healthz it says

Cannot GET /healthz

This is my server.js running with a GET request to check the liveness and readiness

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

// App
const app = express();
app.get('/healthz', (req, res) => {
  res.send('fine');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

and this is my deployment.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
        name: node-web-app
spec:
  replicas: 2
  selector:
    matchLabels:
            name: node-web-app
  template:
    metadata:
      labels:
        # you can specify any labels you want here
              name: node-web-app
    spec:
      containers:
      - name: node-web-app
        # image must be the same as you built before (name:tag)
        image: banuka/node-web-app
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        imagePullPolicy: Never
      terminationGracePeriodSeconds: 60
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3
---
kind: Service
apiVersion: v1
metadata:
  labels:
    # these labels can be anything
    name: node-web-app-clusterip
  name: node-web-app-clusterip
spec:
  selector:
    name: node-web-app
  ports:
    - protocol: TCP
      port: 80
      # target is the port exposed by your containers (in our example 8080)
      targetPort: 8080
---
kind: Service
apiVersion: v1
metadata:
  labels:
    name: node-server-nodeport
  name: node-server-nodeport
spec:
  # this will make the service a NodePort service
  type: NodePort
  selector:
    name: node-web-app
  ports:
    - protocol: TCP
      # new -> this will be the port used to reach it from outside
      # if not specified, a random port will be used from a specific range (default: 30000-32767)
      nodePort: 32555
      port: 80
      targetPort: 8080

But it is not working, and how can I point the livenessprobe pod to my deployment so it I can get the health of my pods/deployment.

-- BhagyaKolitha Jayalath
kubernetes
minikube
node.js

1 Answer

11/25/2019

Try placing the following snippet in the spec field of your deployment file -

  containers:
  - image: banuka/node-web-app
    name: node-web-app
    command: ["node"]
    args: ["server.js"]
    ports:
    - containerPort: 8080
    stdin: true
    tty: true
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 2

This will give a http get request to container's IP address and if the response returned is like 200 or 300 then it will mark it successful

-- Tushar Mahajan
Source: StackOverflow