From df3587f2f671eb14c7c8ebaebcf26541176ccc0f Mon Sep 17 00:00:00 2001
From: Leah Tacke genannt Unterberg <leah.tgu@pads.rwth-aachen.de>
Date: Thu, 31 Oct 2024 14:34:11 +0100
Subject: [PATCH] worked on app deployment

---
 apply-all.bat            |  1 +
 backend-deployment.yaml  | 60 ++++++++++++++++++++++++++++++
 config-map.yaml          | 13 -------
 config-maps.yaml         | 79 ++++++++++++++++++++++++++++++++++++++++
 deployment.yaml          | 62 -------------------------------
 frontend-deployment.yaml | 62 +++++++++++++++++++++++++++++++
 service.yaml             | 17 ---------
 services.yaml            | 33 +++++++++++++++++
 8 files changed, 235 insertions(+), 92 deletions(-)
 create mode 100644 apply-all.bat
 create mode 100644 backend-deployment.yaml
 delete mode 100644 config-map.yaml
 create mode 100644 config-maps.yaml
 delete mode 100644 deployment.yaml
 create mode 100644 frontend-deployment.yaml
 delete mode 100644 service.yaml
 create mode 100644 services.yaml

diff --git a/apply-all.bat b/apply-all.bat
new file mode 100644
index 0000000..715c6bf
--- /dev/null
+++ b/apply-all.bat
@@ -0,0 +1 @@
+kubectl apply -f .\backend-deployment.yaml -f .\frontend-deployment.yaml -f .\config-maps.yaml -f .\services.yaml -n mdata
\ No newline at end of file
diff --git a/backend-deployment.yaml b/backend-deployment.yaml
new file mode 100644
index 0000000..080b123
--- /dev/null
+++ b/backend-deployment.yaml
@@ -0,0 +1,60 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: maed-exporter-backend
+  namespace: mdata
+  labels:
+    app.kubernetes.io/name: maed-exporter-backend
+    app.kubernetes.io/version: "0.1.0"
+spec:
+  selector:
+    matchLabels:
+      app.kubernetes.io/name: maed-exporter-backend
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app.kubernetes.io/name: maed-exporter-backend
+    spec:
+      containers:
+        - name: maed-exporter-backend
+          image: registry.git-ce.rwth-aachen.de/machine-data/maed-exporter/maed-exporter-backend:release
+          imagePullPolicy: Always
+          ports:
+            - containerPort: 8080
+          envFrom:
+            - configMapRef:
+                name: maed-exporter-env
+          volumeMounts:
+            - mountPath: /exports
+              name: exports
+          resources:
+            limits:
+              memory: "2Gi"
+              cpu: "1000m"
+            requests:
+              memory: "512Mi"
+              cpu: "500m"
+          livenessProbe:
+            httpGet:
+              path: /health
+              port: 8080
+            initialDelaySeconds: 30
+            periodSeconds: 15
+            timeoutSeconds: 2
+            failureThreshold: 3
+          readinessProbe:
+            httpGet:
+              path: /health
+              port: 8080
+            initialDelaySeconds: 30
+            periodSeconds: 10
+            timeoutSeconds: 2
+            failureThreshold: 3
+      volumes:
+        - name: exports
+          emptyDir:
+            sizeLimit: "2Gi"
+
+
+
diff --git a/config-map.yaml b/config-map.yaml
deleted file mode 100644
index 48a6598..0000000
--- a/config-map.yaml
+++ /dev/null
@@ -1,13 +0,0 @@
-apiVersion: v1
-kind: ConfigMap
-metadata:
-  name: maed-exporter-configmap
-data:
-  FRONTEND_PORT: "8090"
-  EXPORT_DIR: "/exports/"
-  API_PORT: "8080"
-  API_BASE: "http://localhost"
-  API_PREFIX: "/api"
-  API_URL: "$(API_BASE):$(API_PORT)"
-  VITE_API_URL: "$(API_URL)"
-  CORS_ORIGIN: "http://localhost:8090"
\ No newline at end of file
diff --git a/config-maps.yaml b/config-maps.yaml
new file mode 100644
index 0000000..af55f58
--- /dev/null
+++ b/config-maps.yaml
@@ -0,0 +1,79 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: maed-exporter-env
+  labels:
+    app.kubernetes.io/name: maed-exporter-app
+data:
+  FRONTEND_PORT: "8090"
+  API_BASE: "backend-service.mdata.svc.cluster.local"
+  API_PORT: "8080"
+  API_PREFIX: "/api"
+  EXPORT_DIR: "/exports/"
+  CORS_ORIGIN: "frontend-service.mdata.svc.cluster.local:8090"
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: nginx-cfg
+  labels:
+    app.kubernetes.io/name: maed-exporter-app
+data:
+  # hash $cookie_simple_session consistent;
+  default.conf.template: |
+    upstream api {
+        server ${API_BASE}:${API_PORT} max_fails=3 fail_timeout=30s;
+    }
+
+    server {
+        listen ${FRONTEND_PORT};
+
+        location / {
+            index index.html;
+            root /usr/share/nginx/html;
+            try_files $uri $uri/ /index.html =404;
+        }
+
+        location /health {
+          access_log off;
+          add_header 'Content-Type' 'application/json';
+          return 200 '{"status":"healthy"}';
+      }
+    
+      location ${API_PREFIX} {
+          proxy_pass http://api;
+          proxy_set_header Host $http_host;
+          proxy_set_header X-Real-IP $remote_addr;
+          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+          proxy_set_header X-Forwarded-Proto $scheme;
+      }
+    }
+  default.conf: |
+    upstream api {
+      hash $cookie_simple_session consistent;
+      server backend-service.mdata.svc.cluster.local:8080 max_fails=3 fail_timeout=30s;
+    }
+    
+    server {
+      listen ${FRONTEND_PORT};
+    
+      location / {
+          index index.html;
+          root /usr/share/nginx/html;
+          try_files $uri $uri/ /index.html =404;
+      }
+    
+      location /health {
+          access_log off;
+          add_header 'Content-Type' 'application/json';
+          return 200 '{"status":"healthy"}';
+      }
+    
+      location /api {
+          proxy_pass http://api;
+          proxy_set_header Host $http_host;
+          proxy_set_header X-Real-IP $remote_addr;
+          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+          proxy_set_header X-Forwarded-Proto $scheme;
+      }
+    }
\ No newline at end of file
diff --git a/deployment.yaml b/deployment.yaml
deleted file mode 100644
index 4e803b5..0000000
--- a/deployment.yaml
+++ /dev/null
@@ -1,62 +0,0 @@
-apiVersion: apps/v1
-kind: Deployment
-metadata:
-  name: maed-exporter-app
-  namespace: mdata
-  labels:
-    app.kubernetes.io/name: maed-exporter-app
-    app.kubernetes.io/version: "0.1.0"
-spec:
-  selector:
-    matchLabels:
-      app.kubernetes.io/name: maed-exporter-app
-  replicas: 1
-  revisionHistoryLimit: 5
-  strategy:
-    type: Recreate
-  template:
-    metadata:
-      labels:
-        app.kubernetes.io/name: maed-exporter-app
-    spec:
-      containers:
-        - name: maed-exporter-backend
-          image: registry.git-ce.rwth-aachen.de/machine-data/maed-exporter/maed-exporter-backend#release
-          imagePullPolicy: IfNotPresent
-          ports:
-            - containerPort: 8080
-              protocol: TCP
-          envFrom:
-            - configMapKeyRef:
-                name: maed-exporter-configmap
-          volumeMounts:
-            - mountPath: /exports
-              name: exports
-          resources:
-            limits:
-              memory: "2Gi"
-              cpu: "1000m"
-            requests:
-              memory: "512Mi"
-              cpu: "500m"
-      volumes:
-        - emptyDir:
-            sizeLimit: "2Gi"
-          name: exports
-      - name: maed-exporter-frontend
-          image: registry.git-ce.rwth-aachen.de/machine-data/maed-exporter/maed-exporter-frontend#release
-          imagePullPolicy: IfNotPresent
-          ports:
-            - containerPort: 8090
-              protocol: TCP
-          envFrom:
-            - configMapKeyRef:
-                name: maed-exporter-configmap
-          resources:
-            limits:
-              memory: "1Gi"
-              cpu: "1000m"
-            requests:
-              memory: "1Gi"
-              cpu: "500m"
-      
\ No newline at end of file
diff --git a/frontend-deployment.yaml b/frontend-deployment.yaml
new file mode 100644
index 0000000..9dadfbe
--- /dev/null
+++ b/frontend-deployment.yaml
@@ -0,0 +1,62 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: maed-exporter-frontend
+  namespace: mdata
+  labels:
+    app.kubernetes.io/name: maed-exporter-frontend
+    app.kubernetes.io/version: "0.1.0"
+spec:
+  selector:
+    matchLabels:
+      app.kubernetes.io/name: maed-exporter-frontend
+  replicas: 1
+  revisionHistoryLimit: 5
+  strategy:
+    type: Recreate
+  template:
+    metadata:
+      labels:
+        app.kubernetes.io/name: maed-exporter-frontend
+    spec:
+      containers:
+        - name: maed-exporter-frontend
+          image: registry.git-ce.rwth-aachen.de/machine-data/maed-exporter/maed-exporter-frontend:release
+          imagePullPolicy: Always
+          ports:
+            - containerPort: 8090
+              protocol: TCP
+          envFrom:
+            - configMapRef:
+                name: maed-exporter-env
+          volumeMounts:
+            - name: nginx-cfg
+              mountPath: /etc/nginx/templates/default.conf.template #/etc/nginx/conf.d/default.conf
+              subPath: default.conf.template
+          resources:
+            limits:
+              memory: "1Gi"
+              cpu: "1000m"
+            requests:
+              memory: "1Gi"
+              cpu: "500m"
+          livenessProbe:
+            httpGet:
+              path: /health
+              port: 8090
+            initialDelaySeconds: 10
+            periodSeconds: 15
+            timeoutSeconds: 2
+            failureThreshold: 3
+          readinessProbe:
+            httpGet:
+              path: /health
+              port: 8090
+            initialDelaySeconds: 5
+            periodSeconds: 10
+            timeoutSeconds: 2
+            failureThreshold: 3
+      volumes:
+        - name: nginx-cfg
+          configMap:
+            name: nginx-cfg
diff --git a/service.yaml b/service.yaml
deleted file mode 100644
index 1744317..0000000
--- a/service.yaml
+++ /dev/null
@@ -1,17 +0,0 @@
-apiVersion: v1
-kind: Service
-metadata:
-  name: maed-exporter-service
-  namespace: mdata
-  labels:
-    app.kubernetes.io/name: maed-exporter-app
-spec:
-  externalTrafficPolicy: Cluster
-  ports:
-    - port: 80
-      protocol: TCP
-      targetPort: 8090
-  selector:
-    app.kubernetes.io/name: maed-exporter-app
-  type: NodePort
-  sessionAffinity: ClientIP
\ No newline at end of file
diff --git a/services.yaml b/services.yaml
new file mode 100644
index 0000000..62e4c25
--- /dev/null
+++ b/services.yaml
@@ -0,0 +1,33 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: backend-service
+  namespace: mdata
+  labels:
+    app.kubernetes.io/name: maed-exporter-app
+spec:
+  selector:
+    app.kubernetes.io/name: maed-exporter-backend
+  ports:
+    - protocol: TCP
+      port: 8080        # Cluster IP port
+      targetPort: 8080  # Port on the backend container
+  type: ClusterIP        # Exposes the service only within the cluster
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: frontend-service
+  namespace: mdata
+  labels:
+    app.kubernetes.io/name: maed-exporter-app
+spec:
+  selector:
+    app.kubernetes.io/name: maed-exporter-frontend
+  ports:
+    - protocol: TCP
+      port: 8090
+      targetPort: 8090  # Port on the frontend container
+  type: NodePort
+  externalTrafficPolicy: Cluster
+  sessionAffinity: ClientIP
\ No newline at end of file
-- 
GitLab