Google Container Engine makes it easy to run docker containers in the cloud. Google Container Engine uses Kubernetes, an open source container scheduler, to ensure that your cluster is running exactly the way you want it to at all times.
Follow along this lab to learn how to launch a container on Google Container Engine.
If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.
These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.
Note that all these accounts will be disabled soon after the codelab is over.
Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.
Here's what you should see once logged in:
When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :
In this section you'll create a Google Container Engine cluster.
Navigate to the the Google Cloud Console from another browser tab/window, to https://console.cloud.google.com. Use the login credential given to you by the lab proctor.
Click on the menu icon in the top left of the screen and select API Manager from the drop down.
Search for "Google Compute Engine" in the search box. Click on "Google Compute Engine" in the results list that appears.
On the Google Compute Engine page click "Enable"
Once it has enabled click the arrow to go back.
Now search for "Google Container Engine API" and enable it as well.
Launch Cloud Shell by clicking on the terminal icon in the top toolbar.
Cloud Shell is a browser based terminal to a virtual machine that has the Google Cloud Platform tools installed on it and some additional tools (like editors and compilers) that are handy when you are developing or debugging your cloud application.
We'll be using the
gcloud command to create the cluster. First, though, we need to set the compute zone so that the virtual machines in our cluster are created in the correct region. We can do this using
gcloud config set compute/zone. Enter the following in Cloud Shell.
$ gcloud config set compute/zone us-central1-b
You can create a new container cluster with the
gcloud command like this:
$ gcloud container clusters create hello-world --num-nodes 1
This command creates a new cluster called "hello-world" with one node (VM). Since we'll only be launching one container a single node is fine. If you wanted to launch multiple containers you could specify a different number of nodes when you create the cluster.
Launching the cluster may take a bit of time but once it is up you should see output in Cloud Shell that looks like this:
NAME ZONE MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS hello-world us-central1-b 1.1.8 184.108.40.206 n1-standard-1 1.1.8 1 RUNNING
You can also verify that the cluster started successfully by checking the instances list:
$ gcloud compute instances list
The results should list one instance with a status of "RUNNING".
Now that we have a cluster running it is time to deploy a wordpress container to it. We'll be using the
tutum/wordpress image. This image containers all you need to run a wordpress site, including a MySQL database in a single Docker Container.
A pod is one or more containers that "travel together". That might mean they are administered together or they have the same network requirements. For this example we only have one container in our pod.
Create the pod using
$ kubectl run wordpress --image=tutum/wordpress --port=80
This command starts up the docker image on one of the nodes in the cluster. You can see the pod running using
kubectl as well.
$ kubectl get pods
You should get back a result that looks something like:
NAME READY STATUS RESTARTS AGE wordpress-297829341-pw6c4 1/1 Running 0 7m
By default a pod is only accessible to other machines inside the cluster. In order to use the wordpress container that was created it needs to be exposed as a service.
Expose the pod with the
expose command. The
--type=LoadBalancer flag creates an external IP that this pod can use to accept traffic.
$ kubectl expose pod <pod name> --name=wordpress --type=LoadBalancer
kubectl expose creates a service, the forwarding rules for the load balancer, and the firewall rules that allow external traffic to be sent to the pod. You can see more details of this by running
$ kubectl describe services wordpress Name: wordpress Namespace: default Labels: run=wordpress Selector: run=wordpress Type: LoadBalancer IP: 10.51.252.165 LoadBalancer Ingress: 220.127.116.11 Port: <unnamed> 80/TCP NodePort: <unnamed> 31392/TCP Endpoints: 10.48.0.7:80 Session Affinity: None
Note the value in the LoadBalancer Ingress field. This is how you can access your container from outside the cluster.
Note: It may take a minute or so for the LoadBalancer Ingress field to get populated.
Open a new browser window or tab and navigate to the IP address in the LoadBalancer Ingress field from the previous step. You should see the wordpress launch page.
Google Container Engine and Kubernetes provide a powerful and flexible way to run containers on Google Cloud Platform. Kubernetes can also be used on your own hardware or on other Cloud Providers.
This example only used a single container but it is simple to setup multiple container environments or multiple instances of a single container as well.