我正在尝试使用 cloud-init 设置 kvm 客户机(目前为 Debian 11 和 CentOS Stream 8),并且正在寻求帮助来更正元数据和用户数据文件。其他一切似乎都正常,但网络接口设置未设置,尽管我确实看到 cloud-init 日志显示它们已设置。请参见下文:
对于 Debian:
cat << EOF > /var/lib/libvirt/images/$meta_data_file
instance-id: $vm_name
local-hostname: $vm_name
hostname: $vm_name
fqdn: $vm_name
manage_etc_hosts: true
EOF
cat << EOF > /var/lib/libvirt/images/$cloud_config_file
#cloud-config
# Hostname management
preserve_hostname: false
hostname: $vm_name
fqdn: $vm_name
network:
version: 2
ethernets:
eth0:
match:
name: e*
dhcp4: false
addresses:
- 10.10.0.25/24
gateway4: 10.10.0.254
nameservers:
addresses:
- 10.50.0.23
- 10.50.0.17
- 10.50.0.18
search: [testing,production,admin,internal]
routes:
- to: 10.50.0.0/24
via: 10.10.0.249
users:
- default
- name: admin
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel, sudo, admin
home: /home/admin
shell: /bin/bash
hashed_passwd: $adminpasswd
lock_passwd: false
ssh_pwauth: True
chpasswd: { expire: False }
ssh-authorized-keys:
- ssh-rsa ...
- ssh-rsa ...
# only cert auth via ssh (console access can still login)
ssh_pwauth: True
disable_root: false
chpasswd:
list: |
root:$rtpwd
expire: False
runcmd:
# disable dhcp for eth0
- [ sh, -c, sed -e '/iface eth0 inet dhcp/s/^/#/g' -i /etc/network/interfaces ]
bootcmd:
- cloud-init-per always fix-debian-autonet rm /etc/udev/rules.d/75-cloud-ifupdown.rules
- cloud-init-per always fix-debian-netconfig rm /run/network/interfaces.d/*
- cloud-init-per once ifdown ifdown ens3
- cloud-init-per once bugfix rm /run/network/interfaces.d/ens3
- cloud-init-per once ifup ifup ens3
# Configure where output will go
output:
all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server
ssh_svcname: ssh
ssh_deletekeys: True
ssh_genkeytypes: ['rsa', 'ecdsa']
package_update: true
package_upgrade: true
packages:
- bind9-utils
- vim
- freeipa-client
- cloud-utils-growpart
power_state:
delay: "+2" #minutes
mode: reboot
message: Run completed
timeout: 120 #seconds
condition: True
EOF
对于 Centos Stream 8:
cat << EOF > /var/lib/libvirt/images/$cloud_config_file
#cloud-config
# Hostname management
preserve_hostname: false
hostname: $vm_name
fqdn: $vm_name
network:
version: 2
ethernets:
eth0:
match:
name: e*
dhcp4: false
addresses:
- 10.50.0.26/24
gateway4: 10.50.0.254
nameservers:
addresses:
- 10.15.0.23
- 10.15.0.17
- 10.15.0.18
search: [testing,production,admin,internal]
routes:
- to: 10.15.0.0/24
via: 10.50.0.249
users:
- default
- name: admin
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel, sudo, admin
home: /home/admin
shell: /bin/bash
hashed_passwd: $adminpasswd
lock_passwd: false
ssh_pwauth: True
chpasswd: { expire: False }
ssh-authorized-keys:
- ssh-rsa ...
- ssh-rsa ...
ssh_pwauth: True
disable_root: false
chpasswd:
list: |
root:$rtpwd
expire: False
# Configure where output will go
output:
all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server
ssh_svcname: ssh
ssh_deletekeys: True
ssh_genkeytypes: ['rsa', 'ecdsa']
package_update: true
package_upgrade: true
packages:
- bind9-utils
- vim
- freeipa-client
- cloud-utils-growpart
power_state:
delay: "+2" #minutes
mode: reboot
message: Run completed
timeout: 120 #seconds
condition: True
EOF
请问我做错了什么?
答案1
我的第一个错误似乎是将网络配置添加到。使用单独的网络配置文件( )和适用于 debian 和 centos 的 ENI 版本user-data
使其正常工作。network-config
对于 Debian:
cat << EOF > $network_config
version: 1
config:
- type: physical
name: eth0
subnets:
- type: static
address: 10.10.0.26
gateway: 10.10.0.254
- type: route
destination: 10.100.0.0/24
gateway: 10.10.0.249
- type: nameserver
address:
- 10.100.0.23
- 10.100.0.17
- 10.100.0.18
search:
- testing.mydom
- production.mydom
- admin.mydom
EOF
对于 centos:
cat << EOF > $network_config
version: 1
config:
- type: physical
name: eth0
subnets:
- type: static
address: 10.10.0.26
gateway: 10.10.0.254
- type: route
destination: 10.100.0.0/24
gateway: 10.10.0.249
- type: nameserver
address:
- 10.100.0.23
- 10.100.0.17
- 10.100.0.18
search:
- testing.mydom
- production.mydom
- admin.mydom
EOF
我们将看看 Ubuntu 镜像发生的情况(并修复包“更新”和安装)。