🐾

2025: Datadog CSI Driverを導入してPodセキュリティを標準のenforceに実現

に公開

なぜDatadogがCSI Driverを作ってたの

KubernetesでDatadog Agentとの通信はUDSで実現しているので、Kubernetes PSS標準(Baseline)には許容できないhostPathを利用している。

Applications...communicate with the Datadog Agent to send telemetry data...through Datadog APM... [by the] communication mode set on the Datadog Cluster Agent’s Admission Controller. With the sockets option, communication takes place through...Unix domain sockets (UDS). UDS makes it easier for the Agent to identify the origin of packets sent to the socket. However, [it requires] mounting socket files via hostPath volumes...incompatible with namespaces governed by non-privileged Pod Security Standards (PSS).

PodにDatadog用のラベルを貼ると、Pod SpecにhostPathが追加される。

$ kubectl run nginx --image nginx -l admission.datadoghq.com/enabled=true
pod/nginx created

$ kubectl get pod nginx -o yaml | grep hostPath -A3
  - hostPath:
      path: /var/run/datadog
      type: DirectoryOrCreate
    name: datadog

Podに属するNamespaceをPSS標準にしてみると、「hostPath volumesが存在する」というワーニングが出る。

$ kubectl label ns default pod-security.kubernetes.io/enforce=baseline --dry-run=server
Warning: existing pods in namespace "default" violate the new PodSecurity enforce level "baseline:latest"
Warning: nginx: hostPath volumes

To resolve this issue, Datadog has released a CSI (Container Storage Interface) driver that mounts UDS sockets into pods by using CSI volumes instead of hostPath volumes. CSI volumes are compatible with all PSS levels, including the restricted level, making it possible for teams to adopt UDS-based observability features without violating security constraints.
https://www.datadoghq.com/blog/datadog-csi-driver/

それを解消するために、今年六月下旬、DataDogチームはDatadog CSIをリリースし、ついにDatadogを使ってるNamespaceでもPSS標準にできるようになった。(Datadog自体のNamespaceはhostPathが必要なので、クラスタ的にはまだPSS標準にはできない)

DataDog CSI Driverの仕組み

前提条件

  • Kubernetes v1.13+
  • DataDog Agent v7.67+

Agentとの通信モードは、Admission Controllerで設定する。

Datadog’s Admission Controller automatically configures observability communication for pods based on the selected mode: service, hostip, socket, or the newly added csi. When csi mode is enabled, the Admission Controller injects a CSI volume instead of a hostPath volume—ensuring compliance with any enforced PSS policies.

Admission Controllerのモードをcsiにすると、Admission ControllerはPodにhostPathの代わりにCSI Volumeをインジェクトする。

The admission controller handles this transformation transparently, as shown in the sequence diagram below. When a user creates a pod, the Admission Controller mutates the pod spec to include a CSI volume source for socket-based communication. When the pod is scheduled, the kubelet requests the CSI volume, and the Datadog CSI driver mounts the appropriate socket into the container.

Admission Controllerは単純にMutating Webhookを使い、Podがスケジュールする時CSI VolumeをPod Specに挿入する。

実際に使ってみる

datadog-csi-driverをchartとしてインストール。

helm repo add datadog https://helm.datadoghq.com
helm install my-datadog-csi-driver datadog/datadog-csi-driver --version 0.4.4

Datadog Operatorの設定でcsiをオンにする。

apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
  name: datadog
spec:
  global:
    credentials:
      apiSecret:
        secretName: datadog-secret
        keyName: api-key
    csi: # ここを追加
      enabled: true # ここを追加

重要:ただし、APMかDSDをオンにした場合、Agentとの通信はデフォルトでsocketになる。

Starting from Helm chart v3.22.0 and Datadog Operator v1.1.0, the communication mode is automatically set to socket if either APM socket or DSD socket is enabled.

csiにしたい場合は、features.admissionController.agentCommunicationModeで指定すること。

features.admissionController.agentCommunicationMode
AgentCommunicationMode corresponds to the mode used by the Datadog application libraries to communicate with the Agent. It can be “hostip”, “service”, or “socket”.
https://docs.datadoghq.com/containers/datadog_operator/configuration/

現在、公式Docsにはcsiという選択肢は入っていないが、最新版を使ってみたところ、agentCommunicationModeの値にcsiを入れ、正常にcsiモードに切り替えた。

kind: DatadogAgent
apiVersion: datadoghq.com/v2alpha1
metadata:
  name: datadog
spec:
  global:
    clusterName: my-cluster
    site: ap1.datadoghq.com
    credentials:
      apiSecret:
        secretName: datadog-secret
        keyName: api-key
    csi: # ここを追加
      enabled: true # ここを追加
  features:
    apm:
      enabled: true # apmをオンにした場合
    agentCommunicationMode: "csi" # agentCommunicationModeを指定すること

Datadogを再デプロイし、csiモードでPodを立ち上げてみる。

$ kubectl run nginx --image nginx -l admission.datadoghq.com/enabled=true
pod/nginx created

$ kubectl get pod nginx -o yaml | grep hostPath -A3 # 何も返さない
$ kubectl get pod nginx -o yaml | grep csi -A3
  - csi:
      driver: k8s.csi.datadoghq.com
      readOnly: true
      volumeAttributes:
        type: DSDSocketDirectory
--
  - csi:
      driver: k8s.csi.datadoghq.com
      readOnly: true
      volumeAttributes:
        type: APMSocketDirectory

hostPathではなくcsiを使っていた。

PSS標準をenforceしてみる。

$ kubectl label ns default pod-security.kubernetes.io/enforce=baseline --dry-run=server 
namespace/default not labeled (server dry run)

問題なく貼られるようになった。

Discussion