- 29 Nov 2023
- 4 Minutes to read
- Print
- DarkLight
How To Install or Upgrade Prometheus
- Updated on 29 Nov 2023
- 4 Minutes to read
- Print
- DarkLight
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:
- Use the Helm Installation Guide to install Helm (if not already installed).
- Use the following command to add the Prometheus Helm repository:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
- 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.
- To install the latest version, use the following command:
Procedure Using the YAML Manifest
To install Prometheus:
- (Optional) Create a namespace for Prometheus.
Example:
# namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: prometheus
- Apply the namespace.
Example:
kubectl apply -f namespace.yaml
- 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 formathostname:port
) of the endpoints or services exposing metrics. - Apply the ConfigMap.
Example:
kubectl apply -f prometheus-configmap.yaml
- 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
- Apply the Prometheus deployment.
Example:
kubectl apply -f prometheus-deployment.yaml
- 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
- Apply the service.
Example:
kubectl apply -f prometheus-service.yaml
- 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:
- Use the Helm Installation Guide to install Helm (if not already installed).
- Use the following command to add the Prometheus Helm repository:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
- Use the following command to check the available versions of the Prometheus Helm chart:
helm search repo prometheus-community/prometheus --versions
- Identify the version of Prometheus you want to upgrade to.
- 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. - 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:
- 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 formathostname:port
) of the endpoints or services exposing metrics.
- Update the current Prometheus ConfigMap
- If you created a new version of the ConfigMap, apply the updated file.
Example:
kubectl apply -f updated-prometheus-configmap.yaml
- 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
- Modify the existing deployment to use the updated Prometheus container image and any other changes.
- If you created a new version of the deployment, apply the updated file.
Example:
kubectl apply -f updated-prometheus-deployment.yaml
- 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>
- 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.