How to Run ASP.NET Core Locally with HTTPS in Kubernetes: A Guide

How to Run ASP.NET Core Locally with HTTPS in Kubernetes: A Guide
Kubernetes has become the go-to platform for managing containerized applications. It’s a powerful tool that can simplify the deployment, scaling, and management of your applications. In this blog post, we’ll guide you through the process of running ASP.NET Core locally with HTTPS in Kubernetes. This guide is designed for data scientists and other technical professionals who want to leverage the power of Kubernetes for their ASP.NET Core applications.
Prerequisites
Before we start, ensure you have the following installed on your local machine:
- Docker
- Kubernetes (Minikube for local development)
- .NET Core SDK
- OpenSSL (for generating SSL certificates)
Step 1: Create an ASP.NET Core Application
First, let’s create a new ASP.NET Core application. Open your terminal and run the following command:
dotnet new webapi -n AspNetCoreKubernetes
This command creates a new ASP.NET Core Web API project named AspNetCoreKubernetes
.
Step 2: Dockerize the Application
Next, we need to containerize our application using Docker. Create a Dockerfile
in the root directory of your project with the following content:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["AspNetCoreKubernetes.csproj", "./"]
RUN dotnet restore "./AspNetCoreKubernetes.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "AspNetCoreKubernetes.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "AspNetCoreKubernetes.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AspNetCoreKubernetes.dll"]
This Dockerfile uses multi-stage builds to create a lean Docker image for our application.
Step 3: Generate SSL Certificates
To run our application over HTTPS locally, we need to generate SSL certificates. We’ll use OpenSSL for this. Run the following commands in your terminal:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt
This command generates a new SSL certificate and key valid for 365 days.
Step 4: Create a Kubernetes Secret
Now, we need to create a Kubernetes secret to store our SSL certificates. Run the following command:
kubectl create secret tls localhost-tls --cert=localhost.crt --key=localhost.key
This command creates a new Kubernetes secret named localhost-tls
containing our SSL certificates.
Step 5: Deploy the Application to Kubernetes
Finally, we can deploy our application to Kubernetes. Create a deployment.yaml
file with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: aspnetcore-kubernetes
spec:
replicas: 1
selector:
matchLabels:
app: aspnetcore-kubernetes
template:
metadata:
labels:
app: aspnetcore-kubernetes
spec:
containers:
- name: aspnetcore-kubernetes
image: aspnetcore-kubernetes:latest
ports:
- containerPort: 80
- containerPort: 443
volumeMounts:
- name: secrets
mountPath: /https
readOnly: true
volumes:
- name: secrets
secret:
secretName: localhost-tls
This file defines a Kubernetes deployment for our application. It mounts the localhost-tls
secret to the /https
directory in the container.
To deploy the application, run the following command:
kubectl apply -f deployment.yaml
Conclusion
Congratulations! You’ve successfully run an ASP.NET Core application locally with HTTPS in Kubernetes. This setup allows you to develop and test your applications in an environment similar to production, ensuring a smooth deployment process. Remember to replace the self-signed certificates with ones from a trusted Certificate Authority before deploying to production.
We hope this guide has been helpful. Stay tuned for more technical guides on leveraging the power of Kubernetes for your applications.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.