Questão de Service Configuration #019e3a4f
Which are the two primary modes for Service discovery within a Kubernetes cluster?
Environment variables and DNS
API Calls and LDAP
Labels and Radius
Selectors and DHCP
Explicação Oficial / Explicações da Comunidade
In Kubernetes, service discovery is the mechanism by which pods and services locate each other. The two primary modes are environment variables and DNS.
How it Works
Environment Variables: When a pod is created, Kubernetes injects a set of environment variables for each service that exists in the same namespace at the time of the pod's creation. These variables include the service's cluster IP and port, following the naming convention <SERVICE_NAME>_SERVICE_HOST and <SERVICE_NAME>_SERVICE_PORT. This allows the pod to connect to the service using these variables. However, environment variables are static and only reflect services that existed before the pod started; they do not update if new services are added later.
DNS: Kubernetes runs a DNS service (typically CoreDNS) within the cluster. Each service is automatically assigned a DNS name (e.g., my-service.my-namespace.svc.cluster.local). Pods can resolve this name to the service's cluster IP. DNS-based discovery is dynamic and works for all services, regardless of when they were created. It is the recommended approach for most use cases.
Why the Correct Answer is Right
The correct answer identifies the two primary modes: environment variables and DNS. These are the built-in mechanisms provided by Kubernetes for service discovery. Environment variables are simple and require no additional configuration, while DNS offers a more flexible and dynamic solution.
Why the Incorrect Answers are Wrong
- API Calls and LDAP: While Kubernetes exposes an API server, API calls are not a primary service discovery mode for pods. LDAP (Lightweight Directory Access Protocol) is an external directory service and not a native Kubernetes discovery mechanism.
- Labels and Radius: Labels are used for organizing and selecting objects (e.g., via selectors), but they are not a direct service discovery method. RADIUS is a networking protocol for authentication, authorization, and accounting, unrelated to Kubernetes service discovery.
- Selectors and DHCP: Selectors are used to identify which pods belong to a service, but they are part of the service definition, not a discovery mode. DHCP (Dynamic Host Configuration Protocol) is used for IP address assignment in traditional networks, not for service discovery in Kubernetes.