前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Packer构建自定义镜像如何使用ssh密钥

Packer构建自定义镜像如何使用ssh密钥

原创
作者头像
arunma
发布2024-12-13 20:26:27
发布2024-12-13 20:26:27
1530
举报

本文将介绍如何使用 Packer 构建自定义镜像,并通过 SSH 密钥进行连接。此外,我们还将确保创建的自定义镜像实例同样支持 SSH 密钥连接。


1、packer安装及使用 https://cloud.tencent.com/document/product/213/4942https://www.packer.io/

2、申请ssh密钥https://console.cloud.tencent.com/cvm/sshkey/index?rid=1&pid=-1

  • 注意:创建ssh密钥后会自动下载私钥,保存好私钥

3、定义支持ssh密钥的packer模版,设置aksk

代码语言:json
复制
{
  "variables": {
    "secret_id": "{{env `TENCENTCLOUD_SECRET_ID`}}",
    "secret_key": "{{env `TENCENTCLOUD_SECRET_KEY`}}",
    "version": "v1.0.0"
  },
  "builders": [
    {
      "type": "tencentcloud-cvm",
      "secret_id": "{{user `secret_id`}}",
      "secret_key": "{{user `secret_key`}}",
      "region": "ap-guangzhou",
      "zone": "ap-guangzhou-6",
      "instance_type": "SA3.MEDIUM2",
      "source_image_id": "img-9xqekomx",
      "ssh_username": "root",
      "ssh_keypair_name": "skey-atjkudf9",
      "ssh_private_key_file": "/root/tf-test/packer/cvm_test.pem",
      "image_name": "Packer-test-{{user `version`}}",
      "disk_type": "CLOUD_PREMIUM",
      "packer_debug": true,
      "associate_public_ip_address": true,
      "run_tags": {
        "packer": "packer-test"
      }
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "pwd"
      ]
    }
  ]
}

字段说明:

ssh_username:密钥id(来自第二步创建)

ssh_private_key_file:私钥文件地址(来自第二步创建后自动下载)

4、构建镜像

代码语言:shell
复制
packer build xxx.json

5、构建过程

代码语言:shell
复制
tencentcloud-cvm: output will be in this color.

==> tencentcloud-cvm: Trying to check image name: Packer-test-v1.0.0...
    tencentcloud-cvm: Image name: useable
==> tencentcloud-cvm: Trying to check source image: img-9xqekomx...
    tencentcloud-cvm: Image found: CentOS Stream 9 64bit
==> tencentcloud-cvm: Using existing SSH private key
    tencentcloud-cvm: Loaded 1678 bytes private key data
==> tencentcloud-cvm: Trying to create a new vpc...
    tencentcloud-cvm: Vpc created: vpc-r2607xcf
==> tencentcloud-cvm: Trying to create a new subnet...
    tencentcloud-cvm: Subnet created: subnet-dv6xl1f6
==> tencentcloud-cvm: Trying to create a new securitygroup...
    tencentcloud-cvm: Securitygroup created: sg-ac3t25fx
==> tencentcloud-cvm: Trying to create securitygroup polices...
    tencentcloud-cvm: Securitygroup polices created
==> tencentcloud-cvm: Trying to create a new instance...
    tencentcloud-cvm: RunInstances reqeust: skey-atjkudf9
    tencentcloud-cvm: Waiting for instance ready
    tencentcloud-cvm: Instance created: ins-rce7y5la
==> tencentcloud-cvm: Using SSH communicator to connect: 81.71.64.81
==> tencentcloud-cvm: Waiting for SSH to become available...
==> tencentcloud-cvm: Connected to SSH!
==> tencentcloud-cvm: Provisioning with shell script: /tmp/packer-shell2694718859
    tencentcloud-cvm: /root
==> tencentcloud-cvm: Trying to create a new image: Packer-test-v1.0.0...
    tencentcloud-cvm: Waiting for image ready
    tencentcloud-cvm: Image created: img-lblj0aea
==> tencentcloud-cvm: Cleaning up instance...
==> tencentcloud-cvm: Cleaning up securitygroup...
==> tencentcloud-cvm: Cleaning up subnet...
==> tencentcloud-cvm: Cleaning up vpc...
Build 'tencentcloud-cvm' finished after 3 minutes 11 seconds.

==> Wait completed after 3 minutes 11 seconds

==> Builds finished. The artifacts of successful builds are:
--> tencentcloud-cvm: Tencentcloud images(ap-guangzhou: img-lblj0aea) were created.

6、选用自定义镜像创建的cvm包含公钥

7、使用私钥登录,实例创建完成后点击登录

验证公钥可以登录后在~/.ssh路径查看

如有疑问联系作者或者packer插件开发者 https://github.com/hashicorp/packer-plugin-tencentcloud

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、packer安装及使用 https://cloud.tencent.com/document/product/213/4942或https://www.packer.io/
  • 2、申请ssh密钥https://console.cloud.tencent.com/cvm/sshkey/index?rid=1&pid=-1
  • 3、定义支持ssh密钥的packer模版,设置aksk
  • 4、构建镜像
  • 5、构建过程
  • 6、选用自定义镜像创建的cvm包含公钥
  • 7、使用私钥登录,实例创建完成后点击登录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档