4259696a96e93c6be3d5db4e1da0647b926f218f
[osm/devops.git] / installers / k8s / install_osm_k8s_monitoring.sh
1 #!/bin/bash
2
3 # Copyright 2019 Minsait - Indra S.A.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # Author: Jose Manuel Palacios (jmpalacios@minsait.com)
17 # Author: Jose Antonio Martinez (jamartinezv@minsait.com)
18
19 # Obtain the path where the script is located
20 HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
21
22 echo $HERE
23 # Load component versions to be deployed
24 source $HERE/versions_monitoring
25 V_OPERATOR=""
26 V_MONGODB_EXPORTER=""
27 V_MYSQL_EXPORTER=""
28
29 V_OPERATOR=$PROMETHEUS_OPERATOR
30 V_MONGODB_EXPORTER=$PROMETHEUS_MONGODB_EXPORTER
31 V_MYSQL_EXPORTER=$PROMETHEUS_MYSQL_EXPORTER
32
33
34 function usage(){
35 echo -e "usage: $0 [OPTIONS]"
36 echo -e "Install OSM Monitoring"
37 echo -e " OPTIONS"
38 echo -e " -n <namespace> : use specified kubernetes namespace - default: monitoring"
39 echo -e " -s <service_type>: service type (ClusterIP|NodePort|LoadBalancer) - default: NodePort"
40 echo -e " --debug : debug script"
41 echo -e " --dump : dump arguments and versions"
42 echo -e " -h / --help : print this help"
43 }
44
45 NAMESPACE=monitoring
46 HELM=""
47 DEBUG=""
48 DUMP_VARS=""
49 SERVICE_TYPE=""
50 while getopts ":h-:n:s:" o; do
51 case "${o}" in
52 h)
53 usage && exit 0
54 ;;
55 n)
56 NAMESPACE="${OPTARG}"
57 ;;
58
59 s)
60 SERVICE_TYPE="${OPTARG}"
61 ;;
62
63 -)
64 [ "${OPTARG}" == "help" ] && usage && exit 0
65 [ "${OPTARG}" == "debug" ] && DEBUG="y" && continue
66 [ "${OPTARG}" == "dump" ] && DUMP_VARS="y" && continue
67 echo -e "Invalid option: '--$OPTARG'\n" >&2
68 usage && exit 1
69 ;;
70
71 \?)
72 echo -e "Invalid option: '-$OPTARG'\n" >&2
73 usage && exit 1
74 ;;
75 *)
76 usage && exit 1
77 ;;
78 esac
79 done
80
81 function dump_vars(){
82 echo "Args...."
83 echo "NAMESPACE=$NAMESPACE"
84 echo "SERVICE_TYPE=$SERVICE_TYPE"
85 echo "DEBUG=$DEBUG"
86 echo "Versions...."
87 echo "V_OPERATOR=$V_OPERATOR"
88 echo "V_MONGODB_EXPORTER=$V_MONGODB_EXPORTER"
89 echo "V_MYSQL_EXPORTER=$V_MYSQL_EXPORTER"
90 }
91
92 if [ -n "$SERVICE_TYPE" ] ; then
93 if [ [ $SERVICE_TYPE != "ClusterIP" ] || [ $SERVICE_TYPE != "NodePort" ] || [ $SERVICE_TYPE != "LoadBalancer" ] ] ; then
94 echo "Wrong service type..."
95 usage && exit 1
96 fi
97 else
98 SERVICE_TYPE="NodePort"
99 fi
100
101 if [ -n "$DEBUG" ] ; then
102 set -x
103 fi
104
105 if [ -n "$DUMP_VARS" ] ; then
106 dump_vars
107 fi
108
109 # Check if helm is installed
110 helm > /dev/null 2>&1
111 if [ $? != 0 ] ; then
112 echo "Helm is not installed, installing ....."
113 curl https://get.helm.sh/helm-v2.15.2-linux-amd64.tar.gz --output helm-v2.15.2.tar.gz
114 tar -zxvf helm-v2.15.2.tar.gz
115 sudo mv linux-amd64/helm /usr/local/bin/helm
116 rm -r linux-amd64
117 rm helm-v2.15.2.tar.gz
118 fi
119
120 echo "Checking if helm-tiller is installed..."
121 kubectl --namespace kube-system get serviceaccount tiller > /dev/null 2>&1
122 if [ $? == 1 ] ; then
123 # tiller account for kubernetes
124 kubectl --namespace kube-system create serviceaccount tiller
125 kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
126 # HELM initialization
127 helm init --service-account tiller
128
129 # Wait for Tiller to be up and running
130 while true
131 do
132 tiller_status=`kubectl -n kube-system get deployment.apps/tiller-deploy --no-headers | awk '{print $2'}`
133 if [ ! -z "$tiller_status" ]
134 then
135 if [ $tiller_status == "1/1" ]
136 then
137 echo "Go...."
138 break
139 fi
140 fi
141 echo "Waiting for tiller READY...."
142 sleep 2
143 done
144 fi
145
146 # create monitoring namespace
147 echo "Creating namespace $NAMESPACE"
148 kubectl create namespace $NAMESPACE
149
150 # Prometheus operator installation
151 $HERE/change-charts-prometheus-operator.sh
152 echo "Creating stable/prometheus-operator"
153 helm install --namespace $NAMESPACE --version=$V_OPERATOR --name osm-monitoring --set kubelet.serviceMonitor.https=true,prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false $HERE/helm_charts/prometheus-operator
154
155 # Change osm-monitoring-grafana-config-dashboards to have folders
156 kubectl -n $NAMESPACE delete configmap osm-monitoring-grafana-config-dashboards
157 kubectl -n $NAMESPACE apply -f $HERE/grafanaproviders.yaml
158
159 # Exporters installation
160
161 # MongoDB
162 # exporter
163 echo "Creating stable/prometheus-mongodb-exporter"
164 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
165 #dashboard:
166 kubectl -n $NAMESPACE apply -f $HERE/mongodb-exporter-dashboard.yaml
167
168 # Mysql
169 # exporter
170 echo "Creating stable/prometheus-mysql-exporter"
171 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
172 #dashboard:
173 kubectl -n $NAMESPACE apply -f $HERE/mysql-exporter-dashboard.yaml
174
175 # Kafka
176 # exporter
177 helm install --namespace $NAMESPACE --name osm-kafka-exporter $HERE/helm_charts/prometheus-kafka-exporter
178 # dashboard:
179 kubectl -n $NAMESPACE apply -f $HERE/kafka-exporter-dashboard.yaml
180
181 # Deploy summary dashboard
182 kubectl -n $NAMESPACE apply -f $HERE/summary-dashboard.yaml
183
184 # Patch prometheus, alertmanager and grafana with service type
185 # By default is created with ClusterIP type
186 if [ $SERVICE_TYPE == "NodePort" ] ; then
187 kubectl --namespace $NAMESPACE patch service osm-monitoring-grafana -p '{"spec":{"type":"NodePort"}}'
188 kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-alertmanager -p '{"spec":{"type":"NodePort"}}'
189 kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-prometheus -p '{"spec":{"type":"NodePort"}}'
190 fi
191
192 if [ $SERVICE_TYPE == "LoadBalancer" ] ; then
193 kubectl --namespace $NAMESPACE patch service osm-monitoring-grafana -p '{"spec":{"type":"LoadBalancer"}}'
194 kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-alertmanager -p '{"spec":{"type":"LoadBalancer"}}'
195 kubectl --namespace $NAMESPACE patch service osm-monitoring-prometheus-prometheus -p '{"spec":{"type":"LoadBalancer"}}'
196 fi
197
198 # Restart grafana to be sure patches are applied
199 echo "Restarting grafana POD..."
200 pod_grafana=`kubectl -n monitoring get pods | grep grafana | awk '{print $1}'`
201 kubectl --namespace $NAMESPACE delete pod $pod_grafana
202