Use Init Containers
You can use Kubernetes init containers to instrument the .NET Agent. In this method, the init container copies the agent binaries into the application container at runtime. The deployment spec used by the application references two containers:
- Application container based on an image that does not contain any .NET Agent binaries
- Second init container based on an image that only contains the .NET Agent binaries.
The deployment spec is updated to reference these two containers and copy the agent binaries from the init container to the application container at deployment time. Once the copy is performed, the init container terminates.
To use init containers to copy the .NET Agent for Linux binaries, perform these steps:
- Build the .NET Core Application Image
- Build the .NET Agent for Linux Init Container Image
- Add the Init Container to the Deployment Spec
- Set the .NET Agent for Linux Environment Variables
- Set the APPDYNAMICS_AGENT_UNIQUE_HOST_ID Environment Variable
- Copy the AppDynamicsConfig.json File to the Container
- (On-Premises Controller Only) Copy the Controller Certs to the Container
- Example Configuration for Using an Init Container
Build the .NET Core Application Image
Build a .NET Core application image. Do not include the .NET Agent for Linux binaries.
Build the .NET Agent for Linux Init Container Image
Build the .NET Agent for Linux image separately from the application image. You can reuse this image across multiple .NET Core application deployments.
Alternatively, the init container can reference a pre-built image from Splunk AppDynamics on Docker Hub.
Add the Init Container to the Deployment Spec
Edit the deployment spec to add the required sections that allow you to copy the agent binaries from the init container to the application image.
The following snippet from a deployment spec shows the required volumes
, volumeMounts
, and initContainer
definitions. The code example assumes the .NET Core application image is published to dotnet-samples:aspnetapp
and the init container image uses the pre-built image docker.io/appdynamics/dotnet-core-agent:<version>
(where <version>
is the .NET Agent version; for example, 21.5.0):
kind: Deployment
spec:
containers:
- name: dotnet-app
image: microsoft/dotnet-samples:aspnetapp
volumeMounts:
- mountPath: /opt/appdynamics
name: appd-agent-repo
initContainers:
- name: appd-agent
image: docker.io/appdynamics/dotnet-core-agent:<version>
volumeMounts:
- mountPath: /appdynamics
name: appd-agent-repo
volumes:
- name: appd-agent-repo
emptyDir: {}
Set the .NET Agent for Linux Environment Variables
To set all of the required .NET Agent environment variables, you must follow the steps in Best Practices to Configure Agents in Kubernetes listed below:
Use ConfigMaps to Configure the App Server Agent
Use Secrets for the Controller Access Key
Set Application-Specific Configuration in the Deployment Spec
APPDYNAMICS_AGENT_TIER_NAME
in the deployment spec:spec:
containers:
- name: dotnet-app
env:
- name: APPDYNAMICS_AGENT_TIER_NAME
value: dotnet-service
Set the APPDYNAMICS_AGENT_UNIQUE_HOST_ID Environment Variable
APPDYNAMICS_AGENT_UNIQUE_HOST_ID
environment variable is supported in version 20.7.0+ of the .NET Agent for Linux. For earlier versions, a property must be set in AppDynamicsConfig.json
based on a runtime value. See this example deployment spec.Set the APPDYNAMICS_AGENT_UNIQUE_HOST_ID
environment variable to enable APM correlation with the Cluster Agent. Since the value depends on a runtime value, set this environment variable in the container startup command using the values documented in Manually Configure App Agents to Correlate with the Cluster Agent. For example, for a Kubernetes environment with the Docker runtime, set the environment variable as shown (export
is required):
kind: Deployment
spec:
containers:
image: microsoft/dotnet-samples:aspnetapp
command: ["/bin/sh"]
args: ["-c", "export APPDYNAMICS_AGENT_UNIQUE_HOST_ID=$(sed -rn '1s#.*/##; 1s/(.{12}).*/\\1/p' /proc/self/cgroup) && dotnet aspnetapp.dll"]
...
Copy the AppDynamicsConfig.json File to the Container
Some .NET Agent for Linux options must be set in the AppDynamicsConfig.json
file. This includes setting the outputtype
to console
, which facilitates sending the agent logs to log aggregation tools and viewing the logs using kubectl logs.
(On-Premises Controller Only) Copy the Controller Certs to the Container
If the .NET Agent for Linux communicates with an on-premises Controller, the Controller certs must be copied to the container. See Enable SSL for the .NET Agent.
Example Configuration for Using an Init Container
This Dockerfile is an example of building the .NET Agent for Linux init container image using a multi-stage build. A complete example of a deployment spec that uses an init container to copy the agent binaries can be found on Github: dotnet-app.yaml.