DEV Community

Christian Zink
Christian Zink

Posted on • Updated on • Originally published at levelup.gitconnected.com

How to Add MySql & MongoDB to a Kubernetes .Net Core Microservice Architecture

How to add a MySQL DB and a MongoDB Replica Set in K8S on Docker Desktop using Persistent Volumes and Access the Databases from ASP.NET Core C# and Angular

Alt Text Overview diagram of the components

In this guide, you will use databases in a raw microservice-based cloud architecture. It starts with a single MySQL instance and continues to a MongoDB replica set with a headless Kubernetes service. Both databases use persistent volumes. The databases are accessed by ASP.NET Core backend services and use Angular as the frontend. The application is exposed via an Ingress controller.

Contents

  1. MySQL in Kubernetes with Persistent Volume

  2. MongoDB Replica Set in Kubernetes as StatefulSet

  3. Access the Databases in ASP.NET Core REST-API Services

  4. Call the ASP.NET Core APIs from Angular

  5. Deploy the Angular App and the ASP.NET Core Services to Kubernetes

  6. Final Thoughts and Outlook


1. MySQL in Kubernetes with Persistent Volume

You will deploy a single MySQL pod in Kubernetes on Docker Desktop. A service will make it accessible on port 3306. You will use a Kubernetes persistent volume so the MySQL data survives restarts or deletions of the pod.

MySQL Deployment in Kubernetes on Docker Desktop:
Alt Text

Preparation

Install Docker Desktop for Windows and enable Kubernetes:

Create the Deployment

Create the file mysql.yaml:

Apply the yaml:

C:\dev\demo>kubectl apply -f mysql.yaml
Enter fullscreen mode Exit fullscreen mode

(Optional) Use a Temporary MySQL Client Container to Access and Test the MySQL Deployment

Create the container and connect to the MySQL database server (enter the command as one line):

C:\dev\demo>kubectl run -it — rm — image=mysql:5.6 — restart=Never mysql-client — mysql -h mysql -ppassword
Enter fullscreen mode Exit fullscreen mode

Create a new database:

mysql>create database testdb;
Enter fullscreen mode Exit fullscreen mode

You can delete the MySQL pod:

C:\dev\demo>kubectl delete pod -l app=mysql
Enter fullscreen mode Exit fullscreen mode

Kubernetes will automatically recreate the pod. The persistent volume guarantees that the sql data survives the deletion and recreation of pods.

You will have to secure your MySQL configuration to use it in a production environment. Use Kubernetes secrets, etc.

Further reading in the official Kubernetes documentation: Run a Single-Instance Stateful Application (MySQL)


2. MongoDB Replica Set in Kubernetes as StatefulSet

You will use a StatefulSet to deploy two MongoDB pods to Kubernetes. Each pod uses its own persistent storage. Then you configure the two instances as a MongoDB replica set.

A headless service makes the instances accessible on port 27017 and the deterministic names “mongod-0.mongodb-service” and “mongod-1.mongodb-service”:

Alt Text

Create the StatefulSet

Create the file mongodb.yaml:

Apply the yaml:

C:\dev\demo>kubectl apply -f mongodb.yaml
Enter fullscreen mode Exit fullscreen mode

Configure the MongoDB Replica Set

You will manually configure the replica set. See this article for how to uses a sidecar container for an automatic configuration: Running MongoDB on Kubernetes with StatefulSets

Connect to the mongod-0 pod:

C:\dev\demo>kubectl exec -it mongod-0 -c mongod-container bash
Enter fullscreen mode Exit fullscreen mode

Start the mongo client:

root@mongod-0:/# mongo
Enter fullscreen mode Exit fullscreen mode

Configure the replica set (enter the command as one line):

MainRepSet:PRIMARY> rs.initiate({ _id: “MainRepSet”, version: 1, members: [{ _id: 0, host: “mongod-0.mongodb-service.default.svc.cluster.local:27017” }, { _id: 1, host: “mongod-1.mongodb-service.default.svc.cluster.local:27017” } ]});
Enter fullscreen mode Exit fullscreen mode

You will have to secure your MongoDB configuration to use it in a production environment. Use Kubernetes secrets, etc.

Further reading: MongoDB StatefulSet in Kubernetes by Deeptiman Pattnaik


3. Access the Databases in ASP.NET Core REST-API Services

You will create one ASP.NET Core web API to access the MySQL database and a second ASP.NET Core web API to connect to the MongoDB replica set. Finally, you will create a docker image for each API.

This post focuses on database access. So I keep the other aspects short. See my previous post for details on how to use K8S on Docker Desktop with Ingress to develop locally for the cloud using an ASP.NET Core REST API and Angular.

Install Visual Studio Community (it’s free) with the ASP.NET and web development workload.

Create an API Using the MySQL Service

Create an ASP.NET Core 5.0 Web API project and call it “MySqlApi”. Activate Docker and use the “Linux” setting. Disable HTTPS.

Install the “MySqlConnector” NuGet package.

Further reading: https://mysqlconnector.net/tutorials/connect-to-mysql/

Add a new controller “MySqlController”:

The code will query and return all databases in the MySQL server.

For easier understanding, all code is placed in one method. Please make sure to use proper configuration, etc. when running in a production environment.

Dockerize the API

In the Visual Studio MySqlApi project rename the file “Dockerfile” in the Project-Explorer to “dockerfile” (first character lowercase). Then right-click the dockerfile and select “Create Docker Image”. This will also push the image to docker.

Create an API Using the MongoDB Service

Create a second Web API project and call it “MongoDbApi”. Use the same settings as for the “MySqlApi” project.

Install the “MongoDB.Driver” NuGet package.

Add a new controller “MongoDbController”:

The code will query and return all collections in the MongoDB replica set.

Create the docker image as you did for the MySqlApi project.


4. Call the ASP.NET Core APIs from Angular

You will create an Angular app to load and display the data from the ASP.NET APIs. Finally, you create a docker image that uses NGINX to serve the app.

Download and install Node.js/NPM.

Install Angular (I used Angular 9 when writing this guide):

C:\dev\demo>npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

Create the app:

C:\dev\demo>ng new DemoApp
Enter fullscreen mode Exit fullscreen mode

Adjust the code in DemoApp\src\app

Add the HttpClientModule to app.module.ts:

Call the REST-APIs in app.component.ts:

Display the loaded data in app.component.html:

Dockerize the Angular App

Create the file DemoApp\src\app\dockerfile

Make sure the file “dockerfile” has no file extension. This will use NGINX to serve the app:

Create the file “.dockerignore” (with a dot at the beginning):

Open a command prompt and build the docker image (the dot at the end is important):

C:\dev\demo\DemoApp>docker build -t demoapp .
Enter fullscreen mode Exit fullscreen mode

5. Deploy the Angular App and the ASP.NET Core Services to Kubernetes

Install the NGINX Ingress controller (in one line on the command line):

C:\dev\demo>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.41.2/deploy/static/provider/cloud/deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Create kubernetes.yaml:

Apply the yaml:

C:\dev\demo>kubectl apply -f kubernetes.yaml
Enter fullscreen mode Exit fullscreen mode

You are done! Open your browser on http port 80 and see how your Kubernetes microservices are working:


6. Final Thoughts and Outlook

You created the working basis for a microservice-based cloud architecture and used two different types of databases.

Please make sure to adjust the code to use it in a production environment: Clean up the code and apply security best practices, use Kubernetes secrets and the latest secure images. Apply Angular and .NET Core design patterns, error handling, etc.

See my follow-up posts how to add MySql and MongoDB to your architecture and how to use events for inter-microservice communication.

I will show you more in further posts: Kubernetes secrets, security aspects like SSL, logging, debugging, CI/CD, Helm charts, (code) quality, (auto) scaling and self-healing, etc.

See my other stories how to:

Please contact me if you have any questions, ideas, or suggestions.

Top comments (0)