Deploying an ASP.NET Core application to Azure Kubernetes Service (AKS) involves several steps, including containerizing the application, pushing it to a container registry, creating AKS resources, and deploying the application. Here's a step-by-step guide.
Step 1. Containerize the ASP.NET Core App.
Step 1.1. Add a Dockerfile to Your ASP.NET Core Project.
Create a Dockerfile in the root of your project if you don’t already have one.
# Use the official ASP.NET Core runtime as a base image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
# Use the official .NET SDK image for building the app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyApp.csproj", "./"]
RUN dotnet restore "MyApp.csproj"
COPY . .
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
# Use the runtime image and copy the built app
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
Step 1.2. Build and Test the Docker Image.
Run the following commands to build and test your Docker image locally.
# Build the Docker image
docker build -t myapp:1.0 .
# Run the Docker container
docker run -p 8080:80 myapp:1.0
Access the app at http://localhost:8080.
Step 2. Push the Docker Image to Azure Container Registry.
Step 2.1. Create an Azure Container Registry (ACR)
Run the following commands to create an Azure Container Registry.
az acr create --resource-group myResourceGroup --name myContainerRegistry --sku Basic
Step 2.2. Log in to ACR
az acr login --name myContainerRegistry
Step 2.3. Tag and Push the Docker Image.
# Tag the image for ACR
docker tag myapp:1.0 mycontainerregistry.azurecr.io/myapp:1.0
# Push the image to ACR
docker push mycontainerregistry.azurecr.io/myapp:1.0
Step 3. Create an AKS Cluster.
Step 3.1. Create the AKS Cluster.
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
Step 3.2. Get AKS Credentials.
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
This sets the Kubernetes context to your AKS cluster.
Step 4. Deploy the ASP.NET Core App to AKS.
Step 4.1. Create a Kubernetes Deployment YAML File.
Create a file named deployment.yaml.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: mycontainerregistry.azurecr.io/myapp:1.0
ports:
- containerPort: 80
env:
- name: ASPNETCORE_ENVIRONMENT
value: "Production"
---
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Step 4.2. Apply the Deployment to AKS.
kubectl apply -f deployment.yaml
Step 4.3. Verify the Deployment.
kubectl get all
Step 5. Access the Application.
- Get the external IP address of the service.
kubectl get service myapp-service
- Access the app in a browser using the external IP.
Step 6. Monitor and Manage.
Monitor Pods and Logs
# List Pods
kubectl get pods
# View Logs
kubectl logs <pod-name>
Scale the Deployment
kubectl scale --replicas=3 deployment/myapp-deployment
Step 7. Clean Up.
To remove the AKS cluster and related resources.
az aks delete --resource-group myResourceGroup --name myAKSCluster --yes --no-wait