spring-bootkubernetesprometheus

Spring Boot with Kubernetes Issue for Prometheus (Cannot file prometheus yml file for configuration)


I try to implement an example of Spring Boot with Couchbase through Prometheus and Grafana. After implementing Docker process, I passed the process for Kubernetes.

Here is the project file shown below

-> root
     -> k8s
          -> prometheus_deployment.yaml
     -> data
          -> prometheus
               -> config
                    -> prometheus.yml

Here is the prometheus-deployment.yml shown below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          args:
            - '--config.file=/etc/prometheus/prometheus.yml'
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config
              mountPath: /etc/prometheus
      volumes:
        - name: prometheus-config
          hostPath:
            path: ./data/prometheus/config/prometheus.yml
            type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
spec:
  selector:
    app: prometheus
  ports:
    - protocol: TCP
      port: 9090
      targetPort: 9090
  type: NodePort

After I ran all kuberentes files through kubectl apply -f k8s, I got this error message shown below.

Error: Error response from daemon: create ./data/prometheus/config/prometheus.yml: "./data/prometheus/config/prometheus.yml" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path

How can I fix it?


Solution

  • Here is the solution shown below

    1 ) Define prometheus-configmap.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prometheus-config
      namespace: default
    data:
      prometheus.yml: |
        global:
          scrape_interval: 120s # By default, scrape targets every 15 seconds.
          evaluation_interval: 120s # By default, evaluate rules every 15 seconds.
    
        # A scrape configuration containing exactly one endpoint to scrape:
        # Here it's Prometheus itself.
        scrape_configs:
          # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
          - job_name: 'prometheus'
            # Override the global default and scrape targets from this job every 5 seconds.
            scrape_interval: 5s
            # metrics_path defaults to '/metrics'
            # scheme defaults to 'http'.
            static_configs:
              - targets: ['localhost:9090']
    
          - job_name: 'Spring Boot Application input'
            metrics_path: '/actuator/prometheus'
            scrape_interval: 2s
            static_configs:
              - targets: ['todowithcouchbase:2323']
                labels:
                  application: 'To Do List with Couchbase'
    

    2 ) Revise prometheus-deployment.yml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus
      template:
        metadata:
          labels:
            app: prometheus
        spec:
          containers:
            - name: prometheus
              image: prom/prometheus:latest
              args:
                - '--config.file=/etc/prometheus/prometheus.yml'
              ports:
                - containerPort: 9090
              volumeMounts:
                - name: prometheus-config
                  mountPath: /etc/prometheus/prometheus.yml
                  subPath: prometheus.yml # Ensure only the file is mounted
          volumes:
            - name: prometheus-config
              configMap:
                name: prometheus-config
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: prometheus-service
    spec:
      selector:
        app: prometheus
      ports:
        - protocol: TCP
          port: 9090
          targetPort: 9090
      type: NodePort