Updated April 15, 2023
Introduction to Kubernetes Endpoints
Kubernetes Endpoints are defined as to where the Kubernetes that will create the endpoints automatically which are known as endpoints an object that has a similar name as the service and is stored. In general, we can define the Kubernetes endpoints which get or store one or more IP addresses of pods that are assigned to it dynamically along with ports also. Therefore, the Kubernetes endpoints can be viewed using the kubcetl software, and the proxies will be directed to these IP addresses and ports of the pods which are consequently assigned whenever the service receives the requests.
What are Kubernetes Endpoints?
Endpoints in Kubernetes is a resource to track the IP addresses of the objects or pods which are dynamically assigned to it and which works as a service selector which matches a pod label by adding the IP addresses to the endpoints and these points can be viewed using software kubectl get endpoints.
Therefore, we can generally say these endpoints act as resources which are an abstraction that can be linked to the services and it will define the list of endpoints that implements the service. Whenever we want to use endpoint resources we need to import these resources using namespace and name which must be uniquely defined. Any service that has endpoints objects that holds IP addresses of pods and whenever these pods will be added or removed then these endpoints object gets updated, sent across the entire network again and also consume every node which may further result in lots of traffic in the network which requires hard labor for each node.
Kubernetes Endpoints Example
In this, we will see two different ways of using this Kubernetes endpoint examples one with importing the kuberetes_endpoints resource and the other way executing on the kubectl terminal both gives similar outputs.
Let’s see the usage of kubernetes_endpoints resource and the example with sample code is as shown below:
resource "kubernetes_endpoints" "example" {
metadata {
name = "example"
}
subset {
address {
ip = "10.0.3.4"
}
address {
ip = "10.0.3.5"
}
port {
name = "http"
port = 80
protocol = "TCP"
}
port {
name = "https"
port = 443
protocol = "TCP"
}
}
subset {
address {
ip = "10.0.2.4"
}
address {
ip = "10.0.2.5"
}
port {
name = "http"
port = 80
protocol = "TCP"
}
port {
name = "https"
port = 733
protocol = "TCP"
}
}
}
resource "kubernetes_service" "example" {
metadata {
name = "${kubernetes_endpoints.example.metadata.0.name}"
}
spec {
port {
port = 8080
target_port = 80
}
port {
port = 8443
target_port = 443
}
}
}
The above is the sample code in which we are using Kubernetes endpoints resources which we can see using the above endpoints that are using arguments and attributes which are used to track the IP addresses of ports that are used for implementing services. The above code uses arguments such as annotations, labels, name, namespace, etc, and attributes such as generations, IP, port, protocol, node_name, etc. The above sample usage can be imported using its name and namespace for example we can see in the above code as shown below:
$ terraform import kubernetes_endpoints.example default/ terraform-example-name
Now we will see an example Kubernetes services uses same attributes and arguments as in the above sample code as in the below we will see as soon as pod appear labels matching starts the traffic begins and therefore the endpoints list of this service will be obtained by adding an IP address of the pod so the example can be created similarly as shown in the below code.
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx
ports:
- name: web
containerPort: 80
protocol: TCP
--- apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
Here, we can see we have created a pod and service with the default type of cluster IP, therefore for Kubernetes endpoints when we import and run the below command in the kubetcl terminal we get the below attributes along with their results.
In this, above terminal, we can see few attributes such as name: which provides the name of endpoint resource, namespace: defines the space within the name of the provided endpoint resource, labels: it contains keys and values to organize and categorize the endpoint resources, annotations: which also consists of key-value that is stored with endpoints resource that is used to store arbitrary metadata, selectors, IP’s, port, endpoints, targetPort, metadata, etc.
Kubernetes Custom Endpoints
This usually is an extension of Kubernetes API which are not installed during Kubernetes defaults and this represents the customization of any particular Kubernetes endpoints installation and these are used for building Kubernetes functions which makes Kubernetes more modular. Therefore, the custom endpoint can create endpoints that will be pointed to any pointed resources. So let us see the sample example below:
kind: Service
apiVersion: av1
metadata:
name: extrn-svc
spec:
ports:
- name: ex
protocol: TCP
port: 80
targetPort: 80
Then without specifying the selector attribute we escribe the endpoints object as shown below:
kind: Endpoints
apiVersion: av1
metadata:
name: extrn-svc
subsets:
- addresses:
- ip: 139.59.205.180
ports:
- port: 80
name: ex
In the above code, we can see name must always be the same for both service and endpoints, addresses here refer to send traffic. Therefore, to create them and check for service and its endpoints as shown in the below terminal where we again get same attributes as in the previous example but in this, we also get another extra attribute called Endpoints which provides the IP address.
There are many other different commands in Kubernetes Endpoints to check for working services, curl installation in a pod, checking for available services using its name, etc. Also, we can see in the above terminal we can see there is an attribute Endpoints which provides the IP address of resources of service.
Conclusion
In this article, we conclude that Kubernetes Endpoints are used as resources in the Kubernetes service to keep track of IP addresses of pods or ports. In this article, we have seen examples of how we get endpoints where kubectl software describes for creation of endpoints which creates a collection of endpoints that implements the actual service.
Recommended Articles
We hope that this EDUCBA information on “Kubernetes Endpoints” was beneficial to you. You can view EDUCBA’s recommended articles for more information.