Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Build and deploy image mode for RHEL on OpenShift Virtualization

November 11, 2024
Alessandro Rossi
Related topics:
ContainersDeveloper ToolsEdge computingVirtualization
Related products:
Image mode for Red Hat Enterprise LinuxRed Hat Enterprise LinuxRed Hat OpenShiftRed Hat OpenShift Virtualization

Share:

    Image mode for Red Hat Enterprise Linux (RHEL), introduced as a tech preview in RHEL 9.4, offers a streamlined way to build, deploy, and maintain your operating system using container images. In this article, we will explore how to create and deploy a RHEL image mode web server on Red Hat OpenShift Virtualization.

    Build your web server

    First, we will build the RHEL image to provide access to an Apache httpd webserver based on a Containerfile that contains the following customizations:

    • A bootc-user with password redhat
    • The Apache httpd package installed
    • The Apache httpd service enabled
    • A custom index.html file serving our home page

    The final Containerfile is as follows:

    FROM registry.redhat.io/rhel9/rhel-bootc:9.4
    RUN dnf -y update && dnf -y install tmux mkpasswd
    RUN pass=$(mkpasswd --method=SHA-512 --rounds=4096 redhat) && useradd -m -G wheel bootc-user -p $pass
    RUN echo "%wheel        ALL=(ALL)       NOPASSWD: ALL" > /etc/sudoers.d/wheel-sudo
    RUN dnf -y install httpd && \
        systemctl enable httpd && \
        mv /var/www /usr/share/www && \
        sed -ie 's,/var/www,/usr/share/www,' /etc/httpd/conf/httpd.conf
    RUN echo "Welcome to the bootc-http instance!" > /usr/share/www/html/index.html
    EXPOSE 80

    All you need to do now is to log in to your registry; you can use a self-hosted registry or a public registry like quay.io:

    podman login $REGISTRY_URL

    You can now build and push the image using Podman:

    podman build -t $REGISTRY_URL/$USERNAME/awesome-webserver:v1.0
    podman push $REGISTRY_URL/$USERNAME/awesome-webserver:v1.0

    Once the image is ready, you can proceed to the next step, converting the image to the KVM QCOW2 format using bootc-image-builder.

    Convert your container image to QCOW2

    Red Hat OpenShift Virtualization can use QCOW2 images as a disk image source to deploy a virtual machine; hence, we will convert the previously built image to the required format.

    We can leverage the official Red Hat bootc-image-builder image to perform this task.

    First, create a folder named output where the final image will be stored:

    mkdir -p output

    Then run (as a privileged container) the image conversion:

    sudo podman run \
       --rm \
       -it \
       --privileged \
       --pull=newer \
       --security-opt label=type:unconfined_t \
       -v $(pwd)/output:/output \
       -v /var/lib/containers/storage:/var/lib/containers/storage \
       registry.redhat.io/rhel9/bootc-image-builder:latest \
       --type qcow2 \
       $REGISTRY_URL/$USERNAME/awesome-webserver:v1.0

    It will take some time to generate the final image, but after the process, we will have our image at this path: output/qcow2/disk.qcow2

    Create the necessary resources for the VM

    OpenShift Virtualization supports using a container image as a source for importing QCOW2 disks and converting them to bootable disks for the VMs. In this step, we will take the output of the previous process and embed it into a new container image, which will then be imported into OpenShift Virtualization to use for creating our brand new VM.

    FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS builder
    ADD --chown=107:107 output/qcow2/disk.qcow2 /disk/
    RUN chmod 0440 /disk/*
    FROM scratch
    COPY --from=builder /disk/* /disk/

    As you can see, all this Containerfile does is to place the QCOW2 image into the default disk/path where OpenShift Virtualization expects the disk image to be for importing.

    As usual, we will build and push the image, this time with a dedicated tag:

    podman build -t $REGISTRY_URL/$USERNAME/awesome-webserver:v1.0-ocpv
    podman push $REGISTRY_URL/$USERNAME/awesome-webserver:v1.0-ocpv

    The next step is where the magic happens. We will create a volume resource (Figure 1) that will take care of configuring the necessary jobs to fetch the image and create a bootable source for the VM we will create.

    The OpenShift Virtualization Catalog tab details, with the Volumes project drop-down menu at the top and the Add volume button highlighted in the top right corner.
    Figure 1: In the OpenShift dashboard, click Add volume to create the volume resource.

    Configure the input as follows (see Figure 2):

    • Registry URL: Replace REGISTRY_URL and YOURUSERNAME with the registry you are using and your username
    • Volume name: awesome-webserver
    • Cron: */2 * * * *
    • Preference: rhel.9
    • InstanceType: u1.small
    The Add volume details modal with the fields described in the preceding list, including a sample registry URL.
    Figure 2: Configure the volume source and destination details.

    We can verify that after a couple of minutes, the volume resource will appear in the OpenShift Virtualization catalog (Figure 3).

    The Create new VirtualMachine tab lists the example volume resource that was just added.
    Figure 3: The new volume resource has been added to the OpenShift Virtualization catalog.

    You can select the new boot volume, an instance type u1 Small, and proceed to the creation of the VM.

    After a few seconds, the VM will be up and running! Figure 4 shows the result.

    Under the VirtualMachines tab, the example virtual machine is listed with the status Running.
    Figure 4: The virtual machine displays the Running status upon successful creation.

    Expose and test the web server

    Now that our VM is running, let's access our web server and verify that it is serving our index page.

    To do so, we need to first create a service for the VM. In the Networking → Services tab, click Create Service (Figure 5) and paste the following YAML definition to ensure that the service is selecting the VM pod and exposing the port 80, where our server is listening:

    kind: Service
    apiVersion: v1
    metadata:
      name: http-server
      namespace: awesome-webserver
    spec:
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      selector:
        vm.kubevirt.io/name: my-awesome-webserver-vm
    Under Create Service, a block of YAML code is shown with a blue Create button beneath.
    Figure 5: Enter the YAML definition to set up the service for the VM.

    Once the service is in place, we can go into Networking → Routes to create an HTTP route to connect to our web server, as shown in Figure 6.

    Under Routes, the user can configure the HTTP route by completing the Name, Hostname, Path name, Service, and Target port fields.
    Figure 6: Create an HTTP route to connect to the web server.

    In your browser, navigate to the URL that was provided when you created the route to verify that the server is properly running. You should see a welcome message as illustrated in Figure 7.

    The message "Welcome to the bootc-http instance!" is shown in the browser.
    Figure 7: Navigate to the route URL to verify the web server is running.

    Wrap up

    In this article, we demonstrated how easy it is to build a RHEL image mode container and use Red Hat OpenShift Virtualization to deploy it in a few minutes without any additional tools or modifications. You can also automate this to enable deployment at scale for multiple instances and different hypervisors, cloud providers, or bare metal instances.

    Get started with image mode for Red Hat Enterprise Linux and learn more about bringing your virtual machines to OpenShift Virtualization.

    Related Posts

    • Announcing image mode for Red Hat Enterprise Linux

    • Image mode for Red Hat Enterprise Linux quick start: AI inference

    • Introducing image mode for RHEL and bootable containers in Podman Desktop

    • Creating a VMDK using image mode for Red Hat Enterprise Linux

    • How to back up and restore virtual machines with OpenShift

    • OpenShift Virtualization for vSphere admins: A change in the traditional storage paradigm

    Recent Posts

    • How to use RHEL 10 as a WSL Podman machine

    • MINC: Fast, local Kubernetes with Podman Desktop & MicroShift

    • How to stay informed with Red Hat status notifications

    • Getting started with RHEL on WSL

    • llm-d: Kubernetes-native distributed inferencing

    What’s up next?

    Learn how to utilize GitOps in OpenShift to manage your virtual machines in this hands-on learning path.

    Start learning
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue