Introduction
Docker and containers are a relative new tool, being more and more used on the software development. This tool helps you to create easily configurable enviroments for your applications, you don`t need anymore to have several SDKs and databases installed on your machine.
But, sometimes when your application grows and have several containers running, you may need to orchestrate and manage the behave of the whole application. And here comes Kubernetes to do this job for you.
Kubernetes is a docker container orchestrator, which helps you define the behavior of your containers, like doing autoscalling, loag balance, memory control and create a private network for your application. This tool is traditionally used on the cloud applications (because you don`t have hardware limitations, making possible horizontal scalability in seconds), but you can run on your machine too!
The tool that makes this possible is Kind (a.ka. Kubernetes in Docker), that emulates a kubernetes cluster on a docker container. This tool basically is going to put all your application`s containers inside the kubernetes docker container created by kind.
Configuration
To run this application, you only need these tools installed on your machine:
These tools are avaliable on all operating systems (Windows, Linux and Mac OS), you can easily install them on the command-line by package control tools (homebrew, chocolately apt-get and others).
Application
The application that we are going to run is irrelevant, is just a REST API with one GET endpoint that return a Guid. The focus is on containerize and run it on a cluster.
You can download the application on my Github account, the repo is public:
The project is very simple, you have the src folder where the .NET application is stored and the kubernetes folder where we are going to store the kubernetes policies (the scripts that we define the behave of the cluster).
In this application, we are only talk about two relevant files that our cluster depends on them:
- The Dockerfile on ./src/GuidGenerator.API
- The manifest.yaml on ./kubenetes
The Dockerfile is the file where we are going to build the image of our application, in this case our .NET REST API. Here how the file looks like:
# Image used to build the application
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env# Choose the directory of the application
WORKDIR /app # Copy all from outside to the inside the container
COPY . ./# Build the .NET application
RUN dotnet restore
RUN dotnet publish -c Release -o out# Image used to run the application
FROM mcr.microsoft.com/dotnet/aspnet:6.0# Choose the directory of the application
WORKDIR /app# Copy generated files to current path
COPY --from=build-env /app/out .# What is going to be executed where the container is up
ENTRYPOINT ["dotnet", "GuidGenerator.API.dll"]
And on the manifest file, is the policy used by kubernetes, where we setup, configure and provision the cluster behaviour. The file looks like it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: guid-api
spec:
selector:
matchLabels:
app: guid-api
template:
metadata:
labels:
app: guid-api
spec:
containers:
- name: guid-api
image: guid-api
imagePullPolicy: IfNotPresent
env:
- name: PORT
value: "8080"
resources:
limits:
memory: "64Mi"
cpu: "128m"
ports:
- containerPort: 8080
name: http
---apiVersion: v1
kind: Service
metadata:
name: guid-api
spec:
selector:
app: guid-api
ports:
- port: 80
targetPort: http
When everything is installed, we can start to setup our enviroment.
Setup the Enviroment
The firs step is to download the project on your machine, and on the terminal stay on the root folder of the project, like on the picture below:
Now we need to containerize our .NET REST API, creating a docker image to be runned on the cluster.
docker build -t guid-api ./src/GuidGenerator.API
This command might take a while, after it finishes, you can verify if the image was created sucessfully, with the command:
docker images
Now we are going to start the cluster:
kind create cluster
This command might take a while too, but after it finishes, it must had created another docker image.
If both images were created, we need to put our application image (guid-api) inside the cluster image (kindest/node).
kind load docker-image guid-api
And apply our policy on this cluster.
kubectl apply -f kubernetes/manifest.yaml
After you apply this manifest file, if you modify the file, just run again the command, and the cluster policy is going to be updated.
After these commands, we need to wait for the cluster to be ready, we can verify this with the following commands:
kubectl get deploy
kubectl get pods
f both shows ready
your application is ready to go. If not, and spent a significant time, may something is broken (must probably on the kubernetes manifest).
To see the log of the .NET application, type:
kubectl logs deploy/guid-api
You cannot access the application yet, because is pointing to a port on localhost inside the cluster. You must point to a port on the localhost of your machine:
The guid-api-5458fcbfd5-qmks8 is the name of the pod of our application, showed on the get pods command before.
kubectl port-forward guid-api-5458fcbfd5-qmks 83000:80
Everything was setup, and now we can seee if our application is running correctly, just click here
We did it! Now our application is running on a cluster. On the nexts articles I am going to show how we can put more applications on our cluster (a frontend and backend) and how you can run this same application on a cloud provider.