service discory
kubernetes find services mainly in two ways: environment variables and DNS
- Environment Variables
kubelet pod added to each set of environment variables corresponding to each service, including simple variable {SVCNAME} _SERVICE_HOST Docker-links and the variable {SVCNAME} _PORT, variable service_name all uppercase, in dash turned underlined.
One of my svc relevant variables as follows:
SVC_MALIBU_SERVICE_HOST=172.21.39.194
SVC_MALIBU_PORT_8080_TCP_ADDR=172.21.39.194
SVC_MALIBU_PORT_8080_TCP_PORT=8080
SVC_MALIBU_SERVICE_PORT=8080
SVC_MALIBU_PORT_8080_TCP=tcp://172.21.39.194:8080
SVC_MALIBU_PORT_8080_TCP_PROTO=tcp
SVC_MALIBU_PORT=tcp://172.21.39.194:8080
Note: When using these variables in the pod, we must first create a good run svc before the pod, or pod which can not read the
- DNS
Like coredns and other cluster-aware DNS server to monitor the kubernetes api, it creates a set of dns record for the new service.
- A record
addition svc outside Headless Service, A adds a record in the form svc-name.svc-namespace.svc.cluster-domain.example of. Svc recorded value of Cluster IP.
"Headless" Service will also be added in the form of an A record svc-name.svc-namespace.svc.cluster-domain.example this name, it will resolve to a group of the Pod Service selection (selector) of the IP. The client uses round-robin policy select from this group in the IP. - SRV record
named port will create a SRV record, format _port-name._port-protocol.svc-name.svc- namespace.svc.cluster-domain.example, ordinary svc analysis result is the port number and CNAME (svc-name .svc-namespace.svc.cluster-domain.example). svc headless type parses a plurality of values, each corresponding to a port and pod CNAME (pod-name.svc-name.svc- namespace.svc.cluster-domain.example)
We in the application configuration file, write often see multiple addresses, such as zookeeper address configured zookeeper://10.0.1.11:2181?backup=10.0.1.12:2181
in k8s, create headless type of svc, will add a record for each pod in kube-dns in the $(podname).$(headless-svc-name).namespace.svc.cluster.local
value of the pod ip. Such stateful-set with the types of controllers, each pod and there will be a fixed hostname domain.
headless service
It does not require load balancing or may be used in the case of inter-pod access. It does not create a cluster ip and proxy rules. ExternalName accepts IPv4 address string, but as DNS names that contain numbers, instead of the IP address.
No selector of service
You can create your own endpoint manual, associated with the service
externalname service
Cname is to return a record, access the same service with other services, but the main difference is that redirection occurs at the DNS level, rather than through a proxy or forwarded.
service also supports configuring session affinity
External IPs
By default, the name is not created for the pod A record, PodSpec optional field hostname and subdomain, add a hostname field, pod host name will be set to the value of the hostname. Setting up subdomain, then the pod to the FQDN $(hostname).$(subdomain).namespace.svc.cluster-domain.example
. If Headless Service Pod and the Namespace in the same, and they have the same sub-domain, KubeDNS cluster service will return A record for the full legal name of the host of the Pod. ( Reference )
PodSpec.dnsPolicy Configuration
Default
: Pod inherit dns configuration from a node running.ClusterFirst
: Any DNS domain suffix cluster configuration does not match the query (for example "www.taobao.com") will be forwarded to inherit from a node upstream dns server.ClusterFirstWithHostNet
: For Pod run with hostNetwork, DNS needs to set its policy for "ClusterFirstWithHostNet", pod inside a cluster dns nameserver address.None
: It ignores the Pod Kubernetes environment, DNS settings, use the Pod Spec in dnsConfig field provides all DNS settings.
Custom pod dns service
dnsConfig field, with dnsPolicy resolv.conf file generated inside the pod.
nameservers: you can specify up to three ip address. Ip lists will be merged into the nameserver address generated from the specified DNS strategy, and remove duplicate addresses.
searches: List of DNS search domains, dns merger strategy also generated list. Kubernetes allows up to six search domain.
options: a list of selectable objects, wherein each object can have a name attribute (required) and a value attribute (optional), is also combined to generate resolv.conf file.