How to setup static IP to VM hosted on Google Comp

2019-04-15 18:27发布

问题:

I am wondering if there is a way to setup a static IP address to a virtual machine (VirtualBox) hosted on a GCE VM instance (as a VM host).

I want to run two VirtualBox VMs on my GCE VM instance and I want to access them publicly.

回答1:

Yes, you can do this, but you should also consider whether you want the additional overhead of running one virtual machine (VirtualBox) inside of another virtual machine (GCE VM). Running directly on GCE VMs would be more efficient and you can easily create/destroy/control these VMs via Google Cloud Platform APIs.

In addition, if you are already using an automation framework for your VirtualBox VMs such as Vagrant, note that Vagrant natively supports GCE VMs, so with a few changes to your configuration, you can use Vagrant to create/modify/connect/destroy your GCE VMs as easily as you would local VirtualBox VMs.

As an alternative to static external IPs, consider also using a domain name mapping to ephemeral external IP(s) of your instance(s). You can use Google Cloud DNS to manage your DNS mappings.

For more info on static external IPs, see the docs:

Static external IP addresses

If you need a static external IP address that is assigned to your project and persists until you explicitly release it, you can reserve a new static external IP address or promote an ephemeral external IP address to a static external IP address. Use gcloud compute with the addresses create command or make a PUT request to the appropriate regional Addresses collection to reserve a static external IP address.

Static external IP addresses assigned to instances are a regional resource and you must select the region where the IP address will belong to when you create the address.

[...]

Restrictions

Static external IP addresses can only be used by one resource at a time. You cannot assign a static external IP address to multiple resources.

There is no way to tell whether an IP address is static or ephemeral after it has been assigned to a resource, except to compare the IP address against the list of static external IP addresses reserved to that project. Use the addresses list sub-command to see a list of static external IP addresses available to the project.

Note that static external IPs also come with some cost:

IP address pricing

Type                                                            Price/Hour

Static IP address (assigned but unused)                           $0.01
Static IP address (assigned and in use)                         No charge
Ephemeral IP address (attached to instance or forwarding rule)  No charge


回答2:

I solved that issue to in 2 steps:

First delete the current possible ephimeral ip configuration:

gcloud compute instances delete-access-config <instance> --access-config-name "External NAT"

Where <instance> is the name of the instance you want to update, and External NAT is the name of the configuration, which probably has that value because of its the default one. And you can check it running this:

gcloud compute instances describe --zone=us-west1-a

And add the static one

If you want to bind an static address, probably to bind it to a DNS address, execute something like this

gcloud compute instances add-access-config <instance> --access-config-name="External NAT" --address=xxx.xxx.xxx.xxx

Remember to always append the --zone to any gcloud command to avoid any ambiguity. You can get the address from the valid addresses configuration you have in your project, which you can be gotten like this:

gcloud compute addresses list

Dont use the NAME but the ADDRESS. You should pick an address in the same zone of your instance. When the address be attached you will see that in the STATUS field of the last query it will say IN USE.

Et voila!