🚀 Day 4 - Deploying Your First Application in Kubernetes: A Step-by-Step Guide

🚀 Day 4 - Deploying Your First Application in Kubernetes: A Step-by-Step Guide

Welcome to Day 4 of the 100 Days Kubernetes with DevOps Challenge! 🎉

Yesterday, we set up a Kubernetes cluster with one master node and two worker nodes on three Linux VMs (Day 3). Now, let's take it a step further by deploying our first application in Kubernetes!

Interested to study about day 2 topic Evaluation and Architecture for Kubernetes

In this guide, you’ll learn how to:

✅ Deploy an application in Kubernetes using Deployments & Services

✅ Expose the application for external access

✅ Verify and troubleshoot your deployment


🎯 Step 1: Prerequisites

Before we begin, ensure you have:

✔️ A Kubernetes cluster (1 master + 2 worker nodes) configured and running

✔️ kubectl installed on your system and configured to access the cluster

✔️ Basic understanding of YAML and Kubernetes objects (I’ll explain as we go!)

If you haven't set up your cluster yet, check out Day 3's blog for a step-by-step installation guide.


🛠 Step 2: Deploy a Simple NGINX Application

We’ll deploy NGINX, a lightweight web server, inside a Kubernetes cluster. This will help us understand Deployments, Pods, and Services.

🔹 Create a Deployment

A Deployment in Kubernetes manages Pods (application instances) and ensures they are running as expected.

📌 Create a YAML file for the Deployment:

Run the following command to create a YAML file:

vi nginx-deployment.yaml        


Article content

Now, add the following YAML configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3  # Run 3 instances of the NGINX pod
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest  # Pulls the latest NGINX image from Docker Hub
        ports:
        - containerPort: 80  # Exposes port 80 inside the container        


Article content

Save the file and exit by providing the below exit command

Press Escape and Input :wq!

Now, deploy the application using:

kubectl apply -f nginx-deployment.yaml        


Article content

💡 What happens here?

  • A Deployment named nginx-deployment is created.
  • Kubernetes automatically launches 3 pods running the NGINX web server.
  • The Pods are automatically managed (if one fails, Kubernetes recreates it).


🔍 Step 3: Verify the Deployment

Check if your deployment is running:

kubectl get deployments        


Article content

Now, check the running Pods:

kubectl get pods        


Article content

Great! Your NGINX application is running inside Kubernetes 🎉.


🌍 Step 4: Expose the Application Using a Service

Right now, the Pods aren't accessible outside the cluster. We need to create a Service to expose them.

📌 Create a Service YAML file:

vi nginx-service.yaml        

Add the following configuration:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80       # Port exposed outside the cluster
      targetPort: 80 # Port inside the Pod
  type: NodePort   # Exposes the service on a static port on each Node        


Article content

Now, deploy the service:

kubectl apply -f nginx-service.yaml        


Article content

Check if the service is running:

kubectl get services        


Article content


🌎 Step 5: Access the Application

Since we used NodePort, the application is exposed on port 30200 (or another random port assigned by Kubernetes).

To access the application:

1️⃣ Find your cluster master node’s external IP:

kubectl get nodes -o wide        


Article content

2️⃣ Open your browser and visit:

http://<EXTERNAL_IP>:30200        

Note: We are not configuring any load balancers now. So, use you master node public ip address.

http://34.120.10.21:30200        

🎉 You should see the default NGINX welcome page!

Article content

🛠 Step 6: Troubleshooting Tips

If you don’t see the NGINX page:

  1. Check the status of pods & services: kubectl get pods kubectl get services
  2. Describe a pod to check logs: kubectl describe pod <POD_NAME> kubectl logs <POD_NAME>
  3. Ensure the firewall allows the NodePort (e.g., 30200).
  4. Ensure the required port are kept open in cloud provider's security group.

Firewall Configuration:

Inbound rules:

Make sure the below ports are open in your VM. Check this from Cloud Provider side.

  • Allow port 22 for SSH access.
  • Allow port 6443 if accessing the Kubernetes API externally.
  • Allow ports 30000-32767 for NodePort services.
  • Open port 30200 (or whatever NodePort is assigned) to access the application externally.

Outbound rules:

  • Allow outbound internet access if pulling images from Docker Hub (nginx:latest).


📌 Final Thoughts

🎯 Today, we successfully deployed our first Kubernetes application and exposed it to the outside world using a Service! 🚀

🔹 Key Takeaways:Deployments ensure Pods stay running and scale as needed. ✅ Services expose applications to external traffic. ✅ kubectl commands help manage and troubleshoot deployments.

📝 Next Steps: 📌 In upcoming days, we’ll explore Ingress Controllers, ConfigMaps, and more advanced Kubernetes concepts. Stay tuned!


📢 Follow the Journey!

This is Day 4 of the 100 Days Kubernetes with DevOps Challenge. Follow along as we build real-world projects step by step!

💬 Found this helpful? Let’s spark a conversation—🔁 Repost and share it with your network!

📢 Follow Bavithran M for more DevOps, Kubernetes, and cloud-native insights.

CareerByteCode

Mohammed A Gafoor Farooqui

Senior Network Security Engineer at Confidential

6mo

Possible to configure SNat with ingress or gateway API ? Whereas users will hitting the external IP of ingress or Gateway API and the ingress or Gateway API will communicate with backend using internal network IPs

Mohammed A Gafoor Farooqui

Senior Network Security Engineer at Confidential

6mo

why people are still using ingress... whereas gateway api are more capable with advance functions ?

RISWAN RAJA A

🌐 Cloud DevOps | ☁️ Azure | 🛠️ Terraform | 🐳 Docker | 🚀 Kubernetes | ⚙️ Infrastructure Automation Enthusiast | 📈 Driving Scalability & Innovation

6mo

Insightful

Deploying your first app in Kubernetes is a pivotal milestone! Remember, while NGINX is popular, exploring lesser-known apps can broaden your skill set. Try deploying a stateful application like Cassandra next for a deeper understanding of Kubernetes storage management.

Bavithran M

Senior Cloud & DevOps Engineer | AWS & Azure Certified | Kubernetes & Automation Advocate | Training | Mentoring | Uplifting IT Professionals

6mo

#connections

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics