凯发k8国际娱乐官网入口-k8凯发> 镜像服务 ims> > 使用packer创建私有镜像
更新时间:2023-07-20 gmt 08:00

使用packer创建私有镜像-凯发k8国际娱乐官网入口

packer是一款可以创建自定义镜像的开源工具。packer包含构建器(builder)、配置器(provisioner)、后处理器(post-processor)三个组件,通过hcl2(hashicorp configuration language)或者json格式的模板文件,可以灵活组合这三种组件并行地、自动化地创建镜像文件。使用packer通过配置代码的形式,降低了创建私有镜像复杂度,并且将创建镜像的过程变成可以配置管理代码的形式,在支持用户灵活定制个性化镜像的同时,也为镜像在不同云平台间的切换提供了一种新的途径。

本节以在centos 8.2 64bit云服务器中创建ubuntu 22.04 server 64bit私有镜像并上传到公有云平台为例,介绍使用packer创建镜像的操作步骤。

约束与限制

不支持packer使用整机镜像作为源镜像来创建私有镜像。

操作流程

安装packer

  1. 登录管理控制台,创建一台弹性云服务器(以centos 8.2 64bit为例),并绑定弹性公网ip。
  2. 登录弹性云服务器。
  3. 在选择与云服务器操作系统及架构类型相对应的packer版本,版本号建议选择最新版本。
  4. 执行以下命令,安装packer(本节操作以packer_1.9.1_linux_amd64.zip为例)。

    wget --no-check-certificate https://releases.hashicorp.com/packer/1.9.1/packer_1.9.1_linux_amd64.zip

    • 需要提前给云服务器绑定弹性公网ip,以便能够访问外网。
    • 如果执行本步骤命令后报“command not found”的错误,说明没有预装wget工具,需要执行yum install wget进行安装。
  5. 执行以下命令,解压packer安装包。

    unzip packer_1.9.1_linux_amd64.zip

  6. 执行以下命令,将packer安装包移动至“/usr/local/bin”目录下。
    mv packer /usr/local/bin

    “/usr/local/bin”目录已被添加到环境变量,您也可以将packer安装包移动至其它目录下,并确保该目录已被添加到环境变量中。

  7. 执行以下命令,查询packer版本号,检查packer是否安装成功。

    packer -v

    • 如果回显信息为packer版本号,表示已完成packer安装。
    • 如果回显信息为“command not found”,表示packer安装失败,请检查packer所在目录是否被添加到环境变量中。

      使用命令env | grep path打印环境变量,查看环境变量path是否包含packer的安装目录。

      如果环境变量path中没有包含packer安装目录,请依次使用以下命令,将packer的安装路径添加到环境变量path中:

      1. 执行以下命令,打开profile文件。

        vim /etc/profile

      2. 按“i”键进入编辑模式,在文件末尾添加“export path=$path:/usr/local/bin”。

        请将/usr/local/bin换成您实际安装packer的目录。

      3. 按“esc”退出编辑模式,输入:wq,按回车键保存修改并退出。
      4. 执行以下命令,使修改生效。

        source /etc/profile

定义packer模板

使用packer创建镜像,需要一个后缀为.pkr.hcl格式模板文件。在模板文件中,您需要指定、,还可以指定后处理器。在配置器中,您可以指定对源镜像的任何操作,可以指定安装软件也可以对相关配置做修改。本示例使用后处理器重定向manifest的输出路径,如果您的packer模板文件中有多个builders(构建器),您可以通过manifest的输出内容,找到每个builder创建镜像的id。关于构建器、配置器以及后处理器的详细介绍请参考。

本节操作以shell配置器为例。

  1. 执行以下命令,创建hwcloud.pkr.hcl文件。

    touch hwcloud.pkr.hcl

  2. 执行以下命令,打开hwcloud.pkr.hcl文件。

    vim hwcloud.pkr.hcl

  3. 按“i”进入编辑模式,根据实际需求编写模板,以下内容仅供参考,参数列表请查看表1 packer模板参数列表
    packer {
      required_plugins {
        huaweicloud = {
          version = ">= 1.0.0"
          source  = "github.com/huaweicloud/huaweicloud"
        }
      }
    }
     
    source "huaweicloud-ecs" "artifact" {
      region            = "xxx"
      availability_zone = "xxx"
      flavor            = "c6.large.2"
      source_image_name = "ubuntu 22.04 server 64bit"
      image_name        = "ubuntu-2204-image-powered-by-packer"
      image_tags = {
        builder = "packer"
        os      = "ubuntu-22.04-server"
      }
     
      ssh_username       = "root"
      eip_type           = "5_bgp"
      eip_bandwidth_size = 5
    }
     
    build {
      sources = ["source.huaweicloud-ecs.artifact"]
     
      provisioner "shell" {
        inline = ["apt-get update -y"]
      }
     
      post-processor "manifest" {
        strip_path = true
        output     = "packer-result.json"
      }
    }

    表1 packer模板参数列表中,region、availability_zone、flavor、source_image_name均为创建私有镜像时使用的云服务器的属性信息。

    表1 packer模板参数列表

    参数

    描述

    是否为必选

    region

    区域名称。参考获取。

    flavor

    云服务器的规格。

    image_name

    待创建私有镜像的名称。

    image_tags

    待创建私有镜像的tags。

    availability_zone

    可用区。参考获取。

    source_image_name

    源镜像名称,可以从镜像服务控制台公共镜像列表获取。

    说明:

    您也可以使用source_image参数指定源镜像的id或者使用source_image_filter参数查询源镜像。

    ssh_username

    待创建私有镜像的ssh登录用户名。

    使用ssh方式登录时,该配置项必选。

    eip_type

    弹性公网ip的线路类型。

    eip_bandwidth_size

    弹性公网ip的带宽大小(mbit/s)。如果在packer创建的镜像实例中需要使用外网,那么该配置项必选。

    说明:

    您也可以使用floating_ip或者reuse_ips参数使用已有的eip。

    provisioner

    创建私有镜像时使用的packer配置器类型。详情请参见。

    post-processor

    创建私有镜像时使用的packer后处理器类型。

    更多配置参数请参见。

使用packer创建镜像

  1. packer模板文件制作完成后,请执行以下命令,导入您的ak、sk。

    export hw_access_key=

    export hw_secret_key=

  2. 请执行以下命令创建镜像。

    packer build hwcloud.pkr.hcl

    huaweicloud-ecs.artifact: output will be in this color.
     
    ==> huaweicloud-ecs.artifact: loading availability zones...
        huaweicloud-ecs.artifact: the specified availability_zone ap-southeast-1a is available
    ==> huaweicloud-ecs.artifact: loading flavor: c6.large.2
    ==> huaweicloud-ecs.artifact: creating temporary keypair: packer_64abc4fd-xxxx-xxxx-xxxx-2139eee76819...
    ==> huaweicloud-ecs.artifact: created temporary keypair: packer_64abc4fd-xxxx-xxxx-xxxx-2139eee76819
        huaweicloud-ecs.artifact: found image id: 19d9079e-xxxx-xxxx-xxxx-642116ad6557
    ==> huaweicloud-ecs.artifact: creating temporary vpc...
        huaweicloud-ecs.artifact: temporary vpc id: 6e309adc-xxxx-xxxx-xxxx-4c3356b972c7
    ==> huaweicloud-ecs.artifact: creating temporary subnet...
        huaweicloud-ecs.artifact: temporary subnet id: 66ab3bc3-xxxx-xxxx-xxxx-d8ca2d9378cc
        huaweicloud-ecs.artifact: the [default] security groups will be used ...
    ==> huaweicloud-ecs.artifact: creating eip ...
        huaweicloud-ecs.artifact: created eip: '0cd696e3-xxxx-xxxx-xxxx-220b8d277604' (159.xxx.xxx.180)
    ==> huaweicloud-ecs.artifact: launching server in az ap-southeast-1a...
        huaweicloud-ecs.artifact: waiting for server to become ready...
        huaweicloud-ecs.artifact: server id: 13f78f88-xxxx-xxxx-xxxx-7e8c27a9ad1f
    ==> huaweicloud-ecs.artifact: using ssh communicator to connect: 159.138.141.180
    ==> huaweicloud-ecs.artifact: waiting for ssh to become available...
    ==> huaweicloud-ecs.artifact: connected to ssh!
    ==> huaweicloud-ecs.artifact: provisioning with shell script: /tmp/packer-shell2456008753
    huaweicloud-ecs.artifact: hit:1 http://repo.huaweicloud.com/ubuntu jammy inrelease
            ......
        huaweicloud-ecs.artifact: fetched 5,536 kb in 3s (2,006 kb/s)
        huaweicloud-ecs.artifact: reading package lists...
    ==> huaweicloud-ecs.artifact: stopping server: 13f78f88-xxxx-xxxx-xxxx-7e8c27a9ad1f ...
        huaweicloud-ecs.artifact: waiting for server to stop: 13f78f88-xxxx-xxxx-xxxx-7e8c27a9ad1f ...
    ==> huaweicloud-ecs.artifact: creating the system image: ubuntu-2204-image-powered-by-packer ...
        huaweicloud-ecs.artifact: image: 62dc6e37-xxxx-xxxx-xxxx-a2a00a677f5b
    ==> huaweicloud-ecs.artifact: terminating the source server: 13f78f88-xxxx-xxxx-xxxx-7e8c27a9ad1f...
    ==> huaweicloud-ecs.artifact: deleted temporary public ip '0cd696e3-xxx-xxxx-xxxx-220b8d277604' (159.xxx.xxx.180)
    ==> huaweicloud-ecs.artifact: deleting temporary subnet: 66ab3bc3-xxxx-xxxx-xxxx-d8ca2d9378cc...
    ==> huaweicloud-ecs.artifact: deleting temporary vpc: 6e309adc-xxxx-xxxx-xxxx-4c3356b972c7...
    ==> huaweicloud-ecs.artifact: deleting temporary keypair: packer_64abc4fd-xxxx-xxxx-xxxx-2139eee76819 ...
    ==> huaweicloud-ecs.artifact: running post-processor:  (type manifest)
    build 'huaweicloud-ecs.artifact' finished after 5 minutes 48 seconds.
     
    ==> wait completed after 5 minutes 48 seconds
     
    ==> builds finished. the artifacts of successful builds are:
    --> huaweicloud-ecs.artifact: an image was created: 62dc6e37-xxxx-xxxx-xxxx-a2a00a677f5b
  3. 命令执行成功后,登录控制台,选择“计算 > 镜像服务”。
  4. 在“私有镜像”列表页面,查看使用packer创建的镜像,如图1所示。
    图1 查看使用packer创建的镜像

相关链接

packer官方指导:

分享:
网站地图