Kubernetes Port Forward Command (2024)

KubernetesBeginner

Kubernetes Port Forward Command (2)

Kubernetes Port Forward Command

Practice Now

Contents

  • Introduction
  • Forwarding a Local Port to a Pod
  • Forwarding Multiple Local Ports to a Pod
  • Forwarding a Local Port to a Pod with Multiple Containers
  • Using Port-Forward with Kubernetes Services
  • Summary

Introduction

In this lab, you will learn how to use the Kubernetes port-forward command to forward a local port to a port on a pod. You will start with simple examples and gradually progress to more complex scenarios.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%%flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/port_forward("`Port-Forward`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/expose("`Expose`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/port_forward -.-> lab-18494{{"`Kubernetes Port Forward Command`"}} kubernetes/create -.-> lab-18494{{"`Kubernetes Port Forward Command`"}} kubernetes/expose -.-> lab-18494{{"`Kubernetes Port Forward Command`"}} kubernetes/apply -.-> lab-18494{{"`Kubernetes Port Forward Command`"}} end

Forwarding a Local Port to a Pod

In this step, you will learn how to forward a local port to a port on a pod.

  1. Start by creating a deployment with one replica and an Nginx container:

    kubectl create deployment nginx --image=nginx --replicas=1
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod -l app=nginx
  3. Use the kubectl port-forward command to forward a local port to the pod:

    kubectl port-forward < pod_name > 19000:80

    Replace <pod_name> with the name of the pod created in step 1, and you can get the <pod_name> with the kubectl get pod -l app=nginx command.

  4. Open a web browser and go to http://localhost:19000 to view the Nginx welcome page.

Forwarding Multiple Local Ports to a Pod

In this step, you will learn how to forward multiple local ports to a pod.

  1. Use the kubectl port-forward command to forward multiple local ports to the pod:

    kubectl port-forward 19443:443 < pod_name > 19080:80

    Replace <pod_name> with the name of the pod created in step 1, and you can get the <pod_name> with the kubectl get pod -l app=nginx command.

  2. Open another terminal and use the ss command at the Linux command line to see if ports 19080 and 19443 are on the host.

Forwarding a Local Port to a Pod with Multiple Containers

In this step, you will learn how to forward a local port to a specific container in a pod with multiple containers.

  1. Create a pod with two containers: Nginx and BusyBox:

    cat << EOF | kubectl apply -f -apiVersion: v1kind: Podmetadata: name: nginx-busyboxspec: containers: - name: nginx image: nginx - name: busybox image: busybox command: - sleep - "3600"EOF
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod nginx
  3. Use the kubectl port-forward command to forward a local port to the Nginx container:

    kubectl port-forward nginx-busybox 19001:80
  4. Open a web browser and go to http://localhost:19001 to view the Nginx welcome page.

Using Port-Forward with Kubernetes Services

In this step, you will learn how to use the kubectl port-forward command with Kubernetes services.

  1. Create a service for the Nginx deployment:

    kubectl expose deployment nginx --port=80 --type=ClusterIP
  2. Use the kubectl port-forward command to forward a local port to the service:

    kubectl port-forward service/nginx 20000:80
  3. Open a web browser and go to http://localhost:20000 to view the Nginx welcome page.

Summary

Congratulations, you have successfully completed the Kubernetes port-forward command lab! In this lab, you learned how to forward a local port to a pod, forward multiple local ports to a pod, forward a pod port to a local port, forward a local port to a specific container in a pod with multiple containers, and use the kubectl port-forward command with Kubernetes services. These skills are essential for debugging issues in a Kubernetes cluster.

topics

LinuxPythonDockerGitShellJava
Kubernetes Exec CommandIn this lab, you will learn how to use the Kubernetes exec command to execute commands inside a container running in a Kubernetes pod. You will start with simple examples and gradually progress to more complex scenarios.Role-Based Access Control on KubernetesIn a Kubernetes cluster, Role-Based Access Control (RBAC) is used to control access to resources and operations within the cluster. RBAC allows cluster administrators to define roles and permissions for users, groups, and service accounts to control access to resources and operations within the cluster. In this lab, you will learn how to use RBAC to control access to resources in a Kubernetes cluster.Running Containers in PodsIn this lab, you will learn how to run containers in Kubernetes Pods. Pods are the smallest and simplest unit in Kubernetes, and they can contain one or more containers. Running containers in Pods provides many benefits, including better resource utilization, easier scaling, and more efficient deployment.

Join Our Discord and Learn Together

Join Now
Kubernetes Port Forward Command (2024)

References

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6641

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.