DNS records

In order to create unique DNS records per VirtualMachineInstance, it is possible to set spec.hostname and spec.subdomain. If a subdomain is set and a headless service with a name, matching the subdomain, exists, kube-dns will create unique DNS entries for every VirtualMachineInstance which matches the selector of the service. Have a look at the DNS for Services and Pods documentation for additional information.

The following example consists of a VirtualMachine and a headless Service which matches the labels and the subdomain of the VirtualMachineInstance:

  1. apiVersion: kubevirt.io/v1alpha3
  2. kind: VirtualMachineInstance
  3. metadata:
  4. name: vmi-fedora
  5. labels:
  6. expose: me
  7. spec:
  8. hostname: "myvmi"
  9. subdomain: "mysubdomain"
  10. domain:
  11. devices:
  12. disks:
  13. - disk:
  14. bus: virtio
  15. name: containerdisk
  16. - disk:
  17. bus: virtio
  18. name: cloudinitdisk
  19. resources:
  20. requests:
  21. memory: 1024M
  22. terminationGracePeriodSeconds: 0
  23. volumes:
  24. - name: containerdisk
  25. containerDisk:
  26. image: kubevirt/fedora-cloud-registry-disk-demo:latest
  27. - cloudInitNoCloud:
  28. userDataBase64: IyEvYmluL2Jhc2gKZWNobyAiZmVkb3JhOmZlZG9yYSIgfCBjaHBhc3N3ZAo=
  29. name: cloudinitdisk
  30. ---
  31. apiVersion: v1
  32. kind: Service
  33. metadata:
  34. name: mysubdomain
  35. spec:
  36. selector:
  37. expose: me
  38. clusterIP: None
  39. ports:
  40. - name: foo # Actually, no port is needed.
  41. port: 1234
  42. targetPort: 1234

As a consequence, when we enter the VirtualMachineInstance via e.g. virtctl console vmi-fedora and ping myvmi.mysubdomain we see that we find a DNS entry for myvmi.mysubdomain.default.svc.cluster.local which points to 10.244.0.57, which is the IP of the VirtualMachineInstance (not of the Service):

  1. [fedora@myvmi ~]$ ping myvmi.mysubdomain
  2. PING myvmi.mysubdomain.default.svc.cluster.local (10.244.0.57) 56(84) bytes of data.
  3. 64 bytes from myvmi.mysubdomain.default.svc.cluster.local (10.244.0.57): icmp_seq=1 ttl=64 time=0.029 ms
  4. [fedora@myvmi ~]$ ip a
  5. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  6. link/ether 0a:58:0a:f4:00:39 brd ff:ff:ff:ff:ff:ff
  7. inet 10.244.0.57/24 brd 10.244.0.255 scope global dynamic eth0
  8. valid_lft 86313556sec preferred_lft 86313556sec
  9. inet6 fe80::858:aff:fef4:39/64 scope link
  10. valid_lft forever preferred_lft forever

So spec.hostname and spec.subdomain get translated to a DNS A-record of the form <vmi.spec.hostname>.<vmi.spec.subdomain>.<vmi.metadata.namespace>.svc.cluster.local. If no spec.hostname is set, then we fall back to the VirtualMachineInstance name itself. The resulting DNS A-record looks like this then: <vmi.metadata.name>.<vmi.spec.subdomain>.<vmi.metadata.namespace>.svc.cluster.local.