Portal:MicroOS/cloud-init

跳转到:导航搜索

使用 cloud-init 配置 openSUSE MicroOS 和 openSUSE Kubic

系统将在启动阶段使用 cloud-init 进行配置。以下数据源按此顺序配置和调用:LocalDisk, NoCloud, OpenStack, None

数据源 LocalDisk

此数据源从本地磁盘读取配置文件。所需的配置文件是 meta-datauser-data 和可选的 vendor-data,必须位于本地磁盘的根目录。

数据源 NoCloud

NoCloud 数据源可以配置为在本地存储或远程网络源上查找配置数据。

远程网络源

对于远程网络源,需要在内核命令行中指定 URL:cloud-init-url=http://my.example.com/cloud.cfg

文件的格式是

#cloud-config
datasource:
  NoCloud:
    # default seedfrom is None
    # if found, then it should contain a url with:
    #    <url>user-data and <url>meta-data
    # seedfrom: http://my.example.com/<path>/

URL 的内容存储在 /etc/cloud/cloud.cfg.d/91_kernel_cmdline_url.cfg 中,即使内核命令行上的 URL 发生更改,也不会被覆盖。

本地存储

要设置网络以访问网络上的配置文件,可以使用本地配置文件。NoCloud 将在所有不包含分区表的设备上搜索,并且包含 vfat 或 iso9660 文件系统。文件系统卷标必须为 cidata

因此,这可以是 ISO 镜像、USB 存储设备或未分区的硬盘。

meta-data

instance-id: iid-abcde001
network-interfaces: |
  auto eth0
  iface eth0 inet dhcp

instance-id 用于确定这是否是“首次启动”。因此,如果对配置进行了更改,则必须更改此项。

网络配置部分中的 auto eth0 表示网络配置应在启动阶段自动启动。在此示例中,接口 `eth0` 配置为使用 dhcp。

静态网络配置如下所示

network-interfaces: |
  auto eth0
  iface eth0 inet static
  address 192.168.1.10
  network 192.168.1.0
  netmask 255.255.255.0
  broadcast 192.168.1.255
  gateway 192.168.1.1

user-data

Header

#cloud-config
debug: True
disable_root: False

所有 user-data 配置文件都必须以 #cloud-config 行开头。要获取调试输出,请设置 debug: True。如果您想使用 root 密码而不是仅 SSH 密钥登录,请设置 disable_root: False

添加授权 SSH 密钥

ssh_deletekeys: False
ssh_pwauth: True
ssh_authorized_keys:
  - ssh-rsa XXXKEY mail@example.com

ssh_deletekeys: False 告诉 cloud-init 不要删除主机旧的私钥和公钥。默认值为 True,这意味着现有密钥将被删除并生成新的密钥。这是一个坏主意,因为 ssh 会在 cloud-init 配置更改后下次尝试登录时抱怨更改或错误的密钥。

ssh_authorized_keys 将公共 SSH 密钥添加到默认用户的 authorized_keys 文件中。除非另有说明,否则为 root

ssh_pwauth: True 允许使用密码通过 ssh 登录(如果设置了密码)。ssh_pwauth: False 将阻止这样做。

设置密码

chpasswd:
  list: |
    root:linux
  expire: True

这将把 root 的密码设置为 linux。变量 expire 定义了用户是否需要在首次登录时更改密码。expire: True 表示他必须在首次登录时更改密码。除了纯文本密码,也可以指定哈希密码

chpasswd:
  list: |
    account:$6$salt$hash

上游文档

添加自定义仓库并配置 zypper

以下是如何添加您选择的自定义仓库

      zypper:
        repos:
          - id: tumbleweed-oss
            name: os-oss
            baseurl: http://download.opensuse.org/tumbleweed/repo/oss/
            enabled: 1
            autorefresh: 1
          - id: tumbleweed-oss-sources
            name: os-oss-sources
            baseurl: http://download.opensuse.org/tumbleweed/repo/src-oss/
            enabled: 0
          - id: tumbleweed-update
            name: os-update
            baseurl: http://download.opensuse.org/update/tumbleweed/
            enabled: 1
            autorefresh: 1
        config:
          reposdir: /etc/zypp/repos.dir
          servicesdir: /etc/zypp/services.d
          download.use_deltarpm: true
          # any setting in /etc/zypp/zypp.conf

此选项将向系统添加一个额外的自定义软件仓库,在本例中为 Tumbleweed OSS 和更新仓库。所有适用于 zypper 的 *.repo 文件中的有效选项都是有效选项。

时区

timezone: Europe/Berlin

设置此实例的时区。timezone 的值必须存在于 /usr/share/zoneinfo 中。

主机名

hostname: myhost
fqdn: myhost.example.com

域名服务器

manage_resolv_conf: true
resolv_conf:
  nameservers: ['8.8.4.4', '8.8.8.8']
  searchdomains:
    - foo.example.com
    - bar.example.com
  domain: example.com
  options:
    rotate: true
    timeout: 1

Salt Minion

salt_minion:
  conf:
    master: salt.example.com

  public_key: |
    -----BEGIN PUBLIC KEY-----
    XXX
    -----END PUBLIC KEY-----

  private_key: |
    -----BEGIN RSA PRIVATE KEY-----
    XXX
    -----END RSA PRIVATE KEY-----

Salt Minion 仅适用于 openSUSE Kubic,不适用于 openSUSE MicroOS

NTP 服务器

ntp:
  servers:
    - ntp1.example.com
    - ntp2.example.com
    - ntp3.example.com
runcmd:
  - /usr/bin/systemctl enable --now ntpd

使用此配置,ntp 将在首次启动期间配置为使用三个 ntp 服务器。此外,ntp 服务将立即启用并启动。系统一次只能运行一个时间服务。systemd-timesyncd、chrony 或其他时间服务需要先禁用。

上游文档

键盘布局

runcmd:
  - /usr/bin/localectl set-keymap de-latin1-nodeadkeys

键盘布局设置为带有 nodeadkeys 的德语布局。

openSUSE Kubic Kubeadm Node

openSUSE Kubic kubeadm 节点的配置目前尚未实现。

openSUSE Kubic Cluster Node

使用 cloud-init 配置 openSUSE Kubic 集群节点目前尚未实现。

runcmd 语句

user-data 文件中只能有一个 runcmd: 语句。所有不同的语句必须组合成一个。

上游文档

openSUSE MicroOS 的示例配置

如果您使用 YaST2 或 autoyast 安装,则无需配置网络、更新通道、用户帐户和密码或类似内容。但是,您仍然可以使用 cloud-init 进行微调和额外的配置。如果您使用现成的虚拟化镜像,则需要类似以下内容。此示例可以使用所有有效的 cloud-init 选项进行增强。

meta-data

instance-id: iid-MICROOS01
network-interfaces: |
  auto eth0
  iface eth0 inet dhcp

user-data

#cloud-config
debug: True
disable_root: False
ssh_pwauth: True
ssh_deletekeys: False
ssh_authorized_keys:
  - ssh-rsa AAAAxxx== mail@exmaple.com
chpasswd:
  list: |
    root:$6$salt$hash
  expire: False
zypper:
  repos:
    - id: tumbleweed-oss
      name: os-oss
      baseurl: http://download.opensuse.org/tumbleweed/repo/oss/
      enabled: 1
      autorefresh: 1
    - id: tumbleweed-update
      name: os-update
      baseurl: http://download.opensuse.org/update/tumbleweed/
      enabled: 1
      autorefresh: 1
  config:
    download.use_deltarpm: true