A template... to clone them all!

In short
  • A minimal VM template is necessary for Ansible to configure newly deployed servers.
  • Choosing PhotonOS, a lightweight distribution maintained by VMware, suitable for containers.
  • VM hardware, operating system installation, and preparation for Ansible access.
  • Cybersecurity point: monitor and limit service accounts and keys.

For Ansible to operate on newly deployed VMs, these VMs must be based on a template with the minimum necessary components.

The following article will cover the preparation of the template intended to serve as a reference for creating the VMs that make up a Kubernetes cluster.

PhotonOS

PhotonOS logo

Click the image to enlarge.

The chosen operating system is PhotonOS.

This is a lightweight Linux distribution maintained by VMware. However, it can be used on many platforms. Due to its low memory footprint, kernel optimized for running on the ESXi hypervisor, and overall enhanced security, it is an excellent choice for running containers.

Its use may not be suitable for all needs, but because it is based on RPM packages, it can still be used for specific tasks.

It falls within the category of "micro distributions." It's up to each individual to choose their target. The use of a microdistribution is not mandatory, but if your goal is limited to running containers, then it makes sense to select an operating system specialized in that area, which will both reduce the overall footprint of your servers and its attack surface by exposing fewer components.

For those who may be concerned about the relationship between VMware and PhotonOS, the distribution remains completely open source. If Broadcom decides to end the project, a fork will always be possible if the community decides.

The operating system can be obtained by downloading the ISO image (or an OVA). Images are available for AWS, GCP, and Azure. It is even possible to find a Raspberry Pi image.

As of the date this article was written, version 5.0 is available for download. I encourage you to always use the latest available version.

VM Hardware

Under VMware

Creating the VM takes only a few minutes. PhotonOS is directly available in the list of operating systems once the Linux family is selected.

Selecting PhotonOS in VMware

Click the image to enlarge.

It is not always necessary to modify the default values regarding hardware configuration. Personally, I simply increase the size of the root disk. In any case, these elements can be modified during cloning operations.

Changing the disk size

Click the image to enlarge.

From this point on, the rest of the tutorial can be applied, regardless of your choice of hypervisor.

Operating System Installation

Starting the installation

Click the image to enlarge.

Once the ISO is loaded, apart from the essential settings such as the server name and its IP configuration, there is nothing complicated.

Accepting the terms of use

Click the image to enlarge.

Selecting the disk

Click the image to enlarge.

Note that several installation profiles are available. Personally, I use Photon minimal; the other options correspond to specific needs.

Selecting the installation profiles

Click the image to enlarge.

You can choose between two kernels: the first is generic, and the second is optimized for VMware. Since this example is running on ESXi, I choose option number 2.

Choosing the kernel

Click the image to enlarge.

However, be aware that I have encountered problems in the past with this kernel. When using binaries outside of the OS's default repository, incompatibilities may occur.

Choose the kernel that best suits you and stick to a generic kernel if you are not using ESXi.

The keyboard is also configured by default as QWERTY; be careful when choosing the root account password.

The installation is extremely fast; in my case, it took just over 1 minute.

Ending the installation

Click the image to enlarge.

Configuration for Ansible

Once the installation is complete, it is necessary to "prepare" your VM to be "templatized," so that it can be cloned at will to quickly create new Kubernetes nodes.

update

To do this, the first step is to update your OS.

To do this, simply use the following command:

tdnf update

Starting the update

Click the image to enlarge.

tdnf corresponds to Tiny DNF, a lightweight version of the DNF package manager. Most of the known commands associated with it work with tdnf.

Feel free to restart once the update is complete.

Creation of users and other settings

Before proceeding, it is preferable to switch the keyboard layout to AZERTY.

To do this, simply type the following command.

localectl set-keymap fr

Keyboard mapping

Click the image to enlarge.

In direct connection with my Ansible configuration on Windows, I create my user dedicated to Ansible. This way, once deployed, I can customize the installation of my PhotonOS systems based on this template using Ansible.

In my case, the user is ansible-windows

useradd ansible-windows
mkdir /home/ansible-windows
chown -R ansible-windows /home/ansible-windows

Creating the ansible-windows user

Click the image to enlarge.

I then use the identity of this user to generate its SSH configuration.

su - ansible-windows
ssh-keygen

Creating the SSH key for ansible-windows

Click the image to enlarge.

I am now going to create a second user, specifically my personal user account, to allow me to connect via SSH. This will simplify my interaction with the template outside of Ansible.

The same operation as for the ansible-windows user, but I'm adding the passwd command to create a password for the user (in this case, bvivi57… don't ask me why…).

Creating a local user for SSH access

Click the image to enlarge.

From now on, I will be able to continue the configuration via my SSH client.

Connecting via SSH with my local user

Click the image to enlarge.

I am switching to root.

su – root

By default, PhotonOS enforces a password expiration for local accounts.

You can check this by running the command as root.

chage -l root

It is possible to change this with the following command.

chage -I -1 -m 0 -M 99999 -E -1 root

Changing password expiration

Click the image to enlarge.

I do the same with my standard user.

chage -I -1 -m 0 -M 99999 -E -1 bvivi57

I know that this is not a good practice, and I don't encourage you to do it, especially in an enterprise environment. Nevertheless, in my case, it simplifies my usage. Be aware that this may also force you to change your password on the next login

Still as root, I modify the NTP configuration to ensure that the template has the correct time. My Windows domain controller serves as my reference source.

Simply edit the file /etc/systemd/timesyncd.conf .

vim /etc/systemd/timesyncd.conf

Changing NTP configuration

Click the image to enlarge.

I add my server using the NTP= option

I restart the service

systemctl restart systemd-timesyncd

Restarting the service

Click the image to enlarge.

Finally, I set the correct timezone using the command

timedatectl set-timezone Europe/Paris

I verify that everything is OK with the command timedatectl

Checking the date

Click the image to enlarge.

Taking advantage of being root and being on my template, I switch to the identity of my user dedicated to Ansible, ansible-windows

su – ansible-windows

Switching to the ansible-windows account

Click the image to enlarge.

Switching to the ansible-windows account

Click the image to enlarge.

I am now adding the public key associated with this same user, but created from my Windows workstation using WSL.

Therefore, I copy this key from my WSL instance for the user ansible-windows, to the file /home/ansible-windows/.ssh/authorized_keys of the user ansible-windows, but on my PhotonOS template.

Copying the public key from WSL

Click the image to enlarge.

(A small tip for those who may have problems copying and pasting to a PhotonOS SSH instance using vim: first, execute the following command to disable vim's visual mode when right-clicking: echo "set mouse-=a" >> ~/.vimrc.)

Finally, I create a sudo file in which I allow the user ansible-windows on my template to obtain root privileges without a password.

vim /etc/sudoers.d/ansible-windows

ansible-windows ALL=(ALL) NOPASSWD:ALL

Configuring sudo for the ansible-windows account

Click the image to enlarge.

Configuring sudo for the ansible-windows account

Click the image to enlarge.

In this way, I can, from my Windows PC on which I have installed Ansible via the WSL layer, manage my PhotonOS template under the identity of the ansible-windows account. I invite you to read the article on configuring Ansible on Windows if this is not clear enough.

I now have a template that has the minimum requirements to be cloned and reproduced as many times as necessary to create my Kubernetes cluster.

Since it has a minimal configuration for being managed by Ansible, each clone can also be configured with Ansible, and this from my Windows workstation.

Demonstration of accessing the template from WSL with Ansible

Click the image to enlarge.

Cybersecurity considerations

Because even BFM talks about cybersecurity, there needs to be a section on the subject.

The logic I have used in this article is managing a server OS from an Ansible instance local to my workstation. The interaction takes place via an SSH connection.

The most critical security element is therefore the private key associated with my ansible-windows user within my WSL instance.

If this key is stolen or compromised, along with network access to the VLAN on which my template is deployed and potential clones, it would be possible for a third party to manage the entire system.

Other configurations are possible, including using a dedicated Ansible server for this purpose, for example, hosting an AWX instance, the GUI for Ansible. In this case, the administrator does not directly use their workstation but authenticates on AWX to then launch their Ansible playbooks. The private keys (nothing prevents you from using different keys depending on your environments) are in this case held by the Ansible server. This is probably more secure in an enterprise environment.

It is possible to strengthen security through some basic measures that can be provided by any AI LLM model like ChatGPT.

  • Use strong keys

  • Generate your keys with 2048 bits, or even 4096 bits, or better yet, use ECDSA or Ed25519 instead of RSA.

    ssh-keygen -t ed25519 -C "[email protected]"

  • Protect your private keys

  • Do not allow your private keys to be accessible by anyone. Make sure you have limited rights only for yourself.

  • Use passphrases

  • I haven't done this here, but it's a good practice to add a password on top of your private key. This way, if it is stolen, it won't be usable, but it will require you to enter the password each time you use it. An SSH agent like Putty Pageant can help with this.

  • Go for it!

  • Implement key rotation. For example, regenerate new keys every three months and update the configurations.

    In short, I often hear, and rightly so, that authentication with an SSH key is more secure than authentication with a login and password. But I also see many 'yolo' approaches on this topic:

  • Private key in Git repositories
  • Private key sent by email or shared among multiple people
  • Private key left on network shares with open access
  • Very long authorized_keys file on servers...

  • As with API tokens, JSON keys, and other mechanisms that depend on a source file, always try to have good tracking of these components in your organization and limit the 'Lord of the Rings' scenario, or one key to rule them all!

    As with service accounts, target your needs as precisely as possible, even if it means having more keys!

    Be careful with your SSH keys

    Click the image to enlarge.