Forwarding Kubernetes Services to External IPs Using Endpoints

Harsh Sharma
2 min readMar 24, 2023

--

Kubernetes is a popular container orchestration platform that allows users to deploy, manage, and scale containerized applications. Kubernetes provides many features to help manage services, including the ability to create and manage Services, which allow for external access to a set of pods running in a cluster. By default, Services in Kubernetes are created with a ClusterIP, which allows them to be accessed from within the cluster, but not from outside of the cluster.

To enable external access to a Kubernetes service, you can use an Endpoint. An Endpoint is a Kubernetes object that maps a Service to a set of IP addresses that can be used to access the Service from outside of the cluster. In this blog post, we will discuss how to forward a Kubernetes Service to an external IP using Endpoints.

Step 1: Create a Kubernetes Service

The first step is to create a Kubernetes Service that you want to expose externally. For example, suppose you have a Service named “my-service” running in a Kubernetes cluster. You can create the Service by running the following command:

kubectl create service clusterip my-service --tcp=80:80

This will create a Service named “my-service” with a ClusterIP that maps to port 80 on the pods running in the cluster.

Step 2: Create an Endpoint

The next step is to create an Endpoint that maps the Service to an external IP address. To do this, you need to know the IP address of the machine that you want to forward traffic to. You can obtain this information by running the following command:

curl ifconfig.me

This will return the external IP address of the machine that you are running the command from.

Once you have the external IP address, you can create an Endpoint by running the following command:

kubectl create endpoint my-service --addresses=<EXTERNAL_IP> --ports=80

This will create an Endpoint named “my-service” that maps to the external IP address and port 80.

Step 3: Verify Connectivity

To verify that the Service is now accessible externally, you can run the following command:

curl <EXTERNAL_IP>

This should return a response from the pods running in the Kubernetes cluster that are associated with the “my-service” Service.

Conclusion

In this blog post, we discussed how to forward a Kubernetes Service to an external IP using Endpoints. By creating an Endpoint that maps the Service to an external IP address, you can enable external access to a Service running in a Kubernetes cluster. This can be useful for exposing services to users or other systems that are outside of the Kubernetes cluster.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Harsh Sharma
Harsh Sharma

Written by Harsh Sharma

DevOps Engineer & Developer & Cyber Security Expert

No responses yet

Write a response