Steps to create PORT forwarding
In this blog, we will show you the steps to create Port Forwarding in the Kubernetes Environment.
REQUIREMENTS
- 2 Node Cluster ( 1 Master VM with 2 Nodes)
- Kubernetes Components
INFRASTRUCTURE OVERVIEW
- Firstly, we need to install and configure the 2-node cluster in our environment. We had already installed and configured the 2 Node cluster in our demo environment. To learn more check our blog post.
Creating a POD using the 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 was created successfully.
- For more information about the generator, click here.
- The new POD is available on the list.
- We can also view the detailed information about the POD using the 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 a 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 K8s 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
Thank you for taking the time to read our blog on “How to create the Port forwarding in the kubernetes?“. We hope you found the information valuable and insightful. If you find any issues with the information provided in this blog don’t hesitate to contact us (info@assistanz.com).
Optimize your kubernetes and never lose a valuable customer again!
Our mission is to ensure that your containers remain lightning-fast and protected at all times by monitoring and maintaining it 24×7 by our experts.
Related Post
How to create POD with labels by using Kubectl and YAML?
How to create a custom namespace in the K8s environment?