Deep dive into Kubernetes Endpoints: Understanding the basics and troubleshooting techniques

Kubernetes is the de facto standard for deploying, scaling, and managing containerized applications. For Kubernetes administrators, having a deep understanding of the platform’s underlying functionalities is crucial to ensure application health, scalability, and fault-tolerance. Even though the declarative nature of Kubernetes simplifies deployments and configuration management, to troubleshoot issues and optimize performance, you often need to dive deeper into the core concepts.

One such concept that’s central to Service discovery and communication within a Kubernetes cluster is Endpoints. Endpoints provide a way for Pods to locate backend services. This article explores the world of Kubernetes Endpoints, covering their inner workings, consumption methods, and troubleshooting techniques. By the end, expect to have picked up valuable insights to effectively leverage Endpoints in managing your Kubernetes deployments.

What are Kubernetes Endpoints?

Endpoints in Kubernetes are responsible for facilitating communication between cluster components. At their core, Endpoints provide a way for Pods to discover and connect to backend services. This is achieved by dynamically updating a list of network addresses associated with a particular service.

When a service is created, Kubernetes automatically initializes corresponding Endpoints and populates them with the IP addresses of the Pods backing that service. This enables Pods to discover and communicate with the backend services they depend on, without needing to hardcode IP addresses or manage service discovery manually.

Consider a web application that has multiple microservices, such as a frontend service, a user authentication service, and a database service. The frontend service, running in one Pod, needs to communicate with the authentication service running in another Pod to verify user credentials. Endpoints enable this communication by providing a stable network address for the authentication service that can be used by the frontend and other relevant services.

What are the benefits of Endpoints?

Here are some additional reasons why Endpoints are a key concept in Kubernetes:

  • Scalability and high availability: As Pods are scaled up or down, the Endpoints are automatically updated to reflect these changes. This ensures service continuity even during deployments or scaling events.
  • Abstraction layer: Endpoints abstract away the underlying Pod IP addresses. This leads to a stable network interface for the services to interact with, regardless of Pod lifecycle changes.
  • Load balancing: Endpoints allow traffic to be distributed evenly across multiple backend Pods, which improves overall system performance and reliability.

What are EndpointSlices?

EndpointSlices are a new feature introduced in Kubernetes v1.21 to address scalability and performance challenges associated with managing a large number of Endpoints. Unlike traditional Endpoints that store all Endpoint information in a single resource, EndpointSlices break down Endpoint data into smaller, more manageable slices. This improves scalability and reduces the overhead of managing Endpoint information, particularly in clusters with a high volume of Services and Pods.

For example, suppose you have a microservice that exposes Endpoints for HTTP and gRPC protocols on ports 80 and 50051, respectively. When using traditional Endpoints, all information for this Service would be stored in a single resource. However, with EndpointSlices, administrators can create separate slices for HTTP and gRPC Endpoints, each containing the relevant port numbers.

Managing Endpoints in Kubernetes

In the following sections, let’s explore how to manage Endpoints in Kubernetes.

How to create endpoints in Kubernetes

In most scenarios, Kubernetes automatically creates Endpoints when a Service is defined with a Pod selector. Manual creation of Endpoints is generally not recommended, as it bypasses the automatic discovery and management mechanisms.

However, for specific scenarios where it can’t be avoided (e.g., exposing external resources), you can define a YAML manifest for the Endpoint and then create it using this command:

kubectl apply -f <manifest-file.yaml>

Here’s a sample YAML file:

apiVersion: v1
kind: Endpoints
metadata:
name: test-service #should match the exact Service name
subsets:
- addresses:
- ip: 10.0.0.1
- ip: 10.0.0.2
ports:
- name: http
port: 80
protocol: TCP

How to fetch endpoints in Kubernetes

To fetch Endpoints in a Kubernetes cluster, use this command:

kubectl get endpoints

It will fetch a list of all endpoints currently defined in the cluster. Output should look like this:

NAME       	ENDPOINTS    	AGE
my-service 10.0.0.1:8080 1d

Alternatively, to access the Endpoints of a Service, you can use this command:

kubectl get endpoints my-service

The output will show a list of Endpoints associated with the Service.

How to update an Endpoint in Kubernetes

Similar to creation, manual updates to Endpoints are discouraged. Kubernetes automatically updates Endpoints whenever Pods are created, deleted, or their IP addresses change. If you need to modify the communication target for a Service, the recommended approach is to update the Service definition (e.g., modify Pod selectors or ports) to trigger automatic Endpoint updates.

With that said, if a manual update can’t be avoided, you can simply update the Endpoint YAML file, and then apply it again using the same kubectl command as above:

kubectl apply -f my-service-endpoints-updated.yaml

How to delete Kubernetes Endpoints

Endpoint deletion can disrupt Service functionality. However, there are scenarios where it may be necessary — for example, when cleaning up after a Service deletion. If you need to delete an Endpoint, run this command:

kubectl delete endpoints <endpoint_name>

Use the above command with caution, as it removes the specified Endpoint object.

Note: For most use cases, rely on Kubernetes' automatic Endpoint management for optimal Service discovery and resilience. Manual intervention should be reserved for troubleshooting or specific configurations.

Troubleshooting Kubernetes Endpoints

Kubernetes has been designed for fault-tolerance from the ground up. However, issues and bottlenecks are not uncommon. In the following sections, we will explore several issues that users report while working with Endpoints, along with troubleshooting advice for each.

Issue 1: A Service has no Endpoints

Description: When you check a Service's Endpoints using kubectl get endpoints <service_name>, you see an empty "Endpoints" column.

Troubleshooting steps:

  • Start by confirming that Pods matching the Service's selector are running and healthy. Use kubectl get pods to check Pod statuses. Ensure that no Pods are in a CrashLoopBackOff or Error state.
  • Double-check the Service definition's selector field. Ensure that it accurately matches the labels of your desired Pods. Typos or incorrect labels will prevent Pods from being recognized by the Service.
  • Make sure that you're checking Endpoints within the correct namespace where your Service is present. Use kubectl get endpoints -n <namespace> to specify the namespace.
  • If Pods are stuck in a Pending state, investigate issues related to the deployment. Use kubectl describe deployment <deployment_name> to identify any errors preventing Pods from getting created.
  • Ensure that your Pods have sufficient resource requests and limits allocated. Resource starvation can prevent Pods from becoming ready, leading to empty Endpoints.

Issue 2: Service traffic not reaching Pods

Description: Even though Endpoints appear healthy, your application experiences issues receiving traffic from the Service.

Troubleshooting steps:

  • Verify that the Service definition uses liveness and readiness probes to determine Pod health. Unhealthy Pods will be excluded from Endpoints, even if technically running.
  • Double-check that the Service port configuration matches the container port exposed by your Pods. Mismatched ports will prevent traffic from reaching the application.
  • Ensure that no firewall rules on worker nodes or within the Pod network policy are blocking communication on the designated port.
  • If you are using Network Policies, review the policies applied to your Pods and Service to ensure they don't inadvertently restrict inbound traffic.

Issue 3: Frequent Endpoint updates

Description: You observe unusually frequent updates to the Endpoints list, potentially impacting Service stability.

Troubleshooting steps:

  • Check whether your Pods are frequently restarting or crashing. Unstable Pods can trigger continuous updates to Endpoints. To fix the problem, you will have to identify and resolve the root cause of the Pod instability.
  • If your deployment strategy involves short Pod lifespans (e.g., canary deployments), this can naturally lead to more frequent Endpoint updates. Consider adjusting your deployment strategy if these updates are causing performance issues.
  • If a Horizontal Pod Autoscaler (HPA) is aggressively scaling Pods up and down, that can also lead to frequent Endpoint updates. Review the HPA configuration and adjust scaling parameters if needed.

Issue 4: Endpoints not updating after Pod changes

Description: Despite adding or removing Pods, the Service's Endpoints list doesn't update.

Troubleshooting steps:

  • In rare cases, the Kubernetes Endpoint controller might experience delays in updating Endpoints. This can be temporary. You can try restarting the kube-controller-manager Pod to potentially resolve the issue.
  • Occasionally, cached information on worker nodes can lead to stale Endpoints data. Restarting Pods or worker nodes can help clear the cache and trigger a refresh.

Issue 5: Stale DNS entries are impacting Service discovery

Description: Even though Endpoints are updated correctly, Pods continue to use outdated Service Endpoints due to cached DNS entries.

Troubleshooting steps:

  • On clients or applications consuming the Service, clear the DNS cache. The specific method varies depending on the operating system or application. For example, tools like dig or nslookup can be used to manage the DNS cache on Linux systems.
  • Consider lowering the Time to Live (TTL) value for the Service in your DNS configuration. A lower TTL causes clients to refresh DNS records more frequently and pick up updated Endpoints information.

Issue 6: Network policy blocking communication

Description: Network policies may be inadvertently blocking communication between Pods and the Service. This can lead to Service discovery failures.

Troubleshooting steps:

  • Review the network policies applied to the Pods, to the Service namespace, or cluster-wide. Look for rules that might be blocking traffic on the designated port or to the Service IP address.
  • If using an Ingress controller, verify that the ingress rules are configured correctly and allow traffic from the desired source to reach the Service.
  • If Pod Security Policies (PSPs) are in place, ensure that they don't restrict network access to the Service or required ports.

Issue 7: Endpoint misconfigurations

Description: Endpoint misconfigurations can occur due to incorrect network configurations or erroneous Service definitions. These misconfigurations can lead to Service discovery failures and communication issues between Pods.

Troubleshooting steps:

  • First, ensure that the selector labels specified in the Service definition YAML file match the labels of the Pods intended to be targeted by the Service.
  • Use the kubectl get endpoints <service-name> command to verify that the Endpoints listed match the IP addresses of the Pods backing the Service.
  • Check the logs of the Pods experiencing connectivity issues to identify any errors related to Service discovery or communication failures.

Issue 8: Endpoint scaling problems

Description: Scaling operations, both automated and manual, might be impacting Endpoint availability and load balancing.

Troubleshooting steps:

  • Use monitoring tools, like Kubernetes Monitoring by Site24x7, to analyze resource utilization metrics such as CPU and memory usage. Identify any resource bottlenecks that may be affecting Pod scalability and adjust resource limits or scale up strategies accordingly.
  • Review auto-scaling policies to ensure that they are configured appropriately for the workload characteristics. Adjust auto-scaling parameters such as target CPU utilization, request thresholds, or custom metrics to optimize Pod scaling behavior.
  • Perform load testing to simulate high traffic scenarios and evaluate the performance of Endpoint scaling mechanisms. Identify any scalability limitations or bottlenecks and implement optimizations to improve responsiveness and reliability.

Issue 9: Endpoint performance degradation

Description: Endpoints may experience performance degradation due to network congestion, resource contention, or inefficient load balancing algorithms. This can result in increased latency, dropped connections, or timeouts for client requests.

Troubleshooting steps:

  • Use network monitoring tools to analyze network traffic patterns and identify potential bottlenecks or congestion points. Optimize network configurations, like MTU settings or QoS (Quality of Service) policies, to improve throughput and reduce latency.
  • Evaluate the configuration of load balancers and ingress controllers to ensure that they are distributing traffic evenly across backend Endpoints. If relevant and viable, consider using external load balancers with features like session persistence or connection pooling to improve performance under high load.
  • Review Pod scheduling strategies to ensure that Pods hosting critical Endpoints are distributed evenly across nodes and have sufficient resources. If needed, implement node affinity and anti-affinity rules to optimize Pod placement and minimize resource contention.

Issue 10: Lack of Endpoint encryption

Description: Endpoints that transmit sensitive data without encryption are vulnerable to interception and data breaches.

Troubleshooting steps:

  • Configure TLS for secure communication across the cluster.
  • Regularly rotate TLS certificates and encryption keys to mitigate the risk of compromise or unauthorized access. Consider using automated certificate management solutions to streamline certificate renewal and rotation processes.

Security considerations with Kubernetes Endpoints

Endpoints can potentially introduce security vulnerabilities if not managed carefully. Here’s a breakdown of key security considerations and best practices to secure Endpoints and reduce your cluster’s attack surface.

Use Transport Layer Security (TLS) encryption

Use TLS encryption to secure communication between Pods and Services. TLS encrypts data in transit, which prevents eavesdropping and tampering to ensure confidentiality and integrity. Fortunately, Kubernetes provides built-in support for managing TLS certificates, via the certificates.k8s.io API.

Enforce granular access control with Kubernetes Role-based Access Control (RBAC)

Use Kubernetes RBAC to restrict access to Services and Endpoints. Define Roles and ClusterRoles that grant specific permissions for interacting with Services. Bind these Roles to Service accounts used by Pods or applications. Moreover, regularly review and audit RBAC policies to ensure compliance with security requirements and minimize the risk of privilege escalation.

Use network policies

Define network policies to enforce segmentation and restrict traffic flow between Pods and Services within the Kubernetes cluster. Network policies allow you to specify ingress and egress rules based on IP addresses, ports, and protocols, effectively isolating workloads and preventing unauthorized communication.

Moreover, comply with least privilege principles to limit access to only necessary Endpoints and Services. This reduces the attack surface and mitigates the risk of lateral movement by potential attackers.

Leverage Endpoint hardening techniques

Use Endpoint hardening techniques to improve the security posture of Kubernetes Endpoints. For example, you can:

  • Disable unnecessary services and protocols to minimize the attack surface and reduce the likelihood of exploitation.
  • Configure Pod security policies to enforce security controls, such as preventing privilege escalation, restricting host access, and limiting container capabilities.
  • Regularly update and patch Kubernetes components and underlying operating systems to address known vulnerabilities and mitigate emerging threats.

Monitor proactively

Implement robust monitoring and logging mechanisms to detect and respond to security incidents and anomalies. You can use tools like Kubernetes Monitoring by Site24x7 to collect and analyze metrics related to Endpoint activity, network traffic, and resource utilization. Additionally, consider configuring centralized logging solutions to aggregate logs from Kubernetes clusters and correlate events for threat detection and forensic analysis.

Additional security considerations

  • Secrets management: Store sensitive information like credentials or API keys securely using Kubernetes Secrets objects. Avoid exposing them directly within Endpoints or Pod configurations.
  • Cluster ingress security: If you are exposing Services externally through an Ingress controller, ensure that proper configurations and security measures are in place. This might involve authentication methods, rate limiting, and Web Application Firewalls (WAFs) to protect against common web application vulnerabilities.
  • Vulnerability management across the cluster: Extend vulnerability scanning beyond container images to include the Kubernetes control plane components and worker nodes. Prompt patching of cluster-wide vulnerabilities is crucial for overall security.

Conclusion

Kubernetes Endpoints are an important concept that every cluster administrator should understand and master. In this post, we covered everything you need to know about Endpoints: what they are, how they work, how to manage them, how to troubleshoot common issues, and how to make them more secure. We hope you found it useful!

To always stay on top of your cluster’s health and performance, don’t forget to try out the comprehensive Kubernetes monitoring solution by Site24x7.

Was this article helpful?

Related Articles

Write For Us

Write for Site24x7 is a special writing program that supports writers who create content for Site24x7 "Learn" portal. Get paid for your writing.

Write For Us

Write for Site24x7 is a special writing program that supports writers who create content for Site24x7 “Learn” portal. Get paid for your writing.

Apply Now
Write For Us