Kubernetes supports 2 primary modes of finding a Service - environment variables and DNS.
When a Pod
is run on a Node
, the kubelet adds a set of environment variables for each active Service
. It supports both Docker links compatible variables (see makeLinkVariables) and simpler {SVCNAME}_SERVICE_HOST
and {SVCNAME}_SERVICE_PORT
variables, where the Service name is upper-cased and dashes are converted to underscores.
从1.13起,k8s内置了CoreDNS
CoreDNS: DNS and Service Discovery
If you have a Service
called "my-service"
in a Kubernetes Namespace
called "my-ns"
, a DNS record for "my-service.my-ns"
is created. Pods
which exist in the "my-ns"
Namespace
should be able to find it by simply doing a name lookup for "my-service"
. Pods
which exist in other **Namespaces
**must qualify the name as "my-service.my-ns"
. The result of these name lookups is the cluster IP.
Kubernetes also supports DNS SRV (service) records for named ports. If the "my-service.my-ns"
**Service
**has a port named "http"
with protocol TCP
, you can do a DNS SRV query for "_http._tcp.my-service.my-ns"
to discover the port number for "http"
.