Skip to content

Commit

Permalink
Update k8s YAML for without volume scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
prasadhonrao committed Oct 26, 2024
1 parent 66302c1 commit e4aa00e
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 58 deletions.
10 changes: 6 additions & 4 deletions deploy/k8s/without-volume/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/without-volume/database/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ metadata:
name: devcamper-db-configmap
namespace: devcamper-namespace
data:
mongodb_db_name: 'devcamper-db'
mongodb_db_name: devcamper-db
5 changes: 2 additions & 3 deletions deploy/k8s/without-volume/database/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions deploy/k8s/without-volume/database/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
4 changes: 2 additions & 2 deletions deploy/k8s/without-volume/database/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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='
15 changes: 0 additions & 15 deletions deploy/k8s/without-volume/database/service-nodeport.yaml

This file was deleted.

6 changes: 3 additions & 3 deletions deploy/k8s/without-volume/database/service.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions deploy/k8s/without-volume/database/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
kubectl delete -f service.yaml

11 changes: 7 additions & 4 deletions deploy/k8s/without-volume/install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,4 +17,5 @@ trap 'cd - > /dev/null' EXIT
run_install namespace
run_install database
run_install webapi
run_install webapp
run_install webapp
run_install network
30 changes: 30 additions & 0 deletions deploy/k8s/without-volume/network/ingress.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions deploy/k8s/without-volume/network/install.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions deploy/k8s/without-volume/network/uninstall.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions deploy/k8s/without-volume/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
7 changes: 4 additions & 3 deletions deploy/k8s/without-volume/webapi/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 7 additions & 0 deletions deploy/k8s/without-volume/webapi/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ spec:
ports:
- containerPort: 5000
env:
- name: NODE_ENV
value: 'production'
- name: mongodb_host
valueFrom:
configMapKeyRef:
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion deploy/k8s/without-volume/webapi/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
22 changes: 12 additions & 10 deletions deploy/k8s/without-volume/webapi/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion deploy/k8s/without-volume/webapi/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
kubectl delete -f service.yaml
15 changes: 15 additions & 0 deletions deploy/k8s/without-volume/webapp/configmap.yaml
Original file line number Diff line number Diff line change
@@ -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"
}
}
38 changes: 38 additions & 0 deletions deploy/k8s/without-volume/webapp/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions deploy/k8s/without-volume/webapp/install.sh
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#!bin/bash
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
12 changes: 12 additions & 0 deletions deploy/k8s/without-volume/webapp/service.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions deploy/k8s/without-volume/webapp/uninstall.sh
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#!bin/bash
kubectl delete -f configmap.yaml
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml

0 comments on commit e4aa00e

Please sign in to comment.