Using a Message Broker - PART 2: Kubernetes

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

Prerequisites

To complete this guide, you need:

Apache Artemis

Apache Artemis deployment

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

artemis-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: artemis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: artemis
  template:
    metadata:
      labels:
        app: artemis
    spec:
      containers:
        - name: artemis
          image: 'quay.io/artemiscloud/activemq-artemis-broker-kubernetes:latest'
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 61616
            - containerPort: 8161
          env:
            - name: AMQ_USER
              value: admin
            - name: AMQ_PASSWORD
              value: admin
            - name: AMQ_DATA_DIR
              value: //home/jboss/data

apply the Deployment configuration to Kubernetes:

kubectl apply -f artemis-deployment.yaml

Apache Artemis service

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

artemis-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: artemis-service
  labels:
    app: artemis
spec:
  ports:
    - protocol: TCP
      port: 61616
      targetPort: 61616
      name: artemis-port
    - protocol: TCP
      port: 8161
      targetPort: 8161
      name: artemis-console-port
  selector:
    app: artemis

apply the Service configuration to Kubernetes:

kubectl apply -f artemis-service.yaml

Image Registry

To make the my-jms-app: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-jms-app:latest Docker Image, to the quay.io Image Registry.

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

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

Tag the Docker image:

podman tag my-jms-app quay.io/tborgato/my-jms-app

Push the my-jms-app Docker Image to it:

podman push quay.io/tborgato/my-jms-app

At this point, the my-jms-app: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-jms-app

Deploy to Kubernetes

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

deployment-my-jms-app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-jms-app-deployment
  labels:
    app: my-jms-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-jms-app
  template:
    metadata:
      labels:
        app: my-jms-app
    spec:
      containers:
      - name: my-jms-app
        image: quay.io/tborgato/my-jms-app
        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: JBOSS_MESSAGING_CONNECTOR_HOST
          value: artemis-service
        - name: JBOSS_MESSAGING_CONNECTOR_PORT
          value: '61616'

apply the Deployment configuration to Kubernetes:

kubectl apply -f deployment-my-jms-app.yaml

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

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

Check the application

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

$ minikube service my-jms-app-deployment --url
http://192.168.39.143:31858
Send and consume messages using a queue (replace "http://192.168.39.143:31858" with the output of the command above!):
$ curl -X GET http://192.168.39.143:31858/hello/message/send?content=Hello%20World
Sent Hello World to getting-started-queue
< Back to Guides