Cloud Computing & MSA

gcloud SDK로 Compute Engine 생성하기

sstdio.h·2020년 2월 6일·조회 6,797

1. 개요

Google Cloud Compute Engine에서 VM 인스턴스를 생성하고 삭제하는 기본 예제이다. 생성에는 gcloud compute instances create 명령어를 사용하고, 삭제에는 gcloud compute instances delete 명령어를 사용한다.

명령을 실행하기 전에 gcloud CLI 인증과 기본 프로젝트 설정이 되어 있어야 한다. 기본 영역(zone)을 설정하지 않았다면 예제처럼 --zone 옵션을 명시하는 것이 좋다.


2. 방법

생성은 gcloud compute instances create 명령어를 사용한다. 인스턴스 이름, 머신 타입, 이미지 프로젝트, 이미지 패밀리, 영역을 함께 지정하면 된다.

  • example-instance: 생성할 인스턴스 이름
  • --machine-type: 사용할 머신 타입
  • --image-project, --image-family: 부팅 디스크에 사용할 OS 이미지
  • --zone: 인스턴스를 생성할 영역

아래 예제는 Ubuntu 이미지를 사용해 일본 리전의 asia-northeast1-b 영역에 인스턴스를 생성한다.


3. 생성 예제

$ gcloud compute instances create example-instance \
>       --machine-type=n1-standard-1 \
>       --image-project=ubuntu-os-cloud \
>       --image-family=ubuntu-1804-lts \
>       --zone=asia-northeast1-b
Created [https://www.googleapis.com/compute/v1/projects/xxx/zones/asia-northeast1-b/instances/example-instance].
NAME              ZONE               MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
example-instance  asia-northeast1-b  n1-standard-1               10.146.0.2   34.84.93.94  RUNNING

생성이 완료되면 출력의 STATUSRUNNING으로 표시된다. 또한 내부 IP와 외부 IP가 함께 출력되므로, 필요한 경우 이 값을 이용해 접속이나 방화벽 설정을 확인할 수 있다.

예제의 ubuntu-1804-lts는 당시 사용한 이미지 패밀리이다. 새로 인스턴스를 만들 때는 현재 사용 가능한 이미지 패밀리를 확인한 뒤 필요한 OS 버전을 선택하는 것이 좋다.


4. 삭제 예제

인스턴스 삭제는 gcloud compute instances delete 명령어로 수행한다. 삭제 시 연결된 디스크가 자동 삭제 대상으로 설정되어 있으면 함께 삭제될 수 있으므로, 필요한 데이터가 있다면 삭제 전에 백업하거나 디스크 보존 옵션을 확인해야 한다.

$ gcloud compute instances delete example-instance
No zone specified. Using zone [asia-northeast1-b] for instance: [example-instance].
The following instances will be deleted. Any attached disks configured
 to be auto-deleted will be deleted unless they are attached to any
other instances or the `--keep-disks` flag is given and specifies them
 for keeping. Deleting a disk is irreversible and any data on the disk
 will be lost.
 - [example-instance] in [asia-northeast1-b]

Do you want to continue (Y/n)?  y

Deleted [https://www.googleapis.com/compute/v1/projects/xxx/zones/asia-northeast1-b/instances/example-instance].

삭제 후에는 다음과 같이 목록을 조회해 인스턴스가 남아 있는지 확인할 수 있다.

$ gcloud compute instances list

5. 기타

5.1. 한국 리전

2월 6일 현재 한국 리전은 보이긴 하나, gcloud SDK로 한국 리전을 지정하면 에러가 발생하여 위 예제는 일본 리전으로 생성했다.

$ gcloud compute instances create example-instance \
>       --machine-type=n1-standard-1 \
>       --image-project=ubuntu-os-cloud \
>       --image-family=ubuntu-1804-lts \
>       --zone=asia-northeast3-a
ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - Invalid value for field 'zone': 'asia-northeast3-a'. Unknown zone.

이 내용은 당시 실행 결과에 대한 기록이다. 같은 명령을 다시 실행할 때는 현재 프로젝트에서 사용할 수 있는 영역 목록을 먼저 확인하는 것이 안전하다.

$ gcloud compute zones list

댓글 0

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.