🎯 Goal

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

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 

🧱 Step 2: Apply the Pod YAML

kubectl apply -f nginx-pod.yaml 

✅ Expected output:

pod/nginx-pod created 

🔍 Step 3: Verify the Pod

kubectl get pods -o wide 

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 

🌐 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 

🚀 Step 5: Apply the Service

kubectl apply -f nginx-service.yaml 

✅ Output:

service/nginx-service created 

🧩 Step 6: Verify the Service

kubectl get svc 

Example output:

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

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 

Once inside the pod shell, run:

wget -qO- http://nginx-service 

You should see the default NGINX welcome page HTML:

<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ... </html> 

Exit the shell:

exit 

2

🧹 Cleanup (Optional)

kubectl delete pod test-pod kubectl delete svc nginx-service kubectl delete pod nginx-pod 

3

🖼️ Architecture Visualization

Here’s the simple flow:

[test-pod] --> [ClusterIP Service: nginx-service] --> [nginx-pod] 

🧠 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


Source: DEV Community.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.