GCP: Deploying Containers
We'll learn how to deploy Docker images on Google Compute Engine virtual machine instances.
To deploy and launch our container on a Compute Engine VM, we provide a Docker image name and configure how our container should run when creating a VM or an instance template. Compute Engine will take care of the rest including supplying an up-to-date Container-Optimized OS image with Docker installed and launching our container when the VM starts up. - Deploying Containers on VMs and Managed Instance Groups
Container-Optimized OS is an operating system image for our Compute Engine VMs that is optimized for running Docker containers. With Container-Optimized OS, we can bring up our Docker containers on Google Cloud Platform quickly, efficiently, and securely. Container-Optimized OS is maintained by Google and is based on the open source Chromium OS project.
Container-Optimized OS from Google Documentation
A managed instance group uses an instance template to create a group of identical instances. We control a managed instance group as a single entity. If we wanted to make changes to instances that are part of a managed instance group, we would make the change to the whole instance group. Because managed instance groups contain identical instances, they offer the following features:
- When our applications require additional compute resources, managed instance groups can automatically scale the number of instances in the group.
- Managed instance groups work with load balancing services to distribute traffic to all of the instances in the group.
- If an instance in the group stops, crashes, or is deleted by an action other than the instance groups commands, the managed instance group automatically recreates the instance so it can resume its processing tasks. The recreated instance uses the same name and the same instance template as the previous instance, even if the group references a different instance template.
- Managed instance groups can automatically identify and recreate unhealthy instances in a group to ensure that all of the instances are running optimally.
Running each microservice on a separate VM on Compute Engine could make the operating system overhead a significant part of our cost. Kubernetes Engine allows us to deploy multiple containers and groups of containers for each VM instance, which can lead to more efficient host VM utilization for microservices with a smaller footprint.
We can only deploy one container for each VM instance. Consider Kubernetes Engine if we need to deploy multiple containers per VM instance.
We can deploy a container to a new managed instance group using Google Cloud Platform Console or the gcloud command line tool by following these steps:
- Create an instance template, based on a Docker image.
- Create a managed instance group from the new instance template.
We'll create an instance template that deploys a container from a Google-provided Nginx (gcr.io/cloud-marketplace/google/nginx1:1.12) Docker image to a managed instance group.
- Go to the Instance templates page.
- Click the "Create instance template" button to create a new instance template.
- Under the Container section, check Deploy container image.
- Specify the Docker image name under Container image and configure options to run the container if desired. For example, we can specify gcr.io/cloud-marketplace/google/nginx1:1.12 for the container image.
- Click Create.
Now that we have an instance template, we can create a managed instance group using the instance template. For example, to create a managed instance group using the gcloud tool with the nginx-template that we just created, we need to run the following command:
$ gcloud compute instance-groups managed create example-group \ --base-instance-name nginx-vm \ --size 2 \ --template instance-template-1 Created [https://www.googleapis.com/compute/v1/projects/linuxvm-216022/zones/us-east1-c/instanceGroupManagers/example-group]. NAME LOCATION SCOPE BASE_INSTANCE_NAME SIZE TARGET_SIZE INSTANCE_TEMPLATE AUTOSCALED example-group us-east1-c zone nginx-vm 0 2 instance-template-1 no
We can connect to a container on a VM using SSH. In this case, use the gcloud beta compute ssh command instead of the standard gcloud compute ssh command. Using the gcloud tool, run the gcloud beta compute ssh command with the --container flag.
gcloud beta compute ssh [INSTANCE_NAME] --container [CONTAINER_NAME]
- [INSTANCE_NAME] is the name of the VM instance.
- [CONTAINER_NAME] is either the name of the standalone VM instance or the name of the instance template, if the instance belongs to a managed instance group.
$ gcloud beta compute ssh nginx-vm-lvxk --container instance-template-1
GCP (Google Cloud Platform)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization