美文网首页
Packer 学习笔记

Packer 学习笔记

作者: 华阳_3bcf | 来源:发表于2019-11-26 17:42 被阅读0次

    是什么

    Packer 是一款轻量级构建镜像的工具。支持主流的云平台和操作系统。

    云平台提供了一些基础镜像,用来给用户生成VM,在绝大多数情况下可以满足客户的要求。但是有些客户由于业务系统对操作系统依赖比较高,希望定制化一些操作系统参数,则可以用自定义镜像来创建。

    一般来说创建自定义操作系统镜像的方法是在云平台找一个基础镜像, 然后对操作系统做定制化更新,再打成镜像。这是Azure的例子。缺点是要手工操作,如果基础镜像更新,还要再做一次。

    Packer的好处就是可以自动化。

    安装Packer

    可以根据官方文档安装Packer https://www.packer.io/intro/getting-started/install.html

    $ wget https://releases.hashicorp.com/packer/1.4.5/packer_1.4.5_linux_amd64.zip
    

    解压后,把文件放在 /usr/local/bin

    定义Packer模板

    使用Packer创建自定义镜像时,需要创建一个JSON格式的模板文件。在该模板文件中,您需要指定创建自定义镜像的生成器和配置器,以Azure 举例, 详情请参见 image builders (生成器)和 Provisioners (配置器)。 Packer具有多种配置器,可用于配置自定义镜像的内容生成方式,常用Shell 和 Ansible。

    Builders 主要定义了 谁(账户),从哪里来(base image),到哪里去(new image)。

    Provisioners 主要定义了对Image做什么改变。

    生成文件 build.json 内容如下

    {
      "builders": [{
        "type": "azure-arm", # 这是Azure云平台
    
        "client_id": "aaa",
        "client_secret": "bbb",
        "tenant_id": "ccc",
        "subscription_id": "ddd",
         # 云平台的用户登陆账号
         
        "managed_image_resource_group_name": "roy-image",
        "managed_image_name": "royPackerImage",
        ## 新生成的image 在哪里,叫什么名字
        
        "os_type": "Linux",
        "image_publisher": "Canonical",
        "image_offer": "UbuntuServer",
        "image_sku": "16.04-LTS",
        # 基础镜像是什么
         
        "azure_tags": {
            "dept": "Engineering",
            "task": "Image deployment"
        },
    
        "location": "East US",
        "vm_size": "Standard_DS2_v2"
      }],
      "provisioners": [{
        "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'",
        "inline": [
          "apt-get update",
          "apt-get upgrade -y",
          "apt-get -y install nginx",
    
          "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"
          # deprovision 操作,为生成image做准备
        ],
        "inline_shebang": "/bin/sh -x",
        "type": "shell"
      }]
    }
    

    使用Packer创建自定义镜像

    $ packer build build.json
    azure-arm output will be in this color.
    
    ==> azure-arm: Running builder ...
    ==> azure-arm: Getting tokens using client secret
    ==> azure-arm: Getting tokens using client secret
        azure-arm: Creating Azure Resource Manager (ARM) client ...
    ==> azure-arm: WARNING: Zone resiliency may not be supported in East US, checkout the docs at https://docs.microsoft.com/en-us/azure/availability-zones/
    ==> azure-arm: Creating resource group ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> Location          : 'East US'
    ==> azure-arm:  -> Tags              :
    ==> azure-arm:  ->> task : Image deployment
    ==> azure-arm:  ->> dept : Engineering
    ==> azure-arm: Validating deployment template ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> DeploymentName    : 'pkrdpnktbus6j2k'
    ==> azure-arm: Deploying deployment template ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> DeploymentName    : 'pkrdpnktbus6j2k'
    ==> azure-arm: Getting the VM's IP address ...
    ==> azure-arm:  -> ResourceGroupName   : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> PublicIPAddressName : 'pkripnktbus6j2k'
    ==> azure-arm:  -> NicName             : 'pkrninktbus6j2k'
    ==> azure-arm:  -> Network Connection  : 'PublicEndpoint'
    ==> azure-arm:  -> IP Address          : '23.101.142.101'
    ==> azure-arm: Waiting for SSH to become available...
    ==> azure-arm: Connected to SSH!
    ==> azure-arm: Provisioning with shell script: /tmp/packer-shell937209190
        azure-arm: Hit:1 http://azure.archive.ubuntu.com/ubuntu xenial InRelease
        azure-arm: Get:2 http://azure.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
        .........
        azure-arm: Processing triggers for ufw (0.35-0ubuntu2) ...
        azure-arm: WARNING! The waagent service will be stopped.
        azure-arm: WARNING! Cached DHCP leases will be deleted.
        azure-arm: WARNING! root password will be disabled. You will not be able to login as root.
        azure-arm: WARNING! /etc/resolvconf/resolv.conf.d/tail and /etc/resolvconf/resolv.conf.d/original will be deleted.
        azure-arm: WARNING! packer account and entire home directory will be deleted.
    ==> azure-arm: Querying the machine's properties ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> ComputeName       : 'pkrvmnktbus6j2k'
    ==> azure-arm:  -> Managed OS Disk   : '/subscriptions/xxx/resourceGroups/packer-Resource-Group-nktbus6j2k/providers/Microsoft.Compute/disks/pkrosnktbus6j2k'
    ==> azure-arm: Querying the machine's additional disks properties ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> ComputeName       : 'pkrvmnktbus6j2k'
    ==> azure-arm: Powering off machine ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> ComputeName       : 'pkrvmnktbus6j2k'
    ==> azure-arm: Capturing image ...
    ==> azure-arm:  -> Compute ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:  -> Compute Name              : 'pkrvmnktbus6j2k'
    ==> azure-arm:  -> Compute Location          : 'East US'
    ==> azure-arm:  -> Image ResourceGroupName   : 'roy-image'
    ==> azure-arm:  -> Image Name                : 'royPackerImage'
    ==> azure-arm:  -> Image Location            : 'eastus'
    ==> azure-arm: Deleting resource group ...
    ==> azure-arm:  -> ResourceGroupName : 'packer-Resource-Group-nktbus6j2k'
    ==> azure-arm:
    ==> azure-arm: The resource group was created by Packer, deleting ...
    ==> azure-arm: Deleting the temporary OS disk ...
    ==> azure-arm:  -> OS Disk : skipping, managed disk was used...
    ==> azure-arm: Deleting the temporary Additional disk ...
    ==> azure-arm:  -> Additional Disk : skipping, managed disk was used...
    Build 'azure-arm' finished.
    
    ==> Builds finished. The artifacts of successful builds are:
    --> azure-arm: Azure.ResourceManagement.VMImage:
    
    OSType: Linux
    ManagedImageResourceGroupName: roy-image
    ManagedImageName: royPackerImage
    ManagedImageId: /subscriptions/xxx/resourceGroups/roy-image/providers/Microsoft.Compute/images/royPackerImage
    ManagedImageLocation: eastus
    

    后续步骤

    用自定义镜像生成新的VM。

    $ az vm create -g roy-image -n royfromImage --image royPackerImage
    {
      "fqdns": "",
      "id": "/subscriptions/xx/resourceGroups/roy-image/providers/Microsoft.Compute/virtualMachines/royfromImage",
      "location": "eastus",
      "macAddress": "00-0D-3A-8E-92-D2",
      "powerState": "VM running",
      "privateIpAddress": "10.0.0.4",
      "publicIpAddress": "40.114.41.93",
      "resourceGroup": "roy-image",
      "zones": ""
    }
    $ ssh 40.114.41.93
    royfromImage:~$ which nginx
    /usr/sbin/nginx
    

    相关文章

      网友评论

          本文标题:Packer 学习笔记

          本文链接:https://www.haomeiwen.com/subject/qijywctx.html