Press enter to see results or esc to cancel.

Linux Build with Custom DNS

Have you found yourself knee deep into a deployment and then realized that your build is dependent on a specific DNS?  This often happens when we are leveraging the cloud for more ephemeral compute functions and are using a standalone (more likely on premise) DNS solution that is not accessible from the cloud.

I had a colleague reach out to me about a customer of theirs that had a similar need.  The systems that they are running may be Redhat or Ubuntu and they needed to be able to point those servers to a specific DNS on premise.

Of course, the first answer in the cloud is to leverage the cloud native tools.  For Azure this is as simple as assigning the custom DNS IP address to the VNET.

But…

If DNS settings need to be specific to the Linux server then you need to configure an approach to update the servers during build.

There are several ways to solve this:

  1. Build directly into the image - even with Azure Image Builder this feels like IaaC abuse
  2. Build customization with cloud-init - love this streamlined approach, more on that
  3. Azure Custom Script (Extension) - less OS agnostic, feels like cheating, but makes a lot of sense here
  4. Or another automation tool such as Chef or Puppet - myriad of options here, but the approach here is just as sound

I started with option 2, Going down the path of using cloud-init - because one of the key advantages is the semi-standard Linux OS agnostic build process.  This is perfect for adding users and more aptly installing applications (because I don't have to consider the package manager).  The issue I was vexed by was getting to a configuration for DNS that didn't involve me defining the network configurations separately from Azure.

The challenge here is netplan versus the resolv.conf approaches for configuring DNS settings.  Redhat came with the Network Manager cli (nmcli) preinstalled and Ubuntu didn't, which meant configuring at the netplan would be more difficult with Ubuntu.  Cloud-init does support the runcmd module - which would allow me to cobble together a similar solution or just point to a script.  At this point I felt like this post configuration would be best served by the Azure Linux custom script extension that I could re-use or update as needed to suit the use case.

See the script (on Github) below, the approach is to use /etc/os-release to determine the OS and then I either update resolv.conf for Ubuntu or the Network configuration for RedHat.

Source: https://github.com/tekgnu/systems/blob/main/build_linux_dns_config.sh *

 

Reference:

How it works - https://learn.microsoft.com/en-us/azure/virtual-machines/linux/azure-dns

How to fix it Ubuntu: https://tomaustin.xyz/2021/03/28/how-to-configure-an-azure-ubuntu-vm-to-use-a-custom-dns-server/

How to fix it Redhat: Red Hat Linux (RHEL) network interface configuration - nixCraft (cyberciti.biz)

 

For extra credit use a linter for your shell scripting, this was helpful for me to dust off my bash -  ShellCheck

 

Please Note * as always this is article and code snippet were meant to offer advice - and should not be expressly run without understanding how everything works. The responsibility of execution, script or knowledge is the liability of anyone performing it.