gcloud compute 사용 도움말


이 페이지에서는 gcloud 명령줄 도구를 사용하여 Google Compute Engine 리소스를 관리할 때 참고하면 유용한 팁을 확인할 수 있습니다. 사용 가능한 모든 gcloud compute 플래그와 명령어의 전체 목록은 기본 제공되는 명령어 도움말(--help)이나 게시된 참조 문서 또는 gcloud 핵심 문서를 참조하세요.

시작하기 전에

  • 아직 인증을 설정하지 않았다면 설정합니다. 인증은 Google Cloud 서비스 및 API에 액세스하기 위해 ID를 확인하는 프로세스입니다. 로컬 개발 환경에서 코드 또는 샘플을 실행하려면 다음과 같이 Compute Engine에 인증하면 됩니다.
    1. Google Cloud CLI를 설치한 후 다음 명령어를 실행하여 초기화합니다.

      gcloud init
    2. Set a default region and zone.

리소스 정보 가져오기

Compute Engine 리소스 관련 정보를 가져오려면 list 명령어를 사용하여 리소스 목록을 반환하거나 describe 명령어를 사용하여 특정 리소스의 세부정보를 반환하면 됩니다.

list 명령어로 리소스 가져오기

list 명령어는 요청된 리소스와 가장 관련이 있는 데이터를 사람이 읽을 수 있는 표로 반환하도록 설계되었습니다. 필요한 경우 결과를 필터링하여 관련성이 더 높은 결과로 구성된 더 짧은 목록을 반환할 수 있습니다.

이름의 정규 표현식 필터링
RE2 구문을 사용해 리소스 이름(예: 인스턴스 또는 디스크 이름)을 일치시킬 수 있습니다 gcloud compute instances list 명령어를 참조하세요.
명령어 플래그
--limit

반환할 최대 결과 수입니다. 이 플래그는 describe 명령어로 리소스 가져오기 섹션에 설명된 --sort-by 플래그와 함께 사용할 때 특히 유용합니다.

--sort-by SORT_BY

정렬 기준이 되는 필드입니다(해당하는 경우). 내림차순으로 정렬하려면 값 앞에 물결 ('~')를 붙입니다. 이 플래그는 --flatten, --sort-by, --filter, --limit의 순서로 적용되는 다른 플래그와 상호작용합니다.

describe 명령어로 리소스 가져오기

describe 명령어는 리소스 1개의 데이터를 표시하도록 설계되었습니다. describe 명령어에는 리소스 이름을 입력해야 합니다. 리소스 이름이 기억나지 않으면 list 명령어를 실행하여 리소스 목록을 가져올 수 있습니다. 예를 들어 다음 두 명령어는 이미지 목록을 나열하여 이미지 이름 및 관련 프로젝트를 가져와 이를 describe 명령어에 입력으로 제공할 수 있는 시나리오를 보여줍니다.

gcloud compute images list
NAME                                PROJECT        FAMILY     DEPRECATED STATUS
...
centos-7-v20170620                  centos-cloud   centos-7              READY
...
debian-9-stretch-v20170619          debian-cloud   debian-9              READY
...
gcloud compute images describe debian-9-stretch-v20170619 --project debian-cloud

describe 명령어의 기본 출력은 YAML 형식이지만, --format 플래그를 사용하여 JSON, YAML, 텍스트 출력 형식 중에서 선택할 수도 있습니다. JSON 형식의 출력은 출력을 파싱하는 경우에 유용하며, 텍스트 형식의 출력은 각 속성을 별도의 줄에 표시합니다.

gcloud compute regions describe us-central1 --format json
{
  "creationTimestamp": "2013-09-06T10:36:54.847-07:00",
  "description": "us-central1",
  "id": "6837843067389011605",
  "kind": "compute#region",
  "name": "us-central1",
  ...
  "status": "UP",
  "zones": [
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a",
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-b",
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f"
  ]
}

예시

list 명령어의 예시

리소스 목록을 나열할 때 쉽게 읽을 수 있는 요약 데이터 표가 제공됩니다. 예를 들어 프로젝트의 인스턴스에 대한 요약 데이터를 반환하려면 instances list 명령어를 사용합니다.

gcloud compute instances list
NAME               ZONE          MACHINE_TYPE  INTERNAL_IP    EXTERNAL_IP     STATUS
example-instance   asia-east1-b  e2-standard-2 10.240.95.199  107.167.182.44  RUNNING
example-instance2  us-central1-a e2-standard-2 10.240.173.254 23.251.148.121  RUNNING
test-instance      us-central1-a e2-standard-2 10.240.118.207 23.251.153.172  RUNNING

--filter 플래그와 key ~ value 연산자가 포함된 정규 표현식을 사용하여 list 명령어의 결과를 필터링할 수 있습니다. 예를 들어 인스턴스 이름에 'test'가 있는 인스턴스만 포함하도록 인스턴스 목록을 필터링합니다.

gcloud compute instances list --filter="name ~ .*test.*"
NAME           ZONE          MACHINE_TYPE  INTERNAL_IP    EXTERNAL_IP     STATUS
test-instance  us-central1-a e2-standard-2 10.240.118.207 23.251.153.172  RUNNING

statusDONE이고 httpStatus200이 아닌 영역 작업의 목록을 반환하려면 operations list 명령어에 zone필터를 사용하고 결과를 grep합니다.

gcloud compute operations list --filter="zone:(us-central1-a)" | grep DONE | grep -v 200
NAME                                                    HTTP_STATUS TYPE   TARGET                               STATUS
operation-1397752585735-4f73fa25b4b58-f0920fd5-254d709f 400         delete us-central1-a/disks/example-instance DONE
operation-1398357613036-4f7cc80cb41e0-765bcba6-34bbd040 409         insert us-central1-a/instances/i-1          DONE
operation-1398615481237-4f8088aefbe08-cc300dfa-2ce113cf 409         insert us-central1-a/instances/i-2          DONE

us-central1-a의 디스크 목록을 이름이 내림차순으로 정렬된 상태(--sort-by ~NAME)로 가져오려면 disks list 명령어를 사용합니다.

gcloud compute disks list --sort-by ~NAME --filter="zone:(us-central1-a)"

list 명령어의 출력을 다른 명령어로 전달하거나 리소스 링크 목록을 사용하는 다른 애플리케이션으로 전달하는 요청과 같은 일부 시나리오에서는 리소스에 대한 전체 URI 링크가 필요할 수 있습니다. 전체 URI 리소스 링크를 표시하려면 list 명령어에 --uri 플래그를 사용하세요.

gcloud compute instances list --uri --filter="name~'^example-.*'"
https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance1
https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance2

인스턴스를 삭제하는 명령어 내에서 위의 list 명령어 결과를 사용하려면 다음을 사용하세요.

gcloud compute instances delete $(gcloud compute instances list --uri --filter="name~'^example-.*'")

describe 명령어의 예시

인스턴스 1개의 세부정보만 가져오려면 영역을 포함하여 인스턴스를 지정합니다. 예를 들어 'asia-east1-b' 영역에서 이름이 'example-instance'인 인스턴스 정보를 반환하려면 다음과 같이 instances describe 명령어를 사용할 수 있습니다.

gcloud compute instances describe example-instance --zone asia-east1-b

기본적으로 YAML 출력이 반환됩니다. 출력을 JSON 또는 텍스트(줄당 속성 하나)로 변경하려면 --format 플래그를 사용합니다. 예를 들어, 동일한 인스턴스의 텍스트 출력을 반환하려면 다음을 사용하세요.

gcloud compute instances describe example-instance --zone asia-east1-b --format text
---
canIpForward:                                False
creationTimestamp:                           2014-04-19T06:43:04.087-07:00
disks[0].autoDelete:                         False
disks[0].boot:                               True
disks[0].deviceName:                         example-instance
...

특정 작업의 세부정보를 가져오려면 operations list 명령어를 사용하여 작업의 정규화된 URI를 찾습니다.

gcloud compute operations list --filter="zone:(us-central1-a)"
NAME                                                    TYPE   TARGET                                      HTTP_STATUS STATUS
operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0 delete us-central1-a/instances/example-instance    200         DONE
operation-1406155180632-4fee4040a67c1-bf581ed8-ab5af2b8 delete us-central1-a/instances/example-instance-2  200         DONE
...

그런 다음 operations describe 명령어에 해당 URI를 사용합니다.

gcloud compute operations describe \
operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0 --zone us-central1-a
endTime: '2014-07-23T15:40:02.463-07:00'
id: '31755455923038965'
insertTime: '2014-07-23T15:39:25.910-07:00'
kind: compute#operation
name: operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0
operationType: delete
progress: 100
...

다음 명령어는 인스턴스 설정을 JSON 형식(--format json)으로 가져옵니다.

gcloud compute instances describe example-instance \
    --zone us-central1-a
    --format json
{
   ...
   "name": "example-instance",
   "networkInterfaces": [
    {
      "accessConfigs": [
        {
          "kind": "compute#accessConfig",
          "name": "external-nat",
          "natIP": "107.167.187.66",
          "type": "ONE_TO_ONE_NAT"
        }
      ],
      "name": "nic0",
      "network": "https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default",
      "networkIP": "10.240.111.51"
    }
   ],
   ...
   "status": "RUNNING"
   ...
}

어떤 사용자로 승인되었는지 확인하기

어떤 계정으로 승인되었는지 알아보려면 다음 명령어를 사용합니다.

gcloud auth list

토큰 새로고침 취소

Google Cloud CLI를 사용하는 시스템에서 계정의 사용자 인증 정보를 취소하려면 다음을 사용하세요.

gcloud auth revoke

이렇게 하면 gcloud init를 사용하여 다시 인증해야 합니다.

리소스에 액세스하는 gcloud CLI의 권한을 취소할 수도 있습니다. 예를 들어, 토큰 새로고침이 손상된 경우 이를 수행할 수 있습니다. gcloud CLI의 권한을 취소하려면 다음 안내를 따르세요.

  1. Google 계정 페이지에 로그인합니다.
  2. 계정 권한 섹션에서 보안을 클릭한 다음 모두 보기를 클릭합니다.
  3. Google Cloud SDK를 선택하고 액세스 권한 취소를 클릭합니다.

인스턴스 재부팅

'us-central1-a' 영역에서 이름이 'example-instance'인 인스턴스를 재설정하려면 다음과 같이 instances reset 명령어를 사용합니다.

gcloud compute instances reset example-instance --zone us-central1-a

재설정으로 인한 영향에 대한 자세한 내용은 인스턴스 재설정 문서를 읽어보세요.