Welcome to my submission for the DevOps Engineer assignment -2. In this repository, you'll find all the necessary files and documentation related to the assignment requirements.
To prepare the provided Node.js codebase for deployment on a local Kubernetes cluster, I followed the following several steps:
Dockerizing your application involves creating a Docker image that contains your Node.js application along with its dependencies.
This command maps port 3000 of the container to port 3000 of your localhost. Now we can access our Node.js application at http://localhost:3000 to check our docker image is working fine or not.
You'll need to create YAML files for Deployment and Service.
Deployment YAML will specify how many instances of your application should run,
while Service YAML will expose your application to other services within the Kubernetes cluster.
Make sure you have a local Kubernetes cluster(in my case Minikube) set up & running in the background, then you can deploy your application using kubectl: Run the commands
After executing these commands, your Node.js application should be deployed to your local Kubernetes cluster.
Note:- Make sure to adjust configurations as per your requirements and your Kubernetes cluster setup.
Fourth, To implement Horizontal Pod Autoscaling (HPA) for the deployed application, you'll need to configure HPA in addition to the Deployment and Service configurations. Here's how I did this:
-
Enable Metrics Server Horizontal Pod Autoscaler requires metrics from the Metrics Server to function properly. Ensure that the Metrics Server is deployed and running in your Kubernetes cluster. If not installed, install using:- kubectl apply-f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
-
Update Deployment YAML Update your Deployment YAML file to include resource requests and limits for CPU and memory. This will allow Kubernetes to monitor my resource usage.
-
Create Horizontal Pod Autoscaler (HPA) YAML Create an HPA YAML file to define the autoscaling behavior.
-
Apply Changes Apply the changes to your Kubernetes cluster using kubectl apply:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f hpa.yaml
Fifth, To define resource limitations (CPU and RAM) for the deployed application pods, we need to set resource requests and limits in the Deployment YAML file.
This will allow Kubernetes to allocate resources appropriately and prevent resource starvation or contention.
Here's how I specifed resource limitations in my Deployment YAML:
In this configuration:
requests specify the minimum amount of resources that the container requires. & limits specify the maximum amount of resources that the container is allowed to use.
After updating the Deployment YAML with resource limitations, you can apply the changes to your Kubernetes cluster:
This ensures that each pod running my Node.js application works to the specified resource limitations, preventing resource contention and ensuring stable performance of my application.
Sixth, To simulate increased traffic and stress test my application using hey (a load testing tool), I followed these steps:
-
Install hey First if haven't already, you need to install hey. You can find installation instructions in the hey GitHub repository: https://github.com/rakyll/hey#install I have a mac with homebrew installed so I installed it easily using brew install hey.
-
Determine Load Parameters Decide on the load parameters you want to simulate, such as the number of requests (-n), the concurrency level (-c), and any additional options you want to use.
-
Run Load Test Run hey with the desired parameters to stress test your application. For example: I ran using :-
hey -n 1000 -c 50 http://your-nodejs-app-url/
This command sends 1000 requests with a concurrency level of 50 to the specified URL.(in my case it's http://localhost:3000/ )
- Analyze Results Once the load test completes, hey will display various metrics such as requests per second, latency statistics, and errors encountered. Analyze these metrics to understand how your application performs under load.
Example Load Test Here's another example of a load test command using hey:
hey -n 2000 -c 150 http://localhost:3000/
& Observe how it responds
-n 2000: Specifies 2000 total requests to be made.
-c 150: Sets the concurrency level to 150, meaning 150 requests will be made concurrently.
You can adjust the parameters according to your specific testing requirements and the expected traffic load on your application.