diff --git a/kubernetes/servicemonitors/README.md b/kubernetes/servicemonitors/README.md new file mode 100644 index 0000000..0a4b1d0 --- /dev/null +++ b/kubernetes/servicemonitors/README.md @@ -0,0 +1,110 @@ +# Introduction to Service Monitors + +In order to understand service monitors, we will need to understand how to monitor +kubernetes environment.
+You will need a base understanding of Kubernetes and have a basic understanding of the `kube-prometheus` monitoring stack.
+ +Checkout the video [How to monitor Kubernetes in 2022](https://youtu.be/YDtuwlNTzRc): + +Monitoring Kubernetes + + +## Create a kubernetes cluster + +``` +# create cluster +kind create cluster --name monitoring --image kindest/node:v1.23.5 + +# see cluster up and running +kubectl get nodes +NAME STATUS ROLES AGE VERSION +monitoring-control-plane Ready control-plane,master 2m12s v1.23.5 +``` + +## Deploy kube-prometheus + +Installation: + +``` +kubectl create -f ./monitoring/prometheus/kubernetes/1.23/manifests/setup/ +kubectl create -f ./monitoring/prometheus/kubernetes/1.23/manifests/ +``` + +Check the install: + +``` +kubectl -n monitoring get pods +``` + +After a few minutes, everything should be up and running: + +``` +kubectl -n monitoring get pods +NAME READY STATUS RESTARTS AGE +alertmanager-main-0 2/2 Running 0 3m10s +alertmanager-main-1 2/2 Running 0 3m10s +alertmanager-main-2 2/2 Running 0 3m10s +blackbox-exporter-6b79c4588b-t4czf 3/3 Running 0 4m7s +grafana-7fd69887fb-zm2d2 1/1 Running 0 4m7s +kube-state-metrics-55f67795cd-f7frb 3/3 Running 0 4m6s +node-exporter-xjdtn 2/2 Running 0 4m6s +prometheus-adapter-85664b6b74-bvmnj 1/1 Running 0 4m6s +prometheus-adapter-85664b6b74-mcgbz 1/1 Running 0 4m6s +prometheus-k8s-0 2/2 Running 0 3m9s +prometheus-k8s-1 2/2 Running 0 3m9s +prometheus-operator-6dc9f66cb7-z98nj 2/2 Running 0 4m6s +``` + +## View dashboards + +``` +kubectl -n monitoring port-forward svc/grafana 3000 +``` + +Then access Grafana on [localhost:3000](http://localhost:3000) + +## Access Prometheus + +``` +kubectl -n monitoring port-forward svc/prometheus-operated 9090 +``` + +Then access Prometheus on [localhost:9090](http://localhost:9090). + +## Create our own Prometheus + + +``` +kubectl apply -n monitoring -f ./kubernetes/servicemonitors/prometheus.yaml + +``` + +View our prometheus `prometheus-applications-0` instance: + +``` +kubectl -n monitoring get pods +``` + +Checkout our prometheus UI + +``` +kubectl -n monitoring port-forward prometheus-applications-0 9090 +``` + +## Deploy a service monitor for example app + +``` +kubectl -n default apply -f ./kubernetes/servicemonitors/servicemonitor.yaml +``` + +After applying the service monitor, if Prometheus is correctly selecting it, we should see the item appear under the [Service Discovery](http://localhost:9090/service-discovery) page in Prometheus.
+Double check with with `port-forward` before proceeding.
+If it does not appear, that means your Prometheus instance is not selecting the service monitor accordingly. Either a label mismatch on the namespace or the service monitor.
+ +## Deploy our example app + +``` +kubectl -n default apply -f kubernetes\servicemonitors\example-app\ +``` + +Now we should see a target in the Prometheus [Targets](http://localhost:9090/targets) page.
\ No newline at end of file diff --git a/kubernetes/servicemonitors/example-app/deployment.yaml b/kubernetes/servicemonitors/example-app/deployment.yaml new file mode 100644 index 0000000..f4cd49e --- /dev/null +++ b/kubernetes/servicemonitors/example-app/deployment.yaml @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: example-deploy + labels: + app: example-app +spec: + selector: + matchLabels: + app: example-app + replicas: 2 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + template: + metadata: + labels: + app: example-app + spec: + containers: + - name: example-app + image: aimvector/python:metrics + imagePullPolicy: Always + ports: + - containerPort: 5000 \ No newline at end of file diff --git a/kubernetes/servicemonitors/example-app/service.yaml b/kubernetes/servicemonitors/example-app/service.yaml new file mode 100644 index 0000000..da0d8c5 --- /dev/null +++ b/kubernetes/servicemonitors/example-app/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: example-service + labels: + app: example-app +spec: + type: ClusterIP + selector: + app: example-app + ports: + - protocol: TCP + name: web + port: 80 + targetPort: 5000 \ No newline at end of file diff --git a/kubernetes/servicemonitors/prometheus.yaml b/kubernetes/servicemonitors/prometheus.yaml new file mode 100644 index 0000000..735d20e --- /dev/null +++ b/kubernetes/servicemonitors/prometheus.yaml @@ -0,0 +1,31 @@ +apiVersion: monitoring.coreos.com/v1 +kind: Prometheus +metadata: + labels: + app.kubernetes.io/component: prometheus + app.kubernetes.io/instance: k8s + app.kubernetes.io/name: prometheus + app.kubernetes.io/part-of: kube-prometheus + app.kubernetes.io/version: 2.32.1 + name: applications + namespace: monitoring +spec: + image: quay.io/prometheus/prometheus:v2.32.1 + nodeSelector: + kubernetes.io/os: linux + replicas: 1 + resources: + requests: + memory: 400Mi + ruleSelector: {} + securityContext: + fsGroup: 2000 + runAsNonRoot: true + runAsUser: 1000 + serviceAccountName: prometheus-k8s + #serviceMonitorNamespaceSelector: {} #match all namespaces + serviceMonitorNamespaceSelector: + matchLabels: + kubernetes.io/metadata.name: default + serviceMonitorSelector: {} #match all servicemonitors + version: 2.32.1 diff --git a/kubernetes/servicemonitors/servicemonitor.yaml b/kubernetes/servicemonitors/servicemonitor.yaml new file mode 100644 index 0000000..891d804 --- /dev/null +++ b/kubernetes/servicemonitors/servicemonitor.yaml @@ -0,0 +1,13 @@ +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + labels: + name: example-app + namespace: default +spec: + endpoints: + - interval: 30s + port: web + selector: + matchLabels: + app: example-app diff --git a/monitoring/prometheus/python-application/dockerfile b/monitoring/prometheus/python-application/dockerfile index 66ed99b..f44b7c0 100644 --- a/monitoring/prometheus/python-application/dockerfile +++ b/monitoring/prometheus/python-application/dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7.3-alpine3.9 as prod +FROM python:3.10.5-alpine3.16 as prod RUN mkdir /app/ WORKDIR /app/ diff --git a/monitoring/prometheus/python-application/src/requirements.txt b/monitoring/prometheus/python-application/src/requirements.txt index e1e23f7..6a7d1e2 100644 --- a/monitoring/prometheus/python-application/src/requirements.txt +++ b/monitoring/prometheus/python-application/src/requirements.txt @@ -1,2 +1,2 @@ -Flask == 1.0.3 -prometheus_client == 0.7.1 \ No newline at end of file +Flask == 2.1.2 +prometheus_client == 0.14.1 \ No newline at end of file