1.5

Virtual Machines and Containers

Virtualization

  • Virtualization abstracts computer hardware (CPU, memory, disks, network cards) of a single computer into separate computing environments.
  • Provides an illusion of running a separate complete computer on top of another.
  • Each environment is complete with its own operating system, including the kernel.
  • Virtualization allows operating systems to run as if they are applications within other operating systems.
  • Made possible by cheap multicore CPUs, cheap memory, and embedded virtualization technology in modern CPUs.
  • Virtualization is different from emulation:
    • Emulation translates every application command at runtime, making it slower.
    • Virtualization is for the entire operating system; an OS natively compiled for a specific CPU architecture can run on that CPU on top of another OS.
    • Virtualization doesn't require real-time translation, making it much faster than emulation.

Hypervisors

  • Hypervisor is software that creates and runs virtual machines, managing abstract hardware for the operating system.
  • The computer running the hypervisor is the host machine.
  • The virtual machine running on the host machine is the guest machine.
  • Nested virtualization: running virtual machines inside another virtual machine.
  • Different operating systems (Linux, Windows, macOS) can run simultaneously if compiled for the same architecture, each with its own kernel.
  • Modern CPUs have special instructions exclusively for hypervisors to enable virtualization; older machines may need to activate these instructions.
  • Management tools with hypervisors allow starting and stopping of virtual machines.

Types of Hypervisors

Type 1: Bare Metal Hypervisors
  • Runs directly on the host hardware, controlling the hardware to manage guest operating systems.
  • No intervening operating system; the hypervisor acts as the host OS.
  • All hosts on the host machine run as virtual machines.
  • One virtual machine on the host is used as a management tool.
  • Forms the basis of cloud computing platforms like Microsoft Azure and Amazon Web Services.
Type 2: Software Hypervisors
  • Software that runs on personal computers.
  • Allows end-users to run virtual machines on their own operating systems (Windows, macOS, Linux).
  • Useful for creating multiple environments (development, testing, acceptance).
  • Guest OS may need slight modifications to work with shared resources from the host OS.
  • KVM (Kernel Virtualization Module) in Linux converts the host OS into a hypervisor, a hybrid between types 1 and 2.
  • Common type 2 hypervisors: VirtualBox, VMware Workstation, Hyper-V.

Connecting to Virtual Machines

  • Using hypervisor software for a direct console connection.
  • Connecting via network protocols for VMs on networks or in the cloud.
  • Linux guest OS (headless):
    • Connect via SSH.
  • Linux guest OS (desktop environment):
    • Client-server protocols such as X2Go (used in this course), VNC, Spice, and Steam viewers.
  • Windows guest OS:
    • Connect via RDP (Remote Desktop Protocol), which is typically pre-installed.

Windows Subsystem for Linux (WSL)

  • Virtualization technology optimized for Windows.
  • Allows access to a Linux VM, including the full kernel for Linux.
  • Integrates Linux into the Windows ecosystem.
  • Install Linux as an app from the Microsoft Store rather than creating a full VM.
  • WSL2 boots faster than traditional VMs but may have slower file system operations.

Containerization

  • Application-level virtualization, leaner and faster than full OS virtualization.
  • A container holds a single application (or small set of related applications) and the system resources needed to run it.
  • Containers reuse the host computer's kernel, so most are Linux containers.
  • Applications inside a container believe they are in a full, isolated system.
  • Isolation improves security, prevents crossover effects, and allows more efficient use of the host computer.
  • System independence: containers have their own system environments independent of the host OS distribution.
  • Hardware independence: containers can easily be moved from one computer to another.
  • Requires granting specific access to host resources.

Comparison: Bare Metal, Virtualization, Containerization

  • Bare Metal:
    • Hardware -> Host OS (Kernel) -> Applications
    • Traditional way of computing; less flexible and potential security issues.
  • Virtualization:
    • Hardware -> Host OS -> Hypervisor -> Guest OS (with individual kernels) -> Applications
    • Increased flexibility, OS isolation, and hardware efficiency.
  • Containerization:
    • Hardware -> Host OS (Kernel) -> Container Engine (e.g., Docker) -> Containers (single applications)
    • More efficient by reusing the host OS kernel; faster boot times.

Container Technologies

  • Docker:
    • Provides an environment for a single application.
    • Uses a Dockerfile: a declarative description of the application's setup inside the container, enabling repeatable creation.
    • Layered architecture with immutable resourcing: containers are created once and replaced for modifications.
  • LXC (Linux Containers):
    • Provides a full Linux installation environment similar to a VM but without its own kernel.
    • Only takes about 3% of the time to boot compared to a full VM.
    • Docker was originally built on top of LXC.

Packages vs. Containers

  • Containers:
    • Complete, isolated environment running on the host kernel.
  • Packages:
    • Share library files with the host OS, potentially leading to conflicts or effects from upgrades.
  • Alternatives to packages (Flatpak, Snap, AppImage) offer container-like isolation on Linux.

Infrastructure as Code

  • Automating the creation and configuration of virtual resources using code.
  • Replaces manual management with automated processes for large-scale cloud computing.
  • Modern declarative tools are preferred over procedural shell scripts.

Tools

  • Provisioning (creating virtual resources):
    • Terraform: for large deployments across multiple cloud providers.
    • Vagrant: for consistently creating development, testing, and staging environments.
  • Configuration (modifying systems over time):
    • Ansible: configures resources in a consistent and repeatable manner.