「cloud-init」- 网络配置(NoCloud)

  CREATED BY JENKINSBOT

问题描述

网络无法在 user-data 中直接配置。

该笔记将记录:在 cloud-init 中,配置网络的方法。

解决方案

方案一、官方方法

cat > user-data <<EOF
#cloud-config
...
EOF

cat > network-config-v2.yaml <<EOF
version: 2
renderer: networkd
ethernets:
  enp1s0:
    addresses:
      - 172.31.253.50/24
    gateway4: 172.31.253.1
    nameservers:
      addresses: [223.6.6.6, 223.5.5.5]
EOF

cloud-localds -v --network-config=network-config-v2.yaml user-data.iso user-data

方案二、变通方法

通过 write_files 与 runcmd 结合:
1)kvm virtualization – how to add network configuration to cloud init image to run a kvm image – Server Fault

补充说明

通过挂载,我们能看到其中的组织方式:

# mount user-data.iso /mnt/
mount: /mnt: WARNING: device write-protected, mounted read-only.

# ls /mnt/
.  ..  meta-data  network-config  user-data

# cat /mnt/network-config 
version: 2
renderer: networkd
ethernets:
  enp1s0:
    addresses:
      - 172.31.253.50/24
    gateway4: 172.31.253.1
    nameservers:
      addresses: [223.6.6.6, 223.5.5.5]

常见问题

Top level network key in network-config but missing ‘config’ or ‘version’

Bug #1906187 “Version tag is not respected when put last” : Bugs : netplan.io package : Ubuntu
cloud-init/DataSourceNoCloud.py at main · canonical/cloud-init

虽然 network-config-v2.yaml 为 netplan 配置格式,但不能包含 network 键。否则虚拟机的网络将配置失败,并在 /var/log/clous-init.log 中提示 …Top level network key in network-config but missing ‘config’ or ‘version’… 错误。

参考文献

netplan – cloud-init ignoring static IP network configuration – Stack Overflow