This feature is still in public preview, but we encourage you to try it out!
You can use metrics from your New Relic account to autoscale applications and services in your Kubernetes cluster by deploying the New Relic Metrics Adapter. This adapter fetches the metric values from New Relic and makes them available for the Horizontal Pod Autoscalers.
The newrelic-k8s-metrics-adapter implements the
external.metrics.k8s.io API to support the use of external metrics based New Relic NRQL queries results. Once deployed, the value for each configured metric is fetched using the NerdGraph API based on the configured NRQL query.
The metrics adapter exposes the metrics over a secured endpoint with TLS.
New Relic metrics adapter in a cluster.
- Kubernetes 1.16 or higher.
- The New Relic Kubernetes integration.
- New Relic's user API key.
- No other External Metrics Adapter installed in the cluster.
To install the New Relic Metrics Adapter, we provide the
newrelic-k8s-metrics-adapter Helm chart, which is also included in the
nri-bundle chart used to deploy all New Relic Kubernetes components.
- If not already installed, install our Kubernetes integration.
- Upgrade the installation to include the New Relic Metrics Adapter with the following command:
helm upgrade --install newrelic newrelic/nri-bundle \ --namespace newrelic --create-namespace --reuse-values \ --set metrics-adapter.enabled=true \ --set newrelic-k8s-metrics-adapter.personalAPIKey=YOUR_NEW_RELIC_PERSONAL_API_KEY \ --set newrelic-k8s-metrics-adapter.config.accountID=YOUR_NEW_RELIC_ACCOUNT_ID \ --set newrelic-k8s-metrics-adapter.config.externalMetrics.external_metric_name.query=NRQL query
Please notice and adjust the following flags:
metrics-adapter.enabled: Must be set to
trueso the metrics adapter chart is installed.
newrelic-k8s-metrics-adapter.personalAPIKey: Must be set to valid New Relic Personal API key.
newrelic-k8s-metrics-adapter.config.accountID: Must be set to valid New Relic account where metrics are going to be fetched from.
newrelic-k8s-metrics-adapter.config.externalMetrics.<var>external_metric_name</var>.<var>query</var>: Adds a new external metric where:
<var>external_metric_name</var>: The metric name.
<var>query</var>: The base NRQL query that is used to get the value for the metric.
Alternatively, you can use a
values.yaml file that can be passed to the helm command with the
Values files can contain all parameters needed to configure the metrics explained in the configuration section.
You can configure multiple metrics in the metrics adapter and change some parameters to modify the behaviour of the metrics cache and filtering. To see the full list and descriptions of all parameters that can be modified, refer to the chart README.md and values.yaml files.
The following example is a Helm values file that enable the metrics adapter on the
nri-bundle chart installation, and configures the
metrics-adapter:enabled: truenewrelic-k8s-metrics-adapter:personalAPIKey: <Personal API Key>config:accountID: <Account ID>externalMetrics:nginx_average_requests:query: "FROM Metric SELECT average(nginx.server.net.requestsPerSecond) SINCE 2 MINUTES AGO"
The default time span for metrics is 1h. Therefore, you should define queries with the
SINCE clause to adjust the time span according to your environment and needs.
There is an HPA consuming the external metric as follows:
kind: HorizontalPodAutoscalerapiVersion: autoscaling/v2beta2metadata:name: nginx-scalerspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxminReplicas: 1maxReplicas: 10metrics:- type: Externalexternal:metric:name: nginx_average_requestsselector:matchLabels:k8s.namespaceName: nginxtarget:type: Valuevalue: 10000
Based on the HPA definition, the controller manager fetches the metrics from the external metrics API which are served by the New Relic metrics adapter.
The New Relic metrics adapter receives the query including the
nginx_average_requests metric name and all the selectors, and searches for a matching metric name in the internal memory based on the configured metrics. Then, it adds the selectors to the query to form a final query that is executed using NerdGraph to fetch the value from New Relic. The above example will generate a query like the following:
FROM Metric SELECT average(nginx.server.net.requestsPerSecond) WHERE clusterName=<clusterName> AND `k8s.namespaceName`='nginx' SINCE 2 MINUTES AGO
Notice that a
clusterName filter has been automatically added to the query to exclude metrics from other clusters in the same account. You can remove it by using the
removeClusterFilter configuration parameter. Also the value is cached for a period of time defined by the
cacheTTLSeconds configuration parameter, whose default is 30 seconds.