Concise guide with commands about deploying .NET Core application to local k8s.
First of all, make sure you’ve installed Kubernetes as described here.
The base image for the container depends on your choice about OS where it is Linux or Windows. You can find a correct one here. Below is the example which I’m using for .NET Core v2.2 witn Linux-based containers. All source code is located under
src folder in my project.
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build WORKDIR /app COPY ./src ./ RUN dotnet publish Project.Name/Project.Name.csproj -c Release -o out FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 WORKDIR /app EXPOSE 80 EXPOSE 443 COPY --from=build /app/Project.Name/out ./ ENTRYPOINT ["dotnet", "Project.Name.dll"]
There are several ways to deploy the application locally: on local Docker, using docker-compose and on local Kubernetes.
If you’re using image you need to refresh the local copy using
pull command. The option
-d will start containers detached. The command
stop will remove deployment.
docker-compose pull docker-compose up -d docker-compose stop
At first, build an image of your application.
docker build --progress plain -f ./docker/Dockerfile -t image-name .
--progress plain will output all logs during the building to the console. You can use the image after this operation complete.
docker images docker run -p 4901:80 image-name docker ps
images will show all images in your local registry. With
run you can start new container with your image. The command
ps will show you running containers. You will need to manually stop container to release resources using
CONTAINER ID from the output of
Here I’ll deploy manually the app by creating a deployment and service on k8s. After that I’ll extract YAML specification of the current deployment to simplify deployment in the future. You need to have local Docker image before going with next steps. Now let’s create a deployment using the image.
kubectl run deployment-name --image=image-name --image-pull-policy=Always
--image-pull-policy set the kubelet to pull image when pods are started. Now create a service (read more about it here):
kubectl expose deployment deployment-name --type=LoadBalancer --port=4901 --target-port=80
--port specify external port. You can use the following command to get information about resources in k8s and delete the service (pod will be automatically terminated by k8s).
kubectl get all kubectl delete svc deployment-name kubectl delete
When we have running pods, service and deployment in k8s, let’s extract YAML specification for this to create all them at once in the future. Below I show commands for extracting configuration to file and applying the file to k8s.
kubectl get deployment,service,pod deployment-name -o yaml --export kubectl apply -f local-deployment.yml
In the file you can specify parameters of the service, number of pods, etc.