This full example describes how to create a deployment with a virtual machine that uses a container-optimized image. For more information on using containers with Compute Engine, see Container-Optimized Compute Engine Images.
This walk-through describes how to:
- Create a simple container manifest.
- Create a configuration and template that uses a container image.
- Deploy your resources and verify that the deployment was successful.
Create a container manifest
To use containers, you must define a container manifest. The manifest describes properties such as the container image, containers to launch, commands to execute on boot, and ports to enable.
Create a file named container_manifest.yaml
with the following contents:
This manifest creates a container named simple-echo that uses the Hello Application container image and launches an echo server that listens on port 8080.
Create a template and configuration
Next, create a template that launches a virtual machine instance with a
container-optimized image. Create a file named container_vm.[jinja|py]
with
the following contents:
Jinja
Python
Create the corresponding schema file, which enforces the structure of the template:
Jinja
Python
Notice there are a number of parameters defined in this template including:
- The
deployment
,name
, andproject
environment variables. Deployment Manager automatically populates those variables without additional action from you. - The
zone
,containerImage
, andcontainerManifest
properties, which will be defined in the configuration.
Create a configuration file named container_vm.yaml
that
looks like the following:
Jinja
Python
Make sure to replace ZONE_TO_RUN
with the desired zone for your virtual
machine. Notice that the file has also defined the container image to use and
the container manifest that you created earlier.
Deploy your virtual machine instance
Finally, deploy your virtual machine instance using the Google Cloud CLI:
gcloud deployment-manager deployments create my-container-deployment \
--config container_vm.yaml
Once the deployment has been created, you can view the details of your deployment. For example:
$ gcloud deployment-manager deployments describe my-container-deployment
creationTimestamp: '2015-04-02T12:24:31.645-07:00'
fingerprint: ''
id: '8602410885927938432'
manifest: https://www.googleapis.com/deploymentmanager/v2/projects/myproject/global/deployments/my-container-deployment/manifests/manifest-1428002671645
name: my-container-deployment
state: DEPLOYED
resources:
NAME TYPE ID UPDATE_STATE ERRORS
my-container-deployment-my-container-vm compute.v1.instance 3775082612632070557 COMPLETED -
Verify that your instance is running
To test that your container instance started up, visit the virtual
machine's external IP address in your browser, which should print
hello world
:
Add a Compute Engine firewall rule to allow you to query traffic on the virtual machine through port 8080:
gcloud compute firewall-rules create allow-8080 --allow tcp:8080
Get your instance's external IP address:
$ gcloud compute instances describe my-container-deployment-my-container-vm ... name: my-container-vm-my-container-deployment networkInterfaces: - accessConfigs: - kind: compute#accessConfig name: external-nat natIP: 104.197.8.138 type: ONE_TO_ONE_NAT name: nic0 network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default networkIP: 10.240.97.220 scheduling: automaticRestart: true onHostMaintenance: MIGRATE selfLink: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/my-container-deployment-my-container-vm status: RUNNING tags: fingerprint: 42WmSpB8rSM= zone: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a ...
In this case, the external IP is
104.197.8.138
.In a browser window, enter in the external IP and port 8080 in the browser bar. For example,
104.197.8.138:8080
.If successful, you should see a
hello world
message.
(Optional) Delete your deployment
If you want to save on costs and no longer want or need your deployment, delete your deployment.
gcloud deployment-manager deployments delete my-container-deployment
What's next
Explore more about Deployment Manager in the Complete User Guides or through the API.
Try out some other tutorials: