mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-05 12:46:18 +01:00
80 lines
1.8 KiB
Plaintext
80 lines
1.8 KiB
Plaintext
// 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 command line
|
|
|
|
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.
|
|
|
|
.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:
|
|
running: false
|
|
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>
|
|
----
|
|
<1> Specify the label that you added to the `spec.template.metadata.labels` stanza of the `VirtualMachine` manifest.
|
|
<2> Specify `ClusterIP`, `NodePort`, or `LoadBalancer`.
|
|
|
|
. 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
|
|
----
|