クラウド初期化
cloud-init は、クラウド インスタンスを初期化するための業界標準ツールであり、最初の起動時にシステム構成を自動的に完了します。 Ubuntu 26.04 Server には、cloud-init がプリインストールされており、AWS、Azure、GCP などの主要なクラウド プラットフォームをサポートしています。
仕組み
Cloud-init は、インスタンスの起動時に次の段階を実行します。
- ジェネレーター - インスタンスがクラウド環境で実行されているかどうかを検出します。
- ローカル - ローカル データ ソースからネットワーク構成を取得します
- ネットワーク - メタデータとユーザーデータを取得します
- Config - 構成モジュールを実行します
- Final - 最終モジュールを実行します (スクリプトの実行など)
# Check cloud-init version
cloud-init --version
# Check cloud-init status
cloud-init status
# View detailed status
cloud-init status --longデータソース
メタデータ
クラウド プラットフォームによって提供されるインスタンス メタデータ (インスタンス ID、IP アドレス、ホスト名など):
# Retrieve metadata on AWS EC2 (IMDSv2)
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/
# Retrieve metadata on GCP
curl -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/
# Retrieve metadata on Azure
curl -H "Metadata: true" "http://169.254.169.254/metadata/instance?api-version=2021-02-01"ユーザーデータ
複数の形式をサポートするユーザー定義の初期化構成。
ユーザーデータの構成
Cloud-config 形式 (最も一般的)
#cloud-config で始まる YAML ファイル:
#cloud-config
# Set hostname
hostname: web-server-01
fqdn: web-server-01.example.com
# Create users
users:
- name: deploy
groups: sudo, docker
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAA... deploy@workstation
- name: monitor
groups: []
shell: /bin/bash
ssh_authorized_keys:
- ssh-ed25519 AAAA... monitor@management
# Set timezone
timezone: America/New_York
# NTP configuration
ntp:
enabled: true
servers:
- pool.ntp.org
- time.google.com
# Locale
locale: en_US.UTF-8
# Change APT mirror
apt:
primary:
- arches: [default]
uri: http://archive.ubuntu.com/ubuntu
# Install packages
packages:
- nginx
- docker.io
- fail2ban
- htop
- curl
- vim
# Upgrade all packages
package_update: true
package_upgrade: true
package_reboot_if_required: true
# Write files
write_files:
- path: /etc/nginx/sites-available/default
content: |
server {
listen 80;
server_name _;
root /var/www/html;
index index.html;
}
owner: root:root
permissions: '0644'
- path: /etc/sysctl.d/99-custom.conf
content: |
net.ipv4.ip_forward = 1
net.core.somaxconn = 65535
owner: root:root
permissions: '0644'
# Run commands (executed during cloud-init config stage)
runcmd:
- sysctl -p /etc/sysctl.d/99-custom.conf
- systemctl enable --now nginx
- systemctl enable --now fail2ban
- ufw allow ssh
- ufw allow 80/tcp
- ufw allow 443/tcp
- ufw --force enable
# Enable/disable SSH password login
ssh_pwauth: false
# SSH key configuration
ssh_keys:
ed25519_private: |
-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----
ed25519_public: ssh-ed25519 AAAA...
# Set passwords (hashed)
chpasswd:
expire: false
users:
- name: deploy
type: RANDOM
# Final message
final_message: "Cloud-init complete! Took $UPTIME seconds."
# Post-boot action
power_state:
mode: reboot
message: "cloud-init configuration complete, rebooting..."
timeout: 30
condition: trueシェルスクリプト形式
#!/bin/bash から始まる:
#!/bin/bash
set -euxo pipefail
# Update the system
apt update && apt upgrade -y
# Install base tools
apt install -y nginx docker.io
# Configure the firewall
ufw allow ssh
ufw allow 80/tcp
ufw --force enable
# Custom script
echo "Setup complete at $(date)" > /var/log/cloud-init-custom.logマルチパート形式 (MIME マルチパート)
複数の形式を組み合わせる:
# Use the cloud-init tool to create a multi-part message
cloud-init devel make-mime \
--attach script.sh:text/x-shellscript \
--attach config.yaml:text/cloud-config \
> combined-userdata.txt一般的な使用例
Docker環境を初期化する
#cloud-config
packages:
- docker.io
- docker-compose-v2
groups:
- docker
users:
- name: deploy
groups: sudo, docker
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAA...
write_files:
- path: /etc/docker/daemon.json
content: |
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
runcmd:
- systemctl enable --now docker
- docker pull nginx:alpineディスクの初期化
#cloud-config
disk_setup:
/dev/vdb:
table_type: gpt
layout: true
overwrite: false
fs_setup:
- label: data
filesystem: ext4
device: /dev/vdb1
overwrite: false
mounts:
- ["/dev/vdb1", "/data", "ext4", "defaults,noatime", "0", "2"]
runcmd:
- mkdir -p /dataクラスターに参加する
#cloud-config
runcmd:
- curl -sfL https://get.k3s.io | K3S_URL=https://master:6443 K3S_TOKEN=mytoken sh -ローカルテスト
マルチパスの使用
# Install Multipass
sudo snap install multipass
# Launch an instance with cloud-config
multipass launch 26.04 --name test --cloud-init cloud-config.yaml
# Enter the instance
multipass shell test
# View cloud-init logs
multipass exec test -- cat /var/log/cloud-init-output.logLXDの使用
# Test cloud-init via LXD
lxc launch ubuntu:26.04 test --config=user.user-data="$(cat cloud-config.yaml)"
lxc exec test -- cloud-init status --wait
lxc exec test -- cat /var/log/cloud-init-output.logローカルで構成を検証する
# Validate cloud-config syntax
cloud-init schema --config-file cloud-config.yaml
# Use the devel subcommand for debugging
cloud-init devel render /path/to/userdata.yamlログとデバッグ
# Main log file
sudo cat /var/log/cloud-init.log
# Output log
sudo cat /var/log/cloud-init-output.log
# View executed modules
sudo cat /run/cloud-init/result.json
# View user data
sudo cat /var/lib/cloud/instance/user-data.txt
# View instance data
sudo cloud-init query --all
# Query specific fields
sudo cloud-init query instance_id
sudo cloud-init query region
sudo cloud-init query local_hostnameクラウド初期化を再実行します
# Clear cloud-init state (for testing)
sudo cloud-init clean
# Clear and reboot to re-execute
sudo cloud-init clean --reboot
# Clear logs only
sudo cloud-init clean --logs
# Manually trigger specific stages
sudo cloud-init init
sudo cloud-init modules --mode config
sudo cloud-init modules --mode finalクラウド初期化を無効にする
非クラウド環境では、cloud-init を無効にすることができます。
# Method 1: Create a disable marker file
sudo touch /etc/cloud/cloud-init.disabled
# Method 2: Via GRUB kernel parameter
# Add the following to GRUB_CMDLINE_LINUX in /etc/default/grub:
# cloud-init=disabled
# Method 3: Uninstall
sudo apt remove cloud-init -y
sudo rm -rf /etc/cloud /var/lib/cloud