How to Add MySql & MongoDB to a Kubernetes .Net Core Microservice Architecture
Tags: dotnet, kubernetes, microservices, angular
Published: 2021-05-04 20:24:53

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv08svanq0lgj1z39aib.png) *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

1. MongoDB Replica Set in Kubernetes as StatefulSet

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

1. Call the ASP.NET Core APIs from Angular

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

1. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a39g6yhqticr1qndx3si.png)

### Preparation

Install [Docker Desktop for Windows](https://hub.docker.com/editions/community/docker-ce-desktop-windows) and enable Kubernetes:

![](https://cdn-images-1.medium.com/max/2744/1*IzjJfH56yoJGrxUQru1fSA.png)

### Create the Deployment

Create the file mysql.yaml:

{% gist https://gist.github.com/Chriz76/5b820c69ca4a919a67910d33f51507ee
%}

**Apply the yaml:**

    C:\dev\demo>kubectl apply -f mysql.yaml

### (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

Create a new database:

    mysql>create database testdb;

You can delete the MySQL pod:

    C:\dev\demo>kubectl delete pod -l app=mysql

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](https://kubernetes.io/docs/tasks/run-application/run-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gsb51j6ru0vtptjopmi.png)

### Create the StatefulSet

Create the file mongodb.yaml:

{% gist https://gist.github.com/Chriz76/13a8a66ee8316af083de44bbe8b4e28a
%}

Apply the yaml:

    C:\dev\demo>kubectl apply -f mongodb.yaml

### 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](https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/)

Connect to the mongod-0 pod:

    C:\dev\demo>kubectl exec -it mongod-0 -c mongod-container bash

Start the mongo client:

    root@mongod-0:/# mongo

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” } ]});
> 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](https://deeptiman.medium.com/mongodb-statefulset-in-kubernetes-87c2f5974821) by [Deeptiman Pattnaik](undefined)*

---

## 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](https://dev.to/christianzink/how-to-build-an-asp-net-core-kubernetes-microservices-architecture-with-angular-on-local-docker-desktop-using-ingress-395n).

Install [Visual Studio Community](https://visualstudio.microsoft.com/en/vs/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/](https://mysqlconnector.net/tutorials/connect-to-mysql/)*

Add a new controller “MySqlController”:

{% gist https://gist.github.com/Chriz76/d42fcc730ddb3a2d69462e802d58c859
%}

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”:

{% gist https://gist.github.com/Chriz76/4db34e6f1e601210532e427720afa39f
%}

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](https://nodejs.org/en/download/).

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

    C:\dev\demo>npm install -g @angular/cli

Create the app:

    C:\dev\demo>ng new DemoApp

Adjust the code in DemoApp\src\app

Add the HttpClientModule to **app.module.ts**:

{% gist https://gist.github.com/Chriz76/5cdb21961b54c3bdd2d27711c7011931
%}

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

{% gist https://gist.github.com/Chriz76/f120e1586762b97aa3e56fc5fed54857
%}

Display the loaded data in **app.component.html**:

{% gist https://gist.github.com/Chriz76/ee315bc08b53ec2350957176f71d2c5a
%}

### 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:

{% gist https://gist.github.com/Chriz76/328cb07ca21f210dd32914906bd9fd92
%}

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

{% gist https://gist.github.com/Chriz76/0a28ea4ac12383e0e30bf4a953cd289d
%}

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

    C:\dev\demo\DemoApp>docker build -t demoapp .

---

## 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

Create kubernetes.yaml:

{% gist https://gist.github.com/Chriz76/544a5d4e7eeee32f20a263cc7914182c
%}

Apply the yaml:

    C:\dev\demo>kubectl apply -f kubernetes.yaml

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

![](https://cdn-images-1.medium.com/max/2000/1*jLbLYA39hkO6oOT-pnKHKA.png)

---

## 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](https://dev.to/christianzink/databases-in-a-kubernetes-angular-net-core-microservice-architecture-22jc)** and **[how to use events for inter-microservice communication](https://dev.to/christianzink/how-to-build-an-event-driven-asp-net-core-microservice-architecture-4fnh)**.

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:**

* [Sign-In with Google in Angular and use JWT based .NET Core API Authentication with Asymmetrically (RSA) Signed Tokens](https://levelup.gitconnected.com/how-to-sign-in-with-google-in-angular-and-use-jwt-based-net-core-api-authentication-rsa-6635719fb86c)

* [Use NSwag to Autogenerate a TypeScript Client to Access a .NET Core service API and use OpenAPI/Swagger to describe the API](https://levelup.gitconnected.com/how-to-jwt-authenticate-with-angular-to-an-asp-net-4cfab5298d08)

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