Instance templates allow you to predefine a machine configuration and image to automatically start your server clones. They are commonly used with autoscaling to facilitate high availability services.
What are instance templates?
Autoscaling may seem like magic, but at the end of the day you are still creating and destroying servers multiple times a day, an exploit that would kill any system administrator if instructed to do it manually.
Automating your work might seem like an overwhelming task, but in reality you are just setting up a startup configuration and making a copy of your server’s files. If you want to automate it properly, you’ll either need to write a startup script to install the dependencies your server needs, or run it in a container, which ends up being largely the same type of configuration.
GCP instance models support both of these methods. You can create an instance template to deploy a container image to an autoscaled or managed instance group, or just create a copy of your server to deploy another. In either case, instance templates also allow you to define the types of servers you want to deploy, either a specific instance type or a specific amount of memory and vCPUs.
Creating instance templates
You have two options for creating instance templates: either from a container image or from an existing instance.
If you want to deploy a container image, you can create this template from the Instance Templates tab in the Compute Engine Console. Select the machine parameters, such as the number of virtual processors and the amount of memory.
Then, under container settings, check “Yes” to deploy a container image and enter the URL of the image. You can use Google container registry for that, or use a public registry like the Docker hub.
In addition to that, you have a few options to set the input command, any parameters or environment variables, as well as to run the container as privileged, which allows root access to storage and network devices like if it was running directly on the host. You can also configure volume mounts and add additional disks to the container.
If you want to create a copy of an existing instance, you can do so from the gcloud command line. You will need a few options: the model name, the name of the source instance, and the name of the disk you want to copy.
gcloud compute instance-templates create [TEMPLATE NAME]
–configure-disk = device-name =[DISK NAME], instantiate of = source-image,
auto-delete = true
Of course, you will have to repeat this process each time you want to make updates to your instance group.
This is definitely not the best way to handle updates, and you have two solutions to this problem: add an install script to handle update deployments at startup, like grab the latest version of git or move to containers. Any task that can be scaled automatically can be containerized, and running in Docker has no performance impact. You can read our guide to moving your application to Docker to learn more.
Using your models in a MIG
Managed Instance Groups (MIG) are groups of instances that support auto-scale and auto-repair, of course, powered by instance templates. You can select the template you want to use in the settings when creating a managed instance group.
If you just want to run a set of instances behind a load balancer, you can use unmanaged instance groups, which don’t need to be configured with instance templates, but require you to administer manually. waiters.