Prometheus

 

1. Prometheus ConfigMap

This ConfigMap contains the configuration for Prometheus, defining which services it will scrape metrics from.

apiVersion: v1

kind: ConfigMap

metadata:

  name: prometheus-config

  namespace: monitoring

  labels:

    app: prometheus

data:

  prometheus.yml: |

    global:

      scrape_interval: 15s

    scrape_configs:

      - job_name: 'kubernetes-apiservers'

        kubernetes_sd_configs:

        - role: endpoints

        scheme: https

        tls_config:

          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

          insecure_skip_verify: true

        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token

        relabel_configs:

        - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]

          action: keep

          regex: default;kubernetes;https


      - job_name: 'kubernetes-nodes'

        kubernetes_sd_configs:

        - role: node

        relabel_configs:

        - action: labelmap

          regex: __meta_kubernetes_node_label_(.+)


      - job_name: 'kubernetes-pods'

        kubernetes_sd_configs:

        - role: pod

        relabel_configs:

        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]

          action: keep

          regex: true

        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]

          action: replace

          target_label: __metrics_path__

          regex: (.+)

        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]

          action: replace

          regex: ([^:]+)(?::\d+)?;(\d+)

          replacement: $1:$2

          target_label: __address__



2. Prometheus Deployment

This Deployment manages the Prometheus Pods.

apiVersion: apps/v1

kind: Deployment

metadata:

  name: prometheus-deployment

  namespace: monitoring

  labels:

    app: prometheus

spec:

  replicas: 1

  selector:

    matchLabels:

      app: prometheus

  template:

    metadata:

      labels:

        app: prometheus

    spec:

      containers:

      - name: prometheus

        image: prom/prometheus:v2.30.0

        args:

        - "--config.file=/etc/prometheus/prometheus.yml"

        - "--storage.tsdb.path=/prometheus/"

        - "--web.console.libraries=/usr/share/prometheus/console_libraries"

        - "--web.console.templates=/usr/share/prometheus/consoles"

        ports:

        - containerPort: 9090

        volumeMounts:

        - name: prometheus-config-volume

          mountPath: /etc/prometheus/

        - name: prometheus-storage-volume

          mountPath: /prometheus/

      volumes:

      - name: prometheus-config-volume

        configMap:

          name: prometheus-config

      - name: prometheus-storage-volume

        emptyDir: {}


3. Prometheus Service

This Service exposes Prometheus to be accessed within the cluster or externally.

apiVersion: v1

kind: Service

metadata:

  name: prometheus-service

  namespace: monitoring

  labels:

    app: prometheus

spec:

  type: NodePort

  ports:

  - port: 9090

    targetPort: 9090

    nodePort: 30000

  selector:

    app: prometheus



4. Namespace (Optional)

Create a separate namespace for monitoring if you haven't already.

apiVersion: v1

kind: Namespace

metadata:

  name: monitoring


5. Apply the YAML Files

  1. Create the Namespace:
    kubectl apply -f namespace.yaml

  2. Apply the ConfigMap:
          kubectl apply -f prometheus-config.yaml
    
      3. Apply the Deployment:

        kubectl apply -f prometheus-deployment.yaml

       4. Apply the Service:

          kubectl apply -f prometheus-service.yaml


Access Prometheus

You can access Prometheus using the NodePort service on http://<node-ip>:30000.

This setup provides a basic Prometheus deployment for monitoring Kubernetes clusters. You can extend this by adding more scrape configs, setting up persistent storage, or integrating it with Grafana for better visualization.

Comments

Popular posts from this blog

Delploy Cluster : Managed K8 & Self Managed K8

ctr | nerdctl | crictl

Deploy To Kubernetes with Jenkins GitOps GitHub Pipeline