We recommend using a Minikube installation for this example. For details, please refer to the installation instructions.
We assume you use the default 2GB memory for your Minikube VM to play with memory limits. You can adjust this value with the --memory
flag.
;
To access the PersistentVolume used in this demo, mount a local directory logs/
into the Minikube VM with the following command:
minikube start --mount --mount-string="$(pwd)/logs:/tmp/example" --memory 2G
The directory is now available within the Minikube VM at the path /tmp/example
.
For this example, we will use the random number service, a simple REST service that returns just a random number. This service is in the image k8spatterns/random-generator:1.0
from Docker Hub.
First, create a ReplicaSet for your stateless service:
kubectl apply -f https://k8spatterns.io/StatelessService/replicaset.yml
Verify the ReplicaSet is working correctly:
kubectl get rs
NAME DESIRED CURRENT READY AGE random-generator 3 3 3 17s
kubectl describe rs random-generator
Name: random-generator Namespace: default Selector: app=random-generator Labels: <none> Annotations: <none> Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed ....
You should see that all three Pods are coming up as expected.
Let’s play with the ReplicaSet and check whether the self-healing works. For this, let’s kill a random Pod:
kubectl delete $(kubectl get pods -l app=random-generator -o name | head -1)
Check whether a new Pod is created so that the declared state of 3 replicas is fulfilled again)
kubectl get pods
NAME READY STATUS RESTARTS AGE random-generator-8r727 1/1 Running 0 5s random-generator-qkv2c 1/1 Running 0 2m1s random-generator-smtsp 1/1 Running 0 2m1s
To access the ReplicaSet from within the cluster, we create a Service of type clusterIP
:
kubectl apply -f https://k8spatterns.io/StatelessService/service.yml
Let’s test the service by calling the Pod from within the cluster:
kubectl run -itq --rm --image=k8spatterns/curl-jq \
--restart=Never curl -- http://random-generator:8080
Can you see how different Pods are hit when you repeat this command multiple times?
Create a PersistentVolume and a PersistentVolumeClaim:
kubectl apply -f https://k8spatterns.io/StatelessService/pv-and-pvc.yml
Update the ReplicaSet to use the PersistentVolumeClaim:
kubectl apply -f https://k8spatterns.io/StatelessService/replicaset-with-pv.yml
Delete all Pods so that they are recreated with the new configuration:
kubectl delete pod -l app=random-generator
Call several times again our service with
kubectl run -itq --rm --image=k8spatterns/curl-jq \
--restart=Never curl -- http://random-generator:8080
Now let’s check your local directory logs/
and see how it gets populated with log files from the three Pods