diff --git a/code/kubernetes/grpc-gateway/deployment.yaml b/code/kubernetes/grpc-gateway/deployment.yaml index d45954b..529ff7f 100644 --- a/code/kubernetes/grpc-gateway/deployment.yaml +++ b/code/kubernetes/grpc-gateway/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 8080 + resources: + requests: + cpu: 40m + memory: 80Mi + limits: + cpu: 120m + memory: 240Mi env: - name: GRPC_GATEWAY_PORT valueFrom: @@ -76,5 +83,24 @@ spec: httpGet: path: /health port: 8080 - initialDelaySeconds: 10 + initialDelaySeconds: 30 timeoutSeconds: 5 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: grpc-gateway-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: grpc-gateway + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/mongo/deployment.yaml b/code/kubernetes/mongo/deployment.yaml index bb1713a..3810484 100644 --- a/code/kubernetes/mongo/deployment.yaml +++ b/code/kubernetes/mongo/deployment.yaml @@ -17,6 +17,13 @@ spec: image: mongo:latest ports: - containerPort: 27017 + resources: + requests: + cpu: 200m + memory: 700Mi + limits: + cpu: 500m + memory: 1Gi env: - name: MONGO_INITDB_DATABASE valueFrom: @@ -42,7 +49,7 @@ spec: - mongosh - --eval - "db.adminCommand('ping')" - initialDelaySeconds: 15 + initialDelaySeconds: 60 timeoutSeconds: 5 readinessProbe: exec: @@ -50,7 +57,7 @@ spec: - mongosh - --eval - "db.adminCommand('ping')" - initialDelaySeconds: 15 + initialDelaySeconds: 20 timeoutSeconds: 5 volumes: - name: mongodb-data diff --git a/code/kubernetes/scripts/README.md b/code/kubernetes/scripts/README.md index 0716185..c44a3d7 100644 --- a/code/kubernetes/scripts/README.md +++ b/code/kubernetes/scripts/README.md @@ -45,6 +45,7 @@ $ ./cluster-info.sh - `--deployments` Shows deployment configurations and statuses for the namespace. - `--resources-pods` Displays real-time CPU and memory usage metrics for each pod. - `--resources-nodes` Displays real-time CPU and memory usage metrics for each node in the cluster. +- `--hpa` Shows all Horizontal Pod Autoscalers in the namespace, including current and target scaling metrics. - `--all` Runs all of the above commands to display full cluster info. ### 4. Delete Cluster diff --git a/code/kubernetes/scripts/cluster-info.sh b/code/kubernetes/scripts/cluster-info.sh index 20723d5..5d56aa5 100644 --- a/code/kubernetes/scripts/cluster-info.sh +++ b/code/kubernetes/scripts/cluster-info.sh @@ -11,6 +11,7 @@ SHOW_SERVICES=false SHOW_DEPLOYMENTS=false SHOW_RESOURCES_PODS=false SHOW_RESOURCES_NODES=false +SHOW_HPA=false if [[ $# -eq 0 ]]; then SHOW_NAMESPACES=true @@ -19,6 +20,7 @@ if [[ $# -eq 0 ]]; then SHOW_DEPLOYMENTS=true SHOW_RESOURCES_PODS=true SHOW_RESOURCES_NODES=true + SHOW_HPA=true fi # Parse flags @@ -30,6 +32,7 @@ while [[ "$#" -gt 0 ]]; do --deployments) SHOW_DEPLOYMENTS=true ;; --resources-pods) SHOW_RESOURCES_PODS=true ;; --resources-nodes) SHOW_RESOURCES_NODES=true ;; + --hpa) SHOW_HPA=true ;; --all) SHOW_NAMESPACES=true SHOW_PODS=true @@ -37,6 +40,7 @@ while [[ "$#" -gt 0 ]]; do SHOW_DEPLOYMENTS=true SHOW_RESOURCES_PODS=true SHOW_RESOURCES_NODES=true + SHOW_HPA=true ;; *) echo "āŒ Unknown flag: $1"; exit 1 ;; esac @@ -53,3 +57,4 @@ $SHOW_SERVICES && echo -e "\nšŸ” Services:" && kubectl get svc -n $CLUSTER_NAME $SHOW_DEPLOYMENTS && echo -e "\nšŸ“‚ Deployments:" && kubectl get deployments -n $CLUSTER_NAME $SHOW_RESOURCES_PODS && echo -e "\nšŸ“Š Resource Usage (Pods):" && kubectl top pods -n $CLUSTER_NAME $SHOW_RESOURCES_NODES && echo -e "\nšŸ–„ļø Resource Usage (Nodes):" && kubectl top nodes +$SHOW_HPA && echo -e "\nšŸ“Œ Horizontal Pod Autoscalers:" && kubectl get hpa -n $CLUSTER_NAME \ No newline at end of file diff --git a/code/kubernetes/scripts/create-cluster.sh b/code/kubernetes/scripts/create-cluster.sh index e20ed38..a5b0b19 100644 --- a/code/kubernetes/scripts/create-cluster.sh +++ b/code/kubernetes/scripts/create-cluster.sh @@ -3,7 +3,7 @@ set -e PROJECT_ID="threadit-api" CLUSTER_NAME="threadit-cluster" -MACHINE_TYPE="e2-standard-4" +MACHINE_TYPE="e2-standard-2" ZONE="europe-west1-b" gcloud config set project $PROJECT_ID @@ -11,12 +11,12 @@ gcloud config set project $PROJECT_ID gcloud container clusters create $CLUSTER_NAME \ --num-nodes=3 \ --enable-autoscaling \ - --min-nodes=0 \ - --max-nodes=4 \ + --min-nodes=1 \ + --max-nodes=5 \ --machine-type=$MACHINE_TYPE \ --zone=$ZONE \ --disk-type=pd-standard \ - --disk-size=20 + --disk-size=25 gcloud container clusters get-credentials $CLUSTER_NAME --zone=$ZONE diff --git a/code/kubernetes/services/comment-service/deployment.yaml b/code/kubernetes/services/comment-service/deployment.yaml index 9b02733..9bd770e 100644 --- a/code/kubernetes/services/comment-service/deployment.yaml +++ b/code/kubernetes/services/comment-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50054 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50054 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50054 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: comment-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: comment-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/community-service/deployment.yaml b/code/kubernetes/services/community-service/deployment.yaml index 0825b7c..598daed 100644 --- a/code/kubernetes/services/community-service/deployment.yaml +++ b/code/kubernetes/services/community-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50052 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50052 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50052 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: community-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: community-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/db-service/deployment.yaml b/code/kubernetes/services/db-service/deployment.yaml index 29693c6..cbf045f 100644 --- a/code/kubernetes/services/db-service/deployment.yaml +++ b/code/kubernetes/services/db-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50051 + resources: + requests: + cpu: 100m + memory: 250Mi + limits: + cpu: 300m + memory: 2Gi env: - name: SERVICE_PORT valueFrom: @@ -35,16 +42,21 @@ spec: - mountPath: /var/secret/gcp/ name: bucket-credentials readOnly: true + livenessProbe: + tcpSocket: + port: 50051 + initialDelaySeconds: 60 + timeoutSeconds: 4 readinessProbe: tcpSocket: port: 50051 - initialDelaySeconds: 8 - timeoutSeconds: 5 - livenessProbe: + initialDelaySeconds: 30 + timeoutSeconds: 4 + startupProbe: tcpSocket: port: 50051 - initialDelaySeconds: 8 - timeoutSeconds: 5 + periodSeconds: 2 + failureThreshold: 90 volumes: - name: bucket-credentials secret: @@ -52,3 +64,22 @@ spec: items: - key: gcs-key.json path: gcs-key.json +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: db-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: db-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/popular-service/deployment.yaml b/code/kubernetes/services/popular-service/deployment.yaml index fbd2b5c..e6daa04 100644 --- a/code/kubernetes/services/popular-service/deployment.yaml +++ b/code/kubernetes/services/popular-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50057 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50057 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50057 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: popular-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: popular-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/search-service/deployment.yaml b/code/kubernetes/services/search-service/deployment.yaml index 9b7cb3b..e51dae7 100644 --- a/code/kubernetes/services/search-service/deployment.yaml +++ b/code/kubernetes/services/search-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50056 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50056 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50056 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: search-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: search-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/thread-service/deployment.yaml b/code/kubernetes/services/thread-service/deployment.yaml index 4092890..11b9ee9 100644 --- a/code/kubernetes/services/thread-service/deployment.yaml +++ b/code/kubernetes/services/thread-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50053 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50053 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50053 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: thread-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: thread-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/services/vote-service/deployment.yaml b/code/kubernetes/services/vote-service/deployment.yaml index e19959e..44b329e 100644 --- a/code/kubernetes/services/vote-service/deployment.yaml +++ b/code/kubernetes/services/vote-service/deployment.yaml @@ -18,6 +18,13 @@ spec: imagePullPolicy: Always ports: - containerPort: 50055 + resources: + requests: + cpu: 20m + memory: 40Mi + limits: + cpu: 60m + memory: 120Mi env: - name: SERVICE_PORT valueFrom: @@ -41,10 +48,29 @@ spec: readinessProbe: tcpSocket: port: 50055 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 5 + timeoutSeconds: 3 livenessProbe: tcpSocket: port: 50055 - initialDelaySeconds: 6 - timeoutSeconds: 4 + initialDelaySeconds: 15 + timeoutSeconds: 3 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: vote-service-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: vote-service + minReplicas: 1 + maxReplicas: 3 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/code/kubernetes/traefik/values.yaml b/code/kubernetes/traefik/values.yaml index 9f28b52..56f8925 100644 --- a/code/kubernetes/traefik/values.yaml +++ b/code/kubernetes/traefik/values.yaml @@ -1,12 +1,23 @@ -# https://github.com/traefik/traefik-helm-chart/blob/master/traefik/VALUES.md +# traefik/values.yaml -# autoscaling: # TODO: If needed, uncomment and configure HPA settings -# enabled: true -# maxReplicas: 2 -# metrics: -# - type: Resource -# resource: -# name: cpu -# target: -# type: Utilization -# averageUtilization: 60 +replicaCount: 1 + +resources: + requests: + cpu: 40m + memory: 80Mi + limits: + cpu: 120m + memory: 240Mi + +# TODO: If needed, uncomment and configure HPA settings +#autoscaling: +# enabled: true +# maxReplicas: 2 +# metrics: +# - type: Resource +# resource: +# name: cpu +# target: +# type: Utilization +# averageUtilization: 60 \ No newline at end of file