Prometheus Installation and Scraping Configuration for Apinizer Gateway
Prometheus Installation and Scraping Configuration
Prometheus is a monitoring system that pulls metrics from specific targets and stores them as time series data. Scraping is the process by which Prometheus periodically collects metrics from target services. The Apinizer Gateway's metrics are provided through port 9091 and Prometheus can pull these metrics in two different ways:
Constant Scraping:
In static scraping, the services to be monitored are defined under static_configs with predefined fixed IP addresses or DNS names. Prometheus queries these services at regular intervals. This method is useful when service addresses do not change or can be defined manually.Dynamic Scraping:
In dynamic scraping, Prometheus automatically discovers services through a service discovery mechanism such as Kubernetes. With kubernetes_sd_config or similar configurations, there is no need to update the Prometheus configuration as services change. This is a great advantage in microservice architectures and constantly changing infrastructures.
These two methods are preferred according to the use case, allowing Prometheus to pull metrics from Apinizer Gateway.
Constant Scraping Configuration:
In order for the Apinizer Gateway to broadcast metrics over port 9091, the relevant container must be given -name as environment variable: METRICS_ENABLED=TRUE parameter must be defined.
In the Constant Scraping configuration, the service name from which Prometheus will scrape the metrics must be specified. For this reason, a service should be created to forward to port 9091 of the Apinizer Worker component.
apinizer-prometheus-svc.yaml
In order for Apinizer Gateway to provide metrics to Prometheus on port 9091, the scraping configuration of Prometheus must be done via ConfigMap. Prometheus' worker-prometheus-service is defined in static_configs at the specified address and constant scraping configuration is performed.
Dynamic Scraping Configuration:
The dynamic scraping method allows Prometheus to automatically discover pods within Kubernetes. Thanks to this method, there is no need to configure manual scraping for each new pod. Prometheus automatically pulls metrics using specific annotations attached to pods.
In order for Gateway metrics to be collected by Prometheus, the METRICS_ENABLED=TRUE variable must be added by selecting the Worker edit deployment option in the Deployments & Services section on the Gateway Environments page.

If Kubernetes management is not done with Apinizer
In order to enable Prometheus to collect metrics from Worker pods, relevant annotations should be added to the spec.template.metadata.annotations section of the relevant Deployment manifest. In addition, in order for Apinizer Gateway to be able to broadcast metrics over port 9091, environment variable -name: METRICS_ENABLED=TRUE parameter must be defined. In this way, the metrics service will be exported over port 9091 and Prometheus will automatically discover the Apinizer Gateway pod running on port 9091 and scrape the metrics.
Prometheus Scraping Configuration
Dynamic scraping must be enabled in Prometheus' ConfigMap configuration using kubernetes_sd_configs to discover anotations in pods.
The following ConfigMap example enables Prometheus to dynamically discover Kubernetes pods: This configuration allows Prometheus to automatically discover Kubernetes pods and collect metrics only from pods with the anotation prometheus.io/scrape: “true”. Thus, by applying dynamic scraping to Apinizer Gateway, metrics can be collected by Prometheus without the need for manual target definition.
Installation of Prometheus
Persistent Storage Configuration
Since Prometheus' metrics will be stored on a node in the Kubernetes cluster, PersistentVolume (PV) and PersistentVolumeClaim (PVC) definitions are required. This configuration allows Prometheus to protect its data in case of shutdown or restart.
The PersistentVolume (PV) and PersistentVolumeClaim (PVC) configuration above allows Prometheus to store its data on a specific node. However, the hostPath used here is dependent on the local file system of the specific node on which Prometheus is running.
For this reason:
- If Prometheus pods are moved to a different node, they will lose their data unless the new node has the same hostPath directory.
- To guarantee that pods always run on the same node, it is necessary to pin pods to specific nodes using nodeAffinity or nodeSelector.
Alternatively, NFS, Ceph, Longhorn or a cloud-based storage solution can be used to store data in a node-independent way.
Modify the Prometheus Deployment YAML file below to suit your systems and install it on your Kubernetes Cluster.
Kubernetes Service is created for Prometheus.
When Prometheus deploys on Kubernetes, it creates a Kubernetes service named prometheus-service and of type NodePort. This service is required for access to Prometheus from outside Kubernetes. However, instead of this service, you can use Ingress or whatever structure you use for the connection method in your organization.