---
title: "Compute Engineのインスタンスを引っ越す！ | grasys blog"
url: "https://blog.grasys.io/post/yusukeh/gce_move/"
description: "こんにちはgrasys長谷川です。 今日は娘の５歳の誕生日なんですぐ帰ります！ｗ Conpute EngineのRegion/Zoneの引っ越しをやってみます。 流れ 作業の流れです。 だいたいこんな感じで引っ越し可能です。 1. InstanceのZone確認 2. 引越し先を決める 3. gcloud compu…"
---

# Compute Engineのインスタンスを引っ越す！

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[yusukeh](/authors/yusukeh/)
-   公開日：2016年10月28日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#disks](/tags/disks/)[#Compute Engine](/tags/compute-engine/)[#Google Cloud](/tags/google-cloud/)

![Compute Engineのインスタンスを引っ越す！](/_astro/ogp.8pwsg8ro_XLVRj.webp)

こんにちはgrasys長谷川です。  
今日は娘の５歳の誕生日なんですぐ帰ります！ｗ

Conpute EngineのRegion/Zoneの引っ越しをやってみます。

## 流れ

作業の流れです。

だいたいこんな感じで引っ越し可能です。

1.  InstanceのZone確認
2.  引越し先を決める
3.  gcloud compute instances moveを発行

> ただし再起動は走ります。

上記で出来ないパターンがあるので出来なかったら以下を行う必要があります。

> Instanceが古いとかかな・・・まあGCPあるあるなのであまり気にしない！

1.  Instanceのdiskのauto-deleteを無効化
2.  Instance 削除（Diskを残して
3.  Disk移動
4.  Instance 作成（既存のDiskから

Image化して新しく立て直す方が簡単だったりしますが・・・ｗ

注意点としてはStatic IPを利用している場合、  
Static IPはRegionに紐づくため引っ越しできません。

## Zones 一覧

Zoneは以下のコマンドで一覧することができます。  
移行先とかはこれで決めて下さい。

Plain textcontent\_copy

```
gcloud compute zones list
NAME               REGION           STATUS  NEXT_MAINTENANCE  TURNDOWN_DATE
asia-east1-c       asia-east1       UP
asia-east1-b       asia-east1       UP
asia-east1-a       asia-east1       UP
...........省略...........
```

## Zoneの確認

### gcloud compute instances list

InstanceのZoneの確認方法はいくつかあるんですが  
以下でインスタンスの一覧がでてくるので確認できます。

| Column | Description |
| --- | --- |
| NAME | Instance Name |
| ZONE | Zone Name |
| MACHINE_TYPE | InstanceのMachine-Type |
| PREEMPTIBLE | プリエンプティブInstance |
| INTERNAL_IP | Google Cloud (GCP) PlatformのInternal Private IP |
| EXTERNAL_IP | いわゆるGlobal IP / Public IP |
| STATUS | RUNNING / TERMINATEDなどInstanceの状態 |

Plain textcontent\_copy

```
gcloud compute instances list
```

### metadata

InstanceにLoginしている状態であればmetadataが利用できます。

Plain textcontent\_copy

```
curl -s -H "Metadata-Flavor: Google" \
  http://metadata/computeMetadata/v1/instance/zone
```

上記以外にもDeveloper Consoleでも確認できます。

### gcloud compute instances move

**再起動が走るので注意**

Plain textcontent\_copy

```
gcloud compute instances move [InstanceName] \
   --zone [今のZone] \
   --destination-zone [移動先のZone]
```

* * *

以下はうまく移動できなかったパターンなので移動できたら読み飛ばしてください。

## Instanceのdiskのパラメータ確認

### gcloud compute instances describe

gcloud コマンドでは以下のように確認できます。

Plain textcontent\_copy

```
gcloud compute instances describe [InstanceName] --zone [Zone]
```

disksの`autoDelete`が`false`になっていることを確認してください。

Plain textcontent\_copy

```
canIpForward: true
cpuPlatform: Intel Broadwell
creationTimestamp: '2016-10-24T19:02:13.476-07:00'
description: ''
disks:
- autoDelete: false
  boot: true
  deviceName: xxxxxx
  index: 0
  interface: SCSI
  kind: compute#attachedDisk
  licenses:
  - https://www.googleapis.com/compute/v1/projects/centos-cloud/global/licenses/centos-6
  mode: READ_WRITE
  source: https://www.googleapis.com/compute/v1/projects/xxxxxxxxxxxxxxx/zones/xxxxxxxxxxx/disks/xxxxxxx
  type: PERSISTENT
```

こちらもDeveloper ConsoleのInstanceの画面

もし`true`になっていたら設定を変更する必要があります。

Plain textcontent\_copy

```
gcloud compute instances set-disk-auto-delete [InstanceName] \
  --no-auto-delete \
  --zone [Zone] \
  --device-name [deviceName]
```

`autoDelete`を再度確認してください。

### Instance削除

Instance削除します。

> –keep-disks パラメータを始めて知るｗ  
> autoDeleteのパラメータうんぬんかんぬんの作業が必要ないｗｗ

Plain textcontent\_copy

```
gcloud compute instances delete [InstanceName] \
  --zone [Zone] \
  --keep-disks=all
```

### gcloud compute disks move

Diskを移動します。

Plain textcontent\_copy

```
gcloud compute disks move [Disk NAME] \
  --zone [Disk Zone]
  --destination-zone [移動先Zone]
```

### Instance作成

先程移動したDiskから新しくInstanceを起動します。  
以下はサンプルなのでちゃんと作って下さい。

Plain textcontent\_copy

```
gcloud compute --project [Project ID] instances create [Instance Name] \
  --zone [Zone] \
  --machine-type [Machine-Type] \
  --network "default" \
  --maintenance-policy "MIGRATE" \
  --scopes default="https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --disk "name=[移動したDisk],device-name=[VM内のDeviceName],mode=rw,boot=yes"
```

これで引っ越し完了！

基本的には簡単でやってみるとすぐできます！

## この記事を書いた人

[![](/_astro/yusukeh.BvUd2yKE_1mQ6Fp.webp)](/authors/yusukeh/)

### [yusukeh](/authors/yusukeh/)

Fly high. Think deep. Move fast. Go far.🦅

-   [X](https://x.com/yusukeh "X")
-   [GitHub](https://github.com/yusukeh "GitHub")
-   [Medium](https://medium.com/@yusukeh "Medium")
-   [Facebook](https://www.facebook.com/yusuke.exzm "Facebook")
-   [Instagram](https://www.instagram.com/yusukehasegawa/ "Instagram")
-   [LinkedIn](https://www.linkedin.com/in/hasegawa-yusuke-091b4164/ "LinkedIn")

[プロフィールと記事一覧](/authors/yusukeh/)
