Newer
Older
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Copyright 2019 Minsait - Indra S.A.
#
# 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.
# Author: Jose Manuel Palacios (jmpalacios@minsait.com)
# Author: Jose Antonio Martinez (jamartinezv@minsait.com)
# Obtain the path where the script is located
HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
echo $HERE
# Load component versions to be deployed
source $HERE/versions_monitoring
V_OPERATOR=""
V_MONGODB_EXPORTER=""
V_MYSQL_EXPORTER=""
V_OPERATOR=$PROMETHEUS_OPERATOR
V_MONGODB_EXPORTER=$PROMETHEUS_MONGODB_EXPORTER
V_MYSQL_EXPORTER=$PROMETHEUS_MYSQL_EXPORTER
function usage(){
echo -e "usage: $0 [OPTIONS]"
echo -e "Install OSM Monitoring"
echo -e " OPTIONS"
echo -e " -n <namespace> : use specified kubernetes namespace - default: monitoring"
echo -e " -s <service_type>: service type (ClusterIP|NodePort|LoadBalancer) - default: NodePort"
echo -e " --debug : debug script"
echo -e " --dump : dump arguments and versions"
echo -e " -h / --help : print this help"
}
NAMESPACE=monitoring
HELM=""
DEBUG=""
DUMP_VARS=""
SERVICE_TYPE=""
while getopts ":h-:n:s:" o; do
case "${o}" in
h)
usage && exit 0
;;
n)
NAMESPACE="${OPTARG}"
;;
s)
SERVICE_TYPE="${OPTARG}"
;;
-)
[ "${OPTARG}" == "help" ] && usage && exit 0
[ "${OPTARG}" == "debug" ] && DEBUG="y" && continue
[ "${OPTARG}" == "dump" ] && DUMP_VARS="y" && continue
echo -e "Invalid option: '--$OPTARG'\n" >&2
usage && exit 1
;;
\?)
echo -e "Invalid option: '-$OPTARG'\n" >&2
usage && exit 1
;;
*)
usage && exit 1
;;
esac
done
function dump_vars(){
echo "Args...."
echo "NAMESPACE=$NAMESPACE"
echo "SERVICE_TYPE=$SERVICE_TYPE"
echo "DEBUG=$DEBUG"
echo "Versions...."
echo "V_OPERATOR=$V_OPERATOR"
echo "V_MONGODB_EXPORTER=$V_MONGODB_EXPORTER"
echo "V_MYSQL_EXPORTER=$V_MYSQL_EXPORTER"
}
if [ -n "$SERVICE_TYPE" ] ; then
if [ [ $SERVICE_TYPE != "ClusterIP" ] || [ $SERVICE_TYPE != "NodePort" ] || [ $SERVICE_TYPE != "LoadBalancer" ] ] ; then
echo "Wrong service type..."
usage && exit 1
fi
else
SERVICE_TYPE="NodePort"
fi
if [ -n "$DEBUG" ] ; then
set -x
fi
if [ -n "$DUMP_VARS" ] ; then
dump_vars
fi
# Check if helm is installed
helm > /dev/null 2>&1
if [ $? != 0 ] ; then
echo "Helm is not installed, installing ....."
curl https://get.helm.sh/helm-v2.15.2-linux-amd64.tar.gz --output helm-v2.15.2.tar.gz
tar -zxvf helm-v2.15.2.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/helm
rm -r linux-amd64
rm helm-v2.15.2.tar.gz
fi
echo "Checking if helm-tiller is installed..."
kubectl --namespace kube-system get serviceaccount tiller > /dev/null 2>&1
if [ $? == 1 ] ; then
# tiller account for kubernetes
kubectl --namespace kube-system create serviceaccount tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
# HELM initialization
helm init --service-account tiller
# Wait for Tiller to be up and running
while true
do
tiller_status=`kubectl -n kube-system get deployment.apps/tiller-deploy --no-headers | awk '{print $2'}`
if [ ! -z "$tiller_status" ]
then
if [ $tiller_status == "1/1" ]
then
echo "Go...."
break
fi
fi
echo "Waiting for tiller READY...."
sleep 2
done
fi
# create monitoring namespace
echo "Creating namespace $NAMESPACE"
kubectl create namespace $NAMESPACE
# Prometheus operator installation
$HERE/change-charts-prometheus-operator.sh
echo "Creating stable/prometheus-operator"
helm install --namespace $NAMESPACE --version=$V_OPERATOR --name osm-monitoring --set kubelet.serviceMonitor.https=true,prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false $HERE/helm_charts/prometheus-operator
# Change osm-monitoring-grafana-config-dashboards to have folders
kubectl -n $NAMESPACE delete configmap osm-monitoring-grafana-config-dashboards
kubectl -n $NAMESPACE apply -f $HERE/grafanaproviders.yaml
# Exporters installation
# MongoDB
# exporter
echo "Creating stable/prometheus-mongodb-exporter"
helm install --namespace $NAMESPACE --version=$V_MONGODB_EXPORTER --name osm-mongodb-exporter --set image.tag='0.10.0',mongodb.uri='mongodb://mongo.osm:27017' stable/prometheus-mongodb-exporter
#dashboard:
kubectl -n $NAMESPACE apply -f $HERE/mongodb-exporter-dashboard.yaml
# Mysql
# exporter
echo "Creating stable/prometheus-mysql-exporter"
helm install --namespace $NAMESPACE --version=$V_MYSQL_EXPORTER --name osm-mysql-exporter --set serviceMonitor.enabled=true,mysql.user="root",mysql.pass=`kubectl -n osm get secret ro-db-secret -o yaml | grep MYSQL_ROOT_PASSWORD | awk '{print $2}' | base64 -d`,mysql.host="mysql.osm",mysql.port="3306" stable/prometheus-mysql-exporter
#dashboard:
kubectl -n $NAMESPACE apply -f $HERE/mysql-exporter-dashboard.yaml
# Kafka
# exporter
helm install --namespace $NAMESPACE --name osm-kafka-exporter $HERE/helm_charts/prometheus-kafka-exporter
# dashboard:
kubectl -n $NAMESPACE apply -f $HERE/kafka-exporter-dashboard.yaml
# Deploy summary dashboard
kubectl -n $NAMESPACE apply -f $HERE/summary-dashboard.yaml
# Patch prometheus, alertmanager and grafana with service type
# By default is created with ClusterIP type
if [ $SERVICE_TYPE == "NodePort" ] ; then
kubectl --namespace $NAMESPACE patch service osm-monitoring-grafana -p '{"spec":{"type":"NodePort"}}'
kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-alertmanager -p '{"spec":{"type":"NodePort"}}'
kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-prometheus -p '{"spec":{"type":"NodePort"}}'
fi
if [ $SERVICE_TYPE == "LoadBalancer" ] ; then
kubectl --namespace $NAMESPACE patch service osm-monitoring-grafana -p '{"spec":{"type":"LoadBalancer"}}'
kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-alertmanager -p '{"spec":{"type":"LoadBalancer"}}'
kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-prometheus -p '{"spec":{"type":"LoadBalancer"}}'
fi
# Restart grafana to be sure patches are applied
echo "Restarting grafana POD..."
pod_grafana=`kubectl -n monitoring get pods | grep grafana | awk '{print $1}'`
kubectl --namespace $NAMESPACE delete pod $pod_grafana