Loading

Conditions based autodiscover

You can define autodiscover conditions in each input to allow Elastic Agent to automatically identify Pods and start monitoring them using predefined integrations. Refer to Inputs to get an idea.

Important

Condition definition is supported both in Elastic Agent managed by Fleet and in standalone scenarios.

For more information about variables and conditions in input configurations, refer to Variables and conditions in input configurations. You can find available variables of autodiscovery in Kubernetes Provider.

To automatically identify a Redis Pod and monitor it with the Redis integration, uncomment the following input configuration inside the Elastic Agent Standalone manifest:

 - name: redis type: redis/metrics use_output: default meta: package: name: redis version: 0.3.6 data_stream: namespace: default streams: - data_stream: dataset: redis.info type: metrics metricsets: - info hosts: - '${kubernetes.pod.ip}:6379' idle_timeout: 20s maxconn: 10 network: tcp period: 10s condition: ${kubernetes.labels.app} == 'redis' 

The condition ${kubernetes.labels.app} == 'redis' will make the Elastic Agent look for a Pod with the label app:redis within the scope defined in its manifest.

For a list of provider fields that you can use in conditions, refer to Kubernetes Provider. Some examples of conditions usage are:

  1. For a pod with label app.kubernetes.io/name=ingress-nginx the condition should be condition: ${kubernetes.labels.app.kubernetes.io/name} == "ingress-nginx".
  2. For a pod with annotation prometheus.io/scrape: "true" the condition should be ${kubernetes.annotations.prometheus.io/scrape} == "true".
  3. For a pod with name kube-scheduler-kind-control-plane the condition should be ${kubernetes.pod.name} == "kube-scheduler-kind-control-plane".

The redis input defined in the Elastic Agent manifest only specifies theinfo metricset. To learn about other available metricsets and their configuration settings, refer to the Redis module page.

To deploy Redis, you can apply the following example manifest:

 apiVersion: v1 kind: Pod metadata: name: redis labels: k8s-app: redis app: redis spec: containers: - image: redis imagePullPolicy: IfNotPresent name: redis ports: - name: redis containerPort: 6379 protocol: TCP 

You should now be able to see Redis data flowing in on index metrics-redis.info-default. Make sure the port in your Redis manifest file matches the port used in the Redis input.

Note

All assets (dashboards, ingest pipelines, and so on) related to the Redis integration are not installed. You need to explicitly install them through Kibana.

Conditions can also be used in inputs configuration in order to set the target host dynamically for a targeted Pod based on its labels. This is useful for datasets that target specific pods like kube-scheduler or kube-controller-manager. The following configuration will enable kubernetes.scheduler dataset only for pods which have the label component=kube-scheduler defined.

 - data_stream: dataset: kubernetes.scheduler type: metrics metricsets: - scheduler bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token hosts: - 'https://${kubernetes.pod.ip}:10259' period: 10s ssl.verification_mode: none condition: ${kubernetes.labels.component} == 'kube-scheduler' 
Note

Pods' labels and annotations can be used in autodiscover conditions. In the case of labels or annotations that include dots(.), they can be used in conditions exactly as they are defined in the pods. For example condition: ${kubernetes.labels.app.kubernetes.io/name} == 'ingress-nginx'. This should not be confused with the dedoted (by default) labels and annotations stored into Elasticsearch(Kubernetes Provider).

Warning

Before the 8.6 release, labels used in autodiscover conditions were dedoted in case the labels.dedot parameter was set to true in Kubernetes Provider configuration (by default true). The same did not apply for annotations. This was fixed in 8.6 release. Refer to the Release Notes section of the version 8.6.0 documentation.

Warning

In some "As a Service" Kubernetes implementations, like GKE, the control plane nodes or even the Pods running on them won’t be visible. In these cases, it won’t be possible to use scheduler metricsets, necessary for this example. Refer scheduler and controller manager to find more information.

Following the Redis example, if you deploy another Redis Pod with a different port, it should be detected. To check this, go, for example, to the field service.address under metrics-redis.info-default. It should be displaying two different services.

To obtain the policy generated by this configuration, connect to Elastic Agent container:

 kubectl exec -n kube-system --stdin --tty elastic-agent-standalone-id -- /bin/bash 

Do not forget to change the elastic-agent-standalone-id to your Elastic Agent Pod’s name. Moreover, make sure that your Pod is inside kube-system. If not, change -n kube-system to the correct namespace.

Inside the container inspect the output of the configuration file you used for the Elastic Agent:

 elastic-agent inspect --variables --variables-wait 1s -c /etc/elastic-agent/agent.yml 

To set the log path of Pods dynamically in the configuration, use a variable in the Elastic Agent policy to return path information from the provider:

 - name: container-log id: container-log-${kubernetes.pod.name}-${kubernetes.container.id} type: filestream use_output: default meta: package: name: kubernetes version: 1.9.0 data_stream: namespace: default streams: - data_stream: dataset: kubernetes.container_logs type: logs prospector.scanner.symlinks: true parsers: - container: ~ paths: - /var/log/containers/*${kubernetes.container.id}.log