Step 1: Create a Simple Web Application
We'll use Node.js and Express for the web application. Here's the code for a basic server that takes a name as input and responds with "Happy Learning ".
1.1: Setup the Project
Initialize the Project
mkdir happy-learning-app cd happy-learning-app npm init -y
Install Dependencies
npm install express
Create the Server Code
Create a file named
server.js
and add the following code:const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); app.post('/greet', (req, res) => { const name = req.body.name; res.send(`Happy Learning ${name}`); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Create a Dockerfile
Create a
Dockerfile
in the project root:FROM node:14 # Create app directory WORKDIR /usr/src/app # Install app dependencies COPY package*.json ./ RUN npm install # Bundle app source COPY . . EXPOSE 3000 CMD ["node", "server.js"]
Build the Docker Image
docker build -t happy-learning-app .
Step 2: Deploy on Kubernetes Using Helm
Setup Helm and Kubernetes Cluster
Ensure you have a Kubernetes cluster running and Helm installed. If you're using a local setup like Minikube, you can start it with:
minikube start
Create Helm Chart
helm create happy-learning-chart
Modify the Helm Chart
Edit
happy-learning-chart/values.yaml
to set the image repository and tag:image: repository: happy-learning-app tag: "latest" pullPolicy: IfNotPresent service: type: ClusterIP port: 80 ingress: enabled: false
Edit the Deployment Template
Edit
happy-learning-chart/templates/deployment.yaml
to reflect the port used by our application:apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "happy-learning-chart.fullname" . }} labels: {{- include "happy-learning-chart.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: {{- include "happy-learning-chart.selectorLabels" . | nindent 6 }} template: metadata: labels: {{- include "happy-learning-chart.selectorLabels" . | nindent 8 }} spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" ports: - containerPort: 3000 livenessProbe: httpGet: path: / port: 3000 readinessProbe: httpGet: path: / port: 3000 resources: {{- toYaml .Values.resources | nindent 12 }}
Build and Push Docker Image to a Registry
If you are using Docker Hub, tag and push your image:
docker tag happy-learning-app <your-dockerhub-username>/happy-learning-app:latest docker push <your-dockerhub-username>/happy-learning-app:latest
Install the Helm Chart
Update the
values.yaml
with the appropriate image repository (your Docker Hub username) and then install the chart:helm install happy-learning happy-learning-chart
Access the Application
Find the service IP to access the application:
kubectl get svc
Use the cluster IP and the exposed port (default 80 in our
values.yaml
) to access the application.
Conclusion
You now have a simple web application that takes a name as input and responds with a greeting. This application has been containerized using Docker and deployed to a Kubernetes cluster using Helm. You can access the application by making a POST request to /greet
with a JSON payload containing the name
field.