Create VMs on Proxmox in Seconds!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i have a question for you guys what is the most annoying and repetitive task that always takes an incredible amount of time when working on proxmox for me it's the installation and configuration of new virtual machines but today i want to show you how to simplify that whole process and how you can create new virtual machines on proxbox in just a few seconds and this won't be just a base installation we will use a bunch of free and open source tools and techniques to create our own linux server images that you can build based on your favorite linux distributions and you can apply users ssh keys nrp address configurations or even provide them with custom shell scripts and software packages so that when you need a fresh new virtual machine with ubuntu and docker installed for example you can just create it from a proxmox template and put this up in just a few seconds this is so cool and i hope it will save you a lot of time when you often create new virtual machines or to test and try out new things on proxmox so let's start and let's create our own custom virtual machine templates today's video might be pretty complex so it could become a longer video i've worked so many hours to get this working on my end and special thanks go out to spider on our discord who always helped me with this project and provided me his own templates so thank you so much okay let's get started with the first part that we need for this project which is called packer and packer is a free and open source tool developed by hashicorp and it is used to build custom machine images so when you need a new linux or windows server you can just use packer to create pre-built images for any platform you need for example you can build images for cloud providers like microsoft azure amazon's aws digital ocean or hypervisors like vmware virtualbox and also proxmox it is a cli tool that automatically creates these images from a template file so you can for example create custom images with installed applications or user accounts pre-configure authentication settings like ssh keys or ssl certificates so everything that you or your organization needs to be present in that machine image and this is pretty useful if you want to automate the deployment of resources like using it for development staging or production environments on multiple platforms or you want to quickly create demo appliances whenever you need them you can install packer on nearly all operating systems like on mac os windows linux and so on i've personally installed this through the windows package manager chocolaty and that it's also available in your terminal and what makes packer so powerful is that you can integrate it in many different systems because it has tons of plugins and extensions these plugins are mostly builders so these are the components that are responsible for creating these machines and generating the images on the platforms but you can also integrate it with provisioners like ansible chef or puppet i think it's always great what hashicorp is building by the way they have so many cool devops and automation tools like terraform and vagrant and now packer has become one of my recent favorites and now let's take a closer look at the proxmox integration that's available in packer and how we're going to use it in this video and in the documentation you can see that it has two different builders available so the proxmox clone builder and the proxmox iso builder we will use the proxmox iso builder for this tutorial because this can create new vm templates in proxmox and if you haven't worked with vm templates in proxmox before so this is a pre-configured system image that you can use to deploy new virtual machines so on my proxmox server you can see i have a few different virtual machines running but there are also some items with a different icon and these are vm templates that i have created for example when i need a new ubuntu server that is already installed i can just use the ubuntu server focal template that i've built and just clone it to create a new virtual machine or maybe i need an ubuntu server that already has docker installed so then i can just clone their second template cloning these images doesn't take long and these virtual machines will be up and running in just a few seconds because they are already installed with an operating system configured with settings or installed with any additional applications and that's extremely useful when you often need to create new virtual machines you can also do this manually and create vm templates from any running virtual machine that you have installed yourself but why do this manually if we have packer so that's what packer will do for us on proxmox pekka will connect to our proxmox server create a new virtual machine do the installation fully automatic and run any scripts or configuration we need and when it's finished it converts this virtual machine into a vm template and then we can use this template on proxmox to clone a new virtual machine from it that always has the exact same configuration no that alone is awesome but what if you'd like to create multiple virtual machines from your template that should have different ip addresses or different configurations or maybe you also want to share this image with your team but you don't want your username or credentials to be included here so that's why we are using another great functionality of vm templates in proxmox which is called cloud init cloud init is the industry standard multi-distribution method for cross-platform cloud instance initialization does it make sense to you no let me explain this in simple words so whenever you need a new instance from a pre-installed image file like in your virtual or cloud machine you usually want to deploy it with different configuration settings so you probably have seen this on public cloud providers before so when you create a new instance like a linux server for example you don't need to install these systems because they are using pre-installed images but you can still inject configuration settings during the setup like a new host name or a new user account and or any password or ssh keys and this is what you can do with cloud init so when a new linux machine is deployed from an image it looks for an existing cloud in it configuration and applies it to the system and because it's an industry standard it's supported by all major linux distributions like debian ubuntu red hat fedora and also freebsd and a few other ones most public cloud providers use it to deploy their machine images with configuration settings and it's also used in private cloud environments like kvm or vmware and proxmox also supports cloud init on their documentation page you can see how that works and how you need to prepare virtual machines with the cloud init package and once you have a new virtual machine created that's prepared correctly in the proxmox cloud init section you can add a username a password dns settings an ssh key and ip configurations for these virtual machines and when you boot this virtual machine the cloud init package inside this vm will apply the new settings to the system this is something packer can also do for us so it can create a custom vm image that is prepared to work with proxbox cloud init integration i know this is a lot of stuff so once i heard about it i was completely confused and yeah it might seem overwhelming in the first place but trust me once you see how it works it will totally make sense to you and of course i will walk you through all of the necessary steps that you need for building this yourself but first i want to say a few words about today's sponsor of the video teleport because with teleport you can protect your remote resources like ssh linux servers kubernetes databases or web applications with two-factor authentication and in audit logging teleport is entirely open source and free to use in the community edition and suppose you want to use it in your company environment and secure your development or operations teams in that case teleport also offers an enterprise version with additional 24 7 support and single sign on it's a great application so just don't let in try it out you will find a link to their website in the description of this video down below okay so i hope this wasn't a too long introduction for you but i think it's always good to have a high level overview of what we're doing before diving a bit deeper into the actual example so let's now do this and start with a packer template that i have created and by the way if you'd like to take a look at these templates and examples i hope you already know my git repositories but in case you don't you will find them on my personal github page so in the boilerplates repository you will find templates for various tools and projects that i'm using in my home lab such as ansible docker kubernetes and no also packer and here i've created some templates which are pretty similar to what we're using in this tutorial these packer templates consist of a bunch of different files so first we have the credentials file which contains the connection settings for the proxmox server because pekka needs to somehow authenticate to our proxmox server and create virtual machines execute tasks and so on the api url contains the ip address of the proxmox server the web interface port and the url path that leads to the api module and we also need to authenticate with an api token and a token secret so if you don't know how to create that in proxmox you just need to go to into the web interface select your data center and go to permissions api tokens add a new api token for a specific user so in my case i've just used the root user because this user already has all the privileges we need in packer but you can also create a separate user according to the proxmox builder documentation with limited permissions of course and then you just need to enter a name for this token id and now it's really important to deselect the privilege separation setting because we want our api token to have the same privileges as a root user note that the api token secret will only show up once so when you close the window and forgot the secret you just need to create another one and then you need to enter these settings in the credentials file so take care of the api token is the username and the token id separated with an exclamation mark now that we have configured our credentials we now can define our packer template and all my templates consist of three different sections so first we will describe the variables used in the credentials file and then we define our resources and the individual settings for them and the last section is to build a section where we define which resources should be built and how they should be provisioned so let's now have a look at the resources we want to create from the type proxmox which i have called ubuntu server focal test 1 in this example so now it's also important that you set the correct parameters for the connection settings they should match the variable names that you have declared above of course and if you have used any self-signed ssl certificate in your proxvox web interface and you get a certificate warning you also need to use the insecure skip tls verify parameter and set this to true and then we start defining the settings for our vm template so if you want to have a complete list of all these different parameters you can just look at the packer documentation for the proxmox builder so there everything is documented but i will walk you through the most important settings of course that i have used here in this template so the general settings should be pretty straightforward such as the proxmox node the unique vm identifier or the vm name and you should also select an iso image from which the vm template is installed so in my example i just used a local iso from an ubuntu server that i installed and downloaded on the proxmox storage before or you can also download it from a url resources in the packer template and then you can also configure the other vm settings like if it should have a guest agent installed the storage controller virtual hard disks or how many cpu cores and memory it should have and also the network configuration is really important because the virtual machine needs to be able to connect to your workstation when you're running packer but i will explain this in a minute why this is so important so just select a bridge interface where you have a dhcp server running so the virtual machine gets an ip address and can connect to your network and then you should also enable the cloud in its settings so that will tell packer to prepare the vm template with the cloud in its storage okay so far so good so now comes something that i think is absolutely cool because you might wonder so how does packer install the operating system automatically from an iso because during the installation procedure ubuntu server asks us a few different things we need to enter in the console like setting up a username or an ip configuration choose the packages that needs to be installed and so on so that's why packer has a list of boot commands that it can inject into the console window when the virtual machine is started so for every button that needs to be pressed or text that you need to enter somewhere you can define instructions in packer to do it automatically so know which buttons you need to press is of course different for every linux distribution because every system has a different installation procedure and these boot commands i'm using in this template are working in ubuntu 20.04 lts and they will press a few buttons in the menu and then finally run a command which is called auto install and so if you don't know how to install this is a new function introduced in the latest version of the ubuntu server and it will perform an unintended installation this is also sometimes called a pre-seated installation so when you are at the boot menu to install ubuntu you can enter this auto install command that will perform this automatic installation by fetching the instructions from a web server and that's why in the auto install command we also need to enter a data source which is called now cloud net and the source definition is a url with an ip address and a port so we just need to run a web server on this ip address that can deliver the installation instructions for ubuntu as a text file and then during the installation the ubuntu server will open a connection to this web server to get and apply these instructions so this is what packer is also doing automatically in the background so when packer creates the virtual machine it will also start a small web server on your workstation and wait for the ubuntu server's connection so that's why the network configuration is so important because if the ubuntu server can't connect to the web server that's running on your workstation it can't fetch the installation instructions when packer runs the auto install command it will replace the dot http ip and http port variables with your workstation ip address and a random port you can also bind the web server to a specific ip address on your workstation and limit the random port numbers by setting these parameters and in the http directory you can define the installation instructions that are served by the packers web server so in this folder you need to create two files the metadata which can be empty and the user data where you put the installation instructions for the ubuntu server so in this file i have defined some basic settings like the language settings and the keyboard layout that the ubuntu server should configure and also some other settings can be customized during the installation procedure so for example this ubuntu server should also install an ssh server and allow password authentication this is important for the packer scripts which will perform provisioning tasks via ssh later and here in the next section you should also define which packages should be installed for example the sudo package and the qmo guest agent and in the user data section you can also define the time zone and a user account which should be created again this is important because packer will use the user accounting later to connect to the vm for provisioning tasks so enter a user that is allowed to execute sudo commands and enter a password or an ssh key you want to deploy on this server so technically you can also add more provisioning tasks in this auto installation script but i'm not using this to install any software on this server in this step it's only important that packer installs the ubuntu server and creates a user account i want to use later because when we go back to our template you can see that in the next steps we define ssh credentials and this should be the same as the one in the user data file of course you can choose whether you want to use a password or a private ssh key file that is important for the following section i know this is a lot of information so trust me i was completely overwhelmed by this as well when i saw it the first time but i would just recommend doing a few tests with your own templates and starting with a simple task so don't over complicate it because then troubleshooting can get a little difficult if it's not working and you actually don't know at which point it's failing so maybe just try to run this script and open a console window of the vm in proxmox while pekka is running and then you can see what it's doing and if it's entering the correct boot command and the correct instructions and if everything works as expected okay so that was the first part where we defined the vm template resources and how packer delivers the auto install instructions to the ubuntu server now in the build section we define how this vm template is created and provisioned so you need to enter the name of the resource that you want to build and then you can add provisioning scripts that pekka will run on this vm via ssh for example so that's why it is so important to configure the ssh authentication so here i added an inline shell script that packer executes when the server is booted up i won't go into too much detail here but you can just easily summarize the first of three steps as a post installation cleanup and preparation tasks for cloud init so remember when i said the image needs to be prepared to work with proxmox cloud init functionality so this is exactly what this script is doing so it will after the installation of the ubuntu server clean the machine id which is important in linux if you want to use it as a clone template and then run a few clean up tasks on the app package manager it will copy a file which is important for the proxmox cloud init functionality so all these free steps are important that you can clone this vm template in inject specific settings later with cloud init like giving this machine a different ip address or adding another user account and add another ssh key to it and after these three steps you can also do anything else like installing docker for example so this section here just executes all the necessary shell commands that i've just copied from the docker installation guide for ubuntu so it will download the packages it will add the apt key and install the docker cli and daemon on this machine and once this is done so when packer has created the machine it has successfully run the provisioning scripts so then it will finally convert this virtual machine to a template in proxmox so let's run this and let's hope for the best before starting the build process i would always verify if the parker template is valid so with the command packer validate you can check if you have any errors in your file so always specify the variables file with the credentials otherwise packer doesn't find it and then when everything is okay you can execute the build task with the same parameters and when you go into the proxmox web interface you should also see in the logs that it's doing something here you can also see it just created this virtual machine and if you go into the console window you can also see when the machine is booting that packer presses the buttons you have defined in the boot command section and it enters the correct auto install command so now the ubuntu server boots up and it should start the unintended installation so here in the packer cli you can also see if it starts the web server and waits for the ubuntu server to connect and once this is successful it will just wait until the server is installed to run the ssh provisioning scripts so this can take some minutes depending on how fast the installation process is on your proxmox server and once this is done the server is booted up packer tries to connect to it via ssh and run the provisioning shell script so here you can see it does the cleanup tasks and it's preparing the cloud init files and here my docker installation shell script is also running and if this is all working as expected in the end you should have a new virtual ubuntu server with docker installed that has the cloud indent files prepared and packer will convert this to a vm template and here it is so that's our new vm template that packer has created for us and it's now ready to be used so let's now see how fast we can deploy this virtual machine from it so hopefully it will just take a few seconds or yeah maybe one or two minutes as i promised you in the title of this video let's see so first let's clone this to a new virtual machine and then in the cloud init section we can enter our username or add any additional public ssh key i also want to give this vm a static ip address and gateway information to connect it to the internet and then if you have entered all your settings in the cloud init section it's also very important that you don't forget to click on regenerate image to apply these settings into the image file so now we can just run this virtual machine and wait until it's booted up and connect via ssh [Music] and that's it the server is now up and ready so yeah the linked clone works blazing fast not even a minute and the machine was ready it has docker automatically installed everything else that we've defined in the shell script so that's really cool the full clone might take a bit longer because it first needs to copy the whole virtual disk but i hope you agree with me that this is absolutely amazing and it can save you so much time when working a lot with virtual machines or proxmox i'm really excited about this so that all might seem pretty easy and awesome but to be honest guys i've gone through hours of testing and troubleshooting before it actually worked on my pc because there are so many things involved into all these different processes which can all go wrong so i'd like to share some of the mistakes i did or things that i have observed you might need to customize or fix based on your environments so first if you start packer and try to build the vm template you might see some application crashes or arrows and even if this seems like a critical application issue very likely packer can't connect to your proxmox server in the first place so mostly your api token or the ip address in your credentials file isn't correct or the api token might not have the correct permissions so check if you have everything done like i've described in this video and then if that somehow works i still often had the case that the auto install procedure on ubuntu didn't work so the abundant server booted but it threw me back into a graphical user interface to proceed the installation manually and then it never continued and this is always the case if the auto install procedure can't connect to your packers web server or the files served by it aren't correctly formatted so first look at any ip address conflicts or connection problems so for example if you're running packer in wsl 2 it's very likely that the ubuntu server can connect to your wsl2 instance because windows creates it on a separate network it's just a masterwork with too so i just ended up running this in powershell or if you're using linux natively on your workstation that should be also fine and i also had the case that packer always picked the wrong ip address for my windows machine because i have so many network interfaces in my pc and it just picked the wrong one in this case i needed to statically define the ip address of my workstation into the auto install command or just bind the packers web server to this ip address also if the user data file isn't formatted correctly it will just go wrong and fall back to the manual installation as well so for example if you didn't indent some parameters in the correct order or you have any misconfiguration here it also doesn't work and troubleshooting all these problems isn't easy because the vm doesn't really say what it's actually doing in the log file so it will just throw you back into the graphical console it's a bit annoying but yeah you might need to go through some trial and error yourself just like i needed now you might also ask how can we take this a step further because we have still created this virtual machine manually and yeah there's also a way how you can automate this whole process with tools like terraform for example so you can create your custom vm templates with packer and then use terraform to create virtual machines from it fully automatic i've already done some tests with the creation of multiple vms for my kubernetes cluster on the server so that might be also a small hint what's the topic of one of the next videos so don't worry there will be more stuff about these of things and i believe this is absolutely amazing because you can use automation tools that cloud providers and enterprise companies are using and practice with them in your own home lab this is something i absolutely love about it and if you're interested in more videos like this and you feel like these videos are helping you you're welcome to return the favor by liking the video and subscribing to the channel that would be really great and as always thanks everybody for watching take care of yourself and i'll catch you in the next video bye bye
Info
Channel: Christian Lempa
Views: 82,627
Rating: undefined out of 5
Keywords:
Id: 1nf3WOEFq1Y
Channel Id: undefined
Length: 25min 4sec (1504 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.