我正在尝试使用 CloudInit 在 KVM 虚拟机管理程序环境中启动虚拟机。因此,我想创建两个新的以太网接口ens3
和ens4
。我已下载 Ubuntu 18.04 映像并创建了以下配置文件:
#cloud-config
package_upgrade: true
users:
- name: <your_user>
groups: wheel
lock_passwd: false
passwd: <your_passord_hash>
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- <your_public_ssh_key>
使用这个,我可以启动虚拟机并通过控制台登录。但我不知道如何使用 cloud-init 配置我的网络接口、IP 地址等。我无法理解 cloud-init 文档。
任何链接或提示都将受到赞赏。
答案1
好的,下面是我
针对 ubuntu 镜像解决问题的方法,您可以使用位于 /etc/netplan/SOMEFILE.yaml 的 netplan 文件,
对于 centos,我注意到镜像没有 net plan,所以我只在我的配置中编写了接口文件,
使用 write_files 在 ubuntu 上创建 netplan 文件,在 centos 上创建接口文件,
然后使用 runcmd 通过运行适当的命令来初始化您的接口,
这里分别是我针对 ubuntu 和 centos 的 config.yaml 文件
#cloud-config
package_upgrade: true
users:
- name: USER
groups: wheel
lock_passwd: false
passwd: PASSWORD_HASH
shell: /bin/bash
ssh_pwauth: true
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa YOUR_KEY
write_files:
- path: /etc/netplan/50-cloud-init.yaml
permissions: '0644'
content: |
network:
version: 2
renderer: networkd
ethernets:
ens3:
addresses: [IP/SUBNET]
routes:
- to: NETWORK
via: NEXT_HOP
ens4:
addresses: [IP/SUBNET]
gateway4: GATEWAY
nameservers:
addresses: [DNS]
runcmd:
- [sudo, ifconfig, IFNAME, up]
- [sudo, ifconfig, IFNAME, up]
- [sudo, netplan, generate]
- [sudo, netplan, apply]
- [sudo, sed ,-i, 's/PasswordAuthentication no/PasswordAuthentication yes/g', /etc/ssh/sshd_config]
- [sudo, systemctl, restart, sshd]
#cloud-config
package_upgrade: true
users:
- name: USER
groups: wheel
lock_passwd: false
passwd: PASSWORD_HASH
shell: /bin/bash
ssh_pwauth: true
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa YOUT_KEY
write_files:
- path: /etc/sysconfig/network-scripts/ifcfg-INTERFACE
permissions: '0644'
content: |
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
IPV4_FAILURE_FATAL="no"
NAME="INTERFACE"
DEVICE="INTERFACE"
ONBOOT="yes"
IPADDR="IP"
PREFIX="PREFIX"
GATEWAY="GATEWAY"
DNS1="DNS"
ZONE=ZONE
- path: /etc/sysconfig/network-scripts/ifcfg-INTERFACE
permissions: '0644'
content: |
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
NAME=INTERFACE
DEVICE=INTERFACE
ONBOOT=yes
IPADDR=IP
PREFIX=PREFIX
IPV6_PRIVACY=no
ZONE=ZONE
runcmd:
- [sudo, ifup, INTERFACE]
- [sudo, ifup, INTERFACE]
- [sudo, sed ,-i, 's/PasswordAuthentication no/PasswordAuthentication yes/g', /etc/ssh/sshd_config]
- [sudo, systemctl, restart, sshd]