Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Continuing with respect to PR 20 and using same Apache based webpage.
Kubernetes HPA Controller (Horizontal Pod Autoscaler) on Minikube Cluster
In this demo, we will see how to deploy HPA controller. HPA will automatically scale the number of pods based on CPU utilization.
Pre-requisites to implement this project:
minikube addons enable metrics-server
What we are going to implement:
Steps to implement HPA:
Update the Deployments:
We'll modify the existing Apache deployment YAML files to include resource requests and limits. This is required for HPA to monitor CPU usage.
#apache-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: apache-deployment spec: replicas: 1 selector: matchLabels: app: apache template: metadata: labels: app: apache spec: containers: - name: apache image: httpd:2.4 ports: - containerPort: 80 resources: requests: cpu: 100m limits: cpu: 200m --- apiVersion: v1 kind: Service metadata: name: apache-service spec: selector: app: apache ports: - protocol: TCP port: 80 targetPort: 80 type: ClusterIP
Apply the updated deployments:
Create HPA Resources
We will create HPA resources for both Apache and NGINX deployments. The HPA will scale the number of pods based on CPU utilization.
#apache-hpa.yaml apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: apache-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: apache-deployment minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 20
Apply the HPA resources:
kubectl port-forward svc/apache-service 8081:80 --address 0.0.0.0 &
Verify HPA
You can check the status of the HPA using the following command:
This will show you the current state of the HPA, including the current and desired number of replicas.
Stress Testing
To see HPA in action, you can perform a stress test on your deployments. Here is an example of how to generate load on the Apache deployment using 'BusyBox':
Inside the container, use 'wget' to generate load:
This will generate continuous load on the Apache service, causing the HPA to scale up the number of pods.