Using Infinispan remote cache - PART 2: Kubernetes

In this guide, you will learn HOW-TO run the Docker Image we built in Using Infinispan remote cache - PART 1: Docker Image on Kubernetes.

Prerequisites

To complete this guide, you need:

Infinispan

We basically will repeat everything we did in WildFly Java Microservice - PART 2: Kubernetes but, before, we will deploy Infinispan on Kubernetes.

Infinispan configmap

Create a file named infinispan-configmap.yaml with the following content:

infinispan-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: infinispan-secret
  labels:
    app: infinispan
data:
  USER: admin
  PASS: 123pippobaudo

apply the ConfigMap configuration to Kubernetes:

kubectl apply -f infinispan-configmap.yaml

Infinispan deployment

Create a file named infinispan-deployment.yaml with the following content:

infinispan-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: infinispan
spec:
  replicas: 1
  selector:
    matchLabels:
      app: infinispan
  template:
    metadata:
      labels:
        app: infinispan
    spec:
      containers:
        - name: infinispan
          image: 'quay.io/infinispan/server:latest'
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5432
          env:
            - name: USER
              valueFrom:
                configMapKeyRef:
                  name: infinispan-secret
                  key: USER
            - name: PASS
              valueFrom:
                configMapKeyRef:
                  name: infinispan-secret
                  key: PASS
            - name: JAVA_OPTIONS
              value: '-Dinfinispan.cluster.stack=kubernetes -Djgroups.dns.query="infinispan-dns-ping.myproject.svc.cluster.local"'

apply the Deployment configuration to Kubernetes:

kubectl apply -f infinispan-deployment.yaml

Infinispan service

Create a file named infinispan-service.yaml with the following content:

infinispan-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: infinispan-service
  labels:
    app: infinispan
spec:
  ports:
    - protocol: TCP
      port: 11222
      targetPort: 11222
  selector:
    app: infinispan

apply the Service configuration to Kubernetes:

kubectl apply -f infinispan-service.yaml
Note
if you are using minikube, and you want to test if Infinispan Server on Kubernetes is working, run kubectl port-forward svc/infinispan-service 11222:11222, then open http://0.0.0.0:11222/console/ in your browser and log in as admin / 123pippobaudo

Image Registry

To make the my-jaxrs-app-infinispan:latest Docker Image available to Kubernetes, you need to push it to some Image Registry that is accessible by the Kubernetes cluster you want to use.

Quay.io

There are many options to achieve this; in this guide, we will push the my-jaxrs-app-infinispan:latest Docker Image, to the quay.io Image Registry.

Create a public repository named my-jaxrs-app-infinispan on quay.io (e.g. https://quay.io/repository/tborgato/my-jaxrs-app-infinispan).

Note
replace tborgato with the name of your account in all the commands that will follow

Tag the Docker image:

podman tag my-jaxrs-app-infinispan quay.io/tborgato/my-jaxrs-app-infinispan

Push the my-jaxrs-app-infinispan Docker Image to it:

podman push quay.io/tborgato/my-jaxrs-app-infinispan

At this point, the my-jaxrs-app-infinispan:latest Docker Image should be publicly available and free to be consumed by any Kubernetes Cluster; you can verify this by running:

podman pull quay.io/tborgato/my-jaxrs-app-infinispan

Deploy to Kubernetes

To deploy our my-jaxrs-app-infinispan Docker Image on minikube, create a file named deployment-my-jaxrs-app-infinispan.yaml (see kubernetes deployment) in the same directory as the Dockerfile and the pom.xml file, with the following content:

deployment-my-jaxrs-app-infinispan.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-jaxrs-app-infinispan-deployment
  labels:
    app: my-jaxrs-app-infinispan
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-jaxrs-app-infinispan
  template:
    metadata:
      labels:
        app: my-jaxrs-app-infinispan
    spec:
      containers:
      - name: my-jaxrs-app-infinispan
        image: quay.io/tborgato/my-jaxrs-app-infinispan
        ports:
        - containerPort: 8080
        - containerPort: 9990
        livenessProbe:
          httpGet:
            path: /health/live
            port: 9990
        readinessProbe:
          httpGet:
            path: /health/ready
            port: 9990
        startupProbe:
          httpGet:
            path: /health/started
            port: 9990
        env:
        - name: INFINISPAN_SERVER_HOST
          value: infinispan-service
        - name: INFINISPAN_SERVER_PORT
          value: '11222'
        - name: INFINISPAN_SERVER_USER
          valueFrom:
            configMapKeyRef:
              name: infinispan-secret
              key: USER
        - name: INFINISPAN_SERVER_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: infinispan-secret
              key: PASS

apply the Deployment configuration to Kubernetes:

kubectl apply -f deployment-my-jaxrs-app-infinispan.yaml

We used minikube as Kubernetes Cluster, hence we expose the deployment as NodePort:

kubectl expose deployment.apps/my-jaxrs-app-infinispan-deployment --type=NodePort --port=8080

Check the application

Find out on what IP address/port, minikube is exposing your service:

$ minikube service my-jaxrs-app-infinispan-deployment --url
http://192.168.39.143:31076

Open http://192.168.39.143:31076/ in your Browser, enter some name in the Name field (e.g. "pippo") and press `Say Hello`".

Stop the application:

kubectl scale deploy my-jaxrs-app-infinispan-deployment --replicas=0

Start the application once again:

kubectl scale deploy my-jaxrs-app-infinispan-deployment --replicas=1

Type a different name in the Name field and press Say Hello again: the page should display something like "last time you were pippo" and that means that the previous name was stored in the Infinispan Server.

< Back to Guides