From 8c6dd3ad445ebe696cbd8426ee68c55b7e06db66 Mon Sep 17 00:00:00 2001 From: Ledu0032 Date: Fri, 21 Apr 2023 09:25:00 +0200 Subject: [PATCH] changes --- Dockerfile | 2 +- chart/templates/deployment.yaml | 9 ++- chart/templates/ingress.yaml | 4 ++ chart/templates/service.yaml | 6 +- grafana/grafana.yaml | 81 +++++++++++++++++++++++++ index.py | 15 ++++- monitoring_flask/docker-compose.yaml | 2 +- prometheus/prometheus_account.yaml | 5 ++ prometheus/prometheus_cluster_role.yaml | 17 ++++++ prometheus/prometheus_config_map.yaml | 39 ++++++++++++ prometheus/prometheus_deployment.yaml | 30 +++++++++ prometheus/prometheus_role_binding.yaml | 12 ++++ prometheus/prometheus_service.yaml | 10 +++ 13 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 grafana/grafana.yaml create mode 100644 prometheus/prometheus_account.yaml create mode 100644 prometheus/prometheus_cluster_role.yaml create mode 100644 prometheus/prometheus_config_map.yaml create mode 100644 prometheus/prometheus_deployment.yaml create mode 100644 prometheus/prometheus_role_binding.yaml create mode 100644 prometheus/prometheus_service.yaml diff --git a/Dockerfile b/Dockerfile index a886c26..d2550dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:alpine3.7 COPY . /app WORKDIR /app -RUN pip install flask && pip3 install --no-cache-dir Flask flask_prometheus_metrics +RUN pip install flask && pip3 install prometheus-flask-exporter EXPOSE 5000 CMD ["python", "./index.py"] diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml index 72e0eec..ce8642a 100644 --- a/chart/templates/deployment.yaml +++ b/chart/templates/deployment.yaml @@ -13,10 +13,15 @@ spec: metadata: labels: app: nginx + annotations: + prometheus.io/scrape: "true" + prometheus.io.scheme: "http" + prometheus.io/path: "/metrics" + prometheus.io/port: "5000" spec: containers: - - name: helloworldv2 - image: ledu32/helloworldv2:latest + - name: helloworldv3 + image: ledu32/helloworldv2 livenessProbe: httpGet: path: / diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml index 321d34c..1e32ede 100644 --- a/chart/templates/ingress.yaml +++ b/chart/templates/ingress.yaml @@ -2,6 +2,10 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress + annotations: + nginx.ingress.kubernetes.io/cors-allow-headers: Content-Type + nginx.ingress.kubernetes.io/cors-allow-methods: POST, GET, OPTIONS + nginx.ingress.kubernetes.io/cors-allow-origin: http://localhost:9090/ spec: rules: http: diff --git a/chart/templates/service.yaml b/chart/templates/service.yaml index e365396..8f5103d 100644 --- a/chart/templates/service.yaml +++ b/chart/templates/service.yaml @@ -3,9 +3,9 @@ kind: Service metadata: name: service annotations: - se7entyse7en.prometheus/scrape: "true" - se7entyse7en.prometheus/path: "/metrics" - se7entyse7en.prometheus/port: "80" + prometheus.io/port: "5000" + prometheus.io/scrape: "true" + prometheus.io/path: "/metrics" spec: type: ClusterIP selector: diff --git a/grafana/grafana.yaml b/grafana/grafana.yaml new file mode 100644 index 0000000..b3b3742 --- /dev/null +++ b/grafana/grafana.yaml @@ -0,0 +1,81 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: grafana-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: grafana + name: grafana +spec: + selector: + matchLabels: + app: grafana + template: + metadata: + labels: + app: grafana + spec: + securityContext: + fsGroup: 472 + supplementalGroups: + - 0 + containers: + - name: grafana + image: grafana/grafana:9.1.0 + imagePullPolicy: IfNotPresent + ports: + - containerPort: 3000 + name: http-grafana + protocol: TCP + readinessProbe: + failureThreshold: 3 + httpGet: + path: /robots.txt + port: 3000 + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 30 + successThreshold: 1 + timeoutSeconds: 2 + livenessProbe: + failureThreshold: 3 + initialDelaySeconds: 30 + periodSeconds: 10 + successThreshold: 1 + tcpSocket: + port: 3000 + timeoutSeconds: 1 + resources: + requests: + cpu: 250m + memory: 750Mi + volumeMounts: + - mountPath: /var/lib/grafana + name: grafana-pv + volumes: + - name: grafana-pv + persistentVolumeClaim: + claimName: grafana-pvc +--- +apiVersion: v1 +kind: Service +metadata: + name: grafana +spec: + ports: + - port: 3000 + protocol: TCP + targetPort: http-grafana + selector: + app: grafana + sessionAffinity: None + type: LoadBalancer \ No newline at end of file diff --git a/index.py b/index.py index 53be5e4..5ab903d 100644 --- a/index.py +++ b/index.py @@ -1,7 +1,18 @@ from flask import Flask +from prometheus_client import make_wsgi_app +from werkzeug.middleware.dispatcher import DispatcherMiddleware +from prometheus_flask_exporter import PrometheusMetrics + + app = Flask(__name__) +metrics = PrometheusMetrics(app) + @app.route("/") def hello(): return "Hello World!" -if __name__ == "__main__": - app.run(host="0.0.0.0", port=int("5000"), debug=True) + + +# Plug metrics WSGI app to your main app with dispatcher +dispatcher = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()}) + +app.run(host='0.0.0.0', port=5000) diff --git a/monitoring_flask/docker-compose.yaml b/monitoring_flask/docker-compose.yaml index ee67956..4370a49 100644 --- a/monitoring_flask/docker-compose.yaml +++ b/monitoring_flask/docker-compose.yaml @@ -3,7 +3,7 @@ services: prometheus: image: prom/prometheus:latest ports: - - 9090:9090 + - 9091:9090 volumes: - "./prometheus.yml:/etc/monitoring_flask/prometheus.yml" diff --git a/prometheus/prometheus_account.yaml b/prometheus/prometheus_account.yaml new file mode 100644 index 0000000..01d3d08 --- /dev/null +++ b/prometheus/prometheus_account.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + namespace: default + name: prometheus \ No newline at end of file diff --git a/prometheus/prometheus_cluster_role.yaml b/prometheus/prometheus_cluster_role.yaml new file mode 100644 index 0000000..afaa4a2 --- /dev/null +++ b/prometheus/prometheus_cluster_role.yaml @@ -0,0 +1,17 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: discover +rules: +- apiGroups: [""] + resources: + - nodes + - services + - endpoints + - pods + verbs: ["get", "list", "watch"] +- apiGroups: + - extensions + resources: + - ingresses + verbs: ["get", "list", "watch"] \ No newline at end of file diff --git a/prometheus/prometheus_config_map.yaml b/prometheus/prometheus_config_map.yaml new file mode 100644 index 0000000..b43e6d9 --- /dev/null +++ b/prometheus/prometheus_config_map.yaml @@ -0,0 +1,39 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: prometheus-server-conf + namespace: default + labels: + name: prometheus-server-conf +data: + prometheus.yml: |- + scrape_configs: + - job_name: 'kubernetes-service-endpoints' + + scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. + # scrape_timeout is set to the global default (10s). + + # Alertmanager configuration + alerting: + alertmanagers: + - static_configs: + - targets: + # - alertmanager:9093 + + # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. + rule_files: + # - "first_rules.yml" + # - "second_rules.yml" + + # A scrape configuration containing exactly one endpoint to scrape: + # Here it's Prometheus itself. + scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: "scraping_pods" + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ["helloworldv2:8080"] \ No newline at end of file diff --git a/prometheus/prometheus_deployment.yaml b/prometheus/prometheus_deployment.yaml new file mode 100644 index 0000000..2d5788b --- /dev/null +++ b/prometheus/prometheus_deployment.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: prometheus + namespace: default + labels: + app: prometheus +spec: + replicas: 1 + selector: + matchLabels: + app: prometheus + template: + metadata: + labels: + app: prometheus + spec: + serviceAccountName: prometheus + containers: + - name: prometheus + image: prom/prometheus:latest + ports: + - containerPort: 9090 + volumeMounts: + - name: config + mountPath: /etc/prometheus + volumes: + - name: config + configMap: + name: prometheus-server-conf \ No newline at end of file diff --git a/prometheus/prometheus_role_binding.yaml b/prometheus/prometheus_role_binding.yaml new file mode 100644 index 0000000..991bf23 --- /dev/null +++ b/prometheus/prometheus_role_binding.yaml @@ -0,0 +1,12 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: prometheus-discoverer +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: discoverer +subjects: +- kind: ServiceAccount + name: prometheus + namespace: default \ No newline at end of file diff --git a/prometheus/prometheus_service.yaml b/prometheus/prometheus_service.yaml new file mode 100644 index 0000000..50846c6 --- /dev/null +++ b/prometheus/prometheus_service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: prometheus + namespace: default +spec: + selector: + app: prometheus + ports: + - port: 9090 \ No newline at end of file