DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Deploying Spring Boot and MongoDB as Containers Using Kubernetes and Docker

Deploying Spring Boot and MongoDB as Containers Using Kubernetes and Docker

Learn how to deploy Spring Boot and MongoDB as containers using Kubernetes and Docker.

Aritra Nag user avatar by
Aritra Nag
·
Nov. 12, 18 · Tutorial
Like (11)
Save
Tweet
Share
23.48K Views

Join the DZone community and get the full member experience.

Join For Free

For this tutorial, you'll have a Dockerized sample spring-boot application that talks to MongoDB for GET/POST REST APIs and deployed in Kubernetes cluster.

Prerequisites

  • minikube
  • kubectl
  • docker
  • maven

Docker is a Linux container management toolkit with a “social” aspect, allowing users to publish container images and consume those published by others. A Docker image is a recipe for running a containerized process, and in this guide, we will build one for a simple Spring boot application.

Find the Github repository here.

Docker File:

FROM openjdk:8-jdk-alpine
ADD target/spring-boot-mongo-docker-1.0.0.jar app.jar
ENV JAVA_OPTS=””
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar


Docker Compose:

version: “2.1”
services:
mongo:
image: mongo:3.2.4
ports:
– 27017:27017
command: –smallfiles

mongo-init:
build: ./mongo-init
links:
– mongo

mongo-client:
image: mongoclient/mongoclient
ports:
– 3030:3000
environment:
– MONGOCLIENT_DEFAULT_CONNECTION_URL=mongodb://mongo:27017
links:
– mongo

# APP ***************************************************************************************
spring-boot-mongo-docker:
image: aritranag20/spring-boot-mongo-docker
ports:
– 8080:8080
links:
– mongo
entrypoint: “java -Djava.security.egd=file:/dev/./urandom -jar /app.jar”


Set Up a Spring Boot App

If you want to run with Maven, execute:

./mvn package && java -jar target/spring-boot-mongo-docker-1.0.0.jar


and go to http://localhost:8080/customer/ to see your persisted customers.

Dockerisation

We need a container with JDK to run our Spring Boot application. There are many images with JDK 8 publicly available already, but just to get the end-to-end experience, we will built a minimal container based on Alpine Linux with JDK 8:

docker build -t  aritranag20/spring-boot-mongo-docker .


Then log into Docker, with your credentials after signing up on docker.io,

docker login


Execute the following command to push the image to the Docker registry.

docker push aritranag20/spring-boot-mongo-docker

Containerize It

If you want to run with Docker, execute:

./docker-compose up


Deploying on Kubernetes

Now that we have the image of our application available in the Docker registry, we can deploy it in a Kubernetes cluster. We will also set up a node for MongoDB to be used as a backend by our application.

Start local Kubernetes cluster with the following command:

minikube start


We can then launch a dashboard for the cluster:

minikube dashboard


Next, create the deployment of our application in the cluster.

kubectl create -f deployment.yml


We can see a description of the service with:

kubectl describe service spring-boot-mongo-docker


Now get the exact address of the service with:

minikube service spring-boot-mongo-docker


which will launch browser and point to the endpoint. For e.g. in my case,

curl http://192.168.99.101:30864/user =>
[{"id":"58bcd7ad5908010005cce257","firstName":"Arun","lastName":null,"email":null,"address":{"street1":null,"street2":null,"town":null,"postcode":null,"state":null}}]

Summary

Congratulations! You’ve just created a Docker container for a Spring Boot app with MongoDB and hosted the app in Kubernetes! Spring Boot apps run on port 8080 inside the container by default and we mapped that to the same port on the pods which is load balanced across multiple replicas of the service and can be assessed by getting the NodePort from  kubectl describe service spring-boot-mongo-docker  command.

Optional

Finally to stop your local kubernetes cluster:

minikube stop


Docker (software) Spring Framework Kubernetes Spring Boot MongoDB

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Application Architecture Design Principles
  • Master Spring Boot 3 With GraalVM Native Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: