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

5 篇文章 0 订阅
2 篇文章 0 订阅

首先这里我们配置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之后会有相应服务:
这里写图片描述
这三个服务开机启动,第一次开机时会进行相应的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步:
这里写图片描述
主要实现取数据的地方是get_data函数
然后通过进入函数mount_cb
这里写图片描述
这里写图片描述
实现机制就是通过blkid -odevice -tLABEL=config-2找到盘vdb,然后创建临时目录并将其挂载即可读取相应数据

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值