• About Us
  • Privacy & Policy
HowTo's
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
HowTo's
No Result
View All Result
Home Kubernetes

Port Forwarding in Kubernetes with kubectl port-forward

June 26, 2024
in Kubernetes, Kubernetes Commands Examples, Kubernetes Commands Tutorial, Kubernetes Tutorial
A A
0
12
SHARES
108
VIEWS
Share on FacebookShare on Twitter

Kubectl is a powerful command-line tool used to interact with Kubernetes clusters. One of its essential functionalities is port-forwarding, which allows you to access services running within a Kubernetes cluster as if they were running on your local machine. This is particularly useful for debugging, accessing web applications, or interacting with internal services without exposing them to the wider network.

Here’s how you can use kubectl port-forward with various examples:

Example 1: Forwarding a single port
To forward port 8080 of a pod named my-pod in the namespace my-namespace to your local machine:

    kubectl port-forward -n my-namespace my-pod 8080
    

This command forwards traffic from port 8080 on the pod my-pod to port 8080 on your local machine.

Example 2: Forwarding multiple ports
Forwarding ports 8080 and 8443 simultaneously from a pod named backend in the default namespace:

    kubectl port-forward backend 8080:80 8443:443
    

This command forwards port 80 of the pod backend to port 8080 locally, and port 443 to port 8443 locally.

Example 3: Forwarding to a specific IP address
Forwarding port 8080 of a pod to a specific IP address (useful for testing connectivity from other machines):

    kubectl port-forward my-pod 8080:8080 --address 0.0.0.0
    

This command makes the service available on all network interfaces of your machine, accessible via the specified IP address.

Example 4: Forwarding from a deployment
Forwarding port 80 of a deployment named frontend in the default namespace:

    kubectl port-forward deployment/frontend 8080:80
    

This command forwards traffic from the first available pod of the deployment frontend to port 8080 on your local machine.

Example 5: Forwarding from a service
Forwarding port 5432 from a service named postgres in the default namespace:

    kubectl port-forward service/postgres 5432:5432
    

This command allows you to connect to the PostgreSQL service running in Kubernetes on port 5432, locally.

Example 6: Forwarding with pod selector
Forwarding port 8080 from any pod with the label app=nginx in the namespace prod:

    kubectl port-forward -n prod pod -l app=nginx 8080:80
    

This command selects all pods with the label app=nginx in the namespace prod and forwards traffic from port 80 to port 8080 locally.

Example 7: Forwarding from a specific pod in a deployment
Forwarding port 8080 from a specific pod of a deployment named api in the namespace dev:

    kubectl port-forward -n dev pod/api-78f8c79d76-9btfc 8080:80
    

This command forwards traffic from port 80 of the pod api-78f8c79d76-9btfc to port 8080 on your local machine.

Example 8: Using random local ports
Forwarding ports from a pod named redis to random ports locally:

    kubectl port-forward redis 0:6379
    

This command forwards the default port 6379 of the pod redis to a randomly chosen port on your local machine.

Example 9: Forwarding from a statefulset
Forwarding port 3306 from a statefulset named mysql in the namespace prod:

    kubectl port-forward statefulset/mysql 3306:3306
    

This command forwards traffic from port 3306 of the MySQL statefulset mysql to port 3306 locally.

Example 10: Verbose output
Forwarding port 8080 of a pod with verbose output for debugging:

    kubectl port-forward -v=8 my-pod 8080:8080
    

This command provides detailed logging (level 8) to help diagnose any issues during the port forwarding process.

Also check similar articles.

Execute Commands in Kubernetes Pods with kubectl exec
Attach to Running Containers with kubectl attach
Retrieve Container Logs Using kubectl logs
Detailed Resource Inspection with kubectl describe
Update Node Taints with kubectl taint

Tags: KubernetesKubernetes Commands ExamplesKubernetes Commands TutorialKubernetes Tutorial
Previous Post

Execute Commands in Kubernetes Pods with kubectl exec

Next Post

Running a Kubernetes API Proxy with kubectl proxy

Related You may like!

howto

Interacting with Kubernetes Plugins using kubectl plugin

June 26, 2024
howto

Configuring kubectl and kubeconfig Files

June 26, 2024

Exploring Kubernetes API Versions with kubectl api-versions

June 26, 2024

Understanding Kubernetes API Resources with kubectl api-resources

June 26, 2024

Generating Shell Completion Code with kubectl completion

June 26, 2024

Managing Kubernetes Annotations with kubectl annotate

June 26, 2024
Next Post
howto

Running a Kubernetes API Proxy with kubectl proxy

howto

Copy Files to and from Kubernetes Containers with kubectl cp

howto

Managing Kubernetes Authorization with kubectl auth

Discussion about this post

Latest Updated

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024
howto

Find Files by Group Name with -group in find Command

August 21, 2024
howto

Locate Files by Group ID Using -gid in find Command

August 21, 2024
howto

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Trending in Week

  • howto

    Using BTRFS Subvolume for User Home Directory in Linux

    22 shares
    Share 9 Tweet 6
  • Downloading Docker Images from a Registry

    13 shares
    Share 5 Tweet 3
  • Configuring SSL Connection Mode in mysqldump

    17 shares
    Share 7 Tweet 4
  • Omit Tablespace Information in mysqldump Output

    13 shares
    Share 5 Tweet 3
  • Setting MySQL Dump Compatibility Mode

    18 shares
    Share 7 Tweet 5
  • Setting Network Buffer Length in mysqldump

    13 shares
    Share 5 Tweet 3
  • Logging out from Docker Registries

    13 shares
    Share 5 Tweet 3
  • Scheduling Nodes in Kubernetes with kubectl uncordon

    12 shares
    Share 5 Tweet 3
  • Managing Default User Creation Settings in Linux

    15 shares
    Share 6 Tweet 4
  • Using Extended INSERT Syntax in mysqldump

    12 shares
    Share 5 Tweet 3
  • About Us
  • Privacy & Policy

© 2024 All Rights Reserved. Howto.swebtools.com.

No Result
View All Result

© 2024 All Rights Reserved. Howto.swebtools.com.