|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * virt/virtual_machines/vm_networking/virt-using-the-default-pod-network-with-virt.adoc |
| 4 | + |
| 5 | +[id="virt-creating-a-service-from-a-virtual-machine_{context}"] |
| 6 | + |
| 7 | += Creating a service from a virtual machine |
| 8 | + |
| 9 | +Create a service from a running virtual machine by first creating a `Service` object to expose the virtual machine. |
| 10 | + |
| 11 | +The `ClusterIP` service type exposes the virtual machine internally, within the cluster. The `NodePort` or `LoadBalancer` service types expose the virtual machine externally, outside of the cluster. |
| 12 | + |
| 13 | +This procedure presents an example of how to create, connect to, and expose a `Service` object of `type: ClusterIP` as a virtual machine-backed service. |
| 14 | + |
| 15 | +[NOTE] |
| 16 | +==== |
| 17 | +`ClusterIP` is the default service `type`, if the service `type` is not specified. |
| 18 | +==== |
| 19 | + |
| 20 | +.Procedure |
| 21 | + |
| 22 | +. Edit the virtual machine YAML as follows: |
| 23 | ++ |
| 24 | + |
| 25 | +[source,yaml] |
| 26 | +---- |
| 27 | +apiVersion: kubevirt.io/v1alpha3 |
| 28 | +kind: VirtualMachine |
| 29 | +metadata: |
| 30 | + name: vm-ephemeral |
| 31 | + namespace: example-namespace |
| 32 | +spec: |
| 33 | + running: false |
| 34 | + template: |
| 35 | + metadata: |
| 36 | + labels: |
| 37 | + special: key <1> |
| 38 | + spec: |
| 39 | + domain: |
| 40 | + devices: |
| 41 | + disks: |
| 42 | + - name: containerdisk |
| 43 | + disk: |
| 44 | + bus: virtio |
| 45 | + - name: cloudinitdisk |
| 46 | + disk: |
| 47 | + bus: virtio |
| 48 | + interfaces: |
| 49 | + - masquerade: {} |
| 50 | + name: default |
| 51 | + resources: |
| 52 | + requests: |
| 53 | + memory: 1024M |
| 54 | + networks: |
| 55 | + - name: default |
| 56 | + pod: {} |
| 57 | + volumes: |
| 58 | + - name: containerdisk |
| 59 | + containerDisk: |
| 60 | + image: kubevirt/fedora-cloud-container-disk-demo |
| 61 | + - name: cloudinitdisk |
| 62 | + cloudInitNoCloud: |
| 63 | + userData: | |
| 64 | + #!/bin/bash |
| 65 | + echo "fedora" | passwd fedora --stdin |
| 66 | +---- |
| 67 | +<1> Add the label `special: key` in the `spec.template.metadata.labels` section. |
| 68 | ++ |
| 69 | + |
| 70 | +[NOTE] |
| 71 | +==== |
| 72 | +Labels on a virtual machine are passed through to the pod. The labels on |
| 73 | +the `VirtualMachine`, for example `special: key`, must match the labels in |
| 74 | +the `Service` YAML `selector` attribute, which you create later |
| 75 | +in this procedure. |
| 76 | +==== |
| 77 | + |
| 78 | +. Save the virtual machine YAML to apply your changes. |
| 79 | + |
| 80 | +. Edit the `Service` YAML to configure the settings necessary to create and expose the `Service` object: |
| 81 | ++ |
| 82 | + |
| 83 | +[source,yaml] |
| 84 | +---- |
| 85 | +apiVersion: v1 |
| 86 | +kind: Service |
| 87 | +metadata: |
| 88 | + name: vmservice <1> |
| 89 | + namespace: example-namespace <2> |
| 90 | +spec: |
| 91 | + ports: |
| 92 | + - port: 27017 |
| 93 | + protocol: TCP |
| 94 | + targetPort: 22 <3> |
| 95 | + selector: |
| 96 | + special: key <4> |
| 97 | + type: ClusterIP <5> |
| 98 | +---- |
| 99 | +<1> Specify the `name` of the service you are creating and exposing. |
| 100 | +<2> Specify `namespace` in the `metadata` section of the `Service` YAML that corresponds to the `namespace` you specify in the virtual machine YAML. |
| 101 | +<3> Add `targetPort: 22`, exposing the service on SSH port `22`. |
| 102 | +<4> In the `spec` section of the `Service` YAML, add `special: key` to the `selector` attribute, which corresponds to the `labels` you added in the virtual machine YAML configuration file. |
| 103 | +<5> In the `spec` section of the `Service` YAML, add `type: ClusterIP` for a |
| 104 | +ClusterIP service. To create and expose other types of services externally, outside of the cluster, such as `NodePort` and `LoadBalancer`, replace |
| 105 | +`type: ClusterIP` with `type: NodePort` or `type: LoadBalancer`, as appropriate. |
| 106 | ++ |
| 107 | + |
| 108 | +. Save the `Service` YAML to store the service configuration. |
| 109 | +. Create the `ClusterIP` service: |
| 110 | ++ |
| 111 | + |
| 112 | +[source,terminal] |
| 113 | +---- |
| 114 | +$ oc create -f <service_name>.yaml |
| 115 | +---- |
| 116 | + |
| 117 | ++ |
| 118 | +. Start the virtual machine. If the virtual machine is already running, restart it. |
| 119 | ++ |
| 120 | + |
| 121 | ++ |
| 122 | +. Query the `Service` object to verify it is available and is configured with type `ClusterIP`. |
| 123 | ++ |
| 124 | + |
| 125 | +.Verification steps |
| 126 | +* Run the `oc get service` command, specifying the `namespace` that you reference in the virtual machine and `Service` YAML files. |
| 127 | ++ |
| 128 | + |
| 129 | +[source, terminal] |
| 130 | +---- |
| 131 | +$ oc get service -n example-namespace |
| 132 | +---- |
| 133 | ++ |
| 134 | + |
| 135 | +.Example output |
| 136 | +[source, terminal] |
| 137 | +---- |
| 138 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 139 | +vmservice ClusterIP 172.30.3.149 <none> 27017/TCP 2m |
| 140 | +---- |
| 141 | ++ |
| 142 | + |
| 143 | +** As shown from the output, `vmservice` is running. |
| 144 | +** The `TYPE` displays as `ClusterIP`, as you specified in the `Service` YAML. |
| 145 | + |
| 146 | +. Establish a connection to the virtual machine that you want to use to back your service. Connect from an object inside the cluster, such as another virtual machine. |
| 147 | ++ |
| 148 | + |
| 149 | +.. Edit the virtual machine YAML as follows: |
| 150 | ++ |
| 151 | + |
| 152 | +[source,yaml] |
| 153 | +---- |
| 154 | +apiVersion: kubevirt.io/v1alpha3 |
| 155 | +kind: VirtualMachine |
| 156 | +metadata: |
| 157 | + name: vm-connect |
| 158 | + namespace: example-namespace |
| 159 | +spec: |
| 160 | + running: false |
| 161 | + template: |
| 162 | + spec: |
| 163 | + domain: |
| 164 | + devices: |
| 165 | + disks: |
| 166 | + - name: containerdisk |
| 167 | + disk: |
| 168 | + bus: virtio |
| 169 | + - name: cloudinitdisk |
| 170 | + disk: |
| 171 | + bus: virtio |
| 172 | + interfaces: |
| 173 | + - masquerade: {} |
| 174 | + name: default |
| 175 | + resources: |
| 176 | + requests: |
| 177 | + memory: 1024M |
| 178 | + networks: |
| 179 | + - name: default |
| 180 | + pod: {} |
| 181 | + volumes: |
| 182 | + - name: containerdisk |
| 183 | + containerDisk: |
| 184 | + image: kubevirt/fedora-cloud-container-disk-demo |
| 185 | + - name: cloudinitdisk |
| 186 | + cloudInitNoCloud: |
| 187 | + userData: | |
| 188 | + #!/bin/bash |
| 189 | + echo "fedora" | passwd fedora --stdin |
| 190 | +---- |
| 191 | ++ |
| 192 | + |
| 193 | +.. Run the `oc create` command to create a second virtual machine, where `file.yaml` is the name of the virtual machine YAML: |
| 194 | ++ |
| 195 | + |
| 196 | +[source,terminal] |
| 197 | +---- |
| 198 | +$ oc create -f <file.yaml> |
| 199 | +---- |
| 200 | ++ |
| 201 | + |
| 202 | +.. Start the virtual machine. |
| 203 | + |
| 204 | +.. Connect to the virtual machine by running the following `virtctl` command: |
| 205 | ++ |
| 206 | + |
| 207 | +[source,terminal] |
| 208 | +---- |
| 209 | +$ virtctl -n example-namespace console <new-vm-name> |
| 210 | +---- |
| 211 | ++ |
| 212 | + |
| 213 | +[NOTE] |
| 214 | +==== |
| 215 | +For service type `LoadBalancer`, use the `vinagre` client to connect your |
| 216 | +virtual machine by using the public IP and port. |
| 217 | +External ports are dynamically allocated when using service type |
| 218 | +`LoadBalancer`. |
| 219 | +==== |
| 220 | ++ |
| 221 | + |
| 222 | +.. Run the `ssh` command to authenticate the connection, where `172.30.3.149` is the ClusterIP of the service and `fedora` is the user name of the virtual machine: |
| 223 | ++ |
| 224 | + |
| 225 | +[source,terminal] |
| 226 | +---- |
| 227 | +$ ssh fedora@172.30.3.149 -p 27017 |
| 228 | +---- |
| 229 | ++ |
| 230 | + |
| 231 | +.Verification steps |
| 232 | +* You receive the command prompt of the virtual machine backing the service you want to expose. You now have a service backed by a running virtual machine. |
0 commit comments