簡體   English   中英

Openstack:Packer + Cloud-Init

[英]Openstack: Packer + Cloud-Init

我想創建一個包含一些自定義軟件和圖形界面的自定義 openstack OpenSUSE15 映像。 我使用了現有的 OpenSUSE15.0 映像和打包程序來構建該映像。 它工作正常。 打包器json文件如下:

    "builders": [
    {
            "type" : "openstack",
            "ssh_username" : "root",
            "image_name": "OpenSUSE_15_custom_kde",
            "source_image": "OpenSUSE 15",
            "flavor": "m1.medium",
            "networks": "public-network"
    }
    ],
    "provisioners":[
    {
            "type": "shell",
            "inline": [
                    "sleep 10",
                    "sudo -s",
                    "zypper --gpg-auto-import-keys refresh",
                    "zypper -n up -y",
                    "zypper -n clean -a",
                    "zypper -n addrepo -f http://download.opensuse.org/repositories/devel\\:/languages\\:/R\\:/patched/openSUSE_Leap_15.0/ R-patched",
                    "zypper -n addrepo -f http://download.opensuse.org/repositories/devel\\:/languages\\:/R\\:/released/openSUSE_Leap_15.0/ R-released",
                    "zypper --gpg-auto-import-keys refresh",
                    "zypper -n install -y R-base R-base-devel R-recommended-packages rstudio",
                    "zypper -n clean -a",
                    "zypper --non-interactive install -y -t pattern kde kde_plasma devel_kernel devel_python3 devel_C_C++ office x11",
                    "zypper -n install xrdp",
                    "zypper -n clean -a",
                    "zypper -n dup -y",
                    "systemctl enable xrdp",
                    "systemctl start xrdp",
                    "cloud-init clean --logs",
                    "zypper -n install -y cloud-init growpart yast2-network yast2-services-manager acpid",
                    "cat /dev/null > /etc/udev/rules.d/70-persistent-net.rules",
                    "systemctl disable cloud-init.service cloud-final.service cloud-init-local.service cloud-config.service",
                    "systemctl enable cloud-init.service cloud-final.service cloud-init-local.service cloud-config.service sshd",
                    "sudo systemctl stop firewalld",
                    "sudo systemctl disable firewalld",
                    "sed -i 's/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=0/g' /etc/default/grub",
                    "exec grub2-mkconfig -o /boot/grub2/grub.cfg '$@'",
                    "systemctl restart cloud-init",
                    "systemctl daemon-reload",
                    "cat /dev/null > ~/.bash_history && history -c && sudo su",
                    "cat /dev/null > /var/log/wtmp",
                    "cat /dev/null > /var/log/btmp",
                    "cat /dev/null > /var/log/lastlog",
                    "cat /dev/null > /var/run/utmp",
                    "cat /dev/null > /var/log/auth.log",
                    "cat /dev/null > /var/log/kern.log",
                    "cat /dev/null > ~/.bash_history && history -c",
                    "rm ~/.ssh/authorized_keys"
                ]
        },
        {
                "type": "file",
                "source": "./cloud_init/cloud.cfg",
                "destination": "/etc/cloud/cloud.cfg"
        }
        ]

}

使用 packer 構建和配置階段沒有錯誤。

在第二階段,當這個基礎鏡像通過 openstack 客戶端通過 heat 模板生成時,我希望完成一些個性化的任務。 創建用戶,授予 ssh 訪問權限(包括調整 sshd_config 文件...)。 這是通過init_image.sh文件完成的。

#!/bin/bash

useradd -m $USERNAME -p $PASSWD -s /bin/bash
usermod -a -G sudo $USERNAME

tee /etc/ssh/banner <<EOF
You are one lucky user, if you bear the key...
EOF


tee /etc/ssh/sshd_config <<EOF 
##  SOME IMPORTANT SSHD CONFIGURATIONS
EOF

sudo -u $USERNAME -H sh -c 'cd ~;mkdir ~/.ssh/;echo "$SSHPUBKEY" > ~/.ssh/authorized_keys;chmod -R 700 ~/.ssh/;chmod 600 ~/.ssh/authorized_keys;'

systemctl restart sshd.service


voldata_dev="/dev/disk/by-id/virtio-$(echo $VOLDATA | cut -c -20)"
mkfs.ext4 $voldata_dev               
mkdir -pv /home/$USERNAME/share
echo "$voldata_dev /home/$USERNAME/share ext4 defaults 1 2" >> /etc/fstab
mount /home/$USERNAME/share
chown -R $USERNAME:users /home/$USERNAME/share/


systemctl enable xrdp
systemctl start xrdp

為此,我創建了以下加熱模板。

heat_template_version: "2018-08-31"
description: "version 2017-09-01 created by HOT Generator at Fri, 05 Jul 2019 12:56:22 GMT."

parameters:
    username:
        type: string
        label: User Name
        description: This is the user name, and will  be also the name of the key and the server
        default: test
    imagename:
        type: string
        label: Image Name
        description: This is the Name of the Image e.g. Ubuntu 18.04
        default: "OpenSUSE Leap 15"
    ssh_pub_key:
        type: string
        label: ssh public key


    flavorname:
        type: string
        label: Flavor Name
        description: This is the Name of the Flavor e.g. m1.small
        default: "m1.small"
    vol_size:
        type: number
        label: Volume Size
        description: This is the size of the volume that should be attached in GB
    default: 10
password: 
    type: string
    label: password
    description: This is the su password and user password


resources: 

  init:
    type: OS::Heat::SoftwareConfig
    properties:
            group: ungrouped
            config:
                str_replace:
                    template: 
                            {get_file: init_image.sh}
                    params:
                            $USERNAME: {get_param: username}
                            $SSHPUBKEY: {get_param: ssh_pub_key}
                            $PASSWD: {get_param: password}
                            $VOLDATA: {get_resource: volume}

  my_key: 
    type: "OS::Nova::KeyPair"
    properties: 
        name: 
                list_join:
                        ["_", [ {get_param: username}, 'key']]  
        public_key: {get_param: ssh_pub_key} 

  my_server: 
    type: "OS::Nova::Server"
    properties: 
            block_device_mapping_v2: [{ device_name: "vda", image : { get_param : imagename }, delete_on_termination : "false", volume_size: 20 }]
            name: {get_param: username}
            flavor: {get_param: flavorname}
            key_name: {get_resource: my_key}
            admin_pass: {get_param: password}
            user_data_format: RAW
            user_data: {get_resource: init}
            networks:
                    - network: "public-network"
      depends_on: 
            - my_key
            - init
            - volume


  volume:
    type: "OS::Cinder::Volume"
    properties:
    # Size is given in GB
            size: {get_param: vol_size}
            name: 
                list_join: ["-", ["vol_",{get_param: username }]]
  volume_attachment:
    type: "OS::Cinder::VolumeAttachment"
    properties:
            volume_id: { get_resource: volume }
            instance_uuid: { get_resource: my_server }
    depends_on:
    - volume
outputs:
    instance_ip:
        description: The IP address of the deployed instances
        value: { get_attr: [my_server, first_address] }

如果我在模板中使用原始圖像,我沒有問題(但是,構建過程需要非常長的時間),我需要重新啟動才能擁有圖形 KDE 界面。

但是,如果我使用帶有打包程序的圖像構建,我的 user_data 會被忽略嗎? 我無法登錄,沒有創建用戶個性化用戶...我錯過了什么? 為什么它不起作用? 如您所見,我清理了 cloud-init,重新啟動了服務......我被困了很長時間......

更新

是機器的可訪問引導日志。

更新 2

這是cloud-init analyze show的輸出:

    -- Boot Record 01 --
The total time elapsed since completing an event is printed after the "@" character.
The time the event takes is printed after the "+" character.

Starting stage: init-local
|`->no cache found @00.01000s +00.00000s
|`->no local data found from DataSourceOpenStackLocal @00.04700s +15.23000s
Finished stage: (init-local) 15.31200 seconds 

Starting stage: init-network
|`->no cache found @16.01000s +00.00100s
|`->no network data found from DataSourceOpenStack @16.01700s +00.02600s
|`->found network data from DataSourceNone @16.04300s +00.00100s
|`->setting up datasource @16.09000s +00.00000s
|`->reading and applying user-data @16.10000s +00.00200s
|`->reading and applying vendor-data @16.10200s +00.00000s
|`->activating datasource @16.12100s +00.00100s
|`->config-migrator ran successfully @16.17900s +00.00100s
|`->config-seed_random ran successfully @16.18000s +00.00100s
|`->config-bootcmd ran successfully @16.18200s +00.00000s
|`->config-write-files ran successfully @16.18200s +00.00100s
|`->config-growpart ran successfully @16.18300s +00.46100s
|`->config-resizefs ran successfully @16.64500s +01.33400s
|`->config-disk_setup ran successfully @17.98100s +00.00300s
|`->config-mounts ran successfully @17.98500s +00.00400s
|`->config-set_hostname ran successfully @17.99000s +00.09800s
|`->config-update_hostname ran successfully @18.08900s +00.01000s
|`->config-update_etc_hosts ran successfully @18.10000s +00.00100s
|`->config-rsyslog ran successfully @18.10100s +00.00200s
|`->config-users-groups ran successfully @18.10400s +00.00200s
|`->config-ssh ran successfully @18.10700s +00.61400s
Finished stage: (init-network) 02.73600 seconds 

Starting stage: modules-config
|`->config-locale ran successfully @35.00200s +00.00400s
|`->config-set-passwords ran successfully @35.00600s +00.00100s
|`->config-zypper-add-repo ran successfully @35.00700s +00.00200s
|`->config-ntp ran successfully @35.01000s +00.00100s
|`->config-timezone ran successfully @35.01100s +00.00200s
|`->config-disable-ec2-metadata ran successfully @35.01300s +00.00100s
|`->config-runcmd ran successfully @35.01800s +00.00200s
Finished stage: (modules-config) 00.05100 seconds 

Starting stage: modules-final
|`->config-package-update-upgrade-install ran successfully @35.87400s +00.00000s
|`->config-puppet ran successfully @35.87500s +00.00000s
|`->config-chef ran successfully @35.87600s +00.00000s
|`->config-mcollective ran successfully @35.87600s +00.00100s
|`->config-salt-minion ran successfully @35.87700s +00.00100s
|`->config-rightscale_userdata ran successfully @35.87800s +00.00100s
|`->config-scripts-vendor ran successfully @35.87900s +00.00500s
|`->config-scripts-per-once ran successfully @35.88400s +00.00100s
|`->config-scripts-per-boot ran successfully @35.88500s +00.00000s
|`->config-scripts-per-instance ran successfully @35.88500s +00.00100s
|`->config-scripts-user ran successfully @35.88600s +00.00100s
|`->config-ssh-authkey-fingerprints ran successfully @35.88700s +00.00100s
|`->config-keys-to-console ran successfully @35.88800s +00.09000s
|`->config-phone-home ran successfully @35.97900s +00.00100s
|`->config-final-message ran successfully @35.98000s +00.00600s
|`->config-power-state-change ran successfully @35.98700s +00.00100s
Finished stage: (modules-final) 00.13600 seconds 

Total Time: 18.23500 seconds

1 boot records analyzed

更新 3

顯然,當不使用zypper up更新zypper up ,cloud-init 表現良好並找到用戶數據。 因此,我不會在配置中更新映像。 但是,一旦配置,更新是有意義的。

在配置結束時,您應該停止 cloud-init 並擦除狀態。 否則當鏡像啟動時 cloud-init 認為它已經執行了第一次啟動

systemctl stop cloud-init 
rm -rf /var/lib/cloud/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM