0
0
Kubernetesdevops~15 mins

kubectl port-forward for local access in Kubernetes - Mini Project: Build & Apply

Choose your learning style9 modes available
Using kubectl port-forward for Local Access
📖 Scenario: You have a Kubernetes cluster running a web application inside a pod. You want to access this application from your local computer without exposing it publicly.
🎯 Goal: Learn how to use kubectl port-forward to connect your local machine to a pod's port inside the Kubernetes cluster.
📋 What You'll Learn
A running Kubernetes cluster with a pod named exactly webapp-pod
The pod webapp-pod must have a container listening on port 8080
kubectl installed and configured to access the cluster
💡 Why This Matters
🌍 Real World
Port forwarding is useful when you want to securely access an application running inside a Kubernetes cluster without exposing it publicly. It helps developers test and debug applications locally.
💼 Career
Understanding port forwarding is important for Kubernetes administrators and developers to troubleshoot and access cluster resources safely during development and maintenance.
Progress0 / 4 steps
1
Identify the pod to forward ports
Use kubectl get pods to list all pods and confirm the pod named webapp-pod is running.
Kubernetes
Need a hint?

This command shows all pods in the current namespace. Look for webapp-pod in the list.

2
Set the local port number for forwarding
Create a shell variable called LOCAL_PORT and set it to 9090 to use as the local port for forwarding.
Kubernetes
Need a hint?

Use LOCAL_PORT=9090 to assign the port number.

3
Run kubectl port-forward command
Use kubectl port-forward to forward local port $LOCAL_PORT to port 8080 on the pod named webapp-pod. Use the syntax: kubectl port-forward pod/webapp-pod $LOCAL_PORT:8080
Kubernetes
Need a hint?

This command connects your local port 9090 to the pod's port 8080.

4
Verify local access to the application
Open a new terminal or tab and run curl http://localhost:$LOCAL_PORT to check if the application is reachable locally through the forwarded port.
Kubernetes
Need a hint?

This command sends a request to your local port 9090, which is forwarded to the pod.