Accessing a virtual machine by using its internal FQDN
You can access a virtual machine on a stable, fully qualified domain name (FQDN) by using headless services. A headless service creates DNS records for each pod instead of a virtual IP, enabling FQDN access without exposing specific ports.
If you created a VM by using the OpenShift Container Platform web console, you can find its internal FQDN listed in the Network tile on the Overview tab of the VirtualMachine details page.
Creating a headless service in a project by using the CLI
To create a headless service in a namespace, add the clusterIP: None parameter to the service YAML definition.
Prerequisites
- You have installed the OpenShift CLI (
oc).
Procedure
- Create a
Servicemanifest to expose the VM, such as the following example:yamlapiVersion: v1 kind: Service metadata: name: mysubdomain spec: selector: expose: me clusterIP: None ports: - protocol: TCP port: 1234 targetPort: 1234metadata.namedefines the name of the service. This must match thespec.subdomainattribute in theVirtualMachinemanifest file.spec.selectordefines the service selector that must match theexpose:melabel in theVirtualMachinemanifest file.spec.clusterIPdefines a headless service.spec.portsdefines the list of ports that are exposed by the service. You must define at least one port. This can be any arbitrary value as it does not affect the headless service.
- Save the
Servicemanifest file. - Create the service by running the following command:terminal
$ oc create -f headless_service.yaml
Mapping a virtual machine to a headless service by using the CLI
To connect to a virtual machine (VM) from within the cluster by using its internal fully qualified domain name (FQDN), you must first map the VM to a headless service. Set the spec.hostname and spec.subdomain parameters in the VM configuration file.
If a headless service exists with a name that matches the subdomain, a unique DNS A record is created for the VM in the form of <vm.spec.hostname>.<vm.spec.subdomain>.<vm.metadata.namespace>.svc.cluster.local.
Prerequisites
- You have installed the OpenShift CLI (
oc).
Procedure
- Edit the
VirtualMachinemanifest to add the service selector label and subdomain by running the following command:terminal$ oc edit vm <vm_name>Example
VirtualMachinemanifest file:yamlapiVersion: kubevirt.io/v1 kind: VirtualMachine metadata: name: vm-fedora spec: template: metadata: labels: expose: me spec: hostname: "myvm" subdomain: "mysubdomain" # ...spec.template.metadata.labels.exposedefines a label that must match thespec.selectorattribute of theServicemanifest that you previously created.spec.template.spec.hostnamedefines the hostname. If this attribute is not specified, the resulting DNS A record takes the form of<vm.metadata.name>.<vm.spec.subdomain>.<vm.metadata.namespace>.svc.cluster.local.spec.template.spec.subdomaindefines the subdomain. Thespec.subdomainattribute must match themetadata.namevalue of theServiceobject.
- Save your changes and exit the editor.
- Restart the VM to apply the changes.
Connecting to a virtual machine by using its internal FQDN
You can connect to a virtual machine (VM) by using its internal fully qualified domain name (FQDN).
Prerequisites
- You have installed the
virtctltool. - You have identified the internal FQDN of the VM from the web console or by mapping the VM to a headless service. The internal FQDN has the format
<vm.spec.hostname>.<vm.spec.subdomain>.<vm.metadata.namespace>.svc.cluster.local.
Procedure
- Connect to the VM console by entering the following command:terminal
$ virtctl console vm-fedora - To connect to the VM by using the requested FQDN, run the following command:terminal
$ ping myvm.mysubdomain.<namespace>.svc.cluster.localExample output:
terminalPING myvm.mysubdomain.default.svc.cluster.local (10.244.0.57) 56(84) bytes of data. 64 bytes from myvm.mysubdomain.default.svc.cluster.local (10.244.0.57): icmp_seq=1 ttl=64 time=0.029 msIn the preceding example, the DNS entry for
myvm.mysubdomain.default.svc.cluster.localpoints to10.244.0.57, which is the cluster IP address that is currently assigned to the VM.