openstack 密码注入之cloud-init 简单介绍

首先这里我们配置metadata注入方式为config driver这样就可以不用依赖网络不用再通过http://169.254.169.254去获取metadata
计算节点修改配置:
vi /etc/nova/nova.conf
force_config_drive=True
执行命令:
service nova-compute restart
在我们创建完虚拟机之后,若我们后端存储是ceph则xml中会有如下字段:

 <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='writeback'/>
      <auth username='compute'>
        <secret type='ceph' uuid='a5d0dd94-57c4-ae55-ffe0-7e3732a24455'/>
      auth>
      <source protocol='rbd' name='compute/bfffc4a2-caff-4543-9e50-9f8b83ff72a3_disk.config'>
        <host name='192.168.20.6' port='6789'/>
        <host name='192.168.20.12' port='6789'/>
        <host name='192.168.20.16' port='6789'/>
      source>
      <backingStore/>
      <target dev='vdz' bus='virtio'/>
      <alias name='virtio-disk25'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    disk>

若使用本地存储,则会有如下字段:

  <disk type="file" device="cdrom">
      <driver name="qemu" type="raw" cache="none"/>
      <source file="/var/lib/nova/instances/30ba8cc0-b2f9-4e38-9a27-6bfa9d82f5f2/disk.config"/>
      <target bus="ide" dev="hdd"/>
    disk>

这就是所谓的config driver那块盘
如下图所示,虚拟机镜像安装完cloud-init之后会有相应服务:
openstack 密码注入之cloud-init 简单介绍_第1张图片
这三个服务开机启动,第一次开机时会进行相应的metadata的解析,根据配置我们使用config driver的话则会相应进行解析config driver的工作,如下举例:

[root@test-for-qos ~]# cat /usr/lib/systemd/system/cloud-init.service
[Unit]
Description=Initial cloud-init job (metadata service crawler)
After=local-fs.target network.target cloud-init-local.service
Requires=network.target
Wants=local-fs.target cloud-init-local.service

[Service]
Type=oneshot
ExecStart=/usr/bin/cloud-init init
RemainAfterExit=yes
TimeoutSec=0

# Output needs to appear in instance console output
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

根据cloud-init的步骤:

    # Cloud-init 'init' stage is broken up into the following sub-stages
    # 1. Ensure that the init object fetches its config without errors
    # 2. Setup logging/output redirections with resultant config (if any)
    # 3. Initialize the cloud-init filesystem
    # 4. Check if we can stop early by looking for various files
    # 5. Fetch the datasource
    # 6. Connect to the current instance location + update the cache
    # 7. Consume the userdata (handlers get activated here)
    # 8. Construct the modules object
    # 9. Adjust any subsequent logging/output redirections using the modules
    #    objects config as it may be different from init object
    # 10. Run the modules for the 'init' stage
    # 11. Done!

我们就直接进入第5步:
openstack 密码注入之cloud-init 简单介绍_第2张图片
主要实现取数据的地方是get_data函数
然后通过进入函数mount_cb
openstack 密码注入之cloud-init 简单介绍_第3张图片
openstack 密码注入之cloud-init 简单介绍_第4张图片
实现机制就是通过blkid -odevice -tLABEL=config-2找到盘vdb,然后创建临时目录并将其挂载即可读取相应数据

你可能感兴趣的:(mysql,openstack,cloudinit,python)