Skip to content

Commit f024b72

Browse files
committed
prometheus setup
0 parents  commit f024b72

6 files changed

+179
-0
lines changed

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# setup prometheus (kube-prometheus-stack) on k8s
2+
3+
easily setup prometheus(kube-prometheus-stack) on kubernetes and set prometheus, grafana and alertmanager config.
4+
5+
# Manual process
6+
7+
## add repo
8+
9+
```bash
10+
11+
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
12+
13+
helm repo update
14+
```
15+
16+
## create namespace
17+
18+
```bash
19+
kubectl create namespace monitoring
20+
```
21+
22+
## add grafana secrets (alert credentials mail, slack ....)
23+
24+
```bash
25+
kubectl apply -f grafana-secret.yml
26+
```
27+
28+
### base64 generate
29+
30+
```bash
31+
echo -n 'the_data' | base64
32+
```
33+
34+
## config
35+
you can modify config data inside `config folder`
36+
37+
## install prometheus
38+
39+
```bash
40+
helm install prometheus prometheus-community/kube-prometheus-stack -n monitoring -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml
41+
```
42+
43+
## upgrade
44+
45+
```bash
46+
helm upgrade prometheus prometheus-community/kube-prometheus-stack -n monitoring -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml
47+
```
48+
49+
## uninstall
50+
51+
```bash
52+
helm uninstall prometheus -n monitoring
53+
```
54+
55+
# automate with bash script
56+
57+
```bash
58+
./setup-prometheus.sh
59+
```

config/alert-manager.yml

Whitespace-only changes.

config/grafana.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
grafana:
2+
smtp:
3+
enabled: true
4+
existingSecret: "grafana-smtp-secret"
5+
passwordKey: "smtp_password"
6+
userKey: "smtp_username"
7+
8+
grafana.ini:
9+
smtp:
10+
enabled: true
11+
existingSecret: "grafana-smtp-secret"
12+
passwordKey: "smtp_password"
13+
userKey: "smtp_username"
14+
host: sandbox.smtp.mailtrap.io:587
15+
from_address: [email protected]
16+
17+
18+
19+
20+

config/prometheus.yml

Whitespace-only changes.

grafana-secret.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: grafana-secret
5+
namespace: monitoring # change to your namespace if different
6+
type: Opaque
7+
data:
8+
smtp_username: "base64_username"
9+
smtp_password: "base64_password"

setup-prometheus.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
3+
# Define Prometheus Helm repository URL
4+
prometheus_repo="https://prometheus-community.github.io/helm-charts"
5+
6+
# Function to check if namespace exists
7+
check_namespace() {
8+
local namespace="$1"
9+
if kubectl get namespace "$namespace" &> /dev/null; then
10+
return 0
11+
else
12+
return 1
13+
fi
14+
}
15+
16+
# Function to create namespace
17+
create_namespace() {
18+
local namespace="$1"
19+
echo "Creating namespace $namespace..."
20+
kubectl create namespace "$namespace"
21+
}
22+
23+
# Function to install Prometheus if not already installed
24+
install_prometheus() {
25+
local namespace="monitoring"
26+
if check_namespace "$namespace"; then
27+
if helm list -n "$namespace" | grep -q "prometheus"; then
28+
echo "Prometheus is already installed in namespace $namespace. You can upgrade."
29+
else
30+
echo "Installing Prometheus in namespace $namespace..."
31+
helm install prometheus prometheus-community/kube-prometheus-stack -n "$namespace" -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml
32+
echo "Prometheus installed successfully."
33+
fi
34+
else
35+
create_namespace "$namespace"
36+
echo "Namespace $namespace created."
37+
kubectl apply -f grafana-secret.yml
38+
echo "Secret for grafana configured."
39+
install_prometheus
40+
fi
41+
}
42+
43+
# Function to upgrade Prometheus
44+
upgrade_prometheus() {
45+
local namespace="monitoring"
46+
kubectl apply -f grafana-secret.yml
47+
echo "Secret for grafana configured."
48+
echo "Upgrading Prometheus in namespace $namespace..."
49+
helm upgrade prometheus prometheus-community/kube-prometheus-stack -n "$namespace" -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml
50+
echo "Prometheus upgraded successfully."
51+
}
52+
53+
# Function to uninstall Prometheus
54+
uninstall_prometheus() {
55+
local namespace="monitoring"
56+
echo "Are you sure you want to uninstall Prometheus from namespace $namespace? (yes/no)"
57+
read confirm
58+
case "$confirm" in
59+
yes)
60+
echo "Uninstalling Prometheus from namespace $namespace..."
61+
helm uninstall prometheus -n "$namespace"
62+
echo "Prometheus uninstalled successfully."
63+
;;
64+
*)
65+
echo "Uninstallation cancelled."
66+
;;
67+
esac
68+
}
69+
70+
# Prompt user for action
71+
echo "Do you want to install, upgrade, or uninstall Prometheus? (install/upgrade/uninstall)"
72+
read action
73+
74+
# Perform action based on user input
75+
case "$action" in
76+
install)
77+
# Update Helm Repositories
78+
echo "Updating Helm repositories..."
79+
helm repo update
80+
install_prometheus
81+
;;
82+
upgrade)
83+
upgrade_prometheus
84+
;;
85+
uninstall)
86+
uninstall_prometheus
87+
;;
88+
*)
89+
echo "Invalid action. Please choose 'install', 'upgrade', or 'uninstall'."
90+
;;
91+
esac

0 commit comments

Comments
 (0)