Hi devs,
In my previous post, I introduced Kubernetes and its powerful features. Now, I want to take a more hands-on approach by walking you through deploying a simple web application. This will help solidify your understanding of Kubernetes concepts while giving you a practical experience.
What We’ll Deploy
We’ll create a basic Nginx web server that serves a static HTML page. This project will demonstrate how to deploy an application, expose it, and manage it within a Kubernetes cluster.
Setting Up Your Environment
Before we begin, ensure you have Minikube and kubectl installed and running on your machine. If you haven’t set them up yet, refer to the installation guides for Minikube and kubectl.
Step 1: Start Minikube
If you haven’t started Minikube yet, run the following command:
minikube start
Step 2: Create a Simple HTML File
Let’s create a basic HTML file that our Nginx server will serve. Create a directory for your project and add an index.html
file:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Kubernetes Nginx App</title>
</head>
<body>
<h1>Hello, Kubernetes!</h1>
<p>This is a simple Nginx web server running in a Kubernetes cluster.</p>
</body>
</html>
Step 3: Create a Dockerfile
Now, create a Dockerfile
to build an image that serves this HTML page using Nginx:
# Use the official Nginx image
FROM nginx:alpine
# Copy the HTML file into the Nginx server
COPY index.html /usr/share/nginx/html/index.html
Step 4: Build the Docker Image
Next, build the Docker image. Run this command in the same directory as your Dockerfile
and index.html
:
docker build -t my-nginx-app .
Step 5: Create a Deployment in Kubernetes
Now let’s create a Kubernetes deployment to manage our Nginx application. Create a file named nginx-deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: my-nginx-app
ports:
- containerPort: 80
Step 6: Deploy to Kubernetes
Apply the deployment using kubectl
:
kubectl apply -f nginx-deployment.yaml
Step 7: Expose the Deployment
Now that we have our Nginx deployment running, we need to expose it so we can access it from outside the cluster. Create a service by running:
apiVersion: v1
kind: Service
metadata:
name: my-nginx-service
spec:
type: NodePort
selector:
app: my-nginx
ports:
- port: 80
targetPort: 80
nodePort: 30001
Apply the service configuration:
kubectl apply -f nginx-service.yaml
Step 8: Access Your Application
Now, you can access your Nginx application by navigating to http://localhost:30001
in your web browser. You should see the "Hello, Kubernetes!" message from your HTML file!
Step 9: Scaling Your Application
If you want to test scaling, you can easily increase the number of replicas. For example, to scale to 4 replicas:
kubectl scale deployment my-nginx-deployment --replicas=4
You can check the status of your deployment and see the new replicas:
kubectl get deployments
Conclusion
This hands-on project demonstrates how easy it is to deploy a simple application using Kubernetes. From creating Docker images to managing deployments and services, you now have a foundational understanding of how to work with K8s.
If you have any questions or want to share your own experiences with Kubernetes, feel free to drop a comment! Happy deploying!
Keep coding
Top comments (0)