How to connect Spring boot microservice to MongoDB cluster

Learn how to deploy MongoDB Cluster (Replica set) in Kubernetes and connect your Spring Boot microservice application to DB.

In this post, would like to briefly explain how to deploy MongoDB as a Cluster (replica set configuration) with 3 pods in a Kubernetes cluster and connect a Spring boot application/microservice to it. There are few key points we need to keep in mind while connecting the spring boot application to a MongoDB cluster (replica set).

replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.

To understand more about MongoDB replica set, refer to official documentation.

Before getting started

  • Ensure you have a Kubernetes cluster with storage for creating PVC/PV for MongoDB.
  • Helm 3.1.x

Let us Start with deploying MongoDB

I am going to use MongoDB(TM) Packaged By Bitnami For Kubernetes Helm Chart to deploy MongoDB in the Kubernetes cluster.

Create Values.yaml file with architecture set to replicaset, initial db creation / authentication details, persistent storage details (if you want to change the defaults), and optionally RBAC, PSP options.

This helm chart supports the replicaset architecture with and without a MongoDB® Arbiter. Refer Chart documentation, MongoDB documentation for more details on Arbiter.

I am going with the default option, which is with Arbiter.

Deploy the Mongodb to Kubernetes Cluster

kubectl create ns mongodb
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install mongo -f values.yaml bitnami/mongodb -n mongodb

With the above values.yaml configuration, it disables MongoDB Arbiter and creates one Statefulset with 3 replicas.

Ensure deployment is successful by running below commands

helm list -n mongodb
kubectl get statefulsets,pods,rs -n mongodb

Validate the deployment of MongoDB using the steps suggested in Chart deployment output.

Create a Simple Spring Boot application to connect to MongoDB

I would like to make use of Spring Data Repositories for Mongo to quickly expose my entity as a RESTful service. You can find the source code for the sample application in Github.

Clone the project and change the pom.xml spring-boot.build-image.imageName property to point to your image repository and run the below command to build the image.

./mvnw spring-boot:build-image

This command will build the container image. If you would like to skip the tests while building the image, you may run

./mvnw spring-boot:build-image -DskipTests

Push the image to your image repository.

docker push harivemula/person:0.0.1-SNAPSHOT

Deploy the Spring Boot Application to Kubernetes cluster

In the application/image we have configuration for our local mongodb, we need to create a new application.yaml with configuration of MongoDB deployed in Cluster.

Create a secret with the mongo credentials (as mentioned in MongoDB Helm values.yaml)

kubectl create secret generic mongo-creds --from-literal=mongodb-username=testuser --from-literal=mongodb-password=secret -n mongodb

Create the application.yaml file with MongoDB connection properties.

If you are connecting to MongoDB Replicaset, you need to use spring.data.mongodb.uri only, instead of using username/password, etc.. properties in the application.yaml.

Make sure you don’t have host/port/username/password/database etc… properties in your default application.properties/yaml as well, you may use spring profiles to avoid conflict with configuration files.

You will get the below error if it finds any of the above properties along with uri.

java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified

Create the ConfigMap with the above application.yaml.

kubectl create cm person-config -n mongodb --from-file=application.yaml=application.yaml

Deploy the application using the below deployment manifest, which creates a deployment and exposes it as a service. Change the image as per your image repository.

Verify your deployment status, check the logs of the pod and ensure it is able to connect to your mongodb cluster.

To easily validate Person API, without exposing the service as Loadbalancer / NodePort, I have deployed a test pod with one of my images.

kubectl run ubuntu-utils --image harivemula/ubuntu-utils -n mongodb
kubectl exec -n mongodb ubuntu-utils -it -- curl --location --request POST 'http://person-service/persons' \
--header 'Content-Type: application/json' \
--data-raw '{
    "firstname": "test1", "lastname": "user"
}'
kubectl exec -n mongodb ubuntu-utils -it -- curl --location --request GET 'http://person-service/persons'

Conclusion

In this post, we have gone through how to deploy MongoDB with replica set configuration into Kubernetes cluster, deployed a simple Spring boot microservice application with specific settings required for it to connect to MongoDB cluster.

Popular Posts

97
Total Page Visits: 16364 - Today Page Visits: 5

Leave a Reply

Your email address will not be published. Required fields are marked *