
Steps to create Port Forwarding in Kubernetes
In this blog, we will show you the steps to create Port Forwarding in Kubernetes Environment.
REQUIREMENTS
- 2 Node Cluster ( 1 Master VM with 2 Nodes)
- Kubernetes Components
INFRASTRUCTURE OVERVIEW
- We have already installed and configured the 2 Node cluster in our demo environment.
- Please check the URL https://blog.assistanz.com/steps-to-install-kubernetes-cluster-manually-using-centos-7/ for more information.
CREATING A POD USING KUBECTL COMMAND
- Login to the kubernetes master server through putty.

- Use the below command to create a POD.
Syntax: kubectl run <pod name> --image=<image-name> --port=<port number of container application> --generator=run-pod/v1
Example: kubectl run web-pod --image=nginx --port=80 --generator=run-pod/v1

- POD created successfully.

- For more information about the generator, please check this URL https://kubernetes.io/docs/reference/kubectl/conventions/#generators
- The new POD is available on the list.

- We can also view the detailed information about the POD using describe command.
kubectl describe pods web-pod


CONFIGURE PORT FORWARDING
- Use the below command to create a Port forwarding for a container.
Syntax: kubectl port-forward <pod name> <local machine port>:<pod container port>
Example: kubectl port-forward web-pod 8888:80

- The port forwarding is in running state.

- Open a duplicate session of the master server and access the container using the command.
curl 127.0.0.1:8888

- You can able to see the nginx homepage.

- This is an easy method to verify the container ports without creating the services.
KUBERNETES LOGS
- We can verify the POD logs using the below command.
Syntax: kubectl logs <pod name>
Example: kubectl logs web-pod

- You can able to see the POD logs as shown below.

OTHER KUBERNETES COMMANDS
- Use the below command to display the POD internal IP.
kubectl get pods -o wide

- To display the POD configuration in YAML format.
Syntax: kubectl get pods <pod name> -o yaml
Example: kubectl get pods web-pod -o yaml

- To display the POD configuration in JSON format.
kubectl get pods web-pod -o json

Thanks for reading this blog. We hope it was useful for you to learn about port forwarding in the kubernetes environment.
