User data plays a pivotal role in the rapid initialization of cloud instances, facilitating the automatic configuration of instances upon boot-up. Whether through user data module, script commands or YAML-formatted cloud-config information, user data imbues instances with unique settings and software, transforming them from generic Ubuntu images into fully configured servers within moments. By leveraging cloud-init, instances gain their distinct personalities, as user data is seamlessly applied during the instance creation process, ensuring efficient and customized cloud computing experiences.
In our previous chapter on User Data, we covered the basics of registering and deploying user data. However, the number of use cases for user data is vast, making it impossible to cover them all. In this tutorial, we will take a more hands-on approach and demonstrate practical examples by deploying some of the most commonly used user data by our customers. We will walk through specific scenarios to show how user data can be effectively utilized to automate and streamline various tasks. By the end of this tutorial, you will have a clearer understanding of how to apply user data and will be able to add additional content on your own. So without further ado, let's get started.
To complete the actions below, you must have:
Setting up Docker using user data enables automated configuration and deployment of Docker environments on our IaaS Cloud. This process streamlines the initialization of Docker containers and services, ensuring consistent and efficient setup across multiple instances. For an easy Docker setup, follow these steps:
1. Navigate to Compute → User Data on the left navigation bar.
2. Click on Register a userdata in the top row:
3. Specify the following information accordingly:
#cloud-config
package_update: true
package_upgrade: true
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
runcmd:
- curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
- add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- systemctl start docker
- systemctl enable docker
- docker run -d -p 80:80
4. Once everything is set up, your Docker user data should look similar to this:
5. Click on OK to save the user data.
6. Once the user data is registered, we can create our instance. Navigate to Compute → Instances on the left navigation bar.
7. Click on the Add instance button in the top row:
8. First, choose a deployment Zone.
9. Next, select the Debian 11 template from the list.
You always have the option to adjust your service offering later on, easily increasing or decreasing your instance's performance. For a detailed step-by-step guide, skip to the Changing Service Offerings chapter.
Optionally, select an additional Data disk. If nothing is selected, you will receive an standard 8GB root disk.
Now, you need to select the previously created Network.
You can select an SSH key pair, which you must create beforehand. This step is optional, but we highly recommend SSH key pairs for added security and ease of access.
Click on Advanced mode and select the Docker user data from the list:
Finally, you can configure some last details and give your instance optionally a Name and Group.
Click on Launch instance to create your instance and initiate Docker deployment right away.
Launching your instance for the first time may take a few minutes. Once your instance is running, you will receive a notification and a popup containing the password for your VM. Copy and save it for future reference.
docker -v
to verify that your Docker environment was successfully deployed.Configuring a LAMP stack with user data automates the deployment of Apache, MySQL, and PHP, optimizing the setup of web servers and databases on our IaaS Cloud. This approach ensures uniformity and efficiency across instances, simplifying the process of creating robust web applications. For a smooth LAMP stack setup, follow these steps:
1. Navigate to Compute → User Data on the left navigation bar.
2. Click on Register a userdata in the top row:
3. Specify the following information accordingly:
#cloud-config
package_update: true
package_upgrade: true
packages:
- software-properties-common
- wget
- curl
- apache2
runcmd:
- curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
- sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y
- service apache2 restart
- apt install mariadb-server mariadb-client -y
- pw=$(openssl rand -base64 18); mysqladmin -u root -h localhost password "$pw"; echo "mysql_password=$pw" >> /home/mysql_access.txt
- mysqladmin reload
4. Once everything is set up, your Docker user data should look similar to this:
5. Click on OK to save the user data.
6. Once the user data is registered, we can create our instance. Navigate to Compute → Instances on the left navigation bar.
7. Click on the Add instance button in the top row:
8. First, choose a deployment Zone.
9. Next, select the Debian11 template from the list.
You always have the option to adjust your service offering later on, easily increasing or decreasing your instance's performance. For a detailed step-by-step guide, skip to the Changing Service Offerings chapter.
Optionally, select an additional Data disk. If nothing is selected, you will receive an standard 8GB root disk.
Now, you need to select the previously created Network.
You can select an SSH key pair, which you must create beforehand. This step is optional, but we highly recommend SSH key pairs for added security and ease of access.
Click on Advanced mode and select the LAMP user data from the list:
Finally, you can configure some last details and give your instance optionally a Name and Group.
Click on Launch instance to create your instance and initiate LAMP deployment right away.
Launching your instance for the first time may take a few minutes. Once your instance is running, you will receive a notification and a popup containing the password for your VM. Copy and save it for future reference.
php -v
or systemctl status apache2
to verify that your LAMP environment was successfully deployed.Your root MySQL password is saved in the file
/home/mysql_access.txt
. Use this password to access your database or change it if necessary.
Congratulations! You've successfully navigated through the practical application of user data in deploying Docker and LAMP stack environments on our IaaS Cloud platform. This hands-on tutorial has demonstrated how user data can automate and streamline the configuration of instances, transforming them into fully operational servers tailored to your specific needs.
With these configuration files, you can easily replicate setups across multiple instances. Need another LAMP-based website or a Docker container? Just spin up a new machine and deploy. If an instance goes down, you can quickly create a new one with all configurations applied, enhancing both replication and disaster recovery. This means less manual work and a more efficient workflow.
This tutorial is just the beginning. Explore additional use cases and customize user data scripts to fit your needs. We encourage you to explore additional use cases and customize user data scripts to fit your unique requirements. Our extensive Documentation and Tutorials are always available to support your ongoing cloud journey.
Should you have any questions or need further assistance, our support team is ready to help. Embrace the capabilities of our IaaS Cloud, and continue to harness its potential for your cloud computing needs. Enjoy your experience and make the most out of the powerful features at your disposal!