This is an example of how to use ApplicationInsights.Kubernetes
inside a worker. The project uses .NET 6, it should work in .NET Core 3.1 with minimal tweaks.
This is what it looks like when ApplicationInsights.Kubernetes
is turned on for your worker application:
And in this example, we will walk you through the key steps.
To finish the walk-through, there are some prerequisites.
- .NET 6 SDK. Refer to https://dot.net for more details.
- This should work in .NET Core 3.1 with some simple tweaks.
- Ability to build a docker image for sanity check from a dockerfile.
- Pick your way to build the docker image.
- We used DockerDesktop in this example.
- A kubernetes cluster to deploy the image to.
- We also used the one provided by DockerDesktop but it should work in any Kubernetes cluster.
- A container registry works with the Kubernetes cluster for deployment.
- We used docker hub in this example.
- An application insights resource for testing the result.
- A clone/fork this repo.
- Starting by navigate to example folder of: examples/WorkerExample.
-
Refer to WorkerExample.csproj for NuGet packages needed. Here's a copy:
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="2.*" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.20.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
Notes: these are current packages by the time of this example. As a common practice, it is recommended you always checking for latest stable ones to use.
-
Add a Worker class. Notes:
- It inherits from
BackgroundService
. - It injects
TelemetryClient
. - It sends one
request operation
and onecustom event
to application insights every 5 seconds.
- It inherits from
-
Refer to Program.cs for setup the worker as a generic host. Here are the critical services to register:
services.AddApplicationInsightsTelemetryWorkerService(); // Enable Application Insights for workers. services.AddApplicationInsightsKubernetesEnricher(); // Enable Application Insights Kubernetes to enhance telemetries. services.AddHostedService<Worker>(); // Register the background service of Worker.
-
Making sure
appsettings.json
will be copied to output folder on build, pay attention to the build task in the project file like this:<ItemGroup> <!-- Build action is `None`, and the option for copy to output directory could be `Always` or `PreserveNewest` --> <None Include="./appsettings.json" CopyToOutputDirectory="PreserveNewest" /> </ItemGroup>
- Remember to update the connection string in
appsettings.json
when you try it on your own. You can get the connection string on theOverview
blade of your application insights resource. Refer Connection Strings for more details. - Using of instrumentation key instead of connection string should work too. You will need to setup
InstrumentationKey
thanConnectionString
inappsettings.json
.
- Remember to update the connection string in
-
Build the image
docker build -t workerapp .
-
Optionally run it locally
docker run -d --name myapp workerapp docker logs myapp
You will see logs like this:
info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /app
-
Optionally, check out the
Live Metrics
in your application insights resource, you shall see request / custom events. -
Delete the container when the test is done
docker rm myapp -f
Notes: we are using dockerhub here as an example. you could use any container registry that deploys to your Kubernetes cluster.
-
Tag the image, bump up the version accordingly, for example:
docker tag workerapp dockerhub_account_name/ai-k8s-worker-example:1.0.0
-
Push the image:
docker push dockerhub_account_name/ai-k8s-worker-example:1.0.0
-
You could use default namespace, but it is recommended to put the test application in a dedicated namespace, for example 'ai-k8s-demo'. To deploy a namespace, use content in k8s-namespace.yaml:
kubectl create -f k8s-namespace.yaml
-
Build a deployment yaml file, refer to k8s.yml.
- Update the namespace and the image to pointing to the one that you intend to use.
-
Setup proper role binding for RBAC enabled clusters
If you have RBAC enabled for your cluster, permissions need to be granted to the service account to access the cluster info for telemetries. Refer to Configure RBAC permissions for details.
For example, deploy a role assignment in namespace of
ai-k8s-demo
by calling:kubectl create -f ..\..\docs\sa-role.yaml
The output looks like this:
clusterrole.rbac.authorization.k8s.io/appinsights-k8s-property-reader created
clusterrolebinding.rbac.authorization.k8s.io/appinsights-k8s-property-reader-binding created -
Deploy it:
Making sure the
image
is up to date, including the version in k8s.yml, and then:kubectl create -f .\k8s.yml
Now, check the live metrics again, you shall see enhanced telemetries:
-
Clean up the deployment:
kubectl delete -f .\k8s.yml
If you have ideas, feature requests or run into problems, feel free to file an issue.