Xenonstack Recommends

Overview of Developing and Deploying Spring Boot Microservices

Acknowledging Data Management
          Best Practices with DataOps

Subscription

Introduction to Spring Boot Microservices

In this blog post, we will discuss about Spring Boot Microservices on Kubernetes, that is how to develop an application with microservice architecture using the Spring Boot framework and then launch a local Minikube cluster to deploy that application as a container on that cluster. You have various choices to build a Spring application with microservices architecture. Still, Spring Boot is one of the most preferred frameworks for building production-ready Spring applications rapidly. Spring Boot is attracting many users due to its enhanced simplification while writing and developing Spring applications. You can deploy these Spring applications as WAR and JAR files, but Containers is another excellent choice. With a general-purpose Container Orchestration platform, you can easily manage a system of microservices; that's where Kubernetes comes into the picture. It is a platform for automating the deployment and management of containerized applications and services. With Kubernetes, you can group multiple containers into logical units to discover and manage applications with microservice architecture.

Why are Spring Boot Microservices Important?

  1. Docker: You can download and install the Docker Community Edition (CE) by referring to their official documentation as per the platform being used by you.
  2. Docker Hub Account: You need to have a Docker Hub Account to push your application image.
  3. Kubernetes Cluster: You will also need a Kubernetes cluster running on your local system. If it's not already set up, you can create a cluster using Minikube or Kind.
  4. Kubectl: kubectl is a command-line utility required to run your commands against Kubernetes clusters. You can install and set it up using the Kubernetes official documentation.

Create a Spring Boot Application with Microservice Architecture

To be done by the designated team. The application should rely on the MongoDB database.

Containerize the Spring Boot Application

Create a file named 'Dockerfile' in the root directory of your application with the following content: This Dockerfile contains the following commands:
  1. FROM that defines the base layer for your application image. In this case use the OpenJDK:8 version.
  2. ENV command is helps to set the desirable environment variables.
  3. EXPOSE defines the listening port within the container.
  4. COPY is used to copy all the JAR files from the build/libs/ directory to the directory set in your APP_HOME environment variable, i.e.,/user/app.
  5. WORKDIR sets your current working directory to the directory defined in your APP_HOME environment variable, i.e.,/user/app.
  6. ENTRYPOINT executes sh -c that opens a shell inside the container.
  7. CMD provides an argument to the command defined in ENTRYPOINT. Thus, it executes java -jar $APP_FILE inside the container's shell.
Now, build the docker image for your application through the following command: Note: The command represents your current working directory that is the root directory of your application. Tag this image and push it on to your Docker Hub Account: With this, your application's image is ready for use.
Because of their flexibility, failure, and self-healing properties, Kubernetes implementation is the best for production purposes. Source - Implementing a Kubernetes Strategy in Your Organization

Deploy the Application on Kubernetes Cluster

Kubernetes -- a general-purpose container-orchestrator --  is designed to run complex applications efficiently with many scalable components.

MongoDB Deployment

This application possesses a microservice architecture and relies heavily on the MongoDB database. Thus, you first need to deploy an instance of MongoDB for your application. Create a file named mongo. yaml with all the necessary resources that include deployment, service, config map, secret, and pvc, as shown below. apiVersion: v1 kind: PersistentVolumeClaim metadata:       name: mongo-pvc spec:     accessModes:          - ReadWriteOnce     resources:         requests:       storage: 256Mi --- apiVersion: v1 kind: Service metadata:       name: mongo spec:     selector:         app: mongo     ports:          - port: 27017       targetPort: 27017 --- apiVersion: v1 kind: ConfigMap metadata:       name: mongodb data:      database-name: myapp --- apiVersion: v1 kind: Secret metadata:       name: mongodb type: Opaque data:      database-password: MTIzNDU2      database-user: cGlvdHI= --- apiVersion: apps/v1 kind: Deployment metadata:       name: mongo spec:      selector:          matchLabels:               app: mongo      template:          metadata:                labels:                app: mongo      spec:          containers:          - name: mongo              image: mongo:latest              ports:                    - containerPort: 27017                env:                     - name: MONGO_INITDB_DATABASE                        valueFrom:                            configMapKeyRef:                                name: mongodb                                key: database-name                     - name: MONGO_INITDB_ROOT_USERNAME                        valueFrom:                            secretKeyRef:                                name: mongodb                                key: database-user                      - name: MONGO_INITDB_ROOT_PASSWORD                        valueFrom:                            secretKeyRef:                                name: mongodb                                key: database-password              volumeMounts:                    - name: storage                        mountPath: /data/db          volumes:          - name: storage              persistentVolumeClaim:                    claimName: mongo-pvc After creating the above file, you are ready to deploy your MongoDB instance with the following command: Now, You can check the status of your MongoDB pod by: Once the MongoDB instance runs the state, you can move further to deploy your Spring Boot Application.

Spring Application Deployment

Since your MongoDB database is ready now, you can create a file named myapp.yaml consisting of all the required resources: apiVersion: apps/v1 kind: Deployment metadata:       name: myapp spec:     replicas: 1     selector:         matchLabels:         app: myapp     template:         metadata:         labels:              app: myapp         spec:        containers:              - name: app                   image: priyankaagrawal28/sample-project/my-app:1.0                   ports:                         - containerPort: 8000                   env:                     - name: MONGO_DATABASE                        valueFrom:                            configMapKeyRef:                                name: mongodb                                key: database-name                     - name: MONGO_USERNAME                        valueFrom:                            configMapKeyRef:                                name: mongodb                                key: database-user                      - name: MONGO_PASSWORD                        valueFrom:                            configMapKeyRef:                                name: mongodb                                key: database-password --- apiVersion: v1 kind: Service metadata:       name: myapp spec:      selector:          app: myapp      ports:          - port: 80       targetPort: 8000      type: NodePort Now you are here, ready to deploy your application on the cluster through the following command: Check the status of your pods by: Once your pods are running, you can access the application on your local browser: http://<your-node-ip>:8000

Summary

This guide walks users through all about how you develop your Spring Boot Application with a microservice architecture and deploy it on your local Kubernetes cluster so that users can connect to the application, which they have exposed as a Service in Kubernetes.

Related blogs and Articles

Overview of New Product Development and it's Best Practices

Enterprise Digital Platform

Overview of New Product Development and it's Best Practices

Introduction of New Product Development The ability to innovate and successfully bring new game-changing, engaging products into the market is crucial for organizations and economies. Transforming vision to live products is a process that seems to be a hurdle to most Entrepreneurs. Building a product is like cooking a dish. Everything needs to be in perfect order and quantity. Product...