This is built and tested with minikube
Step 1:
minikube start
Step 2:
Navigate to the directory where your Dockerfile, requirements.txt and scratch.py is. And build the docker image sampleapp
docker build -t sampleapp:latest .
Step 3:
Create kubernetes objects using the K8S yaml fies
kubectl apply -f deployment.yaml
This will create the micro-service from the sampleapp docker image that we created in Step2. 4 replicas would always be created and maintained by kubernetes.
kubectl apply -f service.yaml
This will create a service that ties with the micro-service that we just created. Other kubernetes pods can access our micro-service using this service we just created hello-python-service:5000
Step 4:
Create the cronjob in kubernetes that writes the HTTP status returned by the micro-service o a log file
kubectl apply -f cron.yaml
This will create a cronjob in kubernetes using a lightweight alpine image and is scheduled to run evey 2 mins. It checks the HTTP response of the micro-service from Step2 and logs http500 to a log file which is mounted to the actual host using volumes
If you are using minikube, then you have to do an extra step before Step 4
minikube mount dir/on/host:dir/on/minikubeVM
This image is bare minimal, we could package our app to this image which would result in an even lesser attack surface
I could even use the same python distroless container to create the service container that checks the health of the micro-service. Then i do no need to use the curl command anymore and everything could be done in python. It would be an interesting set up since the distroless image from Google does not even have a shell to begin with. A really really small attack surface
The microservice itself should log the following metrics
- The number of HTTP requests that result in 2XX or 5XX responses
- Latency of HTTP 2XX responses