Skip to main content
In this lesson, you’ll learn how to use the kubectl port-forward command—a powerful technique that enables local access to internal Kubernetes cluster resources without exposing them to the internet. This method is ideal for testing, debugging, or accessing services in development and user acceptance testing environments. Port forwarding is particularly useful when working with ClusterIP services, such as a typical “notes-app-deployment” running in the “uat” (user acceptance testing) namespace. Since ClusterIP services are only accessible within the cluster, port forwarding creates a secure tunnel from your local machine directly to the service.

Viewing Cluster Services

First, you can list all services across namespaces with the following command:
Here, the “notes-app-deployment” service in the uat namespace is configured as ClusterIP, making it accessible only from within the cluster.

Creating a Secure Tunnel with Port Forwarding

To securely access the internal service, you can use the kubectl port-forward command. This command establishes a local tunnel where traffic from a specified local port is redirected to the service’s port in the cluster. For example, to forward local port 8000 to the service’s port 80 (which then directs traffic to the pod target port 3000), run the following command:
Once the port forward is active, you can access the notes application locally. For instance, execute:
The response might resemble a simple HTML page:
Remember that port forwarding is temporary—it lasts as long as your terminal session remains active.

Verifying Service Configuration

To inspect the service configuration and verify its port settings, retrieve the YAML definition with the following command:
The command outputs a detailed YAML configuration similar to:
In this YAML, notice that the service listens on port 80 and forwards the traffic to port 3000 on the pod.

Accessing Other Internal Services

Similarly, you can port-forward other internal services. For example, if you need secure access to the Kubernetes Dashboard (a ClusterIP service), forward a local port to the Dashboard’s port as shown:
After running the command, open your web browser and navigate to http://localhost:8000 to access the Kubernetes Dashboard securely.

Additional Use: Service Account Tokens

For added flexibility, you can also create service account tokens when needed. This can be accomplished with the following command:
Use service account tokens cautiously—ensure they are stored securely and only used in trusted contexts.

Summary

In summary, the kubectl port-forward command is an essential tool that enables a secure, temporary tunnel from your local machine directly to services or pods within a Kubernetes cluster. This technique is widely used for debugging, testing APIs, or accessing internal services without the need for an external load balancer. For further reading on Kubernetes concepts and commands, check out these resources: This concludes our comprehensive explanation of kubectl port-forward and its many practical applications in managing Kubernetes clusters.

Watch Video