Now that your Docker images are built, deploy them using a Kubernetes resource definition.
To deploy the hazelcast-caching microservice, first create the kubernetes.yaml file in the start directory:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: hazelcast-caching-statefulset
labels:
app: hazelcast-caching
spec:
replicas: 2
serviceName: hazelcast-caching-service
selector:
matchLabels:
app: hazelcast-caching
template:
metadata:
labels:
app: hazelcast-caching
spec:
containers:
- name: hazelcast-caching-container
image: hazelcast-caching:latest
imagePullPolicy: IfNotPresent
ports:
- name: openliberty
containerPort: 9080
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
---
apiVersion: v1
kind: Service
metadata:
name: hazelcast-caching-service
spec:
type: NodePort
selector:
app: hazelcast-caching
ports:
- protocol: TCP
port: 9080
targetPort: 9080
nodePort: 31000
By default, we create 2 replicas of hazelcast-caching microservice behind the hazelcast-caching-service which forwards
requests to one of the pods available in the kubernetes cluster.
MY_POD_NAME is an environment variable made available to the pods so that each microservice knows which pod they are in.
This is going to be used in this guide in order to show which pod is responding to the http request.
As a second step, we will create rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: default-cluster
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: default
namespace: default
Role Based Access Controller(RBAC) configuration is used to give access to Kubernetes Master API from pods which runs
microservices. Hazelcast requires a read access to autodiscover other hazelcast members and form hazelcast cluster.
Run the following commands to deploy the resources as defined in kubernetes.yaml and rbac.yaml in the specified order:
kubectl apply -f rbac.yaml
kubectl apply -f kubernetes.yaml
run the following command to check the status of your pods:
You’ll see an output similar to the following if all the pods are healthy and running:
NAME READY STATUS RESTARTS AGE
hazelcast-caching-statefulset-0 1/1 Running 0 20m
hazelcast-caching-statefulset-1 1/1 Running 0 20m
You should also check if hazelcast cluster is formed by checking one of the pod’s log file:
kubectl logs hazelcast-caching-statefulset-1
Members {size:2, ver:2} [
Member [172.17.0.4]:5701 - 71009ef7-ee18-45f0-8a8f-e9321931e9ce this
Member [172.17.0.5]:5701 - 99222e16-93e5-4453-ac9e-cdf3e80069c6
]
Next you will make requests to your services.
Open your favorite terminal and send http get requests in a loop via curl command.
This request asks for the value of key=1 and prints value and which pod has replied to the request.
You might need to wait up to 30 sec before microservices accepts traffic.
while true; do curl 192.168.99.100:31000/get?key=1;echo; sleep 2; done
You should see an output like below. The value is null because we have not put any data yet.
podname shows that which kubernetes pod replied to the request.
{"value":null,"podName":"hazelcast-caching-statefulset-1"}
{"value":null,"podName":"hazelcast-caching-statefulset-1"}
{"value":null,"podName":"hazelcast-caching-statefulset-0"}
{"value":null,"podName":"hazelcast-caching-statefulset-1"}
{"value":null,"podName":"hazelcast-caching-statefulset-0"}
Break the current loop and put some data into hazelcast-caching microservice.
curl "http://192.168.99.100:31000/put?key=1&value=hazelcast_springboot_openliberty"
Although request has been executed by one specific kubernetes pod, by querying the data in a loop will show data the data
is actually shared by multiple pods. To see this, execute following command again.
while true; do curl 192.168.99.100:31000/get?key=1;echo; sleep 2; done
As you can see both pods are returning the same data.
{"value":"hazelcast_springboot_openliberty","podName":"hazelcast-caching-statefulset-1"}
{"value":"hazelcast_springboot_openliberty","podName":"hazelcast-caching-statefulset-0"}
{"value":"hazelcast_springboot_openliberty","podName":"hazelcast-caching-statefulset-1"}
{"value":"hazelcast_springboot_openliberty","podName":"hazelcast-caching-statefulset-0"}
{"value":"hazelcast_springboot_openliberty","podName":"hazelcast-caching-statefulset-1"}