Deploy a web application using Google Compute Engine
- create a Compute Engine instance
- deploy a sample 'Guestbook' application
- expose the server to the internet by modifying the firewall
- test the connection
- delete the instance and associated disk
An instance can be created via Cloud SDK or via the web UI. We'll use the web UI.
Ensure you are on the correct project
click 'Create Instance'
select the closest region to you
select 'small' machine type
define a boot disk
- this will create a root-persistent disk that contains a boot loader, OS, and file system from the image we select for the disk
- select CoreOs stable
- leave boot disk type as 'standard' SSD is also an option
- default disk size is suitable for this demo
we now have the minimum requirements for an instance:
- machine type
- boot disk
we can view the command line equivalent of the above settings using the 'command line' link at the bottom of the page:
gcloud beta compute --project=r<project-id> instances create <instance-name> \ --zone=europe-west2-a \ --machine-type=g1-small \ --subnet=default \ --network-tier=PREMIUM \ --maintenance-policy=MIGRATE \ --service-account=<service-account> \ --scopes=<scopes> \ --image=coreos-stable-1745-6-0-v20180611 \ --image-project=coreos-cloud \ --boot-disk-size=9GB \ --boot-disk-type=pd-standard \ --boot-disk-device-name=<instance-name>
once the instance is created we can see it running, and SSH into it
There are a few ways to deploy an application to an instance:
- SSH through the browser window
- view the
gcloudcommand by using the menu next to the SSH button on the instance line item
- command can be executed using a locally installed Cloud SDK or through the Cloud Shell
We'll SSH through the browser window:
click SSH on the instance line item
we need a backend to store data entered via the frontend
- we'll use a redis backend
- we'll use Docker to spin up the backend
# create a detached docker container that: # runs in detached mode # has a name of redis_db # and is built from the redis image on Dockerhub $ docker run -d --name redis_db redis
Once the image is created we need to:
- download and build a container for the frontend
- link the backend and frontend containers
- expose the application to the internet
confirm the container is running:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d441e0986893 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp redis_db
download the application.
gitis already installed, so all we need to do is clone it:
$ git clone https://github.com/larrybotha/cp100-compute-engine-python
This repo contains a Docker file and the application, so we can Docker's
buildcommand to build an image.
build the application's Docker image:
$ cd cp100-compute-engine-python # tag the build so that we can reference, and use the current directory as the context for building the image $ docker build -t app-frontend/python-redis .
What this has done is biult an image with all the dependencies our application needs, such as Python runtime, redis etc.
run the container
# run the container # exposing port 80 inside the container to port 80 outside the container # linking the redis_db container we created to the db namespace inside the container # and running the image called app-frontend/python-redis $ docker run -p 80:80 --link redis_db:db app-frontend/python-redis
attempting to visit the external IP (available via the isntance line item in Cloud Console) will timeout - we haven't yet allowed HTTP traffic to the instance
firewall rules can modified in 2 ways:
- go to Firewall rules (opens in a new tab) udner Network settings and add a rule there
- edit the instance directly to allow HTTP traffic
on the instance's page:
- click 'Edit'
- check 'allow HTTP traffic'
- click 'save'
once the firewall rule is configured, an HTTP rule is added to the Firewall rules (opens in a new tab) page
- on the firewall rules the HTTP rule has a
- on the instance there's a Network tag with
http-serversignifying the relationship between the rule and the instance
- on the firewall rules the HTTP rule has a
- the instance is created with a boot disk. This can be automatically deleted when the instance is deleted by configuring settings on the instance itself. By default the boot disk is deleted when the instance is deleted.
- the disks associated with the instances in the current project can be viewed on the Disks (opens in a new tab) page
- the instance can be deleted either through the web UI or the the Cloud SDK