---
isPrivate: true
title: Kubernetes Basic Agent Usage in Agent v5
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > Agent > Agent Guides > Kubernetes Basic Agent Usage in Agent v5
---

# Kubernetes Basic Agent Usage in Agent v5

{% image
   source="https://docs.dd-static.net/images/integrations/kubernetes/k8sdashboard.e88a815688c7dc51eec2a9c306e996e2.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/integrations/kubernetes/k8sdashboard.e88a815688c7dc51eec2a9c306e996e2.png?auto=format&fit=max&w=850&dpr=2 2x"
   alt="Kubernetes Dashboard" /%}

{% alert level="danger" %}
The Datadog Agent v5 is supported up to Kubernetes version 1.8, for latest version of Kubernetes use the Datadog Agent v6.
{% /alert %}

## Overview{% #overview %}

Get metrics from Kubernetes in real time to:

- Visualize and monitor Kubernetes states
- Be notified about Kubernetes failovers and events

For Kubernetes, it's recommended to run the [Agent in a DaemonSet](https://github.com/DataDog/docker-dd-agent). A [Docker image](https://gcr.io/datadoghq/docker-dd-agent) is available with both the Docker and the Kubernetes integrations enabled.

You can also just [run the Datadog Agent on your host](https://docs.datadoghq.com/.md#host-setup) and configure it to gather your Kubernetes metrics.

## Set up Kubernetes{% #set-up-kubernetes %}

### Installation{% #installation %}

#### Container installation{% #container-installation %}

Thanks to Kubernetes, you can take advantage of DaemonSets to automatically deploy the Datadog Agent on all your nodes (or on specific nodes by using nodeSelectors).

*If DaemonSets are not an option for your Kubernetes cluster, [install the Datadog Agent](https://docs.datadoghq.com/integrations/docker_daemon.md) as a deployment on each Kubernetes node.*

If your Kubernetes has RBAC enabled, see how to configure RBAC permissions with your [Datadog-Kubernetes integration](https://docs.datadoghq.com/agent/kubernetes.md).

- Create the following `dd-agent.yaml` manifest:

```yaml

apiVersion: extensions/v1beta1
metadata:
  name: dd-agent
spec:
  template:
    metadata:
      labels:
        app: dd-agent
      name: dd-agent
    spec:
      containers:
      - image: gcr.io/datadoghq/docker-dd-agent:latest
        imagePullPolicy: Always
        name: dd-agent
        ports:
          - containerPort: 8125
            name: dogstatsdport
            protocol: UDP
        env:
          - name: API_KEY
            value: "DATADOG_API_KEY"
          - name: KUBERNETES
            value: "yes"
        volumeMounts:
          - name: dockersocket
            mountPath: /var/run/docker.sock
          - name: procdir
            mountPath: /host/proc
            readOnly: true
          - name: cgroups
            mountPath: /host/sys/fs/cgroup
            readOnly: true
      volumes:
        - hostPath:
            path: /var/run/docker.sock
          name: dockersocket
        - hostPath:
            path: /proc
          name: procdir
        - hostPath:
            path: /sys/fs/cgroup
          name: cgroups
```

Replace `DATADOG_API_KEY` with [your api key](https://app.datadoghq.com/organization-settings/api-keys) or use [Kubernetes secrets](https://kubernetes.io/docs/concepts/configuration/secret) to set your API key [as an environment variable](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables).

- Deploy the DaemonSet with the command:
  ```shell
  kubectl create -f dd-agent.yaml
  ```

**Note**: This manifest enables the Autodiscovery, auto-configuration feature. To disable it, remove the `SD_BACKEND` environment variable definition. To learn how to configure Autodiscovery, see the [Autodiscovery documentation](https://docs.datadoghq.com/getting_started/agent/autodiscovery.md).

#### Host installation{% #host-installation %}

Install the `dd-check-kubernetes` package manually or with your favorite configuration manager.

### Configuration{% #configuration %}

Edit the `kubernetes.yaml` file to point to your server and port, set the masters to monitor:

```yaml

instances:
    host: localhost
    port: 4194
    method: http
```

See the [example kubernetes.yaml](https://github.com/DataDog/integrations-core/blob/master/kubernetes/datadog_checks/kubernetes/data/conf.yaml.example) for all available configuration options.

### Validation{% #validation %}

#### Container running{% #container-running %}

To verify the Datadog Agent is running in your environment as a DaemonSet, execute:

```shell
kubectl get daemonset
```

If the Agent is deployed, a similar output to the text below is displayed, where **desired** and **current** are equal to the number of nodes running in your cluster.

```text
NAME       DESIRED   CURRENT   NODE-SELECTOR   AGE
dd-agent   3         3         <none>          11h
```

#### Agent check running{% #agent-check-running %}

[Run the Agent's `info` subcommand](https://docs.datadoghq.com/agent/configuration/agent-commands.md#agent-status-and-information) and look for `kubernetes` under the Checks section:

```text
Checks
======
    kubernetes
    -----------
      - instance #0 [OK]
      - Collected 39 metrics, 0 events & 7 service checks
```

## Setup Kubernetes state{% #setup-kubernetes-state %}

### Installation{% #installation-1 %}

#### Container installation{% #container-installation-1 %}

If you are running Kubernetes >= 1.2.0, you can use the [kube-state-metrics](https://github.com/kubernetes/kube-state-metrics) project to provide additional metrics (identified by the `kubernetes_state` prefix in the metrics list below) to Datadog.

To run kube-state-metrics, create a `kube-state-metrics.yaml` file using the following manifest to deploy the kube-state-metrics service:

```yaml
apiVersion: extensions/v1beta1
metadata:
  name: kube-state-metrics
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kube-state-metrics
    spec:
      containers:
      - name: kube-state-metrics
        image: gcr.io/google_containers/kube-state-metrics:v1.2.0
        ports:
        - name: metrics
          containerPort: 8080
        resources:
          requests:
            memory: 30Mi
            cpu: 100m
          limits:
            memory: 50Mi
            cpu: 200m
---
apiVersion: v1
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  labels:
    app: kube-state-metrics
  name: kube-state-metrics
spec:
  ports:
  - name: metrics
    port: 8080
    targetPort: metrics
    protocol: TCP
  selector:
    app: kube-state-metrics
```

Then deploy it by running:

```shell
kubectl create -f kube-state-metrics.yaml
```

The manifest above uses Google's publicly available `kube-state-metrics` container, which is also available on [Quay](https://quay.io/coreos/kube-state-metrics). If you want to build it manually, see the [official project documentation](https://github.com/kubernetes/kube-state-metrics).

If you configure your Kubernetes State Metrics service to run on a different URL or port, you can configure the Datadog Agent by setting the `kube_state_url` parameter in `conf.d/kubernetes_state.yaml`, then restarting the Agent. For more information, see the [kubernetes_state.yaml.example file](https://github.com/DataDog/integrations-core/blob/master/kubernetes_state/datadog_checks/kubernetes_state/data/conf.yaml.example). If you have enabled [Autodiscovery](https://docs.datadoghq.com/getting_started/agent/autodiscovery.md), the kube state URL is configured and managed automatically.

#### Host installation{% #host-installation-1 %}

Install the `dd-check-kubernetes_state` package manually or with your favorite configuration manager (On CentOS/AWS, download the [rpm package](https://yum.datadoghq.com/stable/6/x86_64) and see the [installation instructions](https://docs.datadoghq.com/agent/guide/installing-the-agent-on-a-server-with-limited-internet-connectivity.md). Then edit the `kubernetes_state.yaml` file to point to your server and port and set the masters to monitor. See the [example kubernetes_state.yaml](https://github.com/DataDog/integrations-core/blob/master/kubernetes_state/datadog_checks/kubernetes_state/data/conf.yaml.example) for all available configuration options.

### Validation{% #validation-1 %}

#### Container validation{% #container-validation %}

To verify the Datadog Agent is running in your environment as a DaemonSet, execute:

```shell
kubectl get daemonset
```

If the Agent is deployed, you receive a similar output to the text below, where **desired** and **current** are equal to the number of running nodes in your cluster.

```bash
NAME       DESIRED   CURRENT   NODE-SELECTOR   AGE
dd-agent   3         3         <none>          11h
```

#### Agent check validation{% #agent-check-validation %}

[Run the Agent's info subcommand](https://docs.datadoghq.com/agent/configuration/agent-commands.md#agent-status-and-information) and look for `kubernetes_state` under the Checks section:

```bash
Checks
======
    kubernetes_state
    -----------
      - instance #0 [OK]
      - Collected 39 metrics, 0 events & 7 service checks
```

## Setup Kubernetes DNS{% #setup-kubernetes-dns %}

### Installation{% #installation-2 %}

Install the `dd-check-kube_dns` package manually or with your favorite configuration manager.

### Configuration{% #configuration-1 %}

Edit the `kube_dns.yaml` file to point to your server and port, set the masters to monitor. See the [sample kube_dns.yaml](https://github.com/DataDog/integrations-core/blob/master/kube_dns/datadog_checks/kube_dns/data/conf.yaml.example) for all available configuration options.

#### Using with service discovery{% #using-with-service-discovery %}

If you are using one `dd-agent` pod per Kubernetes worker node, you could use the following annotations on your kube-dns pod to retrieve the data automatically.

```yaml

apiVersion: v1
metadata:
  annotations:
    service-discovery.datadoghq.com/kubedns.check_names: '["kube_dns"]'
    service-discovery.datadoghq.com/kubedns.init_configs: '[{}]'
    service-discovery.datadoghq.com/kubedns.instances: '[[{"prometheus_endpoint":"http://%%host%%:10055/metrics", "tags":["dns-pod:%%host%%"]}]]'
```

**Notes:**

- Notice the "dns-pod" tag keeps track of the target DNS pod IP. The other tags are related to the `dd-agent` that is polling the information using service discovery.
- The service discovery annotations need to be applied to the pod. In case of a deployment, add the annotations to the metadata of the template's spec.

### Validation{% #validation-2 %}

[Run the Agent's info subcommand](https://docs.datadoghq.com/agent/configuration/agent-commands.md#agent-status-and-information) and look for `kube_dns` under the Checks section:

```bash
Checks
======
    kube_dns
    -----------
      - instance #0 [OK]
      - Collected 39 metrics, 0 events & 7 service checks
```
