From e4aa00e318c3a194037e6fe4f12f19ea1bda3909 Mon Sep 17 00:00:00 2001 From: prasadhonrao Date: Sat, 26 Oct 2024 23:06:11 +0100 Subject: [PATCH] Update k8s YAML for without volume scenario --- deploy/k8s/without-volume/README.md | 10 +++-- .../without-volume/database/configmap.yaml | 2 +- .../without-volume/database/deployment.yaml | 5 +-- deploy/k8s/without-volume/database/install.sh | 7 +++- .../k8s/without-volume/database/secret.yaml | 4 +- .../database/service-nodeport.yaml | 15 -------- .../k8s/without-volume/database/service.yaml | 6 +-- .../k8s/without-volume/database/uninstall.sh | 4 +- deploy/k8s/without-volume/install.sh | 11 ++++-- .../k8s/without-volume/network/ingress.yaml | 30 +++++++++++++++ deploy/k8s/without-volume/network/install.sh | 13 +++++++ .../k8s/without-volume/network/uninstall.sh | 3 ++ deploy/k8s/without-volume/uninstall.sh | 9 +++-- .../k8s/without-volume/webapi/configmap.yaml | 7 ++-- .../k8s/without-volume/webapi/deployment.yaml | 7 ++++ deploy/k8s/without-volume/webapi/install.sh | 5 ++- deploy/k8s/without-volume/webapi/secret.yaml | 22 ++++++----- .../{service-nodeport.yaml => service.yaml} | 6 +-- deploy/k8s/without-volume/webapi/uninstall.sh | 2 +- .../k8s/without-volume/webapp/configmap.yaml | 15 ++++++++ .../k8s/without-volume/webapp/deployment.yaml | 38 +++++++++++++++++++ deploy/k8s/without-volume/webapp/install.sh | 3 ++ deploy/k8s/without-volume/webapp/service.yaml | 12 ++++++ deploy/k8s/without-volume/webapp/uninstall.sh | 3 ++ 24 files changed, 181 insertions(+), 58 deletions(-) delete mode 100644 deploy/k8s/without-volume/database/service-nodeport.yaml create mode 100644 deploy/k8s/without-volume/network/ingress.yaml create mode 100644 deploy/k8s/without-volume/network/install.sh create mode 100644 deploy/k8s/without-volume/network/uninstall.sh rename deploy/k8s/without-volume/webapi/{service-nodeport.yaml => service.yaml} (63%) create mode 100644 deploy/k8s/without-volume/webapp/configmap.yaml create mode 100644 deploy/k8s/without-volume/webapp/deployment.yaml create mode 100644 deploy/k8s/without-volume/webapp/service.yaml diff --git a/deploy/k8s/without-volume/README.md b/deploy/k8s/without-volume/README.md index 9780e6d..fc25653 100644 --- a/deploy/k8s/without-volume/README.md +++ b/deploy/k8s/without-volume/README.md @@ -1,6 +1,6 @@ # Setup Instructions -Follow the below instructions to setup the application in Kubernetes cluster without using Persistent Volume. Note that the data will be stored in the container and will be lost when the container restarts or when the pod is deleted. +Follow the below instructions to setup the application in Kubernetes cluster using EmptyDir Volume. Note that the data will be lost when the pod is deleted. ## Database Secrets @@ -42,7 +42,7 @@ Follow the below instructions to setup the application in Kubernetes cluster wit 5. Create JWT secret and encode it in base64 format and provide it in webapi/secret.yaml file: ```bash - echo -n 'JWT_SECRET' | base64 + echo -n 'JWT_SECRET ' | base64 ``` ## Deploy the Application @@ -67,13 +67,15 @@ Follow the below instructions to setup the application in Kubernetes cluster wit mongodb://admin:password@localhost:32017/ ``` -3. Delete the database pod using the following command: +3. Restart the container using Docker Desktop to see the data in the database. + +4. Delete the database pod using the following command: ```bash kubectl delete pod -l app=devcamper-db -n devcamper-namespace ``` -4. Note that deployment creates a new pod but the data is lost as the data is stored in the container and not in the Persistent Volume. +5. Note that deployment creates a new pod but the data is lost as the data is stored in the container and not in the Persistent Volume. ## Uninstall the Application diff --git a/deploy/k8s/without-volume/database/configmap.yaml b/deploy/k8s/without-volume/database/configmap.yaml index 05e0a37..774403f 100644 --- a/deploy/k8s/without-volume/database/configmap.yaml +++ b/deploy/k8s/without-volume/database/configmap.yaml @@ -4,4 +4,4 @@ metadata: name: devcamper-db-configmap namespace: devcamper-namespace data: - mongodb_db_name: 'devcamper-db' + mongodb_db_name: devcamper-db diff --git a/deploy/k8s/without-volume/database/deployment.yaml b/deploy/k8s/without-volume/database/deployment.yaml index 9067018..55dc37b 100644 --- a/deploy/k8s/without-volume/database/deployment.yaml +++ b/deploy/k8s/without-volume/database/deployment.yaml @@ -16,11 +16,10 @@ spec: app: devcamper-db spec: containers: - - name: devcamper-db + - name: devcamper-db-container image: mongo ports: - - name: mongodb - containerPort: 27017 + - containerPort: 27017 env: - name: MONGO_INITDB_DATABASE valueFrom: diff --git a/deploy/k8s/without-volume/database/install.sh b/deploy/k8s/without-volume/database/install.sh index a08f6e9..3cb582c 100644 --- a/deploy/k8s/without-volume/database/install.sh +++ b/deploy/k8s/without-volume/database/install.sh @@ -2,5 +2,8 @@ kubectl apply -f configmap.yaml kubectl apply -f secret.yaml kubectl apply -f deployment.yaml -kubectl apply -f service-nodeport.yaml -kubectl apply -f service.yaml \ No newline at end of file +kubectl apply -f service.yaml + + +# Port foward deployment to test DB connectivity from local machine +# kubectl port-forward deployment/devcamper-db-deployment 32017:27017 -n devcamper-namespace \ No newline at end of file diff --git a/deploy/k8s/without-volume/database/secret.yaml b/deploy/k8s/without-volume/database/secret.yaml index 9dc04fb..8b8de8a 100644 --- a/deploy/k8s/without-volume/database/secret.yaml +++ b/deploy/k8s/without-volume/database/secret.yaml @@ -4,5 +4,5 @@ metadata: name: devcamper-db-secret namespace: devcamper-namespace data: - mongodb_username: 'YWRtaW4=' # base64 encoded string for 'admin' - mongodb_password: 'cGFzc3dvcmQ=' # base64 encoded string for 'password' + mongodb_username: 'YWRtaW4=' + mongodb_password: 'cGFzc3dvcmQ=' diff --git a/deploy/k8s/without-volume/database/service-nodeport.yaml b/deploy/k8s/without-volume/database/service-nodeport.yaml deleted file mode 100644 index 38f5651..0000000 --- a/deploy/k8s/without-volume/database/service-nodeport.yaml +++ /dev/null @@ -1,15 +0,0 @@ -# This service is created only to validate the connectivity to the database externally -apiVersion: v1 -kind: Service -metadata: - name: devcamper-db-nodeport-service - namespace: devcamper-namespace -spec: - type: NodePort - selector: - app: devcamper-db - ports: - - protocol: TCP - port: 27017 - targetPort: 27017 - nodePort: 32017 diff --git a/deploy/k8s/without-volume/database/service.yaml b/deploy/k8s/without-volume/database/service.yaml index d6fa98a..6d7883c 100644 --- a/deploy/k8s/without-volume/database/service.yaml +++ b/deploy/k8s/without-volume/database/service.yaml @@ -1,13 +1,13 @@ apiVersion: v1 kind: Service metadata: - name: devcamper-db-clusterip-service + name: devcamper-db-service namespace: devcamper-namespace spec: + type: ClusterIP selector: app: devcamper-db ports: - - name: mongodb + - protocol: TCP port: 27017 targetPort: 27017 - type: ClusterIP diff --git a/deploy/k8s/without-volume/database/uninstall.sh b/deploy/k8s/without-volume/database/uninstall.sh index 66f9516..398ae0c 100644 --- a/deploy/k8s/without-volume/database/uninstall.sh +++ b/deploy/k8s/without-volume/database/uninstall.sh @@ -2,5 +2,5 @@ kubectl delete -f configmap.yaml kubectl delete -f secret.yaml kubectl delete -f deployment.yaml -kubectl delete -f service-nodeport.yaml -kubectl delete -f service.yaml \ No newline at end of file +kubectl delete -f service.yaml + diff --git a/deploy/k8s/without-volume/install.sh b/deploy/k8s/without-volume/install.sh index 82a6fdd..153a8d9 100644 --- a/deploy/k8s/without-volume/install.sh +++ b/deploy/k8s/without-volume/install.sh @@ -1,11 +1,13 @@ #!/bin/bash -# Function to run install script in a directory +# Function to run install scripts in specified directories run_install() { local dir=$1 - cd "$dir" || { echo "Failed to change directory to $dir"; exit 1; } + echo -e "\033[1;34m==================== Installing $dir ====================\033[0m" + cd "$dir" || { echo -e "\033[1;31mFailed to change directory to $dir\033[0m"; exit 1; } bash ./install.sh - cd - > /dev/null || { echo "Failed to return to previous directory"; exit 1; } + cd - > /dev/null || { echo -e "\033[1;31mFailed to return to previous directory\033[0m"; exit 1; } + echo -e "\033[1;34m==================== Finished $dir ====================\033[0m" } # Set trap to ensure we return to the original directory on exit @@ -15,4 +17,5 @@ trap 'cd - > /dev/null' EXIT run_install namespace run_install database run_install webapi -run_install webapp \ No newline at end of file +run_install webapp +run_install network \ No newline at end of file diff --git a/deploy/k8s/without-volume/network/ingress.yaml b/deploy/k8s/without-volume/network/ingress.yaml new file mode 100644 index 0000000..468b6fd --- /dev/null +++ b/deploy/k8s/without-volume/network/ingress.yaml @@ -0,0 +1,30 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: devcamper-ingress + namespace: devcamper-namespace + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx + rules: + - host: devcamper.webapp + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: devcamper-webapp-service + port: + number: 3000 + - host: devcamper.webapi + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: devcamper-webapi-service + port: + number: 5000 diff --git a/deploy/k8s/without-volume/network/install.sh b/deploy/k8s/without-volume/network/install.sh new file mode 100644 index 0000000..b788010 --- /dev/null +++ b/deploy/k8s/without-volume/network/install.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# Install an NGINX Ingress Controller +kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml + +# Wait for the NGINX Ingress Controller to be ready +echo "Waiting for NGINX Ingress Controller to be ready..." +kubectl wait --namespace ingress-nginx \ + --for=condition=ready pod \ + --selector=app.kubernetes.io/component=controller \ + --timeout=90s + +# Apply the ingress configuration +kubectl apply -f ingress.yaml \ No newline at end of file diff --git a/deploy/k8s/without-volume/network/uninstall.sh b/deploy/k8s/without-volume/network/uninstall.sh new file mode 100644 index 0000000..a0ef770 --- /dev/null +++ b/deploy/k8s/without-volume/network/uninstall.sh @@ -0,0 +1,3 @@ +#!bin/bash +kubectl delete -f ingress.yaml +kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml \ No newline at end of file diff --git a/deploy/k8s/without-volume/uninstall.sh b/deploy/k8s/without-volume/uninstall.sh index 3dd7216..69e7345 100644 --- a/deploy/k8s/without-volume/uninstall.sh +++ b/deploy/k8s/without-volume/uninstall.sh @@ -3,9 +3,11 @@ # Function to run uninstall script in a directory run_uninstall() { local dir=$1 - cd "$dir" || { echo "Failed to change directory to $dir"; exit 1; } + echo -e "\033[1;34m==================== Uninstalling $dir ====================\033[0m" + cd "$dir" || { echo -e "\033[1;31mFailed to change directory to $dir\033[0m"; exit 1; } bash ./uninstall.sh - cd - > /dev/null || { echo "Failed to return to previous directory"; exit 1; } + cd - > /dev/null || { echo -e "\033[1;31mFailed to return to previous directory\033[0m"; exit 1; } + echo -e "\033[1;34m==================== Finished $dir ====================\033[0m" } # Set trap to ensure we return to the original directory on exit @@ -15,4 +17,5 @@ trap 'cd - > /dev/null' EXIT run_uninstall database run_uninstall webapi run_uninstall webapp -run_uninstall namespace +run_uninstall network +run_uninstall namespace \ No newline at end of file diff --git a/deploy/k8s/without-volume/webapi/configmap.yaml b/deploy/k8s/without-volume/webapi/configmap.yaml index 72c2e29..2b5b9a8 100644 --- a/deploy/k8s/without-volume/webapi/configmap.yaml +++ b/deploy/k8s/without-volume/webapi/configmap.yaml @@ -4,13 +4,14 @@ metadata: name: devcamper-webapi-configmap namespace: devcamper-namespace data: - mongodb_host: 'devcamper-db-clusterip-service' + mongodb_host: 'devcamper-db-service' mongodb_port: '27017' mongodb_db_name: 'devcamper-db' mongodb_db_params: 'authSource=admin' geocoder_provider: 'mapquest' - rate_limit_window: '100' - rate_limit_max: '1000' + jwt_expire: '30d' jwt_cookie_expire: '30' file_upload_path: './public/uploads' max_file_upload: '1000000' + rate_limit_window: '100' + rate_limit_max: '1000' diff --git a/deploy/k8s/without-volume/webapi/deployment.yaml b/deploy/k8s/without-volume/webapi/deployment.yaml index 5ec2e8c..f2b775a 100644 --- a/deploy/k8s/without-volume/webapi/deployment.yaml +++ b/deploy/k8s/without-volume/webapi/deployment.yaml @@ -29,6 +29,8 @@ spec: ports: - containerPort: 5000 env: + - name: NODE_ENV + value: 'production' - name: mongodb_host valueFrom: configMapKeyRef: @@ -54,6 +56,11 @@ spec: configMapKeyRef: name: devcamper-webapi-configmap key: geocoder_provider + - name: jwt_expire + valueFrom: + configMapKeyRef: + name: devcamper-webapi-configmap + key: jwt_expire - name: jwt_cookie_expire valueFrom: configMapKeyRef: diff --git a/deploy/k8s/without-volume/webapi/install.sh b/deploy/k8s/without-volume/webapi/install.sh index e4e601b..8e03034 100644 --- a/deploy/k8s/without-volume/webapi/install.sh +++ b/deploy/k8s/without-volume/webapi/install.sh @@ -2,4 +2,7 @@ kubectl apply -f configmap.yaml kubectl apply -f secret.yaml kubectl apply -f deployment.yaml -kubectl apply -f service-nodeport.yaml \ No newline at end of file +kubectl apply -f service.yaml + +# Port forward service to test API connectivity from local machine +# kubectl port-forward service/devcamper-webapi-service 5000:5000 -n devcamper-namespace \ No newline at end of file diff --git a/deploy/k8s/without-volume/webapi/secret.yaml b/deploy/k8s/without-volume/webapi/secret.yaml index 302f7b3..8e1d2ad 100644 --- a/deploy/k8s/without-volume/webapi/secret.yaml +++ b/deploy/k8s/without-volume/webapi/secret.yaml @@ -5,13 +5,15 @@ metadata: namespace: devcamper-namespace type: Opaque data: - mongodb_username: YWRtaW4= # base64 encoded string for 'admin' - mongodb_password: cGFzc3dvcmQ= # base64 encoded string for 'password' - geocoder_api_key: '' - smtp_host: '' - smtp_port: '' - smtp_email: '' - smtp_password: '' - from_email: '' - from_name: '' - jwt_secret: '' + mongodb_username: 'YWRtaW4=' # base64 encoded string for 'admin' + mongodb_password: 'cGFzc3dvcmQ=' # base64 encoded string for 'password' + geocoder_api_key: 'OGl4ZGpuSzJMRGpGNTZmdlpaUlZwYzNuYmxEV3p4WFE=' + smtp_host: c2FuZGJveC5zbXRwLm1haWx0cmFwLmlv + smtp_port: MjUyNQ== + smtp_email: YTRhYjBlNDkzMWUzOWY= + smtp_password: MGQ5ZjljZDVlZTdlOGY= + from_email: YWRtaW5AZGV2Y2FtcGVyLmNvbQ== + from_name: RGV2Q2FtcGVyIEFkbWlu + jwt_secret: ZGV2Y2FtcGVyand0c2VjcmV0 + rate_limit_window: MTAw + rate_limit_max: MTAwMA== diff --git a/deploy/k8s/without-volume/webapi/service-nodeport.yaml b/deploy/k8s/without-volume/webapi/service.yaml similarity index 63% rename from deploy/k8s/without-volume/webapi/service-nodeport.yaml rename to deploy/k8s/without-volume/webapi/service.yaml index 7736107..f833e25 100644 --- a/deploy/k8s/without-volume/webapi/service-nodeport.yaml +++ b/deploy/k8s/without-volume/webapi/service.yaml @@ -1,14 +1,12 @@ apiVersion: v1 kind: Service metadata: - name: devcamper-webapi-nodeport-service + name: devcamper-webapi-service namespace: devcamper-namespace spec: - type: NodePort ports: - protocol: TCP - port: 80 + port: 5000 targetPort: 5000 - nodePort: 32018 selector: app: devcamper-webapi diff --git a/deploy/k8s/without-volume/webapi/uninstall.sh b/deploy/k8s/without-volume/webapi/uninstall.sh index 0a7fb30..40249bd 100644 --- a/deploy/k8s/without-volume/webapi/uninstall.sh +++ b/deploy/k8s/without-volume/webapi/uninstall.sh @@ -2,4 +2,4 @@ kubectl delete -f configmap.yaml kubectl delete -f secret.yaml kubectl delete -f deployment.yaml -kubectl delete -f service-nodeport.yaml \ No newline at end of file +kubectl delete -f service.yaml diff --git a/deploy/k8s/without-volume/webapp/configmap.yaml b/deploy/k8s/without-volume/webapp/configmap.yaml new file mode 100644 index 0000000..ee1eaa0 --- /dev/null +++ b/deploy/k8s/without-volume/webapp/configmap.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: devcamper-webapp-configmap + namespace: devcamper-namespace +data: + config.json: | + { + "development": { + "react_app_devcamper_base_api_uri": "http://localhost:5000/api/v1" + }, + "production": { + "react_app_devcamper_base_api_uri": "http://devcamper.webapi/api/v1" + } + } diff --git a/deploy/k8s/without-volume/webapp/deployment.yaml b/deploy/k8s/without-volume/webapp/deployment.yaml new file mode 100644 index 0000000..c871cf1 --- /dev/null +++ b/deploy/k8s/without-volume/webapp/deployment.yaml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: devcamper-webapp-deployment + namespace: devcamper-namespace +spec: + replicas: 1 + selector: + matchLabels: + app: devcamper-webapp + template: + metadata: + labels: + app: devcamper-webapp + spec: + containers: + - name: devcamper-webapp-container + image: prasadhonrao/devcamper-webapp:latest + ports: + - containerPort: 3000 + env: + - name: NODE_ENV + value: 'production' + volumeMounts: + - name: config-volume + mountPath: /app/build/config/config.json + subPath: config.json + resources: + limits: + memory: '2Gi' + cpu: '2' + requests: + memory: '1Gi' + cpu: '1' + volumes: + - name: config-volume + configMap: + name: devcamper-webapp-configmap diff --git a/deploy/k8s/without-volume/webapp/install.sh b/deploy/k8s/without-volume/webapp/install.sh index bfeaf9d..13d4082 100644 --- a/deploy/k8s/without-volume/webapp/install.sh +++ b/deploy/k8s/without-volume/webapp/install.sh @@ -1 +1,4 @@ #!bin/bash +kubectl apply -f configmap.yaml +kubectl apply -f deployment.yaml +kubectl apply -f service.yaml diff --git a/deploy/k8s/without-volume/webapp/service.yaml b/deploy/k8s/without-volume/webapp/service.yaml new file mode 100644 index 0000000..e7c5834 --- /dev/null +++ b/deploy/k8s/without-volume/webapp/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: devcamper-webapp-service + namespace: devcamper-namespace +spec: + ports: + - protocol: TCP + port: 3000 + targetPort: 3000 + selector: + app: devcamper-webapp diff --git a/deploy/k8s/without-volume/webapp/uninstall.sh b/deploy/k8s/without-volume/webapp/uninstall.sh index bfeaf9d..1a6e510 100644 --- a/deploy/k8s/without-volume/webapp/uninstall.sh +++ b/deploy/k8s/without-volume/webapp/uninstall.sh @@ -1 +1,4 @@ #!bin/bash +kubectl delete -f configmap.yaml +kubectl delete -f deployment.yaml +kubectl delete -f service.yaml