diff --git a/deploy b/deploy index 5953385..570bd0c 100755 --- a/deploy +++ b/deploy @@ -14,6 +14,7 @@ NODE_EXPORTER_DEFAULT_VERSION=v0.16.0 KUBE_STATE_METRICS_DEFAULT_VERSION=v1.3.1 NODE_LABEL_DEFAULT_KEY=beta.kubernetes.io/monit NODE_LABEL_DEFAULT_VALUE=prometheus +DEFAULT_NAMESPACE=monitoring ######################################################################################### #external Urls for Prometheus and Alertmanager @@ -36,10 +37,6 @@ if [ -z "${KUBECONFIG}" ]; then export KUBECONFIG=~/.kube/config fi -if [ -z "${NAMESPACE}" ]; then - NAMESPACE=monitoring -fi - #check for uncommitted changes echo -e "${ORANGE}Check for uncommitted changes" echo @@ -54,11 +51,18 @@ else tput sgr0 fi -echo -echo -e "${BLUE}Creating ${ORANGE}'monitoring' ${BLUE}namespace." +#Namespace +read -p "Enter desired namespace to deploy prometheus [$DEFAULT_NAMESPACE]: " NAMESPACE +NAMESPACE=${NAMESPACE:-$DEFAULT_NAMESPACE} tput sgr0 + +echo -e "${BLUE}Creating ${ORANGE}${NAMESPACE} ${BLUE}namespace." kubectl create namespace "$NAMESPACE" -echo + +#Set namespace in various resources +for n in $(egrep -lir --include=*.{yaml,sh} "CUSTOM_NAMESPACE" .); do + sed -i -e 's,CUSTOM_NAMESPACE,'"$NAMESPACE"',g' $n +done kctl() { kubectl --namespace "$NAMESPACE" "$@" diff --git a/manifests/grafana/grafana-dashboards.cm.yaml b/manifests/grafana/grafana-dashboards.cm.yaml index 24549ed..5ac2bfd 100644 --- a/manifests/grafana/grafana-dashboards.cm.yaml +++ b/manifests/grafana/grafana-dashboards.cm.yaml @@ -915,7 +915,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-cluster-rsrc-use - namespace: monitoring - apiVersion: v1 data: k8s-node-rsrc-use.json: |- @@ -1858,7 +1857,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-node-rsrc-use - namespace: monitoring - apiVersion: v1 data: k8s-resources-cluster.json: |- @@ -3186,7 +3184,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-resources-cluster - namespace: monitoring - apiVersion: v1 data: k8s-resources-namespace.json: |- @@ -4025,7 +4022,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-resources-namespace - namespace: monitoring - apiVersion: v1 data: k8s-resources-pod.json: |- @@ -4891,7 +4887,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-resources-pod - namespace: monitoring - apiVersion: v1 data: nodes.json: |- @@ -6003,7 +5998,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-nodes - namespace: monitoring - apiVersion: v1 data: pods.json: |- @@ -6493,7 +6487,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-pods - namespace: monitoring - apiVersion: v1 data: statefulset.json: |- @@ -7346,7 +7339,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-statefulset - namespace: monitoring - apiVersion: v1 data: deployments.json: |- @@ -8260,7 +8252,6 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-deployments - namespace: monitoring - apiVersion: v1 data: k8s-cluster-usage.json: |- @@ -9600,5 +9591,4 @@ items: kind: ConfigMap metadata: name: grafana-dashboard-k8s-cluster-usage - namespace: monitoring kind: ConfigMapList \ No newline at end of file diff --git a/manifests/grafana/grafana-datasources.cm.yaml b/manifests/grafana/grafana-datasources.cm.yaml index a0c03d7..6d8bcf5 100644 --- a/manifests/grafana/grafana-datasources.cm.yaml +++ b/manifests/grafana/grafana-datasources.cm.yaml @@ -13,7 +13,7 @@ data: "name": "prometheus", "orgId": 1, "type": "prometheus", - "url": "http://prometheus-k8s.monitoring.svc:9090", + "url": "http://prometheus-k8s.CUSTOM_NAMESPACE.svc:9090", "version": 1 } ] diff --git a/manifests/kube-state-metrics/kube-state-metrics-rbac.yaml b/manifests/kube-state-metrics/kube-state-metrics-rbac.yaml index 8335172..0c28e0f 100644 --- a/manifests/kube-state-metrics/kube-state-metrics-rbac.yaml +++ b/manifests/kube-state-metrics/kube-state-metrics-rbac.yaml @@ -54,7 +54,7 @@ roleRef: subjects: - kind: ServiceAccount name: kube-state-metrics - namespace: monitoring + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role diff --git a/manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml b/manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml index eb35485..888b239 100644 --- a/manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml +++ b/manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml @@ -2,6 +2,7 @@ apiVersion: v1 kind: ServiceAccount metadata: name: prometheus-operator + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole @@ -87,4 +88,4 @@ roleRef: subjects: - kind: ServiceAccount name: prometheus-operator - namespace: monitoring + namespace: CUSTOM_NAMESPACE diff --git a/manifests/prometheus/prometheus-k8s-rbac.yaml b/manifests/prometheus/prometheus-k8s-rbac.yaml index 74dc432..74af15b 100644 --- a/manifests/prometheus/prometheus-k8s-rbac.yaml +++ b/manifests/prometheus/prometheus-k8s-rbac.yaml @@ -2,13 +2,13 @@ apiVersion: v1 kind: ServiceAccount metadata: name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: Role metadata: name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE rules: - apiGroups: [""] resources: @@ -67,7 +67,7 @@ apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding metadata: name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE roleRef: apiGroup: rbac.authorization.k8s.io kind: Role @@ -75,7 +75,7 @@ roleRef: subjects: - kind: ServiceAccount name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding @@ -89,7 +89,7 @@ roleRef: subjects: - kind: ServiceAccount name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding @@ -103,7 +103,7 @@ roleRef: subjects: - kind: ServiceAccount name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding @@ -116,4 +116,4 @@ roleRef: subjects: - kind: ServiceAccount name: prometheus-k8s - namespace: monitoring + namespace: CUSTOM_NAMESPACE diff --git a/manifests/prometheus/prometheus-k8s-service-monitor-alertmanager.yaml b/manifests/prometheus/prometheus-k8s-service-monitor-alertmanager.yaml index 19669e3..a3724e3 100644 --- a/manifests/prometheus/prometheus-k8s-service-monitor-alertmanager.yaml +++ b/manifests/prometheus/prometheus-k8s-service-monitor-alertmanager.yaml @@ -10,7 +10,7 @@ spec: alertmanager: main namespaceSelector: matchNames: - - monitoring + - CUSTOM_NAMESPACE endpoints: - port: web interval: 30s diff --git a/manifests/prometheus/prometheus-k8s-service-monitor-kube-state-metrics.yaml b/manifests/prometheus/prometheus-k8s-service-monitor-kube-state-metrics.yaml index 6563a4d..baedf7e 100644 --- a/manifests/prometheus/prometheus-k8s-service-monitor-kube-state-metrics.yaml +++ b/manifests/prometheus/prometheus-k8s-service-monitor-kube-state-metrics.yaml @@ -11,7 +11,7 @@ spec: k8s-app: kube-state-metrics namespaceSelector: matchNames: - - monitoring + - CUSTOM_NAMESPACE endpoints: - port: http-metrics interval: 30s diff --git a/manifests/prometheus/prometheus-k8s-service-monitor-node-exporter.yaml b/manifests/prometheus/prometheus-k8s-service-monitor-node-exporter.yaml index e1b083b..555565a 100644 --- a/manifests/prometheus/prometheus-k8s-service-monitor-node-exporter.yaml +++ b/manifests/prometheus/prometheus-k8s-service-monitor-node-exporter.yaml @@ -11,7 +11,7 @@ spec: k8s-app: node-exporter namespaceSelector: matchNames: - - monitoring + - CUSTOM_NAMESPACE endpoints: - port: http-metrics interval: 30s diff --git a/manifests/prometheus/prometheus-k8s-service-monitor-prometheus.yaml b/manifests/prometheus/prometheus-k8s-service-monitor-prometheus.yaml index c3d11e5..200a7f0 100644 --- a/manifests/prometheus/prometheus-k8s-service-monitor-prometheus.yaml +++ b/manifests/prometheus/prometheus-k8s-service-monitor-prometheus.yaml @@ -10,7 +10,7 @@ spec: prometheus: k8s namespaceSelector: matchNames: - - monitoring + - CUSTOM_NAMESPACE endpoints: - port: web interval: 30s diff --git a/manifests/prometheus/prometheus-k8s.yaml b/manifests/prometheus/prometheus-k8s.yaml index 9ed519c..8bf6b89 100644 --- a/manifests/prometheus/prometheus-k8s.yaml +++ b/manifests/prometheus/prometheus-k8s.yaml @@ -45,6 +45,6 @@ spec: storage: PROMETHEUS_STORAGE_VOLUME_SIZE alerting: alertmanagers: - - namespace: monitoring + - namespace: CUSTOM_NAMESPACE name: alertmanager-main port: web diff --git a/teardown b/teardown index 10b54ff..636f18f 100755 --- a/teardown +++ b/teardown @@ -12,14 +12,18 @@ if [ -z "${KUBECONFIG}" ]; then export KUBECONFIG=~/.kube/config fi -if [ -z "${NAMESPACE}" ]; then - NAMESPACE=monitoring -fi +#Namespace +NAMESPACE=$(kubectl get sts --all-namespaces | grep prometheus-k8s | cut -d " " -f1) kctl() { kubectl --namespace "$NAMESPACE" "$@" } +#Set namespace in various resources +for n in $(egrep -lir --include=*.{yaml,sh} "CUSTOM_NAMESPACE" .); do + sed -i -e 's,CUSTOM_NAMESPACE,'"$NAMESPACE"',g' $n +done + kctl delete -f manifests/node-exporter kctl delete -f manifests/kube-state-metrics find manifests/grafana -type f ! -name grafana.pvc.yaml -exec kubectl --namespace "$NAMESPACE" delete -f {} \; @@ -31,7 +35,8 @@ kubectl delete -f manifests/k8s/self-hosted # Hack: wait a bit to let the controller delete the deployed Prometheus server. sleep 5 -kctl delete -f manifests/prometheus-operator +find manifests/prometheus-operator -type f ! -name prometheus-operator-cluster-role-binding.yaml -exec kubectl --namespace "$NAMESPACE" delete -f {} \; +kubectl delete -f manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml kctl delete secret grafana-credentials @@ -48,4 +53,9 @@ kubectl get crd -o 'jsonpath={.items[*].metadata.name}' | xargs -n 1 | fgrep cor #manually remove persistent volumes instructions echo -e "${GREEN}The persistent volumes for Grafana and Prometeus were not removed!" echo -e "${GREEN}To remove them, please run te following command:" -echo -e "${WHITE}kubectl get pvc -n ${NAMESPACE} | grep 'grafana\|prometheus' | cut -d " " -f 1 | xargs -n 1 kubectl delete pvc -n ${NAMESPACE}" \ No newline at end of file +echo -e "${WHITE}kubectl get pvc -n ${NAMESPACE} | grep 'grafana\|prometheus' | cut -d '" "' -f 1 | xargs -n 1 kubectl delete pvc -n ${NAMESPACE}" + +#clean sed generated files +find . -name "*.yaml-e" -exec rm -rf {} \; + +git checkout -- . \ No newline at end of file diff --git a/tools/alertmanager_proxy.sh b/tools/alertmanager_proxy.sh index aad1cdc..4c670b0 100755 --- a/tools/alertmanager_proxy.sh +++ b/tools/alertmanager_proxy.sh @@ -1,4 +1,7 @@ #!/bin/bash -POD=$(kubectl get pods --namespace=monitoring | grep alertmanager-main-0| cut -d ' ' -f 1) -kubectl port-forward $POD --namespace=monitoring 9093:9093 +#Namespace +NAMESPACE=$(kubectl get sts --all-namespaces | grep prometheus-k8s | cut -d " " -f1) + +POD=$(kubectl get pods --namespace=$NAMESPACE | grep alertmanager-main-0| cut -d ' ' -f 1) +kubectl port-forward $POD --namespace=$NAMESPACE 9093:9093 diff --git a/tools/grafana_proxy.sh b/tools/grafana_proxy.sh index 5081129..c7b5fc5 100755 --- a/tools/grafana_proxy.sh +++ b/tools/grafana_proxy.sh @@ -1,4 +1,7 @@ #!/bin/bash -POD=$(kubectl get pods --namespace=monitoring | grep grafana| cut -d ' ' -f 1) -kubectl port-forward $POD --namespace=monitoring 3000:3000 +#Namespace +NAMESPACE=$(kubectl get sts --all-namespaces | grep prometheus-k8s | cut -d " " -f1) + +POD=$(kubectl get pods --namespace=$NAMESPACE | grep grafana| cut -d ' ' -f 1) +kubectl port-forward $POD --namespace=$NAMESPACE 3000:3000 diff --git a/tools/prometheus_proxy.sh b/tools/prometheus_proxy.sh index 068c15c..d967dcb 100755 --- a/tools/prometheus_proxy.sh +++ b/tools/prometheus_proxy.sh @@ -1,4 +1,7 @@ #!/bin/bash -POD=$(kubectl get pods --namespace=monitoring | grep prometheus-k8s-0| cut -d ' ' -f 1) -kubectl port-forward $POD --namespace=monitoring 9090:9090 +#Namespace +NAMESPACE=$(kubectl get sts --all-namespaces | grep prometheus-k8s | cut -d " " -f1) + +POD=$(kubectl get pods --namespace=$NAMESPACE | grep prometheus-k8s-0| cut -d ' ' -f 1) +kubectl port-forward $POD --namespace=$NAMESPACE 9090:9090