简体   繁体   中英

how to use Kubernetes DNS for pods?

On GKE, kube-dns is running on my nodes, I can see the docker containers.

I do have access to Services by name, which is great for all these applications where load balancing is a perfectly suitable solution, but how would I use the DNS to access individual pods?

I know I can look up specific pods in the API, but I need to update the hosts file myself, and keep watching the pod list. DNS is supposed to do that for me so how is it meant to be used within a pod?

The Kube.netes doc says the DNS info needs to be passed to the kubelet but I have no access to that on GKE that I know of, so is it just not setup that way on GKE or is there something to do to activate it?

Some of my services (zookeeper in particular) is aware of other nodes on its own, and tries to connect to them by host name (that is pod name) and that fails unless I update the hosts file myself. I would like to use the integrated DNS service for that.

Any guidance on how to do this would be appreciated.

Thanks

UPDATE

According to the docs, the format is now:

_my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster.local

See the related doc here: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pods

ORIGINAL ANSWER:

as of this date, this is actually not possible... but it is being looked at by the Kubernetes team.

See this issue : https://github.com/kubernetes/kubernetes/issues/13552

UPDATE :

DNS is available for Pods since 09/2015 See PR: https://github.com/kubernetes/kubernetes/pull/13759

in short:

This will give pods dns in the form of <podIP>.<namespace>.pod.<clusterSuffix> Currently can be disabled, but is either on for all pods or off.

Kubernetes statefulset support associate a service name, and define pod dns name by the service name.

Such as your create a zk daemonset, and a zk service, then the dns name of zk daemonset's first pod is zk-0.$(namespace).svc.cluster.local

more details see: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/

Important: The service must be “ headless ” for this to work, that is it's .spec.clusterIP must be None .

Kube.netes provides Stable Network IDs for all components of StatefulSet

Consider following example:

kind: Namespace
apiVersion: v1
metadata:
  name: mynamespace
---
apiVersion: v1
kind: Service
metadata:
  name: myservice
  namespace: mynamespace
  labels:
    app: myapp
spec:
  ports:
    - port: 80
      name: http
  type: ClusterIP
  selector:
    app: myapp
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
  namespace: mynamespace
spec:
  serviceName: myservice
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  # ...

Then you'll have following resolvable DNS entries within the k8s cluster:

  • myservice.mynamespace.svc.cluster.local for loadbalanced access to one of myapp pods through the myservice
  • myapp-0.myservice.mynamespace.svc.cluster.local for direct access to Pod 0 of myapp StatetefulSet
  • myapp-1.myservice.mynamespace.svc.cluster.local for direct access to Pod 1 of myapp StatetefulSet

Documentation: https://kube.netes.io/docs/concepts/workloads/controllers/statefulset/#stable.network-id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM