DEV Community

Cover image for Scenario 1: Deploy a simple NGINX Pod and expose it via a ClusterIP Service in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

Scenario 1: Deploy a simple NGINX Pod and expose it via a ClusterIP Service in Kubernetes

🎯 Goal

Deploy an NGINX Pod, then expose it internally using a ClusterIP Service so other Pods inside the cluster can access it.


🪄 Step 1: Create the NGINX Pod YAML

Create a file named nginx-pod.yaml

You can use Cloud Shell editor (code nginx-pod.yaml) or nano nginx-pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

🧱 Step 2: Apply the Pod YAML

kubectl apply -f nginx-pod.yaml
Enter fullscreen mode Exit fullscreen mode

✅ Expected output:

pod/nginx-pod created
Enter fullscreen mode Exit fullscreen mode

🔍 Step 3: Verify the Pod

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

You should see:

NAME         READY   STATUS    RESTARTS   AGE   IP           NODE
nginx-pod    1/1     Running   0          15s   10.32.0.12   gke-mycluster-default-pool-xxxx
Enter fullscreen mode Exit fullscreen mode

🌐 Step 4: Create a ClusterIP Service

Now, let’s expose it internally with a Service.

Create a file named nginx-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
Enter fullscreen mode Exit fullscreen mode

🚀 Step 5: Apply the Service

kubectl apply -f nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

✅ Output:

service/nginx-service created
Enter fullscreen mode Exit fullscreen mode

🧩 Step 6: Verify the Service

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Example output:

NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx-service    ClusterIP   10.12.8.47     <none>        80/TCP    10s
Enter fullscreen mode Exit fullscreen mode

This means your Service is created inside the cluster — other Pods can now reach NGINX via http://nginx-service:80.

1


🧪 Step 7: Test the Service (Internally)

We’ll spin up a temporary test Pod inside the cluster and use curl:

kubectl run test-pod --image=busybox:latest --restart=Never -it -- /bin/sh
Enter fullscreen mode Exit fullscreen mode

Once inside the pod shell, run:

wget -qO- http://nginx-service
Enter fullscreen mode Exit fullscreen mode

You should see the default NGINX welcome page HTML:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
</html>
Enter fullscreen mode Exit fullscreen mode

Exit the shell:

exit
Enter fullscreen mode Exit fullscreen mode

2


🧹 Cleanup (Optional)

kubectl delete pod test-pod
kubectl delete svc nginx-service
kubectl delete pod nginx-pod
Enter fullscreen mode Exit fullscreen mode

3


🖼️ Architecture Visualization

Here’s the simple flow:

[test-pod] --> [ClusterIP Service: nginx-service] --> [nginx-pod]
Enter fullscreen mode Exit fullscreen mode

🧠 Service discovery: Kubernetes DNS automatically creates an internal DNS record
→ nginx-service.default.svc.cluster.local → resolves to the Pod’s IP.


🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.


— Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)