Bookmark this page

Guided Exercise: Configure External Access to Virtual Machines

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

  1. As the admin user, locate and then navigate to the Red Hat OpenShift web console. Confirm that the VM is running.

    1. 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...
    2. 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".
    3. Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com. Select htpasswd_provider and log in as the admin user with redhatocp as the password.

    4. Navigate to VirtualizationVirtualMachines and then select the web-servers project. Confirm that the hello-web VM is running.

  2. Add the app: helloworld label to the hello-web VM.

    1. From the web console, select the hello-web VM, and then navigate to the YAML tab to access the YAML editor.

    2. 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 Save when done.

    3. Restart the VM so that Kubernetes re-creates an instance that includes the new label. From the web console, click ActionsRestart.

    4. 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          89s
  3. Create 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.

    1. From the web console, navigate to NetworkingServices, select the web-servers project, and then click Create Service. Complete the YAML file with the following content and then click Create.

      apiVersion: v1
      kind: Service
      metadata:
        name: helloworld
        namespace: web-servers
      spec:
        type: ClusterIP
        selector:
          app: helloworld
        ports:
          - protocol: TCP
            port: 80
            targetPort: 80

      Note

      The lab command prepared the ~/DO316/labs/network-external/solutions/helloworld-svc.yaml file so that you can compare it with your version.

    2. 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   46s
  4. Use the OpenShift web console to create a route for the helloworld service.

    1. From the web console, navigate to NetworkingRoutes.

    2. Select the web-servers project and then click Create Route.

    3. Complete the Create Route form by using the following information.

      FieldValue
      Name helloworld
      Service helloworld
      Target port 80 → 80(TCP)
    4. Click Create to create the route. The Route details page is displayed.

    5. Click the link in Location 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.

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish network-external

Revision: do316-4.14-d8a6b80