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 2GThe 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.com/StatelessService/replicaset.ymlVerify the ReplicaSet is working correctly:
kubectl get rsNAME DESIRED CURRENT READY AGE random-generator 3 3 3 17s
kubectl describe rs random-generatorName: 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 podsNAME 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.com/StatelessService/service.ymlLet’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:8080Can 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.com/StatelessService/pv-and-pvc.ymlUpdate the ReplicaSet to use the PersistentVolumeClaim:
kubectl apply -f https://k8spatterns.com/StatelessService/replicaset-with-pv.ymlDelete all Pods so that they are recreated with the new configuration:
kubectl delete pod -l app=random-generatorCall several times again our service with
kubectl run -itq --rm --image=k8spatterns/curl-jq \
--restart=Never curl -- http://random-generator:8080Now let’s check your local directory logs/ and see how it gets populated with log files from the three Pods