This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
# Ch14 lab
## Setup
Deploy the monitoring subsystem:
```
kubectl apply -f lab/monitoring/
```
> Browse to the Prometheus UI - there are no targets
## Sample Solution
This is the background info:
- Prometheus is [configured](./monitoring/prometheus-config.yaml) to find targets in the namespace `kiamol-ch14-lab`
- all exporters are listed on the [Prometheus docs](https://prometheus.io/docs/instrumenting/exporters/)
- the [Elasticsearch exporter](https://github.com/justwatchcom/elasticsearch_exporter) exposes metrics on port 9114.
My solution is in [elasticsearch.yaml](./solution/elasticsearch.yaml).
Create the namespace and deploy the app:
```
kubectl create ns kiamol-ch14-lab
kubectl apply -f lab/elasticsearch.yml -n kiamol-ch14-lab
```
> Back in Prometheus you'll see the target added, with about 300 new metrics
`elasticsearch_cluster_health_up` is the most basic, it should return `1` and show the version info in the labels:
![Prometheus metrics from the Elasticsearch exporter](./docs/prometheus.png)
## Teardown
Remove the namespaces and that removes everything:
```
kubectl delete ns kiamol-ch14-lab
kubectl delete ns kiamol-ch14-lab-monitoring
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: elasticsearch
spec:
selector:
matchLabels:
app: elasticsearch
template:
metadata:
labels:
app: elasticsearch
spec:
containers:
- image: kiamol/ch13-elasticsearch
name: elasticsearch

View File

@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
name: kiamol-ch14-lab-monitoring
labels:
kiamol: ch14

View File

@@ -0,0 +1,40 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: kiamol-ch14-lab-monitoring
data:
prometheus.yml: |-
global:
scrape_interval: 30s
scrape_configs:
- job_name: 'lab-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels:
- __meta_kubernetes_namespace
action: keep
regex: kiamol-ch14-lab
- source_labels:
- __meta_kubernetes_pod_annotationpresent_prometheus_io_port
- __address__
- __meta_kubernetes_pod_annotation_prometheus_io_port
action: replace
regex: true;([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- source_labels:
- __meta_kubernetes_pod_labelpresent_app
- __meta_kubernetes_pod_label_app
regex: true;(.*)
target_label: job
- source_labels:
- __meta_kubernetes_pod_name
target_label: instance
- source_labels:
- __meta_kubernetes_pod_labelpresent_version
- __meta_kubernetes_pod_label_version
regex: true;(.*)
target_label: version

View File

@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: kiamol-ch14-lab-monitoring
spec:
selector:
app: prometheus
type: LoadBalancer
ports:
- port: 9090
targetPort: 9090

View File

@@ -0,0 +1,71 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: kiamol-ch14-lab-monitoring
spec:
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: prometheus
containers:
- name: prometheus
image: prom/prometheus:v2.19.2
args:
- "--config.file=/config/prometheus.yml"
- "--web.enable-lifecycle"
ports:
- containerPort: 9090
volumeMounts:
- name: prometheus-config
mountPath: /config/
volumes:
- name: prometheus-config
configMap:
name: prometheus-config
---
# RBAC configuration - ignore this until we get to chapter 17 :)
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: kiamol-ch14-lab-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: prometheus
labels:
kiamol: ch14
rules:
- apiGroups: [""]
resources:
- nodes
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources:
- configmaps
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus
labels:
kiamol: ch14
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: kiamol-ch14-lab-monitoring

View File

@@ -0,0 +1,24 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: elasticsearch
spec:
selector:
matchLabels:
app: elasticsearch
template:
metadata:
labels:
app: elasticsearch
version: "6.8.10"
annotations:
prometheus.io/port: "9114"
spec:
containers:
- name: elasticsearch
image: kiamol/ch13-elasticsearch
- name: exporter
image: justwatch/elasticsearch_exporter:1.1.0
ports:
- name: metrics
containerPort: 9114