To set environment tags (such as ist, preprod, prod, etc.) for your Kubernetes deployments in Datadog, you can leverage both Kubernetes labels and environment variables. This ensures that Datadog can correctly identify and categorize your services based on their environment. Below are the steps and examples on how to achieve this within your deployment.yaml using Helm templating.
1. Add an Environment Label to the Pod Template
Adding a label to your pod template allows Datadog to automatically pick up the environment information from your Kubernetes metadata. Here's how you can add the env label:
labels:
app: {{ template "caas-arm-2.name" . }}
release: {{ .Release.Name }}
env: {{ .Values.env }} # This line adds the environment label
This label assigns the value of {{ .Values.env }} (e.g., ist, preprod, prod) to the env key. Datadog can then use this label to tag your services accordingly.
Setting the DD_ENV Environment Variable:
yaml
- name: DD_ENV
value: "{{ .Values.env }}" # This line sets the DD_ENV environment variable
The DD_ENV environment variable is used by Datadog's APM and other integrations to categorize metrics, traces, and logs by environment.
2. Ensure Datadog Agent is Configured to Collect Kubernetes Labels
For Datadog to recognize and utilize the env label:
Kubernetes Integration: Make sure the Kubernetes integration in Datadog is enabled and properly configured. By default, Datadog collects Kubernetes metadata, including labels.
Tag Configuration: Verify that your Datadog Agent is set to include Kubernetes labels as tags. You can check this in the Agent’s configuration (datadog-values.yaml if using Helm):
Note: Replace {{ .Chart.Name }} with your container name as needed. This method allows you to add tags specific to each container managed by the Datadog Agent.
Summary
By adding an env label to your pod metadata and setting the DD_ENV environment variable within your containers, you enable Datadog to accurately tag and segregate your services based on their deployment environment. Ensure that your Datadog Agent is configured to collect these labels and environment variables to make full use of the tagging in your monitoring and observability workflows.