Skip to content

Instantly share code, notes, and snippets.

@francois-blanchard
Last active August 24, 2020 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save francois-blanchard/c70a4169e9c03138b59130e43241326e to your computer and use it in GitHub Desktop.
Save francois-blanchard/c70a4169e9c03138b59130e43241326e to your computer and use it in GitHub Desktop.
Google cloud - Manage ip instance with gcloud

Google cloud - Manage ip instance with gcloud

Before you begin

Install & config

  1. Install the gcloud command-line tool.
  2. Set a default region and zone.

List items

$ gcloud projects list
> 
  PROJECT_ID         NAME               PROJECT_NUMBER
  data-collector-01  data collector 01  xxxxxxxxxx
  ...
$ gcloud compute instances list
>
  NAME           ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP     EXTERNAL_IP     STATUS
  alexandria     europe-west1-d  n1-standard-2               10.240.x.x      xx.xxx.xxx.xx   TERMINATED
  ...
$ gcloud compute addresses list
>
  NAME           REGION        ADDRESS          STATUS
  alexandria     europe-west1  xx.xxx.xxx.xx    IN_USE

Assigning an external IP address to an existing instance

1. Check access-config instance

$ gcloud compute instances describe [INSTANCE_NAME]
> 
...
networkInterfaces:
- accessConfigs:
  - kind: compute#accessConfig
    name: External NAT
    natIP: 104.155.61.176
    type: ONE_TO_ONE_NAT
...

If natIP is present

  • You have to dissociate this IP before assign a new IP (step 2.)

Else

  • Your can assign an external IP (step 3.)

2. Dissociate IP

gcloud compute instances delete-access-config [INSTANCE_NAME] \
    --access-config-name [ACCESS_CONFIG_NAME]
  • [INSTANCE_NAME] is the name of the instance.
  • [ACCESS_CONFIG_NAME] is the access config to delete.
# Example :
gcloud compute instances delete-access-config beta-genesis-01 \
    --access-config-name "External NAT"

3. Assign IP

gcloud compute instances add-access-config [INSTANCE_NAME] \
    --access-config-name [ACCESS_CONFIG_NAME] --address [IP_ADDRESS]
  • [INSTANCE_NAME] is the name of the instance.
  • [ACCESS_CONFIG_NAME] is access config to delete.
  • [IP_ADDRESS] is the IP address to add.
# Example :
gcloud compute instances add-access-config beta-genesis-01 \
    --access-config-name "External NAT" --address "104.155.61.176"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment