Configuring Bare Metal Vultr Servers With Cloud-init
Oftentimes, you want to automate the provisioning and configuration of your Vultr cloud infrastructure. Cloud-init is an industry-standard tool that might help.
Join the DZone community and get the full member experience.Join For Free
Oftentimes there will be cases where you will want to automate the provisioning and configuration of your Vultr cloud infrastructure. There are a plethora of tools out there, however,
cloud-init is an industry-standard that is used to initialize and configure VM instances with
What Is Terraform?
Terraform is an Infrastructure-as-code tool that allows users to build, change, and version your infrastructure safely and efficiently. It uses a high-level syntax to declaratively provision and manages infrastructure, allowing the ability to break down the configuration into smaller chunks for better organization, re-use, and maintainability. Information on installing and running Terraform can be found here. By passing the
user_data parameter into a Terraform.yaml file, you can use automation to configure your Vultr instance at boot time. More on that below.
Using Terraform To Configure Cherryservers With Cloud-init
If Terraform is your preferred infrastructure provisioning method then you can find the Vultr Terraform Provider at the Github Repo here.
For any infrastructure provider, when using Terraform as a provisioning tool you will always need to specify the
provider block as seen here:
Here's an example module that utilizes
user-data to configure a Vultr instance at boot time:
With this module, you have a
resource that is designating
vultr_server as the type of resource you want to provision and using variables such as
user_data to handle the provisioning. When you provide the string for
user_data, you are designating a startup script that the bare-metal server will run on boot-up.
Using Cloud-init To Configure Vultr Servers
You can provision new servers via the API to fetch user data of your Vultr via the
cloud-init service. This allows you to automate various server configuration tasks by fetching user data directives upon server deployment. Your provided tasks will be executed when your server boots for the first time. There are two ways of doing this - shell scripts or cloud-init directives. We're going to talk about cloud-init directives.
Cloud-Init directives are executed when your server boots for the first time, but the syntax is slightly different. Your scenario must start with
#cloud-config line, otherwise user data directives will be rejected. For further reference, I recommend checking the cloud-init official documentation: https://cloudinit.readthedocs.io/en/latest/index.html
A simple example of a
cloud-init script that would be passed is:
In order to pass this data scenarios to Vultr API, it must be converted into base64 format. On a Linux system you would do the following for your
This output text then has to be fetched via Vultr API
user_data parameter when ordering a new server.
Putting It All Together
To see this in action, specify the resources
provider so that you can designate Vultr as the platform you're provisioning to. Your final script should look like this:
That's all there is to it! Terraform is a really great tool for automating infrastructure once you understand the syntax and how it works. I hope you liked this article!
Published at DZone with permission of Sudip Sengupta. See the original article here.
Opinions expressed by DZone contributors are their own.