How to Build and Deploy a Scalable Kubernetes App with KNote
Modern software development requires applications to handle traffic spikes smoothly while making the best use of server hardware. Kubernetes serves as the industry standard for managing these containerized applications at scale.
This comprehensive guide walks through building and deploying a scalable, production-ready version of KNote, a classic Node.js note-taking application. You will learn how to transition the app from a local environment into a fully distributed cluster that scales on demand. ๐ ๏ธ Step 1: Containerizing the KNote Application
To run KNote on Kubernetes, the application must be packaged as a lightweight container image. Using a small base image ensures that containers start quickly and consume minimal memory, which is vital for efficient scaling.
Create a Dockerfile in the root directory of your KNote application: dockerfile
# Use a lightweight Node base image for faster initialization FROM node:18-alpine # Set the working directory inside the container WORKDIR /usr/src/app # Copy dependency files first to utilize Docker layer caching COPY package*.json ./ # Install production dependencies only RUN npm ci –only=production # Copy the rest of the application source code COPY . . # Expose the internal port used by the KNote app EXPOSE 3000 # Define the command to launch the application CMD [ “node”, “index.js” ] Use code with caution.
Build and push the image to a container registry like Docker Hub:
# Build the Docker image tagged with your registry username docker build -t your-registry-username/knote:v1.0.0 . # Upload the image to the public or private registry docker push your-registry-username/knote:v1.0.0 Use code with caution. ๐ฆ Step 2: Designing the Declarative Manifest
Kubernetes uses declarative configuration files (YAML) to manage application states. For a scalable KNote architecture, separate the frontend web deployment from the backend data storage (such as MongoDB). Create a deployment manifest named knote.yaml:
apiVersion: apps/v1 kind: Deployment metadata: name: knote-web labels: app: knote spec: replicas: 3 selector: matchLabels: app: knote template: metadata: labels: app: knote spec: containers: - name: knote image: your-registry-username/knote:v1.0.0 ports: - containerPort: 3000 resources: limits: cpu: “500m” memory: “512Mi” requests: cpu: “250m” memory: “256Mi” — apiVersion: v1 kind: Service metadata: name: knote-service spec: type: LoadBalancer selector: app: knote ports: - protocol: TCP port: 80 targetPort: 3000 Use code with caution. ๐ Step 3: Deploying to the Kubernetes Cluster
Apply the configuration files to your live cluster using the Kubernetes command-line interface, kubectl.
Apply the manifest to build out the structural components inside the cluster: kubectl apply -f knote.yaml Use code with caution.
Verify pod creation to ensure all three requested application instances are spinning up properly: kubectl get pods -l app=knote Use code with caution.
Track the external IP assigned by the load balancer to access the web application: kubectl get service knote-service –watch Use code with caution.
Once the load balancer displays an IP address, open it in any browser to verify that KNote is live and fully functioning. ๐ Step 4: Implementing Horizontal Autoscaling
Relying on a static number of application instances leaves a system vulnerable to traffic surges. To make KNote truly scalable, configure a Horizontal Pod Autoscaler (HPA). The HPA monitors resource usage and dynamically creates or destroys pods to match the load.
Run the following imperative command to create an automated scaling policy for your KNote deployment:
kubectl autoscale deployment knote-web –cpu-percent=70 –min=3 –max=10 Use code with caution. Understanding the HPA Policy
Target Metric: The HPA tracks average CPU consumption across all active instances. If the average CPU load climbs past 70%, the cluster initiates scaling procedures.
Lower Bound (min): The cluster guarantees a minimum baseline of 3 pods to maintain high availability, even during zero-traffic hours.
Upper Bound (max): The application can scale up to 10 pods to absorb heavy user traffic without overwhelming cluster infrastructure.
You can inspect the operational state, target thresholds, and current metrics of your auto-scaler at any time: kubectl get hpa knote-web Use code with caution. ๐งน Step 5: Clean Up Resources
To avoid incurring ongoing infrastructure fees, remove the cloud resources created during this tutorial once testing is complete:
Deploying a scalable web application with Docker and Kubernetes
Leave a Reply