What DNS Name Can You Use to Reach the Kubernetes API Server From Within a Pod?


Heather Bennett

The Kubernetes API server is a crucial component in managing and interacting with a Kubernetes cluster. When working within a pod, you may need to communicate with the API server for various reasons. To do so, you need to know the DNS name that can be used to reach the Kubernetes API server from within a pod.

Understanding the DNS Name

In Kubernetes, pods have their own DNS resolver to facilitate communication between them and other resources within the cluster. This DNS resolver allows pods to resolve service names and other endpoints using domain names.

By default, when you create a Kubernetes cluster, there is a special DNS service called kube-dns or CoreDNS deployed in the cluster. This service provides DNS resolution for all internal resources including pods, services, and API endpoints.

Finding the DNS Name

To find the DNS name that can be used to reach the Kubernetes API server from within a pod, you can use the environment variables injected into each pod by Kubernetes.

One of these environment variables is KUBERNETES_SERVICE_HOST, which contains the IP address of the Kubernetes API server. Another environment variable is KUBERNETES_SERVICE_PORT, which contains the port number on which the API server is listening.

You can combine these two variables to construct the DNS name in this format:

  • DNS Name: kubernetes.default.svc.cluster.local

The full URL for accessing the Kubernetes API server from within a pod would be:


Example Usage

Let’s say you have a pod running in your Kubernetes cluster and you want to make an API call to the Kubernetes API server. You can use the DNS name mentioned above to reach the API server.

Here’s an example of how you can use this DNS name in a code snippet:

import requests

api_url = f"https://${{KUBERNETES_SERVICE_HOST}}:${{KUBERNETES_SERVICE_PORT}}/api/v1/namespaces/default/pods"
response = requests.get(api_url)

if response.status_code == 200:
    print("API call successful!")
    print("API call failed!")

In this example, we are using the requests library in Python to make an HTTP GET request to the Kubernetes API server. The URL is constructed using the DNS name and environment variables provided by Kubernetes.


The DNS name kubernetes.local can be used to reach the Kubernetes API server from within a pod. By leveraging the environment variables injected by Kubernetes, you can easily construct the URL required to interact with the API server and perform various operations.

This knowledge is valuable when developing applications or scripts that need to interact with the Kubernetes API server from within a pod, allowing seamless integration and management of your Kubernetes cluster.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy