Deploy a Containerized Asp.net Core 3.1 Web Api to Kubernetes
It's been a while since I last wrote and it's been a very interesting journey so
far. Busy as usual, it's been hard to find the time to write something
meaningful here. This time I'm bringing you something I've been playing with for
a while and that I hope it can bring your development to another level. In this
article, I will show you how easy it is to containerize your .net core
applications into a Docker container and then deploy it to a Kubernetes cluster.
I'm also preparing my Rpi cluster to become my main Kubernetes cluster and I
will write about it once everything is up and running.
Requirements
First, you'll need the following tools and requirements (Note that I'm using
Windows 10 as my main OS, so everything will be configured around it):
1) Docker Desktop for Windows:
At the time of this article, I'm using Docker 2.2.0.5 which should be the latest one. You can get the latest from here: (https://docs.docker.com/docker-for-windows/install/) and follow the steps from the website.
2) Enable Kubernetes
Once everything is installed, open Docker preferences and select Kubernetes.
Enable all the options there and restart Docker. That should bring back Docker
and Kubernetes and you should see them online as in the picture below:
3) Enable File Sharing
You need to make sure you enable File Sharing so the container can see your local drive.
4) Get VS Code and Install Docker plugin (v1.1.0)
I won't go into much detail about .net core installation as I'm guessing you
should all be able to build and run asp.net core applications locally (https://code.visualstudio.com/Download). In this step, you'll need to download VS Code if you haven't already and the Docker plugin which will allow us to create docker files easily as they already have a pre-built template.
5) Get VS Code Kubernetes plugin (v1.2.0)
Install the plugin to generate the YAML files that will be needed to configure the cluster. This requires the YAML component and at the time of this article, I was using version 0.8.0.
6) API Testing
In order to test the API, I recommend using Swagger (https://swagger.io/tools/open-source/getting-started/) as it will easily expose the API functions and arguments that you can test.
Alternatively, you can use Postman (https://www.postman.com/downloads/) which is also a great tool for this.
Dockerizing your Web API
Now we are ready to go to our Web API project and create a docker file. Note that I'm using a complex project for this exercise and not just a simple hello world. This API has additional complex requests and it also requires a
Database. It also runs a few services and exposes most of the functionality via Swagger UI.
Go to the folder of your project and type "code ." to launch vscode on the project. Then go to view -> command palette and type "docker add" as in the picture below and select "Docker Add Files to Workspace":
Once you add the template. it will ask you about the following details:
- Application platform: ASP.NET Core.
- Operating System (container): Linux
- Ports to open: 80 and 443
The example used is a bit more complex and it includes several dependencies that need to be copied across during the creation of the container. Below you can find a dependency diagram which shows the project structure:
The application consists of a WebAPI that allows you to submit trading orders and it includes a service layer that performs the triggers internally. It also connects to a SQL Server DB which sits outside the container on my local machine. So, the connection string in the project needs to point to my local machine (192.168.1.129) plus the SQL Server port (1433). Sample connection string: ("DefaultConnection": "user id=user; password=password;Initial Catalog=TradingPlatform;Data Source=192.168.1.129,1433;").
The final docker file should look as follows:
The file mentions that it will create an asp.net core 3.1 base layer and that it will switch to the working directory app exposing ports 80 and 443. The second image includes the SDK and it will copy all our source code there. And it will run the different commands to restore the dependencies and finally to run the dotnet build command on our solution.
Now that we have our docker file, we can run it using the following commands (open a new terminal via VSCode and type "docker build -t trading-platform:v1 .". The output should look like:
If everything works as expected, the API should be in the docker container and we should be able to see that everything is running as expected (using command docker images):
Now we just need to run our container (docker run -it --rm -p 8080:80 trading-platform:v1) and test that everything is working correctly. Note that the exposed port is 8080.
As you can see in the image below, the API is up and running and I can explore it via Swagger UI on localhost:8080/swagger/api/index,html which is the port we have exposed through our container:
Deploying it to Kubernetes
Now that we have our docker container with an ASP.NET Core Web API that talks to a SQL Server DB and that will now be deployed to a Kubernetes cluster. Your Kubernetes cluster should already be up and running if you had followed the steps above during the Docker installation.
Check that your Kubernetes context is switched to docker-desktop:
You can check that the configuration is correct by using "kubectl config get-contexts":
This will allow us to select the group we want to work with. Note that I have additional clusters created on my local.
Now we need to create the deployment file (deployment.yml). Generate a new deployment.yml file in your folder and then via VS Code, type "deployment" and that will bring up the inline annotation from the Kubernetes plugin. Then fill in the gaps with the information you need to set up the cluster and pods as shown in the information below:
We will provide a deployment name "trading-platform-deployment", the name of the pod "trading-platform-pod" and the name of the docker container to use, which in our case is called "trading-platform:v1". We will then specify port 80 as the port of the container.
If everything goes well, you should see your deployment in Kubernetes ready and also the pods. We can also see that the app is running by inspecting the logs:
In order to make it publicly available, we need to provide a service layer that will give us the IP to reach the container. Now we need to generate a service.yml file (press cntrl+space to trigger the Kubernetes plugin and select the deployment service option to scaffold the service template):
The service is linked to the Pod and we specify the port we want to expose (8080) and the port in the container (80) and also the type of service which is LoadBalancer in our case. We can now see that everything is running correctly using the following commands:
And presto! Now we have our API running on a Docker Container and deployed to a Kubernetes cluster and to make it more real, the project is a complex project with services and additional libraries and also with a connection to a SQL Server DB.
If we browse to (http://localhost:8080/swagger/api/index.html) we will be able to reach the API.
Once completed, if you want to stop the service and pod, type:
- kubectl delete service trading-platform-service
- kubectl delete pod trading-platform-deployment-6bf776f966-8xs7r
Comments
Post a Comment