- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`The DatadogPodAutoscaler (DPA) is a Kubernetes custom resource definition (CRD) that enables autoscaling of Kubernetes workloads using Datadog Kubernetes Autoscaling (DKA). This guide demonstrates how to manage DatadogPodAutoscaler resources using Terraform and HashiCorp’s Kubernetes provider to deploy an autoscaling configuration.
Before you begin, ensure you have the following:
kubectl
This guide uses a multi-stage deployment approach to ensure proper dependency creation for Terraform.
.
├── providers.tf # Provider configurations
├── variables.tf # Input variables
├── main.tf # Stage 1: Datadog secret and operator (CRDs)
├── terraform.tfvars # Example variable values
├── datadogagent/ # Stage 2: DatadogAgent CRD resource
│ └── main.tf # DatadogAgent manifest
├── nginx-dpa/ # Stage 3: Nginx application with DPA
│ └── main.tf # Nginx namespace, deployment, and DPA
A multi-stage deployment approach is essential when working with Kubernetes custom resource definitions (CRDs) and Terraform. This ordered approach is necessary to ensure that you create and install the dependencies required for each stage in the process.
Kubernetes CRDs must be installed in the cluster before you can create custom resources that use them. The DatadogPodAutoscaler CRD is created when you install the Datadog Operator in Stage 1. Terraform needs to know about these CRDs before it can manage resources that depend on them.
The Terraform Kubernetes provider discovers available resource types at initialization time. If you try to create a DatadogPodAutoscaler resource before the CRD is installed, Terraform will fail because it doesn’t recognize the custom resource type.
First, set up the following configuration files for each stage in the process.
providers.tf
provider "kubernetes" {
config_path = "~/.kube/config"
}
provider "helm" {
kubernetes = {
config_path = "~/.kube/config"
}
}
main.tf
resource "helm_release" "datadog_operator" {
name = "datadog-operator"
namespace = "datadog"
repository = "https://helm.datadoghq.com"
chart = "datadog-operator"
version = "2.11.1" # You can update to the latest stable version
create_namespace = true
}
datadogagent/variables.tf
variable "datadog_api_key" {
description = "Datadog API key"
type = string
sensitive = true
}
variable "datadog_app_key" {
description = "Datadog application key"
type = string
sensitive = true
}
datadogagent/main.tf
provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_secret" "datadog_secret" {
metadata {
name = "datadog-secret"
namespace = "datadog"
}
data = {
api-key = var.datadog_api_key
app-key = var.datadog_app_key
}
type = "Opaque"
}
resource "kubernetes_manifest" "datadog" {
manifest = {
apiVersion = "datadoghq.com/v2alpha1"
kind = "DatadogAgent"
metadata = {
name = "datadog"
namespace = "datadog"
}
spec = {
features = {
autoscaling = {
workload = {
enabled = true
}
}
eventCollection = {
unbundleEvents = true
}
}
global = {
site = "datadoghq.com"
credentials = {
apiSecret = {
secretName = "datadog-secret"
keyName = "api-key"
}
appSecret = {
secretName = "datadog-secret"
keyName = "app-key"
}
}
clusterName = "minikube-dka-demo"
kubelet = {
tlsVerify = false
}
}
override = {
clusterAgent = {
env = [
{
name = "DD_AUTOSCALING_FAILOVER_ENABLED"
value = "true"
}
]
}
nodeAgent = {
env = [
{
name = "DD_AUTOSCALING_FAILOVER_ENABLED"
value = "true"
}
]
}
}
}
}
}
nginx-dpa/main.tf
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.0"
}
}
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
# Create namespace for the application
resource "kubernetes_namespace" "nginx_demo" {
metadata {
name = "nginx-dka-demo"
}
}
# Nginx deployment
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx"
namespace = kubernetes_namespace.nginx_demo.metadata[0].name
labels = {
app = "nginx"
}
}
spec {
replicas = 3
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
image = "nginx:latest"
name = "nginx"
port {
container_port = 80
}
resources {
limits = {
cpu = "500m"
memory = "512Mi"
}
requests = {
cpu = "250m"
memory = "256Mi"
}
}
}
}
}
}
}
# Service for the nginx deployment
resource "kubernetes_service" "nginx" {
metadata {
name = "nginx-service"
namespace = kubernetes_namespace.nginx_demo.metadata[0].name
}
spec {
selector = {
app = "nginx"
}
port {
port = 80
target_port = 80
}
type = "ClusterIP"
}
}
# DatadogPodAutoscaler for nginx
resource "kubernetes_manifest" "datadogpodautoscaler_nginx_dka_demo_nginx" {
manifest = {
"apiVersion" = "datadoghq.com/v1alpha2"
"kind" = "DatadogPodAutoscaler"
"metadata" = {
"name" = "nginx"
"namespace" = "nginx-dka-demo"
}
"spec" = {
"applyPolicy" = {
"mode" = "Apply"
"scaleDown" = {
"rules" = [
{
"periodSeconds" = 1200
"type" = "Percent"
"value" = 20
},
]
"stabilizationWindowSeconds" = 600
"strategy" = "Max"
}
"scaleUp" = {
"rules" = [
{
"periodSeconds" = 120
"type" = "Percent"
"value" = 50
},
]
"stabilizationWindowSeconds" = 600
"strategy" = "Max"
}
"update" = {
"strategy" = "Auto"
}
}
"constraints" = {
"containers" = [
{
"enabled" = true
"name" = "nginx"
},
]
"maxReplicas" = 100
"minReplicas" = 3
}
"objectives" = [
{
"podResource" = {
"name" = "cpu"
"value" = {
"type" = "Utilization"
"utilization" = 70
}
}
"type" = "PodResource"
},
]
"owner" = "Local"
"targetRef" = {
"apiVersion" = "apps/v1"
"kind" = "Deployment"
"name" = "nginx"
}
}
}
}
After you have set up the configuration files for each stage, follow these steps to deploy the components in the correct order.
Deploy the Datadog Operator and CRDs:
terraform init
terraform apply
Verify that the Datadog Operator and CRDs are deployed:
kubectl get crd
kubectl get pods -n datadog
You should see that the Datadog CRDs are created and the datadog-operator pod is running.
Create a terraform.tfvars
file with your Datadog credentials:
cat > datadogagent/terraform.tfvars << EOF
datadog_api_key = "your-api-key-here"
datadog_app_key = "your-app-key-here"
EOF
TF_VAR_datadog_api_key
and TF_VAR_datadog_app_key
environment variables in your shell.cd datadogagent
terraform init
terraform apply
Verify that the Datadog Agent is deployed:
kubectl get datadogagent -n datadog
You should see the Datadog Agent custom resource created. It should be in the Running
state before proceeding. Also verify that the Datadog Agent and datadog-cluster-agent pods are running:
kubectl get pods -n datadog
Deploy the nginx application with DatadogPodAutoscaler:
cd ../nginx-dpa
terraform init
terraform apply
After deployment, verify that all components are working correctly.
Check Datadog Agent status:
kubectl get datadogagent -n datadog
kubectl describe datadogagent datadog -n datadog
Check DatadogPodAutoscaler status:
kubectl get datadogpodautoscaler -n nginx-dka-demo
kubectl describe datadogpodautoscaler nginx-autoscaler -n nginx-dka-demo
Congratulations, you have a workload managed by the Datadog Kubernetes Autoscaler!
To remove all resources, follow the reverse order of the deployment stages:
Clean up the deployed application (Stage 3):
cd nginx-dpa
terraform destroy
Clean up the Datadog Agent (Stage 2):
cd ../datadogagent
terraform destroy
Clean up the Datadog Operator and CRDs (Stage 1):
cd ..
terraform destroy
Check DatadogPodAutoscaler events:
kubectl get events -n nginx-dka-demo --sort-by='.lastTimestamp'
Check Cluster Agent logs:
kubectl logs -n datadog -l agent.datadoghq.com/component=cluster-agent
추가 유용한 문서, 링크 및 기사: