// Module included in the following assemblies:
//
// * virt/vm_networking/virt-creating-service-vm.adoc
// * virt/virtual_machines/virt-accessing-vm-ssh.adoc

:_mod-docs-content-type: PROCEDURE
[id="virt-creating-service-cli_{context}"]
= 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 {oc-first}.

.Procedure

. Edit the `VirtualMachine` manifest to add the label for service creation:
+
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: example-vm
  namespace: example-namespace
spec:
  runStrategy: Halted
  template:
    metadata:
      labels:
        special: key <1>
# ...
----
<1> Add `special: key` to the `spec.template.metadata.labels` stanza.
+
[NOTE]
====
Labels on a virtual machine are passed through to the pod. The `special: key` label must match the label in the `spec.selector` attribute of the `Service` manifest.
====

. Save the `VirtualMachine` manifest file to apply your changes.

. Create a `Service` manifest to expose the VM:
+
[source,yaml]
----
apiVersion: v1
kind: Service
metadata:
  name: example-service
  namespace: example-namespace
spec:
# ...
  selector:
    special: key <1>
  type: NodePort <2>
  ports: <3>
    protocol: TCP
    port: 80
    targetPort: 9376
    nodePort: 30000
----
<1> Specify the label that you added to the `spec.template.metadata.labels` stanza of the `VirtualMachine` manifest.
<2> Specify `ClusterIP`, `NodePort`, or `LoadBalancer`.
<3> Specifies a collection of network ports and protocols that you want to expose from the virtual machine.

. Save the `Service` manifest file.
. Create the service by running the following command:
+
[source,terminal]
----
$ oc create -f example-service.yaml
----

. Restart the VM to apply the changes.

.Verification

* Query the `Service` object to verify that it is available:
+
[source,terminal]
----
$ oc get service -n example-namespace
----
