Skip to content

Instantly share code, notes, and snippets.

@MdSahil-oss
Last active December 22, 2023 15:05
Show Gist options
  • Save MdSahil-oss/93eafb2f405383f9180b6bb8fe93691a to your computer and use it in GitHub Desktop.
Save MdSahil-oss/93eafb2f405383f9180b6bb8fe93691a to your computer and use it in GitHub Desktop.

Monitoring an application with prometheus

I configured a web application with prometheus to monitor its:

  • version
  • http_requests_total
  • http_request_duration_seconds
  • http_request_duration_seconds_count
  • http_request_duration_seconds_sum

Used Technologies:

  • AWS
  • Kubernete
  • Docker
  • Prometheus & Grafana (Monitoring)

How I built this project:

  • First of all, A web application was prepared to host all of the metrics data at endpoint /metrics with the help of prometheus library so that the configured prometheus can scrape metrics data from the web application endpoint metrics at an interval.

  • Then, A Container image was built of the application, That can be found here.

  • Then I deployed a deployment resource in my running Kubernetes cluster on AWS EKS:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app.kubernetes.io/name: prometheus-example-app
      name: prometheus-example-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app.kubernetes.io/name: prometheus-example-app
      template:
        metadata:
          labels:
            app.kubernetes.io/name: prometheus-example-app
        spec:
          containers:
          - name: prometheus-example-app
            image: mdsahiloss/prometheus-example-app:latest
            ports:
            - name: web
              containerPort: 8080
    
  • And then, I Created a ClusterIP service inside K8s, So that other resources in K8s can communicate with the application pods.

  • And then, I installed a promentheus operator in my running Kubernetes cluster on AWS EKS, By following the instructions available here.

  • And then created a resource ServiceMonitor (CRD of this resource is provided by the installed Prometheus-Operator) inside K8s cluster in default namespace:

     apiVersion: monitoring.coreos.com/v1
     kind: ServiceMonitor
     metadata:
       labels:
         app.kubernetes.io/name: prometheus-example-app-svc-monitor
       name: prometheus-example-app-svc-monitor
     spec:
       selector:
         matchLabels:
           app.kubernetes.io/name: prometheus-example-app-svc
       endpoints:
         - port: http
       namespaceSelector:
         any: true
    

That's it after this, I was able to see all the metrics generated by my application on the prometheus web which is runnning in my K8s Cluster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment