Configure a service and a route to establish external access for a web server that is hosted on a virtual machine.
Outcomes
Create a service of the ClusterIP type.
Confirm that the service endpoint resolves to the virtual machine instance.
Create a route to expose the virtual machine for external access.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster API is reachable. It also creates the web-servers namespace and starts a virtual machine named hello-web in that namespace.
[student@workstation ~]$ lab start network-external
Instructions
As the admin user, locate and then navigate to the Red Hat OpenShift web console. Confirm that the VM is running.
From a command line, log in to your Red Hat OpenShift cluster as the admin user.
[student@workstation ~]$ oc login -u admin -p redhatocp \
https://api.ocp4.example.com:6443
Login Successful
...output omitted...Set the network-servers project as the active project.
[student@workstation ~]$ oc project web-servers
Now using project "network-servers" on server "https://api.ocp4.example.com:6443".Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com.
Select and log in as the admin user with redhatocp as the password.
Navigate to → and then select the web-servers project.
Confirm that the hello-web VM is running.

Add the app: helloworld label to the hello-web VM.
From the web console, select the hello-web VM, and then navigate to the tab to access the YAML editor.
Add the app: helloworld label in the .spec.template.metadata.labels path.
As a reminder, Kubernetes uses this labels section that Kubernetes uses to assign the label to the virt-launcher pod.
Do not change the other labels sections.
...output omitted...
spec:
dataVolumeTemplates:
...output omitted...
template:
metadata:
creationTimestamp: null
labels:
app: helloworld
flavor.template.kubevirt.io/small: "true"
kubevirt.io/domain: hello-web
kubevirt.io/size: small
...output omitted...Click when done.

Restart the VM so that Kubernetes re-creates an instance that includes the new label. From the web console, click → .
From the command line on the workstation machine, confirm that Kubernetes re-creates the VMI resource and the virt-launcher pod.
You might have to run the command several times because it takes time for the pod to start.
[student@workstation ~]$ oc get vmi,pods
NAME AGE PHASE IP NODENAME READY
virtualmachineinstance.kubevirt.io/hello-web 89s Running 10.8.2.33 worker02 True
NAME READY STATUS RESTARTS AGE
pod/virt-launcher-hello-web-ldr5b 1/1 Running 0 89sCreate a service named helloworld with the ClusterIP type.
Use the app: helloworld label for the selector.
The helloworld service must accept traffic on TCP port 80 and forward it to the VM on port 80.
From the web console, navigate to → , select the web-servers project, and then click .
Complete the YAML file with the following content and then click .
apiVersion: v1
kind: Service
metadata:
name: helloworld
namespace: web-servers
spec:
type: ClusterIP
selector:
app: helloworld
ports:
- protocol: TCP
port: 80
targetPort: 80
The lab command prepared the ~/DO316/labs/network-external/solutions/helloworld-svc.yaml file so that you can compare it with your version.
From the command line, confirm that the helloworld service has an active endpoint.
[student@workstation ~]$ oc get endpoint
NAME ENDPOINTS AGE
helloworld 10.8.2.33:80 46sUse the OpenShift web console to create a route for the helloworld service.
From the web console, navigate to → .
Select the web-servers project and then click .
Complete the form by using the following information.
| Field | Value |
|---|---|
helloworld
| |
helloworld
| |
80 → 80(TCP)
|

Click to create the route. The page is displayed.
Click the link in to confirm that you can access the web application that is running on the VM.

The web application displays a Hello world page in a new web browser tab.