MongoDB Atlas Peering: Spring Boot App hosted in AKS

Curious in learning how you can create Peering Endpoints between Azure Kubernetes Services (AKS) and MongoDB Atlas using major cloud vendors like Azure? This blog takes you through a step-by-step process in order to use your Atlas Cluster in the spring boot app, hosted in AKS.

Posted on March 5, 2021 by Rajesh Vinayagam

Organizations today look for application development that is agile and that can scale quickly as required. Modern applications architecture prefer Kubernetes and MongoDB for application orchestration and data management needs. And while most major cloud vendors such as AWS, Azure, and Google offer Kubernetes as a Managed Service(PaaS), MongoDB offers a fully-managed database service called Atlas. Peering or Private Endpoints help connect to an Atlas cluster running on AWS, Azure or Google Cloud.

In this blog, lets see how we can create Peering between Azure Kubernetes Services (AKS) Cluster and MongoDB Atlas running on Azure.

AKS cluster are usually deployed within customers’ Virtual Network (VNet), and MongoDB Atlas clusters are deployed within their own VNet. We first create a VNet Peering between AKS and Mongo Atlas, and then use the private address space allocated in Azure AKS as the whitelisted IP’s in Atlas.


Pre-requisites:

Mongo Atlas (Azure)

  1. Create an Atlas account.
  2. Create an Organization with one project.
  3. Create an Atlas Cluster (Peering option is available only on M10 and above).
  4. Create a DB User with access to any database.

Kubernetes Cluster (AKS)

  1. Create an Azure Subscription, if you don’t have one already.
  2. Create a Resource Group and have a region similar to the one in the Atlas Cluster.
  3. Create Kubernetes service, Container Registry and VPN under the above resource group.
  4. Deploy the Spring Boot App.
  5. Create an external service as Load Balancer.

Creating an Atlas Cluster:

Under the project, choose Create Cluster and follow the wizard shown below.

Select the required cloud provider. For this instance, we are using Azure, and then choose the required region.

For Cluster Tier use M10, as Peering is allowed only from m10 and above. It usually takes about 10 minutes for the cluster to be created. Once that is done, we generate a DB user.

Adding a New DB User:

Add a DB user with read and write access to the database. This will be used in the connection string for connecting to a database.


Creating an AKS Cluster

Create a Kubernetes service, following the wizard shown.

AKS : Create AKS Cluster

Use the default setting for others. One critical step lies in choosing the networking configuration. By default, AKS uses kubenet for network configuration. We choose Azure CNI instead to use VPN Peering. AKS doesn’t allow a change to these settings once the cluster has already been created, so it has to done while the cluster is being created.

Now, we can choose to use the created nirtual network and subnets, or create a new one. This virtual network will be used later for the MongoDB Peering.

AKS Cluster: Create New Virtual Network and Subnet

Our next step is to use the container register under the Integration tab. Here too, we can opt to use the existing container registry or create a new one.

Container registry is a repository for the spring boot application. We can pull this image from the registry and deploy it in the AKS when needed.

AKS Cluster : Integration with Container Registry

Network Access:

As a part of its security requirements, Atlas controls inbound traffic to the Cluster using Network Access settings. Currently, Atlas provides three options for this control.

  1. IP Address List: Configure the list of IP addresses (single IP address or a CIDR – notated range of addresses) for Atlas to allow connection requests.
  2. Peering: Atlas supports network peering connection from AWS, Azure and Google Cloud.
  3. Private Endpoint.

We will now configure Network Peering between Azure VNet and Atlas VNet.

Peering

Choose Network Access, and go to the Peering Tab to add a new Peering Connection

Add a New Peering Connection
Choose Azure as a Cloud Provider

Get the  information listed from the Azure account, and configure it in the Atlas wizard on prompt as shown:

  1. Subscription ID.
  2. Tenant ID.
  3. Resource Group Name  (name of the resource group under which the VNET is created in Azure.
  4. VNet name: The VNet name of the Azure.
replace the information in <> with your account details.

After configuration, Atlas provides you with a set of instruction that can be executed either in Azure CLI or Azure Cloud Shell (use bash instead of powershell). As a part of this step we create a service principal and a required role definition for Peering with Azure VNet.

On completing the steps, Peering can be initiated. Atlas and Azure would now show Active peering as seen below.
Atlas: Active Peering

Switch back to the Azure portal and view the Peering status.

Azure VNet Peering Status

IP Address List:

Once the Peering is active, the next step is to add the list of Private IP Address range from Azure VNet (subnet used while creating a AKS).

Add New IP Address

From the Azure portal, get the subnet Address used during AKS creation, and add them in the Atlas:

Peering is now setup between Azure VNet and Atlas Vnet! Let us now see how this can be leveraged in the spring boot application.

Connection String:

Atlas provides different connection strings based on the connection type. Once the Peering is established, choose “Private IP for Peering” under connection type.

Connection Type: Private IP for Peering.

Use the connection string for Java, and have them configured in your spring boot app.

Select Connection Method

Build and Deploy Spring Boot App in AKS:

The repository shown below holds a simple spring boot app for connecting to MongoDB, and do basic CRUD operation.

Docker Build and Push to Container Repository:

From the git repository, update the connection string, and issue the following commands:

  • mvn clean package -DskipTests=true
  • docker build -t <containerrepositoryname>.azurecr.io/springmongopoc:v1.0 .

Once the build is complete, push the image to the Azure container registry:

  • az login
  • az acr login -n <containerrepositoryname>
  • docker push <containerrepositoryname>.azurecr.io/springmongopoc:v1.0

Deploy App to AKS and Expose endpoint:

Login to AKS Cluster and deploy the app:

  • az aks get-credential –resource-group=<your resource group name> –name=<yourclustername>
  • kubectl create deployment mongospringpoc –image=<containerrepositoryname>.azurecr.io/springmongopoc:v1.0
  • kubectl expose deployment mongospringpoc –port=8080 –type=LoadBalancer

Access exposed REST end point to verify:

You can now access the Atlas Cluster from the spring boot app., hosted in AKS!

Subheader 1
Text Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna.


Subheader 2
Text Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna.

Subheader 3
Text Lorem Ipsum Dolor Sit Amet, Consetetur Sadipscing Elitr, Sed Diam Nonumy Eirmod Tempor Invidunt Ut Labore Et Dolore Magna Aliquyam Erat, Sed Diam Voluptua. At Vero Eos Et Accusam Et Justo Duo Dolores Et Ea Rebum. Stet Clita Kasd Gubergren, No Sea Takimata Sanctus Est Lorem Ipsum Dolor Sit Amet. Lorem Ipsum Dolor Sit Amet, Consetetur Sadipscing Elitr, Sed Diam
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna.

Rajesh Vinayagam
Distinguished Architect

Share this Article

Latest Insights

Tag Cloud

Share this Article