0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DevOpsAdvent Calendar 2018

Day 20

Pakcer で GCP のイメージを作ってみるの巻

Posted at

Pakcer で GCP のイメージを作ってみるの巻

Packer でイメージ作る最中に色々やってみる の GCP 版をやってみたいと思います。

前提条件

  • GCP のサービスアカウントキーを packer と同じフォルダに account.json として置きます。
  • サービスアカウントは、GCE と Storage に権限があれば大丈夫だと思います。
  • gcloud CLI のインストールも必要ぽいです。

では、やっていきます

gcp.json
{
  "builders": [
    {
      "type": "googlecompute",
      "account_file": "account.json",
      "project_id": "<プロジェクトID>",
      "machine_type": "f1-micro",
      "source_image_family": "ubuntu-1804-lts",
      "region": "asia-northeast1",
      "zone": "asia-northeast1-b",
      "image_description": "Ubuntu Machine Image",
      "image_name": "ubuntu1804",
      "disk_size": 16,
      "disk_type": "pd-ssd",
      "ssh_username": "root"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 30",
        "apt-get update",
        "apt-get install -y nginx"
      ]
    }
  ]
}

ubuntu は 18.04 使ってます。
GCP 特有のもの以外は以前と同じです。

やってみた

> ./packer validate gcp.json
Template validated successfully.

まぁ、問題ないですね。

> ./packer build gcp.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: ubuntu-1804-bionic-v20181203a
==> googlecompute: Creating instance...
    googlecompute: Loading zone: asia-northeast1-b
    googlecompute: Loading machine type: f1-micro
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
    googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
    googlecompute: IP: 104.198.118.161
==> googlecompute: Using ssh communicator to connect: 104.198.118.161
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Timeout waiting for SSH.
==> googlecompute: Deleting instance...
    googlecompute: Instance has been deleted!
==> googlecompute: Deleting disk...
    googlecompute: Disk has been deleted!
Build 'googlecompute' errored: Timeout waiting for SSH.

==> Some builds didn't complete successfully and had errors:
--> googlecompute: Timeout waiting for SSH.

==> Builds finished but no artifacts were created.

おや・・・失敗した。。。default ネットワークの SSH のポートの IP 制限してたの忘れてました。
気を取り直して・・・

> ./packer build gcp.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: ubuntu-1804-bionic-v20181203a
==> googlecompute: Creating instance...
    googlecompute: Loading zone: asia-northeast1-b
    googlecompute: Loading machine type: f1-micro
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
    googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
    googlecompute: IP: 35.187.211.90
==> googlecompute: Using ssh communicator to connect: 35.187.211.90
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Connected to SSH!
==> googlecompute: Provisioning with shell script: /tmp/packer-shell568665213
    googlecompute: Hit:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic InRelease
    googlecompute: Get:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
    googlecompute: Get:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
    googlecompute: Get:4 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/multiverse Sources [181 kB]
    googlecompute: Get:5 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main Sources [829 kB]
    googlecompute: Get:6 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/restricted Sources [5324 B]
    googlecompute: Get:7 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/universe Sources [9051 kB]
    googlecompute: Get:8 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
    googlecompute: Get:9 http://archive.canonical.com/ubuntu bionic InRelease [10.2 kB]
    googlecompute: Get:10 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB]
    googlecompute: Get:11 http://archive.canonical.com/ubuntu bionic/partner amd64 Packages [2308 B]
    googlecompute: Get:12 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB]
    googlecompute: Get:13 http://archive.canonical.com/ubuntu bionic/partner Translation-en [1272 B]
    googlecompute: Get:14 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [151 kB]
    googlecompute: Get:15 http://security.ubuntu.com/ubuntu bionic-security/main Sources [66.7 kB]
    googlecompute: Get:16 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/multiverse Translation-en [108 kB]
    googlecompute: Get:17 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe Sources [121 kB]
    googlecompute: Get:18 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/restricted Sources [2064 B]
    googlecompute: Get:19 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse Sources [3824 B]
    googlecompute: Get:20 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main Sources [223 kB]
    googlecompute: Get:21 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [461 kB]
    googlecompute: Get:22 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [173 kB]
    googlecompute: Get:23 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [705 kB]
    googlecompute: Get:24 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [171 kB]
    googlecompute: Get:25 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6372 B]
    googlecompute: Get:26 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [3356 B]
    googlecompute: Get:27 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe Sources [2068 B]
    googlecompute: Get:28 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3468 B]
    googlecompute: Get:29 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [1604 B]
    googlecompute: Get:30 http://security.ubuntu.com/ubuntu bionic-security/universe Sources [28.2 kB]
    googlecompute: Get:31 http://security.ubuntu.com/ubuntu bionic-security/multiverse Sources [1336 B]
    googlecompute: Get:32 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [236 kB]
    googlecompute: Get:33 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [88.9 kB]
    googlecompute: Get:34 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [109 kB]
    googlecompute: Get:35 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [62.0 kB]
    googlecompute: Get:36 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1440 B]
    googlecompute: Get:37 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [996 B]
    googlecompute: Fetched 26.6 MB in 6s (4388 kB/s)
    googlecompute: Reading package lists...
    googlecompute: Reading package lists...
    googlecompute: Building dependency tree...
    googlecompute: Reading state information...
    googlecompute: The following package was automatically installed and is no longer required:
    googlecompute:   grub-pc-bin
    googlecompute: Use 'apt autoremove' to remove it.
    googlecompute: The following additional packages will be installed:
    googlecompute:   fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
    googlecompute:   libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
    googlecompute:   libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
    googlecompute:   libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common
    googlecompute:   nginx-core
    googlecompute: Suggested packages:
    googlecompute:   libgd-tools fcgiwrap nginx-doc ssl-cert
    googlecompute: The following NEW packages will be installed:
    googlecompute:   fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
    googlecompute:   libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
    googlecompute:   libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
    googlecompute:   libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx
    googlecompute:   nginx-common nginx-core
    googlecompute: 0 upgraded, 18 newly installed, 0 to remove and 24 not upgraded.
    googlecompute: Need to get 2460 kB of archives.
    googlecompute: After this operation, 8194 kB of additional disk space will be used.
    googlecompute: Get:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libjpeg-turbo8 amd64 1.5.2-0ubuntu5.18.04.1 [110 kB]
    googlecompute: Get:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 fonts-dejavu-core all 2.37-1 [1041 kB]
    googlecompute: Get:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 fontconfig-config all 2.12.6-0ubuntu2 [55.8 kB]
    googlecompute: Get:4 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libfontconfig1 amd64 2.12.6-0ubuntu2 [137 kB]
    googlecompute: Get:5 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libjpeg8 amd64 8c-2ubuntu8 [2194 B]
    googlecompute: Get:6 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libjbig0 amd64 2.1-3.1build1 [26.7 kB]
    googlecompute: Get:7 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libtiff5 amd64 4.0.9-5 [152 kB]
    googlecompute: Get:8 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libwebp6 amd64 0.6.1-2 [185 kB]
    googlecompute: Get:9 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic/main amd64 libxpm4 amd64 1:3.5.12-1 [34.0 kB]
    googlecompute: Get:10 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libgd3 amd64 2.2.5-4ubuntu0.2 [119 kB]
    googlecompute: Get:11 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx-common all 1.14.0-0ubuntu1.2 [37.4 kB]
    googlecompute: Get:12 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-geoip amd64 1.14.0-0ubuntu1.2 [11.2 kB]
    googlecompute: Get:13 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-image-filter amd64 1.14.0-0ubuntu1.2 [14.5 kB]
    googlecompute: Get:14 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-xslt-filter amd64 1.14.0-0ubuntu1.2 [13.0 kB]
    googlecompute: Get:15 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-mail amd64 1.14.0-0ubuntu1.2 [41.8 kB]
    googlecompute: Get:16 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-stream amd64 1.14.0-0ubuntu1.2 [63.6 kB]
    googlecompute: Get:17 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx-core amd64 1.14.0-0ubuntu1.2 [413 kB]
    googlecompute: Get:18 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx all 1.14.0-0ubuntu1.2 [3596 B]
    googlecompute: debconf: unable to initialize frontend: Dialog
    googlecompute: debconf: (TERM is not set, so the dialog frontend is not usable.)
    googlecompute: debconf: falling back to frontend: Readline
    googlecompute: debconf: unable to initialize frontend: Readline
    googlecompute: debconf: (This frontend requires a controlling tty.)
    googlecompute: debconf: falling back to frontend: Teletype
    googlecompute: dpkg-preconfigure: unable to re-open stdin:
    googlecompute: Fetched 2460 kB in 1s (1684 kB/s)
    googlecompute: Selecting previously unselected package libjpeg-turbo8:amd64.
    googlecompute: (Reading database ... 60716 files and directories currently installed.)
    googlecompute: Preparing to unpack .../00-libjpeg-turbo8_1.5.2-0ubuntu5.18.04.1_amd64.deb ...
    googlecompute: Unpacking libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.1) ...
    googlecompute: Selecting previously unselected package fonts-dejavu-core.
    googlecompute: Preparing to unpack .../01-fonts-dejavu-core_2.37-1_all.deb ...
    googlecompute: Unpacking fonts-dejavu-core (2.37-1) ...
    googlecompute: Selecting previously unselected package fontconfig-config.
    googlecompute: Preparing to unpack .../02-fontconfig-config_2.12.6-0ubuntu2_all.deb ...
    googlecompute: Unpacking fontconfig-config (2.12.6-0ubuntu2) ...
    googlecompute: Selecting previously unselected package libfontconfig1:amd64.
    googlecompute: Preparing to unpack .../03-libfontconfig1_2.12.6-0ubuntu2_amd64.deb ...
    googlecompute: Unpacking libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    googlecompute: Selecting previously unselected package libjpeg8:amd64.
    googlecompute: Preparing to unpack .../04-libjpeg8_8c-2ubuntu8_amd64.deb ...
    googlecompute: Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
    googlecompute: Selecting previously unselected package libjbig0:amd64.
    googlecompute: Preparing to unpack .../05-libjbig0_2.1-3.1build1_amd64.deb ...
    googlecompute: Unpacking libjbig0:amd64 (2.1-3.1build1) ...
    googlecompute: Selecting previously unselected package libtiff5:amd64.
    googlecompute: Preparing to unpack .../06-libtiff5_4.0.9-5_amd64.deb ...
    googlecompute: Unpacking libtiff5:amd64 (4.0.9-5) ...
    googlecompute: Selecting previously unselected package libwebp6:amd64.
    googlecompute: Preparing to unpack .../07-libwebp6_0.6.1-2_amd64.deb ...
    googlecompute: Unpacking libwebp6:amd64 (0.6.1-2) ...
    googlecompute: Selecting previously unselected package libxpm4:amd64.
    googlecompute: Preparing to unpack .../08-libxpm4_1%3a3.5.12-1_amd64.deb ...
    googlecompute: Unpacking libxpm4:amd64 (1:3.5.12-1) ...
    googlecompute: Selecting previously unselected package libgd3:amd64.
    googlecompute: Preparing to unpack .../09-libgd3_2.2.5-4ubuntu0.2_amd64.deb ...
    googlecompute: Unpacking libgd3:amd64 (2.2.5-4ubuntu0.2) ...
    googlecompute: Selecting previously unselected package nginx-common.
    googlecompute: Preparing to unpack .../10-nginx-common_1.14.0-0ubuntu1.2_all.deb ...
    googlecompute: Unpacking nginx-common (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package libnginx-mod-http-geoip.
    googlecompute: Preparing to unpack .../11-libnginx-mod-http-geoip_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking libnginx-mod-http-geoip (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package libnginx-mod-http-image-filter.
    googlecompute: Preparing to unpack .../12-libnginx-mod-http-image-filter_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking libnginx-mod-http-image-filter (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package libnginx-mod-http-xslt-filter.
    googlecompute: Preparing to unpack .../13-libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package libnginx-mod-mail.
    googlecompute: Preparing to unpack .../14-libnginx-mod-mail_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking libnginx-mod-mail (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package libnginx-mod-stream.
    googlecompute: Preparing to unpack .../15-libnginx-mod-stream_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking libnginx-mod-stream (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package nginx-core.
    googlecompute: Preparing to unpack .../16-nginx-core_1.14.0-0ubuntu1.2_amd64.deb ...
    googlecompute: Unpacking nginx-core (1.14.0-0ubuntu1.2) ...
    googlecompute: Selecting previously unselected package nginx.
    googlecompute: Preparing to unpack .../17-nginx_1.14.0-0ubuntu1.2_all.deb ...
    googlecompute: Unpacking nginx (1.14.0-0ubuntu1.2) ...
    googlecompute: Processing triggers for ufw (0.35-5) ...
    googlecompute: Processing triggers for ureadahead (0.100.0-20) ...
    googlecompute: Setting up libjbig0:amd64 (2.1-3.1build1) ...
    googlecompute: Setting up fonts-dejavu-core (2.37-1) ...
    googlecompute: Setting up nginx-common (1.14.0-0ubuntu1.2) ...
    googlecompute: debconf: unable to initialize frontend: Dialog
    googlecompute: debconf: (TERM is not set, so the dialog frontend is not usable.)
    googlecompute: debconf: falling back to frontend: Readline
    googlecompute: Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
    googlecompute: Setting up libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.1) ...
    googlecompute: Processing triggers for libc-bin (2.27-3ubuntu1) ...
    googlecompute: Processing triggers for systemd (237-3ubuntu10.9) ...
    googlecompute: Setting up libnginx-mod-mail (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up libxpm4:amd64 (1:3.5.12-1) ...
    googlecompute: Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
    googlecompute: Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up libwebp6:amd64 (0.6.1-2) ...
    googlecompute: Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
    googlecompute: Setting up fontconfig-config (2.12.6-0ubuntu2) ...
    googlecompute: Setting up libnginx-mod-stream (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up libtiff5:amd64 (4.0.9-5) ...
    googlecompute: Setting up libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    googlecompute: Setting up libgd3:amd64 (2.2.5-4ubuntu0.2) ...
    googlecompute: Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up nginx-core (1.14.0-0ubuntu1.2) ...
    googlecompute: Setting up nginx (1.14.0-0ubuntu1.2) ...
    googlecompute: Processing triggers for ureadahead (0.100.0-20) ...
    googlecompute: Processing triggers for ufw (0.35-5) ...
    googlecompute: Processing triggers for libc-bin (2.27-3ubuntu1) ...
==> googlecompute: Deleting instance...
    googlecompute: Instance has been deleted!
==> googlecompute: Creating image...
==> googlecompute: Deleting disk...
    googlecompute: Disk has been deleted!
Build 'googlecompute' finished.

==> Builds finished. The artifacts of successful builds are:
--> googlecompute: A disk image was created: ubuntu1804

今度こそいけましたね。
イメージも作成されてます。

nginx 接続確認

今作ったイメージを使用して、インスタンスを適当に上げてみましょう。
HTTP(80)が開いていればとりあえずアクセスできます。
で、やってみると・・・

Screenshot_2018-12-20 Welcome to nginx .png

いけてますね。

まとめ

GCP のイメージ作成もそれほど難しくないですね。
CicleCI なんかで自動化して作成されるようにしていきたいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?