How To Install or Upgrade Prometheus
  • 29 Nov 2023
  • 4 Minutes to read
  • Dark
    Light

How To Install or Upgrade Prometheus

  • Dark
    Light

Article summary

Background

Prometheus has become a fundamental component of many Kubernetes-based monitoring solutions. RealTheory can notify you if Prometheus is a required component in your environment that is not present on your cluster or if the version of Prometheus that is installed is not in compliance with organizational standards.

Solution - Installation

If Prometheus is a required component in your Kubernetes environment, it is most likely that a Prometheus Helm chart or the yaml files for a Prometheus ConfigMap, Deployment, and Service already exist. Contact your Operations or DevOps team to determine if there are company resources for installing Prometheus BEFORE using either of the following procedures.

Procedure Using HELM Chart Package Manager

To install Prometheus:

  1. Use the Helm Installation Guide to install Helm (if not already installed).
  2. Use the following command to add the Prometheus Helm repository:
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  3. Do one of the following:
    • To install the latest version, use the following command:
      helm install prometheus prometheus-community/prometheus
    • To install a specific version, use the following command:
      helm install prometheus prometheus-community/prometheus --version <version>
      where <version> is the version number of Prometheus you want to install.

Procedure Using the YAML Manifest

To install Prometheus:

  1. (Optional) Create a namespace for Prometheus.
    Example:
    # namespace.yaml
    apiVersion: v1
    kind: Namespace
    metadata:
    name: prometheus
  2. Apply the namespace.
    Example:
    kubectl apply -f namespace.yaml
  3. Create a Prometheus ConfigMap (prometheus-configmap.yaml).
    Basic example:
    # prometheus-configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: prometheus-server-conf
    namespace: prometheus
    data:
    prometheus.yaml: |
    global:
    scrape_interval: 15s
    scrape_configs:
    - job_name: 'prometheus'
    static_configs:
    - targets: [<services, applications, or infrastructure components>]
    where -targets: is an array that contains the addresses (in the format hostname:port) of the endpoints or services exposing metrics.
  4. Apply the ConfigMap.
    Example:
    kubectl apply -f prometheus-configmap.yaml
  5. Create a Prometheus deployment using the official Prometheus container image.
    Example:
    # prometheus-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: prometheus-server
    namespace: prometheus
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: prometheus-server
    template:
    metadata:
    labels:
    app: prometheus-server
    spec:
    containers:
    - name: prometheus
    image: prom/prometheus:latest
    ports:
    - containerPort: 9090
    volumeMounts:
    - name: prometheus-server-conf
    mountPath: /etc/prometheus/prometheus.yml
    subPath: prometheus.yaml
    volumes:
    - name: prometheus-server-conf
    configMap:
    name: prometheus-server-conf
  6. Apply the Prometheus deployment.
    Example:
    kubectl apply -f prometheus-deployment.yaml
  7. Create a Prometheus service to expose the Prometheus user interface.
    Example:
    # prometheus-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
    name: prometheus-server
    namespace: prometheus
    spec:
    selector:
    app: prometheus-server
    ports:
    - name: web
    port: 80
    targetPort: 9090
  8. Apply the service.
    Example:
    kubectl apply -f prometheus-service.yaml
  9. Use the following command to verify the Prometheus pods are running:
    kubectl get pods -n <namespace>
    where <namespace> is the namespace where Prometheus is installed.

Solution - Upgrade

If a specific version of Prometheus is required in your Kubernetes environment, it is most likely that a Prometheus Helm chart or yaml files for a Prometheus ConfigMap, Deployment, and Service already exist. Contact your Operations or DevOps team to determine if there are company resources for installing Prometheus BEFORE using either of the following procedures.

Procedure Using HELM Chart Package Manager

To upgrade Prometheus:

  1. Use the Helm Installation Guide to install Helm (if not already installed).
  2. Use the following command to add the Prometheus Helm repository:
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  3. Use the following command to check the available versions of the Prometheus Helm chart:
    helm search repo prometheus-community/prometheus --versions
  4. Identify the version of Prometheus you want to upgrade to.
  5. Use the following command to upgrade to the appropriate version:
    helm upgrade prometheus prometheus-community/prometheus --version <version>
    where <version> is the version of Prometheus you want to upgrade to.
  6. Use the following command to verify that the Prometheus pods are running and have been updated:
    kubectl get pods -n <namespace>
    where <namespace> is the namespace where Prometheus is installed.

Procedure Using YAML Manifest

To upgrade Prometheus:

  1. If there are changes to the Prometheus configuration, do one of the following:
    • Update the current Prometheus ConfigMap
      Example:
      kubectl edit configmap prometheus-server-conf -n <namespace>
    • Create a new version of the ConfigMap
      Example:
      # updated-prometheus-configmap.yaml
      apiVersion: v1
      kind: ConfigMap
      metadata:
      name: prometheus-server-conf
      namespace: prometheus
      data:
      prometheus.yaml: |
      global:
      scrape_interval: 15s
      scrape_configs:
      - job_name: 'prometheus'
      static_configs:
      - targets: [<services, applications, or infrastructure components>]
      where -targets: is an array that contains the addresses (in the format hostname:port) of the endpoints or services exposing metrics.
  2. If you created a new version of the ConfigMap, apply the updated file.
    Example:
    kubectl apply -f updated-prometheus-configmap.yaml
  3. Update the Prometheus deployment in one of the following ways:
    • Modify the existing deployment to use the updated Prometheus container image and any other changes.
      Example:
      kubectl edit deployment prometheus-server -n <namespace>
    • Create a new version of the deployment
      Example:
      # updated-prometheus-deployment.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
      name: prometheus-server
      namespace: prometheus
      spec:
      replicas: 1
      selector:
      matchLabels:
      app: prometheus-server
      template:
      metadata:
      labels:
      app: prometheus-server
      spec:
      containers:
      - name: prometheus
      image: prom/prometheus:latest
      ports:
      - containerPort: 9090
      volumeMounts:
      - name: prometheus-server-conf
      mountPath: /etc/prometheus/prometheus.yml
      subPath: prometheus.yaml
      volumes:
      - name: prometheus-server-conf
      configMap:
      name: prometheus-server-conf
  4. If you created a new version of the deployment, apply the updated file.
    Example:
    kubectl apply -f updated-prometheus-deployment.yaml
  5. Use the following commands to initiate a rolling restart to apply the changes:
    Example:
    kubectl scale deployment prometheus-server --replicas=0 -n <namespace>
    kubectl scale deployment prometheus-server --replicas=1 -n <namespace>
  6. Use the following command to verify the Prometheus pods are running and have been updated:
    kubectl get pods -n <namespace>
    where <namespace> is the namespace where Prometheus is installed.

Was this article helpful?