10.5.3. Creating a service by using the CLI
You can create a service and associate it with a virtual machine (VM) by using the command line.
Prerequisites
- You configured the cluster network to support the service.
-
You have installed the OpenShift CLI (
oc).
Procedure
Edit the
VirtualMachinemanifest to add the label for service creation. Addspecial: keyto thespec.template.metadata.labelsstanza:apiVersion: kubevirt.io/v1 kind: VirtualMachine metadata: name: example-vm namespace: example-namespace spec: runStrategy: Halted template: metadata: labels: special: key # ...注記Labels on a virtual machine pass through to the pod. The
special: keylabel must match the label in thespec.selectorattribute of theServicemanifest.-
Save the
VirtualMachinemanifest file to apply your changes. Create a
Servicemanifest to expose the VM:apiVersion: v1 kind: Service metadata: name: example-service namespace: example-namespace spec: # ... selector: special: key type: NodePort ports: protocol: TCP port: 80 targetPort: 9376 nodePort: 30000-
spec.selectordefines the label that you added to thespec.template.metadata.labelsstanza of theVirtualMachinemanifest. -
spec.typedefines the type of service by the way it is exposed. Choose one ofClusterIP,NodePort, orLoadBalancer. -
spec.portsdefines a collection of network ports and protocols to expose from the virtual machine.
-
-
Save the
Servicemanifest file. Create the service by running the following command:
$ oc create -f example-service.yaml- Restart the VM to apply the changes.
Verification
Query the
Serviceobject to verify that it is available:$ oc get service -n example-namespace