This example walks through the steps to deploy an ASP.NET Core 2.1 MVC application to a Kubernetes cluster with Application Insights for Kubernetes
on. The steps also applies for ASP.NET Core 2.2 applications.
A simple cluster role sample yaml is also included to describe how to make it work in a Role-based access control(RBAC) enabled clusters.
Tip: Read this for more information about RBAC.
Note: This is an example that does NOT follow all best practices, including security-related best practices. E.g. Application Insights instrumentation key is not adequately protected (it should be deployed as a secret).
-
.NETCore SDK 2.1.300 or above
-
.NET Core SDK is required in this example. Go to https://dot.net to download the latest SDK. Make sure you have
2.1.300
orabove
:dotnet --version 2.1.301
-
-
A Kubernetes Cluster that you can manage with kubectl.
-
If you don't have any, an easy way is to go to Azure AKS to get a managed cluster. Verify that the credential is properly set for kubectl to work:
user@user-pc:~$ kubectl get nodes NAME STATUS ROLES AGE VERSION aks-nodepool1-10984277-0 Ready agent 17d v1.9.9 aks-nodepool1-10984277-1 Ready agent 17d v1.9.9 aks-nodepool1-10984277-2 Ready agent 17d v1.9.9 user@user-pc:~$
-
-
A container image repository
- The image built will be pushed into an image repository. Dockerhub is used in this example.
- Let's start by creating an ASP.NET Core MVC applicaiton:
dotnet new mvc
- Add the NuGet Packages:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
dotnet add package Microsoft.ApplicationInsights.Kubernetes
- Enable Application Insights and Application Insights for Kubernetes in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddApplicationInsightsTelemetry(); // Enables Application Insights
services.AddApplicationInsightsKubernetesEnricher(); // Enables Application Insights for Kubernetes.
...
}
- It is optional but recommended to update the base images:
docker pull mcr.microsoft.com/dotnet/core/sdk:2.1
docker pull mcr.microsoft.com/dotnet/core/aspnet:2.1
- Add Dockerfile to the project folder. Build the docker container (dockeraccount/aik8sbasic_rbac, for example) using Dockerfile and upload it to an image registry.
docker build . -t dockeraccount/aik8sbasic_rbac:latest
docker push dockeraccount/aik8sbasic_rbac:latest
- If the cluster is RBAC enabled, the service account used will need to bind to proper cluster role so that the application can fetch Kubernetes related properties.
In saRole.yaml, a cluster role named appinsights-k8s-property-reader
is created and then bind to the default service account. Permissions needed are listed in the resources property. To deploy it, update the value for the namespace
and then:
kubectl create -f k8s/saRole.yaml
- Create the Kubernetes spec for the deployment and the service. Referencing k8s.yaml. Please update the variable of
APPINSIGHTS_INSTRUMENTATIONKEY
to your own application insights instrumentation key.
Deploy it:
kubectl create -f k8s/k8s.yaml
Once properly set up, your telemetry data will all be decorated with Kubernetes properties on it:
-
Enable Application Insights Profiler to optimize the performance for your application.