1
0
wiki/Technology/OperatingSystem/Automation/Cloud-init-快速部署.html

805 lines
549 KiB
HTML
Raw Permalink Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>Cloud-init</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="Cloud-init"/><meta property="og:description" content="Cloud-init 是一个在云计算环境中自动化配置虚拟机实例的工具,支持多种云平台如 AWS、Azure、Google Cloud 等。它通过用户提供的配置脚本或元数据进行初始化确保实例快速、统一地完成设置过程。Cloud-init 具有自动化配置、跨平台支持、模块化设计和开源社区支持的优势。."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../static/icon.png"/><meta name="description" content="Cloud-init 是一个在云计算环境中自动化配置虚拟机实例的工具,支持多种云平台如 AWS、Azure、Google Cloud 等。它通过用户提供的配置脚本或元数据进行初始化确保实例快速、统一地完成设置过程。Cloud-init 具有自动化配置、跨平台支持、模块化设计和开源社区支持的优势。."/><meta name="generator" content="Quartz"/><link href="../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/OperatingSystem/Automation/Cloud-init-快速部署"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561
<h3 id="概述">概述<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#概述" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Cloud-init 是一个用于在云计算环境中自动化配置虚拟机实例的工具。它能够在实例启动时根据用户提供的配置脚本或元数据进行初始化和配置使得实例能够快速、统一地完成设置过程。Cloud-init 被广泛用于各种云平台,如 AWS、Azure、Google Cloud、OpenStack 等。</p>
<h4 id="历史与发展">历史与发展<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#历史与发展" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><a href="https://cloud-init.io/" class="external">Cloud-init<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 最早由 Canonical 公司开发,最初用于 Ubuntu 服务器的自动化配置。随着时间的推移Cloud-init 的功能不断扩展,逐渐支持更多的操作系统和云平台。现在,它已经成为云计算环境中不可或缺的配置工具,拥有一个活跃的开源社区,持续推动其发展和改进。</p>
<h4 id="为什么选择-cloud-init">为什么选择 Cloud-init<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#为什么选择-cloud-init" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ol>
<li><strong>自动化配置</strong>Cloud-init 可以自动执行实例初始化和配置,减少手动干预,提升效率。</li>
<li><strong>跨平台支持</strong>Cloud-init 支持多种云平台和操作系统,具有广泛的适用性。</li>
<li><strong>模块化设计</strong>:通过模块化架构,用户可以根据需求选择和定制功能。</li>
<li><strong>开源和社区支持</strong>Cloud-init 是开源软件,拥有活跃的社区和丰富的文档支持。</li>
</ol>
<h3 id="主要特性">主要特性<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#主要特性" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ul>
<li>**无代理设计:**Cloud-init 采用无代理设计,不需要在实例中运行长期驻留的代理进程。这种设计简化了系统架构,减少了资源占用和潜在的安全风险。</li>
<li>**基于云元数据的初始化:**Cloud-init 利用云服务提供的元数据服务在实例启动时获取必要的配置信息。元数据通常包含实例的网络配置、用户数据脚本、公钥等Cloud-init 通过读取这些元数据进行相应的配置操作。</li>
<li>**支持多种云平台:**Cloud-init 支持广泛的云平台包括但不限于Amazon Web Services (AWS)、Microsoft Azure、Google Cloud Platform (GCP)、OpenStack、Oracle Cloud Infrastructure (OCI)、DigitalOcean。</li>
</ul>
<h4 id="主要优势">主要优势<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#主要优势" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ul>
<li><strong>无代理设计</strong>:无需安装和维护长期驻留的代理进程,减少系统资源占用和维护开销。</li>
<li><strong>快速初始化</strong>:在实例启动时立即执行配置,适用于快速部署环境。</li>
<li><strong>多平台支持</strong>:支持多种云服务提供商和操作系统,灵活适用。</li>
<li><strong>易于使用</strong>:使用 YAML 格式配置文件,简洁易读,方便编写和维护。</li>
</ul>
<h4 id="潜在劣势">潜在劣势<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#潜在劣势" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ul>
<li><strong>功能有限</strong>:主要用于初始配置,不适合复杂的持续配置管理和任务编排。</li>
<li><strong>单次执行</strong>:通常在实例启动时执行一次,不适合需要频繁更新配置的场景。</li>
<li><strong>依赖云平台</strong>:依赖于云服务提供商提供的元数据服务,在非云环境中使用受限。</li>
</ul>
<h3 id="模块化架构">模块化架构<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#模块化架构" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<pre><code class="mermaid">graph TD
A[cloud-init]
B1[Cloud Config]
B2[Cloud Boot]
B3[Cloud Init]
B4[Cloud Final]
C1[Data Sources]
C2[Handlers]
C3[Modules]
C4[Cloud-Init Config]
D1[Read Metadata]
D2[Write Files]
D3[Install Packages]
D4[Run Commands]
A --> B1
A --> B2
A --> B3
A --> B4
B1 --> C1
B1 --> C2
B1 --> C3
B1 --> C4
B2 --> D1
B2 --> D2
B3 --> D3
B4 --> D4
</code></pre>
<p>Cloud-init 采用模块化架构,每个功能模块负责特定的配置任务。例如,网络配置模块、用户数据处理模块、包管理模块等。用户可以根据需求启用或禁用特定模块,并且可以编写自定义模块以满足特殊需求。</p>
<h3 id="应用场景">应用场景<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#应用场景" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="云实例初始化">云实例初始化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#云实例初始化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>**Cloud-init 最常见的应用场景是云实例的初始化。**在实例启动时Cloud-init 自动获取并应用配置完成系统设置、网络配置、用户账户创建、SSH 密钥安装等任务,使实例能够快速投入使用。</p>
<h4 id="配置管理">配置管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 可以用作配置管理工具执行复杂的系统配置任务。例如安装和配置软件包、设置系统参数、管理用户权限等。通过用户数据脚本用户可以定义详细的配置步骤Cloud-init 在实例启动时自动执行这些步骤。</p>
<h4 id="应用部署">应用部署<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#应用部署" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 还可以用于自动化应用部署。用户可以在用户数据脚本中包含应用安装和配置指令Cloud-init 在实例启动时执行这些指令,完成应用的自动化部署。这种方式适用于 DevOps 流程,能够加速应用的交付和更新。</p>
<h2 id="cloud-init-安装与配置">Cloud-init 安装与配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-安装与配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="系统要求">系统要求<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#系统要求" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Cloud-init 支持多种操作系统包括但不限于Ubuntu、CentOS、Fedora、Red Hat Enterprise Linux (RHEL)、Debian、SUSE Linux Enterprise Server (SLES)、Oracle Linux、FreeBSD。</p>
<p>这些操作系统在不同的云平台上广泛使用,使得 Cloud-init 能够在多种环境中提供一致的自动化配置功能。</p>
<h4 id="必要的依赖项">必要的依赖项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#必要的依赖项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在安装 Cloud-init 之前,确保系统满足以下基本依赖项:</p>
<ul>
<li>**Python**Cloud-init 主要用 Python 编写,因此需要系统中预装有 Python通常是 Python 3</li>
<li>**网络工具:**如 <code>curl</code><code>wget</code>,用于从数据源获取元数据。</li>
<li>**包管理工具:**如 <code>apt</code><code>yum</code><code>zypper</code>,用于安装 Cloud-init 和相关依赖包。</li>
</ul>
<h3 id="安装步骤">安装步骤<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#安装步骤" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="通过包管理器安装">通过包管理器安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#通过包管理器安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在大多数 Linux 发行版中Cloud-init 都可以通过包管理器安装。根据具体的操作系统选择合适的包管理工具:</p>
<ul>
<li>
<p>Debian/Ubuntu 系列:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt-get</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cloud-init</span></span></code></pre></figure>
</li>
<li>
<p>RHEL/CentOS 系列:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> yum</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cloud-init</span></span></code></pre></figure>
</li>
<li>
<p>SUSE 系列:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zypper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cloud-init</span></span></code></pre></figure>
</li>
</ul>
<h4 id="从源代码安装">从源代码安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#从源代码安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ol>
<li>
<p>克隆 Cloud-init 源代码仓库:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/canonical/cloud-init.git</span></span></code></pre></figure>
</li>
<li>
<p>切换到源码目录:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cloud-init</span></span></code></pre></figure>
</li>
<li>
<p>安装必要的依赖:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt-get</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-pip</span></span></code></pre></figure>
</li>
<li>
<p>安装 Cloud-init</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> setup.py</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span></span></code></pre></figure>
</li>
</ol>
<h2 id="cloud-init-自动化管理基础">Cloud-init 自动化管理基础<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-自动化管理基础" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="cloud-init-语法">Cloud-init 语法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-语法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Cloud-init 配置文件由多个配置段组成,每个配置段负责特定的初始化任务。常见的配置段包括用户定义、网络配置、包管理等。</p>
<h4 id="配置示例">配置示例<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置示例" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>以下是一个 Cloud-init 配置文件的示例结构:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 1. 定义用户和其相关配置</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置了一个名为 `ubuntu` 的用户,并设置了 SSH 公钥、sudo 权限和 shell。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> sudo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'ALL=(ALL) NOPASSWD:ALL'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> groups</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">sudo</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr...</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 用户的 SSH 公钥</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> shell</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/bin/bash</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 2. 配置网络设置</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启用了 DHCP 以自动获取 IP 地址。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">network</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ethernets</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> eth0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> dhcp4</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 3. 配置系统包更新和升级选项</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 启用了系统包更新和升级。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">package_update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">package_upgrade</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 4. 配置 apt 包管理器的相关选项</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">apt</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> sources</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> my_ppa</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> source</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;ppa:my/ppa&quot;</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> preserve_sources_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">false</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 5. 配置 snap 包管理器的相关选项</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">snappy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> snaps</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">core</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> channel</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">stable</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> channel</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">stable</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 6. 创建或修改两个文件</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">write_files</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">path</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/etc/myconfig.conf</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> content</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> This is my config file</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">path</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/etc/motd</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> content</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> Welcome to my custom server!</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 7. 在实例引导过程中运行命令</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">bootcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo 'Booting...'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mkdir -p /etc/mydir</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 8. 在实例初始化过程中运行自定义命令</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo 'Hello, world!'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">cloud-init</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">status</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">--wait</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 9. 配置系统的语言环境locale</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">locale</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">en_US.UTF-8</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 10. 配置系统时区</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">timezone</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">America/New_York</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 11. 配置 NTP网络时间协议服务</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">ntp</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> servers</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ntp.ubuntu.com</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ntp.ubuntu.net</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 12. 配置用户的 SSH 公钥,用于无密码登录</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">ssh_authorized_keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr...</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 13. 配置根文件系统自动扩展</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">resize_rootfs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 14. 配置在 Cloud-init 最终阶段运行的模块</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cloud_final_modules</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">scripts-user</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">phone-home</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">service</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 15. 配置实例初始化完成后发送通知</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">phone_home</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">http://example.com/phone_home</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> post</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">instance_id</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">hostname</span></span></code></pre></figure>
<h3 id="cloud-init-模块">Cloud-init 模块<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-模块" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="常用模块">常用模块<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#常用模块" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 通过模块化设计,将不同的初始化任务分配给不同的模块。以下是一些常用的 Cloud-init 模块:</p>
<div class="table-container"><table><thead><tr><th>模块</th><th>描述</th><th>运行阶段</th><th>权重</th></tr></thead><tbody><tr><td><strong>bootcmd</strong></td><td>在系统引导过程中运行命令,早于其他 Cloud-init 配置执行。</td><td>boot 阶段</td><td>1</td></tr><tr><td><strong>growpart</strong></td><td>扩展分区大小。</td><td>init 阶段</td><td>2</td></tr><tr><td><strong>resize_rootfs</strong></td><td>配置根文件系统自动扩展。</td><td>init 阶段</td><td>3</td></tr><tr><td><strong>set_hostname</strong></td><td>设置实例的主机名。</td><td>init 阶段</td><td>4</td></tr><tr><td><strong>write_files</strong></td><td>创建或修改文件。</td><td>init 阶段</td><td>5</td></tr><tr><td><strong>users</strong></td><td>定义用户和其相关配置如用户名、SSH 密钥等。</td><td>init 阶段</td><td>6</td></tr><tr><td><strong>locale</strong></td><td>配置系统的语言环境locale</td><td>init 阶段</td><td>7</td></tr><tr><td><strong>timezone</strong></td><td>配置系统时区。</td><td>init 阶段</td><td>8</td></tr><tr><td><strong>ntp</strong></td><td>配置 NTP网络时间协议服务。</td><td>init 阶段</td><td>9</td></tr><tr><td><strong>apt</strong></td><td>配置 apt 包管理器的相关选项。</td><td>init 阶段</td><td>10</td></tr><tr><td><strong>yum</strong></td><td>配置 yum 包管理器的相关选项。</td><td>init 阶段</td><td>11</td></tr><tr><td><strong>snappy</strong></td><td>配置 snap 包管理器的相关选项。</td><td>init 阶段</td><td>12</td></tr><tr><td><strong>ssh_authorized_keys</strong></td><td>配置用户的 SSH 公钥,用于无密码登录。</td><td>init 阶段</td><td>13</td></tr><tr><td><strong>package_update</strong></td><td>配置系统包更新选项。</td><td>init 阶段</td><td>14</td></tr><tr><td><strong>package_upgrade</strong></td><td>配置系统包升级选项。</td><td>init 阶段</td><td>15</td></tr><tr><td><strong>apt-configure</strong></td><td>配置 apt 包管理器。</td><td>init 阶段</td><td>16</td></tr><tr><td><strong>ssh</strong></td><td>配置 SSH 相关设置,如主机密钥和 SSH 服务的相关配置。</td><td>init 阶段</td><td>17</td></tr><tr><td><strong>disable-ec2-metadata</strong></td><td>禁用 EC2 元数据服务。</td><td>init 阶段</td><td>18</td></tr><tr><td><strong>grub-dpkg</strong></td><td>配置 GRUB 引导加载程序。</td><td>init 阶段</td><td>19</td></tr><tr><td><strong>rsyslog</strong></td><td>配置 rsyslog 服务。</td><td>init 阶段</td><td>20</td></tr><tr><td><strong>snap</strong></td><td>配置 Snap 包管理器。</td><td>init 阶段</td><td>21</td></tr><tr><td><strong>mounts</strong></td><td>配置文件系统的挂载点。</td><td>init 阶段</td><td>22</td></tr><tr><td><strong>runcmd</strong></td><td>在实例初始化过程中运行自定义命令。</td><td>final 阶段</td><td>23</td></tr><tr><td><strong>phone_home</strong></td><td>配置实例初始化完成后发送通知。</td><td>final 阶段</td><td>24</td></tr><tr><td><strong>cloud_final_modules</strong></td><td>配置在 Cloud-init 最终阶段运行的模块。</td><td>final 阶段</td><td>25</td></tr></tbody></table></div>
<h4 id="分阶段执行">分阶段执行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#分阶段执行" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>当 Cloud-init 运行时,它会在三个主要阶段中按顺序执行配置文件中的模块:</p>
<ol>
<li><strong>网络阶段 (cloud_init_modules)</strong>
<ul>
<li>处理网络相关的配置。</li>
<li>设置主机名、获取用户数据和元数据。</li>
</ul>
</li>
<li><strong>配置阶段 (cloud_config_modules)</strong>
<ul>
<li>配置用户账户和权限。</li>
<li>安装和配置软件包。</li>
<li>处理 SSH 密钥和认证设置。</li>
</ul>
</li>
<li><strong>最终阶段 (cloud_final_modules)</strong>
<ul>
<li>执行用户定义的脚本和命令。</li>
<li>触发配置管理插件。</li>
<li>记录配置状态和信息。</li>
</ul>
</li>
</ol>
<h4 id="模块执行频率">模块执行频率<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#模块执行频率" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 的模块可以配置不同的执行频率:</p>
<ul>
<li><strong>Per instance</strong>:模块在实例的首次引导时运行。例如,克隆一个实例或从保存的镜像创建新实例时,指定为 per instance 的模块会再次运行。</li>
<li><strong>Per once</strong>:模块只运行一次。例如,如果克隆实例或从保存的镜像创建新实例,指定为 per once 的模块不会在这些实例上再次运行。</li>
<li><strong>Per always</strong>:模块在每次引导时都运行。</li>
</ul>
<h4 id="自定义模块开发">自定义模块开发<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自定义模块开发" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>用户可以根据需求编写自定义模块,以扩展 Cloud-init 的功能。自定义模块需遵循 Cloud-init 的模块开发规范,并放置在合适的目录中(如 <code>/etc/cloud/cloud.cfg.d/</code>)。以下是一个简单的自定义模块示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># custom_module.py</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> os</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> cloudinit </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> util</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> handle</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(name, cfg, cloud, log, args):</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> log.info(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Running custom module&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> util.write_file(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'/var/log/custom_module.log'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Custom module executed'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<p>将自定义模块集成到 Cloud-init 配置中:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cloud_final_modules</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">custom_module</span></span></code></pre></figure>
<h3 id="cloud-init-数据源">Cloud-init 数据源<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-数据源" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Cloud-init 通过数据源获取实例的元数据和用户数据。一般 Cloud-init 使用用户数据、元数据和厂商数据来完成实例初始化:</p>
<ul>
<li><strong>用户数据</strong>:用户在 <code>cloud.cfg</code> 文件和 <code>cloud.cfg.d</code> 目录中指定的指令,如要运行的脚本、要安装的软件包等。</li>
<li><strong>元数据</strong>:与特定数据源关联的数据,如服务器名称和实例 ID。不同的云平台决定实例从哪里查找元数据和用户数据。</li>
<li><strong>厂商数据</strong>:由云提供商提供,包含用于自定义镜像的信息。</li>
</ul>
<p>**Cloud-init 的主要配置文件是 <code>/etc/cloud/cloud.cfg</code>。**该文件定义了 Cloud-init 在实例启动时执行的全局配置和行为。<strong>拓展数据源的文件通常位于 <code>/etc/cloud/cloud.cfg.d/</code> 目录下。</strong></p>
<h4 id="数据源类型">数据源类型<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据源类型" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>数据源类型决定了 Cloud-init 如何获取和处理这些数据。常见的数据源类型包括:</p>
<ul>
<li>NoCloud用于本地测试和开发数据存储在 ISO 镜像或本地文件中。</li>
<li>EC2用于 Amazon Web Services (AWS)。</li>
<li>Azure用于 Microsoft Azure。</li>
<li>GCE用于 Google Cloud Platform。</li>
<li>OpenStack用于 OpenStack 环境。</li>
</ul>
<h4 id="常用数据源">常用数据源<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#常用数据源" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>以下是一些常用的数据源配置示例:</p>
<h5 id="nocloud">NoCloud<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nocloud" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">NoCloud</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> NoCloud</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> seedfrom</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">/path/to/nocloud/seed/</span></span></code></pre></figure>
<h5 id="ec2">EC2<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#ec2" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Ec2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> Ec2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> metadata_urls</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'http://169.254.169.254'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span></code></pre></figure>
<h5 id="gce">GCE<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gce" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">GCE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> GCE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> metadata_server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'http://metadata.google.internal'</span></span></code></pre></figure>
<h5 id="azure">Azure<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#azure" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Azure</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> Azure</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> retries</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span></span></code></pre></figure>
<h4 id="数据源优先级和故障处理">数据源优先级和故障处理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据源优先级和故障处理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 可以配置多个数据源并按照优先级顺序依次尝试。当一个数据源不可用时Cloud-init 会自动尝试下一个数据源。优先级顺序通过 <code>datasource_list</code> 配置项定义:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource_list</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">NoCloud</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Ec2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">GCE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Azure</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span></code></pre></figure>
<p>可以通过配置故障处理选项,控制 Cloud-init 在数据源不可用时的行为:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">datasource</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> Ec2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> max_wait</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">120</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 最大等待时间(秒)</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 每次请求的超时时间(秒)</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> retries</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 重试次数</span></span></code></pre></figure>
<h2 id="cloud-init-的生命周期">Cloud-init 的生命周期<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-的生命周期" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>Cloud-init 的生命周期分为多个阶段,每个阶段负责特定的初始化和配置任务。理解这些阶段有助于更好地使用和调试 Cloud-init。下面是 Cloud-init 的详细生命周期描述:</p>
<pre><code class="mermaid">sequenceDiagram
participant Systemd
participant CloudInitGenerator
participant CloudInitLocal
participant CloudInitNetwork
participant CloudInitConfig
participant CloudInitFinal
Systemd ->> CloudInitGenerator: 运行 Cloud-init 生成器
CloudInitGenerator ->> CloudInitGenerator: 检查配置文件和内核参数
CloudInitGenerator ->> CloudInitGenerator: 确定数据源
CloudInitGenerator ->> Systemd: 生成相应的 systemd 单元文件
Systemd ->> CloudInitLocal: 启动 Cloud-init 本地阶段
CloudInitLocal ->> CloudInitLocal: 识别本地数据源并应用初始网络配置
CloudInitLocal ->> CloudInitLocal: 读取并处理本地存储的元数据
CloudInitLocal ->> CloudInitLocal: 设置主机名和其他基础配置
Systemd ->> CloudInitNetwork: 启动 Cloud-init 网络阶段
CloudInitNetwork ->> CloudInitNetwork: 获取完整的用户数据和元数据
CloudInitNetwork ->> CloudInitNetwork: 运行 cloud_init_modules 列出的模块
CloudInitNetwork ->> CloudInitNetwork: 处理和执行用户提供的配置脚本
Systemd ->> CloudInitConfig: 启动 Cloud-init 配置阶段
CloudInitConfig ->> CloudInitConfig: 运行 cloud_config_modules 列出的模块
CloudInitConfig ->> CloudInitConfig: 配置用户账户、安装和配置软件包
Systemd ->> CloudInitFinal: 启动 Cloud-init 最后阶段
CloudInitFinal ->> CloudInitFinal: 运行 cloud_final_modules 列出的模块
CloudInitFinal ->> CloudInitFinal: 执行用户定义的脚本和命令
CloudInitFinal ->> CloudInitFinal: 记录最终的配置状态和信息
</code></pre>
<h3 id="1-生成器阶段-generator-stage">1. 生成器阶段 (Generator Stage)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-生成器阶段-generator-stage" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>在系统引导过程中systemd 会运行 Cloud-init 生成器generator。生成器的主要任务是确定是否在当前引导中启用 Cloud-init 以及选择适当的数据源。生成器阶段包括以下任务:</p>
<ul>
<li>检查配置文件和内核参数,决定是否启用 Cloud-init。</li>
<li>确定使用哪个数据源(例如 EC2、Azure、NoCloud 等)。</li>
<li>生成相应的 systemd 单元文件,以便在后续阶段运行 Cloud-init 服务。</li>
</ul>
<h3 id="2-本地阶段-local-stage">2. 本地阶段 (Local Stage)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-本地阶段-local-stage" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>本地阶段是 Cloud-init 的第一个主要执行阶段发生在实例启动的早期阶段。在本地阶段Cloud-init 会执行以下操作:</p>
<ul>
<li>识别本地数据源,并应用初始网络配置。</li>
<li>读取并处理本地存储的元数据。</li>
<li>处理基本的网络配置,如静态 IP 设置或 DHCP 回退机制。</li>
<li>设置实例的主机名和其他基础配置。</li>
</ul>
<p>本地阶段的主要目的是确保实例在能够访问外部网络之前完成基本的配置。</p>
<h3 id="3-网络阶段-network-stage">3. 网络阶段 (Network Stage)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-网络阶段-network-stage" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>网络阶段是 Cloud-init 的第二个主要执行阶段依赖于实例已经有了基础网络连接。在这一阶段Cloud-init 会执行以下任务:</p>
<ul>
<li>通过网络访问数据源,获取完整的用户数据和元数据。</li>
<li>运行 <code>/etc/cloud/cloud.cfg</code> 文件中 <code>cloud_init_modules</code> 部分列出的模块。</li>
<li>处理和执行用户提供的配置脚本和指令。</li>
</ul>
<p>这一阶段确保实例能够通过网络访问必要的资源,并完成初步的配置任务。</p>
<h3 id="4-配置阶段-config-stage">4. 配置阶段 (Config Stage)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#4-配置阶段-config-stage" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>配置阶段是 Cloud-init 的第三个主要执行阶段负责更详细的系统配置。在这一阶段Cloud-init 会执行以下操作:</p>
<ul>
<li>运行 <code>/etc/cloud/cloud.cfg</code> 文件中 <code>cloud_config_modules</code> 部分列出的模块。</li>
<li>配置用户账户和权限、安装和配置软件包、设置系统参数等。</li>
</ul>
<p>配置阶段处理实例的核心配置任务,确保系统按预期进行初始化和设置。</p>
<h3 id="5-最后阶段-final-stage">5. 最后阶段 (Final Stage)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#5-最后阶段-final-stage" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>最后阶段是 Cloud-init 的最后一个主要执行阶段负责执行所有剩余的配置任务和用户定义的脚本。在这一阶段Cloud-init 会执行以下操作:</p>
<ul>
<li>运行 <code>/etc/cloud/cloud.cfg</code> 文件中 <code>cloud_final_modules</code> 部分列出的模块。</li>
<li>执行用户定义的脚本和命令,如软件包安装、服务启动等。</li>
<li>记录最终的配置状态和信息。</li>
</ul>
<p>最后阶段确保所有配置任务完成,并记录实例的最终状态。</p>
<h2 id="cloud-init-高级应用技巧">Cloud-init 高级应用技巧<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-高级应用技巧" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="cloud-init-用户数据">Cloud-init 用户数据<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-用户数据" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="用户数据格式和用法">用户数据格式和用法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#用户数据格式和用法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 用户数据用于在实例启动时传递自定义的配置和脚本。用户数据支持多种格式:</p>
<ul>
<li><strong>Cloud ConfigYAML 格式)</strong>:使用 <code>#cloud-config</code> 作为头标识。</li>
<li><strong>Shell 脚本</strong>:使用 <code>#!</code><code>#cloud-boothook</code> 作为头标识。</li>
<li><strong>MIME 多部分消息</strong>:支持混合多种类型的数据。</li>
</ul>
<p>示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">example-user</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3Nza...</span></span>
<span data-line> </span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Hello, World!&quot; > /var/log/hello.log</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">Content-Type</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">multipart/mixed; boundary=&quot;===============BOUNDARY==&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">MIME-Version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1.0</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">--===============BOUNDARY==</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">Content-Type</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">text/cloud-config</span></span>
<span data-line> </span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">example-user</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3Nza...</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">--===============BOUNDARY==</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">Content-Type</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">text/x-shellscript</span></span>
<span data-line> </span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Hello, World!&quot; > /var/log/hello.log</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">--===============BOUNDARY==--</span></span></code></pre></figure>
<h4 id="脚本和配置的集成">脚本和配置的集成<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#脚本和配置的集成" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>用户数据可以包含多个脚本和配置指令Cloud-init 会按照定义的顺序依次执行。例如,可以先配置用户,然后运行一个脚本来安装软件:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">example-user</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3Nza...</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">apt-get update</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">apt-get install -y nginx</span></span></code></pre></figure>
<h4 id="用户数据的传递和解析">用户数据的传递和解析<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#用户数据的传递和解析" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>用户数据可以通过多种方式传递到实例中:</p>
<ul>
<li><strong>Cloud Provider 控制台</strong>:通过云服务提供商的控制台界面输入用户数据。</li>
<li><strong>API 调用</strong>:使用云服务提供商的 API 传递用户数据。</li>
<li><strong>配置文件</strong>:在本地环境中,通过配置文件传递用户数据。</li>
</ul>
<p>Cloud-init 会在实例启动时解析这些数据,并根据配置执行相应的任务。</p>
<h3 id="调试和日志">调试和日志<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#调试和日志" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Cloud-init 在实例启动时会生成日志,记录执行的每个步骤。这些日志通常保存在 <code>/var/log/cloud-init.log</code><code>/var/log/cloud-init-output.log</code> 文件中。通过检查这些日志,可以调试和验证用户数据是否正确执行。</p>
<h2 id="cloud-init-与其他工具的比较">Cloud-init 与其他工具的比较<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-与其他工具的比较" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="table-container"><table><thead><tr><th><strong>维度</strong></th><th><strong>Cloud-init</strong></th><th><strong>Ansible</strong></th><th><strong>Puppet</strong></th></tr></thead><tbody><tr><td><strong>架构</strong></td><td>无代理,实例初始化</td><td>无代理,远程执行</td><td>代理 - 服务器,持续管理</td></tr><tr><td><strong>设计理念</strong></td><td>云实例启动时配置</td><td>配置管理和应用部署</td><td>声明式,持续配置</td></tr><tr><td><strong>使用场景</strong></td><td><strong>云实例首次配置</strong></td><td>持续配置管理,多节点</td><td>大规模持续管理</td></tr><tr><td><strong>主要优势</strong></td><td>轻量,快速配置,多平台支持</td><td>强大编排,灵活管理</td><td>自动纠正,模块丰富</td></tr><tr><td><strong>主要劣势</strong></td><td>功能有限,单次执行,依赖云平台</td><td>需维护控制节点,执行慢</td><td>需安装代理,学习曲线陡</td></tr><tr><td><strong>适用场景</strong></td><td>初始配置,快速部署,统一环境</td><td>动态配置,多节点管理</td><td>持续监控,大规模部署</td></tr><tr><td><strong>不适用场景</strong></td><td>复杂多节点,持续监控</td><td><strong>快速初始配置</strong></td><td><strong>轻量初始配置</strong></td></tr></tbody></table></div>
<h2 id="cloud-init-在实际环境中的应用">Cloud-init 在实际环境中的应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-在实际环境中的应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="cloud-init-在云环境中的应用">Cloud-init 在云环境中的应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-在云环境中的应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="在-aws-中的使用">在 AWS 中的使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-aws-中的使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在 AWS 中Cloud-init 通过 EC2 实例元数据服务获取用户数据和元数据进行初始化配置。以下是一个示例:</p>
<ol>
<li>
<p>创建一个 EC2 实例时,在“高级详细信息”部分提供用户数据。</p>
</li>
<li>
<p>例如,用户数据可以是一个简单的 Shell 脚本或 Cloud-config 配置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello from EC2&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /var/log/cloud-init.log</span></span></code></pre></figure>
<p>或者:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">service nginx start</span></span></code></pre></figure>
</li>
<li>
<p>启动实例后Cloud-init 会在首次启动时自动执行这些配置。</p>
</li>
</ol>
<h4 id="在-azure-中的使用">在 Azure 中的使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-azure-中的使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在 Azure 中Cloud-init 支持通过 Azure 元数据服务进行初始化配置。</p>
<ol>
<li>
<p>创建虚拟机时,选择“云初始化”选项来提供用户数据。</p>
</li>
<li>
<p>例如,一个简单的 Cloud-config 文件:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">default</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">apache2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">systemctl start apache2</span></span></code></pre></figure>
</li>
<li>
<p>启动虚拟机后Cloud-init 将读取用户数据并执行相应配置。</p>
</li>
</ol>
<h4 id="在-google-cloud-中的使用">在 Google Cloud 中的使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-google-cloud-中的使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在 Google Cloud 中Cloud-init 支持通过 GCE 元数据服务进行实例初始化配置。</p>
<ol>
<li>
<p>创建 VM 实例时,在“管理、安全性、磁盘、网络、单租户”部分提供启动脚本。</p>
</li>
<li>
<p>例如,一个简单的启动脚本:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/bin/bash</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello from GCE&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /var/log/cloud-init.log</span></span></code></pre></figure>
<p>或者:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mysql-server</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">systemctl start mysql</span></span></code></pre></figure>
</li>
<li>
<p>启动实例后Cloud-init 会读取用户数据并执行配置。</p>
</li>
</ol>
<h3 id="cloud-init-在容器化环境中的应用">Cloud-init 在容器化环境中的应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-在容器化环境中的应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="容器实例初始化">容器实例初始化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#容器实例初始化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 主要用于虚拟机实例的初始化,而在容器化环境中,初始化和配置任务通常由容器编排工具(如 Kubernetes和容器镜像自身完成。不过在一些场景下Cloud-init 也可以用于初始化容器主机环境。</p>
<h4 id="与-docker-的集成">与 Docker 的集成<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#与-docker-的集成" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>虽然 Cloud-init 本身不直接用于 Docker 容器的管理,但它可以用于配置运行 Docker 容器的主机。例如,配置主机环境并安装 Docker</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker.io</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">systemctl start docker</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker run hello-world</span></span></code></pre></figure>
<h4 id="与-kubernetes-的集成">与 Kubernetes 的集成<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#与-kubernetes-的集成" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 可以用于配置 Kubernetes 集群的节点,在节点启动时自动完成必要的配置和初始化步骤:</p>
<ol>
<li>
<p>安装 Kubernetes 必要的软件包:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubeadm</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubelet</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubectl</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubeadm join --token &lt;token> &lt;master-ip>:&lt;port></span></span></code></pre></figure>
</li>
<li>
<p>启动节点后Cloud-init 会自动加入节点到 Kubernetes 集群。</p>
</li>
</ol>
<h3 id="cloud-init-在传统-it-环境中的应用">Cloud-init 在传统 IT 环境中的应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cloud-init-在传统-it-环境中的应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="虚拟机初始化">虚拟机初始化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#虚拟机初始化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在传统的虚拟化环境中(如 VMware、KVMCloud-init 也可以用于虚拟机的初始化和配置。通过本地数据源NoCloud来提供用户数据和元数据</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">admin</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3Nza...</span></span></code></pre></figure>
<h4 id="本地部署配置">本地部署配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#本地部署配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在本地数据中心环境中Cloud-init 可以用于统一配置管理和自动化部署。例如,通过本地镜像或 ISO 文件传递配置数据,实现自动化初始化:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">systemctl start nginx</span></span></code></pre></figure>
<h4 id="跨平台管理">跨平台管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#跨平台管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Cloud-init 的跨平台支持使得它可以用于多种操作系统和环境中的一致性管理。在不同的平台上使用相同的 Cloud-config 文件,实现统一的配置和管理:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#cloud-config</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">users</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">user</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ssh-authorized-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ssh-rsa AAAAB3Nza...</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">packages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">git</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runcmd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">git clone https://github.com/example/repo.git /opt/repo</span></span></code></pre></figure></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#cloud-init" data-for="cloud-init">Cloud-init</a></li><li class="depth-1"><a href="#概述" data-for="概述">概述</a></li><li class="depth-1"><a href="#主要特性" data-for="主要特性">主要特性</a></li><li class="depth-1"><a href="#模块化架构" data-for="模块化架构">模块化架构</a></li><li class="depth-1"><a href="#应用场景" data-for="应用场景">应用场景</a></li><li class="depth-0"><a href="#cloud-init-安装与配置" data-for="cloud-init-安装与配置">Cloud-init 安装与配置</a></li><li class="depth-1"><a href="#系统要求" data-for="系统要求">系统要求</a></li><li class="depth-1"><a href="#安装步骤" data-for="安装步骤">安装步骤</a></li><li class="depth-0"><a href="#cloud-init-自动化管理基础" data-for="cloud-init-自动化管理基础">Cloud-init 自动化管理基础</a></li><li class="depth-1"><a href="#cloud-init-语法" data-for="cloud-init-语法">Cloud-init 语法</a></li><li class="depth-1"><a href="#cloud-init-模块" data-for="cloud-init-模块">Cloud-init 模块</a></li><li class="depth-1"><a href="#cloud-init-数据源" data-for="cloud-init-数据源">Cloud-init 数据源</a></li><li class="depth-0"><a href="#cloud-init-的生命周期" data-for="cloud-init-的生命周期">Cloud-init 的生命周期</a></li><li class="depth-1"><a href="#1-生成器阶段-generator-stage" data-for="1-生成器阶段-generator-stage">1. 生成器阶段 (Generator Stage)</a></li><li class="depth-1"><a href="#2-本地阶段-local-stage" data-for="2-本地阶段-local-stage">2. 本地阶段 (Local Stage)</a></li><li class="depth-1"><a href="#3-网络阶段-network-stage" data-for="3-网络阶段-network-stage">3. 网络阶段 (Network Stage)</a></li><li class="depth-1"><a href="#4-配置阶段-config-stage" data-for="4-配置阶段-config-stage">4. 配置阶段 (Config Stage)</a></li><li class="depth-1"><a href="#5-最后阶段-final-stage" data-for="5-最后阶段-final-stage">5. 最后阶段 (Final Stage)</a></li><li class="depth-0"><a href="#cloud-init-高级应用技巧" data-for="cloud-init-高级应用技巧">Cloud-init 高级应用技巧</a></li><li class="depth-1"><a href="#cloud-init-用户数据" data-for="cloud-init-用户数据">Cloud-init 用户数据</a></li><li class="depth-1"><a href="#调试和日志" data-for="调试和日志">调试和日志</a></li><li class="depth-0"><a href="#cloud-init-与其他工具的比较" data-for="cloud-init-与其他工具的比较">Cloud-init 与其他工具的比较</a></li><li class="depth-0"><a href="#cloud-init-在实际环境中的应用" data-for="cloud-init-在实际环境中的应用">Cloud-init 在实际环境中的应用</a></li><li class="depth-1"><a href="#cloud-init-在云环境中的应用" data-for="cloud-init-在云环境中的应用">Cloud-init 在云环境中的应用</a></li><li class="depth-1"><a href="#cloud-init-在容器化环境中的应用" data-for="cloud-init-在容器化环境中的应用">Cloud-init 在容器化环境中的应用</a></li><li class="depth-1"><a href="#cloud-init-在传<E59CA8>
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../postscript.js" type="module"></script></html>