website/docs/hellonode.md

375 lines
17 KiB
Markdown
Raw Normal View History

---
2016-07-29 17:36:25 +00:00
assignees:
- dchen1107
- pwittrock
---
* TOC
{:toc}
2016-03-05 01:31:45 +00:00
## Introduction
The goal of this codelab is for you to turn a simple Hello World node.js app into a replicated application running on Kubernetes. We will show you how to take code that you have developed on your machine, turn it into a Docker container image, and then run that image on [Google Container Engine](https://cloud.google.com/container-engine/).
Heres a diagram of the various parts in play in this codelab to help you understand how pieces fit with one another. Use this as a reference as we progress through the codelab; it should all make sense by the time we get to the end.
![image](/images/hellonode/image_1.png)
Kubernetes is an open source project which can run on many different environments, from laptops to high-availability multi-node clusters, from public clouds to on-premise deployments, from virtual machines to bare metal. Using a managed environment such as Google Container Engine (a Google-hosted version of Kubernetes) will allow you to focus more on experiencing Kubernetes rather than setting up the underlying infrastructure.
2016-03-05 01:31:45 +00:00
## Setup and Requirements
If you don't already have a Google Account (Gmail or Google Apps), you must [create one](https://accounts.google.com/SignUp). Then, sign-in to Google Cloud Platform console ([console.cloud.google.com](http://console.cloud.google.com)) and create a new project:
![image](/images/hellonode/image_2.png)
![image](/images/hellonode/image_3.png)
Remember the project ID; it will be referred to later in this codelab as `$PROJECT_ID`.
Make sure you have a Linux terminal available, you will use it to control your cluster via command line. You can use [Google Cloud Shell](https://console.cloud.google.com?cloudshell=true), it has the software this codelab uses pre-installed so that you can skip most of the environment configuration steps below.
It may be helpful to store your project ID into a variable as many commands below use it:
```shell
export PROJECT_ID="your-project-id"
```
Next, [enable billing](https://console.cloud.google.com/billing) in the Cloud Console in order to use Google Cloud resources and [enable the Container Engine API](https://console.cloud.google.com/project/_/kubernetes/list).
New users of Google Cloud Platform receive a [$300 free trial](https://console.cloud.google.com/billing/freetrial?hl=en). Running through this codelab shouldnt cost you more than a few dollars of that trial. Google Container Engine pricing is documented [here](https://cloud.google.com/container-engine/pricing).
Next, make sure you [download Node.js](https://nodejs.org/en/download/). You can skip this and the steps for installing Docker and Cloud SDK if you're using Cloud Shell.
2016-03-10 15:16:50 +00:00
2016-03-25 22:54:38 +00:00
Then install [Docker](https://docs.docker.com/engine/installation/), and [Google Cloud SDK](https://cloud.google.com/sdk/).
Finally, after Google Cloud SDK installs, run the following command to install [`kubectl`](http://kubernetes.io/docs/user-guide/kubectl-overview/):
```shell
gcloud components install kubectl
```
You're all set up with an environment that can build container images, run Node apps, run Kubernetes clusters locally, and deploy Kubernetes clusters to Google Container Engine. Let's begin!
2016-03-05 01:31:45 +00:00
## Create your Node.js application
The first step is to write the application. Save this code in a folder called "`hellonode/`" with the filename `server.js`:
#### server.js
```javascript
var http = require('http');
2016-03-05 01:31:45 +00:00
var handleRequest = function(request, response) {
response.writeHead(200);
response.end("Hello World!");
}
var www = http.createServer(handleRequest);
www.listen(8080);
```
Now run this simple command :
```shell
2016-03-07 08:26:58 +00:00
node server.js
```
You should be able to see your "Hello World!" message at http://localhost:8080/. If using Cloud Shell, use [Web Preview](https://cloud.google.com/shell/docs/using-web-preview) to view the URL.
Stop the running node server by pressing Ctrl-C.
Now lets package this application in a Docker container.
2016-03-05 01:31:45 +00:00
## Create a Docker container image
Next, create a file, also within `hellonode/` named `Dockerfile`. A Dockerfile describes the image that you want to build. Docker container images can extend from other existing images so for this image, we'll extend from an existing Node image.
#### Dockerfile
```conf
FROM node:4.4
EXPOSE 8080
COPY server.js .
CMD node server.js
```
This "recipe" for the Docker image will start from the official Node.js LTS image found on the Docker registry, expose port 8080, copy our `server.js` file to the image and start the Node server.
Now build an image of your container by running `docker build`, tagging the image with the Google Container Registry repo for your `$PROJECT_ID`:
```shell
docker build -t gcr.io/$PROJECT_ID/hello-node:v1 .
```
Now there is a trusted source for getting an image of your containerized app.
Let's try your image out with Docker:
```shell
$ docker run -d -p 8080:8080 gcr.io/$PROJECT_ID/hello-node:v1
325301e6b2bffd1d0049c621866831316d653c0b25a496d04ce0ec6854cb7998
```
Visit your app in the browser, or use `curl` or `wget` if youd like :
```shell
$ curl http://localhost:8080
Hello World!
```
**If you recieve a `Connection refused` message from Docker for Mac, ensure you are using the latest version of Docker (1.12 or later). Alternatively, if you are using Docker Toolbox on OSX, make sure you are using the VM's IP and not localhost :**
```shell
$ curl "http://$(docker-machine ip YOUR-VM-MACHINE-NAME):8080"
```
Lets now stop the container. In this example, our app was running as Docker process `2c66d0efcbd4`, which we looked up with `docker ps`:
```shell
docker ps
CONTAINER ID IMAGE COMMAND
2c66d0efcbd4 gcr.io/$PROJECT_ID/hello-node:v1 "/bin/sh -c 'node
2016-03-24 18:09:59 +00:00
docker stop 2c66d0efcbd4
2c66d0efcbd4
```
Now that the image works as intended and is all tagged with your `$PROJECT_ID`, we can push it to the [Google Container Registry](https://cloud.google.com/tools/container-registry/), a private repository for your Docker images accessible from every Google Cloud project (but also from outside Google Cloud Platform) :
```shell
gcloud docker push gcr.io/$PROJECT_ID/hello-node:v1
```
If all goes well, you should be able to see the container image listed in the console: *Compute > Container Engine > Container Registry*. We now have a project-wide Docker image available which Kubernetes can access and orchestrate.
![image](/images/hellonode/image_10.png)
2016-03-05 01:31:45 +00:00
## Create your cluster
A cluster consists of a master API server and a set of worker VMs called nodes.
Create a cluster via the Console: *Compute > Container Engine > Container Clusters > New container cluster*. Set the name to 'hello-world', leaving all other options default. You should get a Kubernetes cluster with three nodes, ready to receive your container image.
![image](/images/hellonode/image_11.png)
Its now time to deploy your own containerized application to the Kubernetes cluster! Please ensure that you have [configured](https://cloud.google.com/container-engine/docs/clusters/operations#configuring_kubectl) `kubectl` to use the cluster you just created (make sure the value of `--zone` flag matches the zone you used for the cluster:
```shell
$ gcloud container clusters get-credentials --zone us-central1-f hello-world
```
**The rest of this document requires both the Kubernetes client and server version to be 1.3. Run `kubectl version` to see your current versions.** For 1.2 see [this document](https://github.com/kubernetes/kubernetes.github.io/blob/release-1.2/docs/hellonode.md).
2016-03-05 01:31:45 +00:00
## Create your pod
A Kubernetes **[pod](/docs/user-guide/pods/)** is a group of containers, tied together for the purposes of administration and networking. It can contain a single container or multiple.
Create a pod with the `kubectl run` command:
```shell
$ kubectl run hello-node --image=gcr.io/$PROJECT_ID/hello-node:v1 --port=8080
deployment "hello-node" created
```
2016-03-24 18:09:59 +00:00
As shown in the output, the `kubectl run` created a **[deployment](/docs/user-guide/deployments/)** object. Deployments are the recommended way for managing creation and scaling of pods. In this example, a new deployment manages a single pod replica running the *hello-node:v1* image.
2016-03-24 18:09:59 +00:00
To view the deployment we just created run:
```shell
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 1 1 1 1 3m
```
2016-03-24 18:09:59 +00:00
To view the pod created by the deployment run:
```shell
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-714049816-ztzrb 1/1 Running 0 6m
```
2016-03-24 18:09:59 +00:00
To view the stdout / stderr from a pod (hello-node image has no output, so logs will be empty in this case) run:
```shell
2016-03-24 18:09:59 +00:00
kubectl logs <POD-NAME>
```
2016-03-24 18:09:59 +00:00
To view metadata about the cluster run:
```shell
2016-03-24 18:09:59 +00:00
kubectl cluster-info
```
2016-03-24 18:09:59 +00:00
To view cluster events run:
```shell
2016-03-24 18:09:59 +00:00
kubectl get events
```
2016-03-24 19:02:17 +00:00
To view the kubectl configuration run:
```shell
kubectl config view
```
Full documentation for kubectl commands is available **[here](/docs/user-guide/kubectl-overview/)**:
At this point you should have our container running under the control of Kubernetes but we still have to make it accessible to the outside world.
2016-03-05 01:31:45 +00:00
## Allow external traffic
By default, the pod is only accessible by its internal IP within the Kubernetes cluster. In order to make the `hello-node` container accessible from outside the Kubernetes virtual network, you have to expose the pod as a Kubernetes **[service](/docs/user-guide/services/)**.
From our development machine we can expose the pod to the public internet using the `kubectl expose` command combined with the `--type="LoadBalancer"` flag. The flag is needed for the creation of an externally accessible ip:
```shell
2016-03-24 18:09:59 +00:00
kubectl expose deployment hello-node --type="LoadBalancer"
```
**If this fails, make sure your client and server are both version 1.3. See the [Create your cluster](#create-your-cluster) section for details.**
The flag used in this command specifies that well be using the load-balancer provided by the underlying infrastructure (in this case the [Compute Engine load balancer](https://cloud.google.com/compute/docs/load-balancing/)). Note that we expose the deployment, and not the pod directly. This will cause the resulting service to load balance traffic across all pods managed by the deployment (in this case only 1 pod, but we will add more replicas later).
The Kubernetes master creates the load balancer and related Compute Engine forwarding rules, target pools, and firewall rules to make the service fully accessible from outside of Google Cloud Platform.
2016-03-24 18:09:59 +00:00
To find the ip addresses associated with the service run:
```shell
$ kubectl get services hello-node
NAME CLUSTER_IP EXTERNAL_IP PORT(S) AGE
hello-node 10.3.246.12 8080/TCP 23s
```
2016-03-24 18:09:59 +00:00
The `EXTERNAL_IP` may take several minutes to become available and visible. If the `EXTERNAL_IP` is missing, wait a few minutes and try again.
```shell
$ kubectl get services hello-node
NAME CLUSTER_IP EXTERNAL_IP PORT(S) AGE
hello-node 10.3.246.12 23.251.159.72 8080/TCP 2m
```
2016-03-24 18:09:59 +00:00
Note there are 2 IP addresses listed, both serving port 8080. `CLUSTER_IP` is only visible inside your cloud virtual network. `EXTERNAL_IP` is externally accessible. In this example, the external IP address is 23.251.159.72.
2016-03-24 21:22:16 +00:00
You should now be able to reach the service by pointing your browser to this address: http://EXTERNAL_IP**:8080** or running `curl http://EXTERNAL_IP:8080`
![image](/images/hellonode/image_12.png)
2016-03-05 01:31:45 +00:00
## Scale up your website
One of the powerful features offered by Kubernetes is how easy it is to scale your application. Suppose you suddenly need more capacity for your application; you can simply tell the deployment to manage a new number of replicas for your pod:
2016-03-05 01:31:45 +00:00
```shell
2016-03-24 18:09:59 +00:00
kubectl scale deployment hello-node --replicas=4
2016-03-05 01:31:45 +00:00
```
You now have four replicas of your application, each running independently on the cluster with the load balancer you created earlier and serving traffic to all of them.
```shell
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 4 4 4 3 40m
```
```shell
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-714049816-g4azy 1/1 Running 0 1m
hello-node-714049816-rk0u6 1/1 Running 0 1m
hello-node-714049816-sh812 1/1 Running 0 1m
hello-node-714049816-ztzrb 1/1 Running 0 41m
```
Note the **declarative approach** here - rather than starting or stopping new instances you declare how many instances you want to be running. Kubernetes reconciliation loops simply make sure the reality matches what you requested and take action if needed.
Heres a diagram summarizing the state of our Kubernetes cluster:
![image](/images/hellonode/image_13.png)
2016-03-05 01:31:45 +00:00
## Roll out an upgrade to your website
As always, the application you deployed to production requires bug fixes or additional features. Kubernetes is here to help you deploy a new version to production without impacting your users.
First, lets modify the application. On the development machine, edit server.js and update the response message:
```javascript
response.end("Hello Kubernetes World!");
```
We can now build and publish a new container image to the registry with an incremented tag:
```shell
docker build -t gcr.io/$PROJECT_ID/hello-node:v2 .
gcloud docker push gcr.io/$PROJECT_ID/hello-node:v2
```
Building and pushing this updated image should be much quicker as we take full advantage of the Docker cache.
Were now ready for Kubernetes to smoothly update our deployment to the new version of the application. In order to change
2016-03-24 18:09:59 +00:00
the image label for our running container, we will need to edit the existing *hello-node deployment* and change the image from
`gcr.io/$PROJECT_ID/hello-node:v1` to `gcr.io/$PROJECT_ID/hello-node:v2`. To do this, we will use the `kubectl set image` command.
```shell
$ kubectl set image deployment/hello-node hello-node=gcr.io/$PROJECT_ID/hello-node:v2
deployment "hello-node" image updated
```
This updates the deployment with the new image, causing new pods to be created with the new image and old pods to be deleted.
```
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 4 5 4 3 1h
```
2016-03-24 18:09:59 +00:00
While this is happening, the users of the services should not see any interruption. After a little while they will start accessing the new version of your application. You can find more details in the [deployment documentation](/docs/user-guide/deployments/).
Hopefully with these deployment, scaling and update features youll agree that once youve setup your environment (your GKE/Kubernetes cluster here), Kubernetes is here to help you focus on the application rather than the infrastructure.
## Observe the Kubernetes Web UI (optional)
Kubernetes comes with a graphical web user interface that is enabled by default with your clusters.
This user interface allows you to get started quickly and enables some of the functionality found in the CLI as a more approachable and discoverable way of interacting with the system.
Enjoy the Kubernetes graphical dashboard and use it for deploying containerized applications, as well as for monitoring and managing your clusters!
![image](/images/docs/ui-dashboard-workloadview.png)
Learn more about the web interface by taking the [Dashboard tour](/docs/user-guide/ui/).
## That's it! Time to tear it down
That's it for the demo! So you don't leave this all running and incur charges, let's learn how to tear things down.
Delete the Deployment (which also deletes the running pods) and Service (which also deletes your external load balancer):
```shell
kubectl delete service,deployment hello-node
```
Delete your cluster:
```shell
$ gcloud container clusters delete --zone us-central1-f hello-world
2016-04-01 13:35:33 +00:00
The following clusters will be deleted.
- [hello-world] in [us-central1-f]
Do you want to continue (Y/n)?
Deleting cluster hello-world...done.
Deleted [https://container.googleapis.com/v1/projects/<$PROJECT_ID>/zones/us-central1-f/clusters/hello-world].
```
This deletes the Google Compute Engine instances that are running the cluster.
Finally delete the Docker registry storage bucket hosting your image(s) :
```shell
$ gsutil ls
gs://artifacts.<$PROJECT_ID>.appspot.com/
$ gsutil rm -r gs://artifacts.<$PROJECT_ID>.appspot.com/
Removing gs://artifacts.<$PROJECT_ID>.appspot.com/...
```
Of course, you can also delete the entire project but note that you must first disable billing on the project. Additionally, deleting a project will only happen after the current billing cycle ends.