Rapid Build for Testing Containers

Containers are everywhere and I’m, personally, behind the times.  I’d like to think that I’ve kept up with new technology trends, but on containers I was caught napping.  So now it’s time to really get cracking and for that I need to have a playground.  There are several online options available, but I’m cheap, so I’ve elected to create my own environment using Hyper-V.

To start things off, let me preface this by saying that I’m not a proponent of any one hypervisor or another, but as I’ve said before, I’m cheap.  Since Hyper-V ships with newer versions of Windows (if you have the hypervisor extensions available), this was my preferred route.  I’m currently running Windows 10 build 1803, so most of my descriptions may look a little different from your own, but the key setup is the same.

First off, I needed to build 6 virtual machines for each type of container that I’m working with.  For me, that’s docker swarm, kubernetes, and Apache Mesos.  So that’s 3 × 6 = 18 virtual machines – all running on CentOS.  You can run them on most major Linux distributions, but I decided to go with CentOS because it’s the most familiar to me.  The first part of this post is about building the virtual machines.  Since I’m a PowerShell fan, that’s how I’m building them.

Container Creation Script

#First some definitions...
# the VmAbbr is the abbreviation that I'm using for the beginning of the name for each VM - I'm using "mesos", "k8s", and "docker" for Apache Mesos, Kubernetes, and Docker Swarm respectively.
$VmAbbr = "mesos"
# The VM Names are basically the names that I want for the VMs.  I could do this with a For...Loop, but I was lazy and just wrote it out.
$VMNames = "$( $VmAbbr )-master", "$( $VmAbbr )-node01", "$( $VmAbbr )-node02", "$( $VmAbbr )-node03", "$( $VmAbbr )-node04", "$( $VmAbbr )-node05"

# This is the path to the DVD that I want to use for booting the CentOS installer.  You can choose a different one here if you like.  It's easier to make this a local path instead of a UNC Share.
$BootDvd = "D:\Hyper-V\ISOs\Operating Systems\Linux\CentOS\CentOS-7-x86_64-Minimal-1804.iso"

# Build a VM for each of my names
ForEach ( $VMName in $VMNames )
{
    # Does a VM with said name already exist?
    if ( Get-VM -Name $VMName -ErrorAction SilentlyContinue)
    {
        #  It does?  Then remove it - no questions asked.
        Remove-VM -Name $VMName -Confirm:$false -Force
    }

    # This is the path for the VHD - for me, it's in the default location as all the other ones, so we'll keep with that.  I join together the Default Path and the name of the new VHD.
    $VHDPath = ( Join-Path -Path ( Get-VMHost | Select-Object -ExpandProperty VirtualHardDiskPath ) -ChildPath "$( $VMName ).vhdx" )

    # Does a file already exist with that name in that location?
    if ( Test-Path -Path $VHDPath )
    {
        # It does?  Sorry - remove it.
        Remove-Item -Path $VHDPath -Force -Confirm:$False
    }
    # Let's create a new VHD using 127GB for the size (I've got plenty of space) and use 1MB block sizes as recommended according to Linux on Hyper-V best practices.  We'll also use dynamic disks
    $VHD = New-VHD -Path $VHDPath -BlockSize 1MB -SizeBytes 127GB -Dynamic

    # Let's build the VM with 1GB of memory to start, using the VHD we just built, the default switch (in my case 'vSwitch'), and make it EFI bootable (Generation 2)
    $VM = New-VM -Name $VMName -MemoryStartupBytes 1GB -VHDPath $VHDPath -SwitchName vSwitch -Generation 2
    # Take that VM and turn off secure boot (required by most *NIX distributions
    $VM | Set-VMFirmware -EnableSecureBoot Off
    # Add a DVD Drive to the VM which points to the Boot Device from above
    $VM | Add-VMDvdDrive -Path $BootDvd
    # Set the boot device to be the DVD we just defined
    $VM | Set-VMFirmware -FirstBootDevice ( $VM | Get-VMDvdDrive )
    # Disable automatic checkpoints - this is my BIGGEST beef with the newer desktop Hyper-V setups and I always forget to remove it, so now I won't
    $VM | Set-VM -AutomaticCheckpointsEnabled:$false
    # Give the VM 2 processors and use Production (not standard) checkpoints
    $VM | Set-VM -ProcessorCount 2 -CheckpointType Production
} # Loop back and go to the next.

That’s pretty much it.  After I’ve done that I can turn them each on and begin to install the operating system.

For me, that means installing just the basic system, disabling the SELinux configuration, and I’ve also disabled kdump because it’s just a test environment.

Next up for me is writing up my experiences with setting up the software on these machines.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.