Instance templates allow you to predefine a machine configuration and image to launch clones of your servers automatically. They are often used with automatic scaling to facilitate high availability services.
What are instance templates?
Automatic scaling may seem like magic, but at the end of the day, you still create and destroy servers several times a day, a feat that would kill all sysadmin if they were instructed to do it manually.
Automating your job may seem like an insurmountable task, but you really only define a startup configuration and make a copy of the server̵7;s files. If you want to automate it properly, you must either write a startup script to install dependencies that your server needs or move to run in a container, which in the end is pretty much the same type of configuration.
GCP instance templates support both of these methods. You can create an instance template to distribute a container image in an auto-scaling or managed instance group, or simply make a copy of your server to distribute another. In both cases, instance templates also allow you to define the types of servers you want to deploy, either a specific instance type or a certain amount of memory and vCPUs.
Create 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 of the Compute Engine console. Select machine settings, such as the number of vCPUs and the amount of memory.
Then select “Yes” under container settings to distribute a container image and enter the URL of the image. You can use Google’s container registry for this or use a public registry such as the Docker hub.
In addition, you have some options for setting the mail command, any parameters or environment variables, as well as running the container as privileged, giving root access to storage and network devices as if it were run directly on the host. You can also configure volume mounts and add additional disks to the container.
If you want to make a copy of an existing instance, you can do so from
gcloud command line. You need some options – the name of the template, the name of the source instance and the name of the disk you want to copy.
gcloud compute instance-templates create [TEMPLATE NAME] --source-instance [SOURCE_INSTANCE] --configure-disk= device-name=[DISK NAME], instantiate-from=source-image, auto-delete=true
Of course, you have to repeat this process every 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 installation script to manage the distribution of updates at startup, such as downloading the latest version from git, or moving to containers. Every task that can be scaled automatically can be containerized, and there is no performance hit to run in Docker. You can read our guide to moving your application to Docker to learn more.
Use your templates in a MIG
Managed Instance Groups (MIG) are groups of instances that support automatic scaling and automatic healing, of course, run by instance templates. You can choose which template you want to use from the settings when creating a managed instance group.
If you only want to run a set of instances behind a load balancer, you can use unmanaged instance groups, which do not need to be configured with instance templates but require you to manually manage the servers.