-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathprometheus_on_minikube.sh
More file actions
executable file
·159 lines (145 loc) · 4.65 KB
/
prometheus_on_minikube.sh
File metadata and controls
executable file
·159 lines (145 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
#
# Copyright (c) 2020, 2021 Red Hat, IBM Corporation and others.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# include the common_utils.sh script to access methods
current_dir="$(dirname "$0")"
source ${current_dir}/common_utils.sh
non_interactive=0
# Call setup by default (and not terminate)
setup=1
# Prometheus release default
tag="v0.16.0"
function install_prometheus() {
echo
echo "Info: Checking pre requisites for prometheus..."
check_kustomize
prometheus_ns="monitoring"
kubectl_cmd="kubectl -n ${prometheus_ns}"
prometheus_pod_running=$(${kubectl_cmd} get pods | grep "prometheus-k8s-1")
if [ "${prometheus_pod_running}" != "" ]; then
echo "Prometheus is already installed and running."
return;
fi
if [ "${non_interactive}" == 0 ]; then
echo "Info: Prometheus needs cadvisor/grafana"
echo -n "Download and install these software to minikube(y/n)? "
read inst
linst=$(echo ${inst} | tr A-Z a-z)
if [ "${linst}" == "n" ]; then
echo "Info: prometheus not installed"
exit 0
fi
fi
mkdir minikube_downloads 2>/dev/null
pushd minikube_downloads >/dev/null
echo "Info: Downloading cadvisor git"
git clone https://github.com/google/cadvisor.git 2>/dev/null
pushd cadvisor/deploy/kubernetes/base >/dev/null
echo
echo "Info: Installing cadvisor"
kubectl kustomize . | kubectl apply -f-
check_err "Error: Unable to install cadvisor"
popd >/dev/null
echo
echo "Info: Downloading prometheus git release - ${tag}"
# Commenting the below lines as the latest prometheus requires more than 2 CPUs and the PR checks fail on github hosted runners
# as they have only 2 CPUs on the host. Hence switching back to prometheus release 0.8.0 (Apr 2021)
# git clone https://github.com/coreos/kube-prometheus.git 2>/dev/null
git clone -b ${tag} https://github.com/coreos/kube-prometheus.git 2>/dev/null
pushd kube-prometheus/manifests >/dev/null
echo
echo "Info: Installing CRDs"
kubectl apply --server-side -f setup
check_err "Error: Unable to setup prometheus CRDs"
kubectl wait --for condition=Established --timeout=60s crd/servicemonitors.monitoring.coreos.com
kubectl wait --for condition=Established --timeout=60s crd/prometheuses.monitoring.coreos.com
kubectl wait --for condition=Established --timeout=60s crd/alertmanagers.monitoring.coreos.com
kubectl wait --for condition=Established --timeout=60s crd/prometheusrules.monitoring.coreos.com
check_err "Error: Unable to setup prometheus CRDs"
kubectl apply --server-side -f .
check_err "Error: Unable to install prometheus"
popd >/dev/null
popd >/dev/null
echo -n "Info: Waiting for all Prometheus Pods to get spawned..."
while true;
do
# Wait for prometheus docker images to get downloaded and spawn the main pod
pod_started=$(${kubectl_cmd} get pods | grep "prometheus-k8s-1")
if [ "${pod_started}" == "" ]; then
# prometheus-k8s-1 not yet spawned
echo -n "."
sleep 5
else
echo "done"
break;
fi
done
check_running prometheus-k8s-1 ${prometheus_ns}
sleep 2
}
function delete_prometheus() {
echo -n "### Removing cadvisor and prometheus"
pushd minikube_downloads > /dev/null
echo
echo "Removing cadvisor"
pushd cadvisor/deploy/kubernetes/base > /dev/null
kubectl kustomize . | kubectl delete -f-
popd > /dev/null
echo
echo "Removing prometheus"
pushd kube-prometheus/manifests > /dev/null
kubectl delete -f . 2>/dev/null
kubectl delete -f setup 2>/dev/null
popd > /dev/null
popd > /dev/null
rm -rf minikube_downloads
}
# Input from user to install/delete prometheus
function usage() {
echo >&2 "usage: $0 [-r <prometheus release tag (default - v0.16.0)] [-a] [-s|t] where -a= non-interactive mode, -s=start, -t=terminate ";
exit 1;
}
# empty argument validation
if [ $# -eq 0 ]; then
usage
fi
while getopts "r:ast" option;
do
case "${option}" in
r)
tag=${OPTARG}
;;
a)
non_interactive=1
;;
s) # For option s to install the prometheus
setup=1
;;
t) # For option t terminating and deleting the prometheus
setup=0
;;
\?) # For invalid option
usage
;;
esac
done
if [ "${setup}" == 1 ]; then
echo "Info: installing prometheus..."
install_prometheus
else
echo "Info: deleting prometheus..."
delete_prometheus
fi