Accessing Kubernetes Service from Localhost

Accessing Kubernetes Service from Localhost

This post will help you with accessing kubernetes service from localhost.

If you are interested in learning, Request you to go through the below recommended tutorial.

Hope you have running Kubernetes cluster and kubectl is already installed on your local to manage your kubernetes cluster. if you dont have existing kubernetes cluster, please refer this link How to Install & Configure Kubernetes Cluster on CentOS 7 / RHEL 7 or How to Install & Configure Kubernetes Cluster on Ubuntu 

Step 1: Find Kubernetes Service IP Address

Firstly, we need to find the IP address allocated to your kubernetes services that you want to access. Use default kubectl command with service option to find service IP address.

kubectl get services

This command will list all the running services in your Kubernetes cluster along with their IP addresses. Specifically if you want to show the service ip address of kubernetes namespace. use 'n' option along with namespace.

kubectl get services -n test-namespace

Step 2: Create Port Forwarding

Let's create port forwarding from your local machine to Kubernetes service. Use kubectl command with port-forward option.

kubectl port-forward <service-name> <local-port>:<service-port>

Where, Replace <service-name> with the name of your Kubernetes service that you want to access, <local-port> with the port number on your local machine & <service-port> with the port number of the Kubernetes service.

For example, if your service name is my-service and running on port 8080 and you want to access the Kubernetes service, Use the below kubectl command with port forward command option to create port forwarding to port 8081 on your local machine.

kubectl port-forward my-service 8081:8080

Step 3: Access Kubernetes Service

Once the port forwarding is created, it will start listening. So you can access your Kubernetes service from your local machine by just visiting the URL http://localhost:<local-port> in any of your web browser.

For example, if port-forwarding is created for port 8081. Then you can access Kubernetes service from localhost with the URL http://localhost:8081.

That's it for this post. Keep practicing and have fun. Leave your comments if any.

Related Searches and Questions asked:

  • How to Configure DNS for Applications Deployed on Kubernetes?
  • Helm: List Installed Charts
  • Helm: Render Chart Templates Locally

  • إرسال تعليق

    0 تعليقات