Cloud-init
Automate the configuration of your cloud instances using cloud-init.
What is Cloud-init?
Cloud-init is an industry standard for cross-platform cloud instance initialization. It is supported on most Linux distributions and is used to perform early initialization of a cloud instance during boot time.
With cloud-init, you can automate many common tasks when deploying instances, such as:
- Setting the hostname
- Adding SSH keys
- Creating users and groups
- Installing packages
- Running custom scripts
- Mounting file systems
- Configuring networking
Cloud-init on MegaTech
On MegaTech, you can provide cloud-init configuration when creating a new instance. This configuration will be applied when the instance first boots up.
All Linux images provided by MegaTech come with cloud-init pre-installed and configured to run at boot time.
Note: Cloud-init is primarily used with Linux instances. Windows instances on MegaTech use a different initialization mechanism.
Cloud-init Configuration Formats
Cloud-init supports several configuration formats:
Cloud Config (YAML)
The most common format is cloud config, which uses YAML syntax. Cloud config files must start with #cloud-config
on the first line.
#cloud-config
hostname: my-instance
users:
- name: myuser
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
package_update: true
packages:
- git
- docker.io
- python3-pip
runcmd:
- systemctl start docker
- systemctl enable docker
Shell Scripts
You can also use shell scripts, which must start with #!/bin/sh
or similar shebang.
#!/bin/bash
# Update system
apt-get update
apt-get upgrade -y
# Install packages
apt-get install -y nginx git python3-pip
# Configure nginx
systemctl start nginx
systemctl enable nginx
# Clone repository
git clone https://github.com/myuser/myproject.git /opt/myproject
Common Cloud-init Tasks
Creating Users
#cloud-config
users:
- name: myuser
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
Installing Packages
#cloud-config
package_update: true
package_upgrade: true
packages:
- nginx
- docker.io
- python3-pip
- build-essential
Running Commands
#cloud-config
runcmd:
- systemctl start docker
- systemctl enable docker
- docker pull nginx:latest
- [ pip3, install, django, flask, requests ]
Writing Files
#cloud-config
write_files:
- path: /etc/nginx/sites-available/default
content: |
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
- path: /var/www/html/index.html
content: |
<!DOCTYPE html>
<html>
<head>
<title>Welcome to my instance!</title>
</head>
<body>
<h1>Instance initialized with cloud-init</h1>
</body>
</html>
Using cloud-init with MegaTech
When creating a new instance through the MegaTech dashboard:
- Navigate to the instance creation page
- Select your preferred Linux image
- Scroll down to the "Advanced Options" section
- Look for the "User Data" or "Cloud-init" field
- Paste your cloud-init configuration
- Complete the instance creation process as usual
When using the MegaTech API to create instances, you can include the cloud-init configuration in the user_data
field of your request.
Debugging Cloud-init
If your cloud-init configuration doesn't seem to be working as expected, you can check the logs to troubleshoot:
Check cloud-init status
cloud-init status
View cloud-init logs
cat /var/log/cloud-init.log
View cloud-init output log
cat /var/log/cloud-init-output.log
Common issues: YAML syntax errors are the most frequent problem with cloud-init configurations. Make sure your indentation is correct and that you're using the proper format for your configuration.
Last updated 8 months ago
Was this helpful?