If you're running containerized workloads on Amazon Elastic Kubernetes Service (EKS), you may have experienced the need to scale your pods up or down based on the workload demands. In such cases, the Horizontal Pod Autoscaler (HPA) is a useful tool that automatically adjusts the number of replicas in a Kubernetes deployment based on resource utilization metrics.
In this article, we'll explore how to configure and use the HPA on Amazon EKS, including the necessary prerequisites, commands, and step-by-step instructions.
PrerequisitesBefore we dive into the setup and configuration of the HPA, ensure that you have the following prerequisites in place:
- An active Amazon EKS cluster
- Kubernetes CLI tool (kubectl) installed and configured
- A running Kubernetes deployment with at least one replica
Configuring Horizontal Pod Autoscaler on Amazon EKSFollow the below steps to configure HPA on your Amazon EKS cluster:
Create a Kubernetes deployment with the desired number of replicas. You can use the following command to create a simple deployment with a single replica:
- Create a Kubernetes deployment
kubectl create deployment nginx --image=nginx
Expose the deployment using a Kubernetes service to make it accessible from outside the cluster:
- Expose the deployment
kubectl expose deployment nginx --port=80 --type=LoadBalancer
Verify that the deployment and service are running correctly using the following commands:
- Check the deployment and service
kubectl get deployments
kubectl get services
Create an HPA for your deployment using the following command:
- Create a Horizontal Pod Autoscaler
kubectl autoscale deployment nginx --cpu-percent=50 --min=1 --max=10
This command creates an HPA that targets the deployment named "nginx," scales up when the CPU utilization of the pods in the deployment exceeds 50%, and scales down when the CPU utilization goes below 50%. The minimum and maximum number of replicas are set to 1 and 10, respectively.
Check the status of the HPA using the following command:
- Check the HPA
kubectl get hpa
You should see the HPA you just created in the output.
Testing the Horizontal Pod AutoscalerTo test the HPA, we need to simulate high resource utilization by running a stress test. You can use the following command to simulate high CPU usage:
kubectl run -i --tty load-generator --image=busybox /bin/sh
while true; do wget -q -O- http://nginx; done
This command creates a busybox pod that continuously sends requests to the Nginx deployment we created earlier.
To monitor the HPA, open a new terminal window and run the following command:
watch kubectl get hpa
This command will update the HPA status every two seconds. You should see the number of replicas increase as the CPU utilization exceeds the threshold.
In this article, we explored how to configure and use the Horizontal Pod Autoscaler on Amazon EKS. With HPA, you can automatically scale your Kubernetes deployments based on resource utilization metrics, making it easier to manage and optimize your containerized workloads.
Related Searches and Questions asked:
That's it for this post. Keep practicing and have fun. Leave your comments if any.
Post a Comment