Skip to content

Commit

Permalink
Merge pull request #111 from camilb/custom-ns
Browse files Browse the repository at this point in the history
Allow deployment in custom namespace
  • Loading branch information
camilb authored Aug 13, 2018
2 parents 094faf2 + ad0df1a commit b8b0e74
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 43 deletions.
18 changes: 11 additions & 7 deletions deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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" "$@"
Expand Down
10 changes: 0 additions & 10 deletions manifests/grafana/grafana-dashboards.cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down Expand Up @@ -1858,7 +1857,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-node-rsrc-use
namespace: monitoring
- apiVersion: v1
data:
k8s-resources-cluster.json: |-
Expand Down Expand Up @@ -3186,7 +3184,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-resources-cluster
namespace: monitoring
- apiVersion: v1
data:
k8s-resources-namespace.json: |-
Expand Down Expand Up @@ -4025,7 +4022,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-resources-namespace
namespace: monitoring
- apiVersion: v1
data:
k8s-resources-pod.json: |-
Expand Down Expand Up @@ -4891,7 +4887,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-resources-pod
namespace: monitoring
- apiVersion: v1
data:
nodes.json: |-
Expand Down Expand Up @@ -6003,7 +5998,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-nodes
namespace: monitoring
- apiVersion: v1
data:
pods.json: |-
Expand Down Expand Up @@ -6493,7 +6487,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-pods
namespace: monitoring
- apiVersion: v1
data:
statefulset.json: |-
Expand Down Expand Up @@ -7346,7 +7339,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-statefulset
namespace: monitoring
- apiVersion: v1
data:
deployments.json: |-
Expand Down Expand Up @@ -8260,7 +8252,6 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-deployments
namespace: monitoring
- apiVersion: v1
data:
k8s-cluster-usage.json: |-
Expand Down Expand Up @@ -9600,5 +9591,4 @@ items:
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-cluster-usage
namespace: monitoring
kind: ConfigMapList
2 changes: 1 addition & 1 deletion manifests/grafana/grafana-datasources.cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
Expand Down
2 changes: 1 addition & 1 deletion manifests/kube-state-metrics/kube-state-metrics-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus-operator
namespace: CUSTOM_NAMESPACE
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand Down Expand Up @@ -87,4 +88,4 @@ roleRef:
subjects:
- kind: ServiceAccount
name: prometheus-operator
namespace: monitoring
namespace: CUSTOM_NAMESPACE
14 changes: 7 additions & 7 deletions manifests/prometheus/prometheus-k8s-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -67,15 +67,15 @@ 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
name: prometheus-k8s
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
namespace: CUSTOM_NAMESPACE
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
Expand All @@ -89,7 +89,7 @@ roleRef:
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
namespace: CUSTOM_NAMESPACE
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
Expand All @@ -103,7 +103,7 @@ roleRef:
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
namespace: CUSTOM_NAMESPACE
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
Expand All @@ -116,4 +116,4 @@ roleRef:
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
namespace: CUSTOM_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
alertmanager: main
namespaceSelector:
matchNames:
- monitoring
- CUSTOM_NAMESPACE
endpoints:
- port: web
interval: 30s
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
k8s-app: kube-state-metrics
namespaceSelector:
matchNames:
- monitoring
- CUSTOM_NAMESPACE
endpoints:
- port: http-metrics
interval: 30s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
k8s-app: node-exporter
namespaceSelector:
matchNames:
- monitoring
- CUSTOM_NAMESPACE
endpoints:
- port: http-metrics
interval: 30s
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
prometheus: k8s
namespaceSelector:
matchNames:
- monitoring
- CUSTOM_NAMESPACE
endpoints:
- port: web
interval: 30s
2 changes: 1 addition & 1 deletion manifests/prometheus/prometheus-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ spec:
storage: PROMETHEUS_STORAGE_VOLUME_SIZE
alerting:
alertmanagers:
- namespace: monitoring
- namespace: CUSTOM_NAMESPACE
name: alertmanager-main
port: web
20 changes: 15 additions & 5 deletions teardown
Original file line number Diff line number Diff line change
Expand Up @@ -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 {} \;
Expand All @@ -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

Expand All @@ -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}"
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 -- .
7 changes: 5 additions & 2 deletions tools/alertmanager_proxy.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions tools/grafana_proxy.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions tools/prometheus_proxy.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b8b0e74

Please sign in to comment.