Lab 7.4: Exposing Prometheus as a Service (NodePort)


To access the Prometheus dashboard over an IP or a DNS name, you need to expose it as a Kubernetes service.

1. Create a file named prometheus-service.yaml and copy the following contents. We will expose Prometheus on all Kubernetes node IP’s on port 30000.

#create the file using vim text editor
vim  prometheus-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: monitoring
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/port:   '9090'
spec:
  selector: 
    app: prometheus-server
  type: NodePort  
  ports:
    - port: 8080
      targetPort: 9090 
      nodePort: 30000

#save and exit the file

The annotations in the above service YAML make sure that the service endpoint is scrapped by Prometheus. The prometheus.io/port should always be the target port mentioned in service YAML.

2. Create the service using the following command:

kubectl create -f prometheus-service.yaml --namespace=monitoring

3. Once created, you can access the Prometheus dashboard using any of the Kubernetes node’s IP on port 30000. If you are on the cloud, make sure you have the right firewall rules to access port 30000 from your workstation.

10.23.X.10:30000

Prometheus dashboard

4. Now if you browse to status --> Targets, you will see all the Kubernetes endpoints connected to Prometheus automatically using service discovery as shown below.

Prometheus targets

5. You can head over to the homepage and select the metrics you need from the drop-down and get the graph for the time range you mention. An example graph for container_cpu_usage_seconds_total is shown below: