This quickstart shows you how to start a cluster of virtual machines and deploy a prebuilt Docker container image with a simple Node.js example app.
Project setup
GCP organises resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial. Select a project, or
Navigate to Kubernetes Engine
Open the in the upper-left corner of the console, then select Kubernetes Engine.
Create a Kubernetes cluster
A cluster consists of at least one cluster master machine and multiple worker machines called nodes. You deploy applications to clusters, and the applications run on the nodes.
-
Click the button.
-
Enter a for this cluster.
-
Choose a for this cluster.
-
Click to create the cluste
Get the sample application code
Cloud Shell is a built-in command-line tool for the console. In this section, you'll start Cloud Shell and use it to get the sample application code. Later, you'll use Cloud Shell to run the example app using a prebuilt container image.
Open Cloud Shell
Open Cloud Shell by clicking the button in the navigation bar in the upper-right corner of the console.
Get the sample code
Clone the sample code:
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ git clone \ > https://github.com/GoogleCloudPlatform/kubernetes-engine-samples Cloning into 'kubernetes-engine-samples'... remote: Enumerating objects: 25, done. remote: Counting objects: 100% (25/25), done. remote: Compressing objects: 100% (22/22), done. remote: Total 703 (delta 10), reused 10 (delta 3), pack-reused 678 Receiving objects: 100% (703/703), 438.05 KiB | 10.68 MiB/s, done. Resolving deltas: 100% (316/316), done. jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ cd \ > kubernetes-engine-samples/hello-app jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$
Exploring the deployment
You are now in the main directory for the sample code.
Exploring the application
View the application code:
main.go
is a web server implementation written in the Go programming language. The server responds to any HTTP request with a 'Hello, world!'
message.
Exploring the image configuration
View the image configuration:
Dockerfile
describes the image that you want Docker to build, including all of its resources and dependencies, and specifies which network port the app should expose.
To learn more about how this file works, refer to the Dockerfile reference in the Docker documentation.
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ cat main.go | more
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START container_hello_app]
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
// register hello function to handle all requests
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
// use PORT environment variable, or default to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// start the web server on port and accept requests
log.Printf("Server listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}
// hello responds to the request with a plain-text "Hello, world" message.
func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving request: %s", r.URL.Path)
host, _ := os.Hostname()
fmt.Fprintf(w, "Hello, world!\n")
fmt.Fprintf(w, "Version: 1.0.0\n")
fmt.Fprintf(w, "Hostname: %s\n", host)
}
// [END container_hello_app]
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ cat Dockerfile
FROM golang:1.8-alpine
ADD . /go/src/hello-app
RUN go install hello-app
FROM alpine:latest
COPY --from=0 /go/bin/hello-app .
ENV PORT 8080
CMD ["./hello-app"]
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$
Deploy the application
Wait for the cluster creation to finish
The cluster creation needs to finish before the tutorial can proceed. To track the progress of this activity and others, click the button in the navigation bar in the upper-right corner of the console.
Set up gcloud and kubectl credentials
Get the gcloud credentials for the cluster that you created:
Replace [cluster-name]
and [cluster-zone]
with the name and zone of the instance that you created.
Build and push the container
Build the image:
Push the image:
Run the application
Run the application on your Kubernetes cluster:
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ gcloud container clusters get-credentials cluster-1 --zone us-central1-c
Fetching cluster endpoint and auth data.
kubeconfig entry generated for cluster-1.
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ docker build -t \
> gcr.io/jyan-51sec-org-test1/hello-app:v1 \
> $PWD
Sending build context to Docker daemon 13.82kB
Step 1/7 : FROM golang:1.8-alpine
1.8-alpine: Pulling from library/golang
550fe1bea624: Pull complete
cbc8da23026a: Pull complete
9b35aaa06d7a: Pull complete
46ca6ce0ffd1: Pull complete
7a270aebe80a: Pull complete
8695117c367e: Pull complete
Digest: sha256:693568f2ab0dae1e19f44b41628d2aea148fac65974cfd18f83cb9863ab1a177
Status: Downloaded newer image for golang:1.8-alpine
---> 4cb86d3661bf
Step 2/7 : ADD . /go/src/hello-app
---> 612555011cce
Step 3/7 : RUN go install hello-app
---> Running in 25a1d115de11
Removing intermediate container 25a1d115de11
---> 7c9fecbfa150
Step 4/7 : FROM alpine:latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:a15790640a6690aa1730c38cf0a440e2aa44aaca9b0e8931a9f2b0d7cc90fd65
Status: Downloaded newer image for alpine:latest
---> a24bb4013296
Step 5/7 : COPY --from=0 /go/bin/hello-app .
---> 77ee4c6b5ed2
Step 6/7 : ENV PORT 8080
---> Running in d02738ddc512
Removing intermediate container d02738ddc512
---> f1c84a77996a
Step 7/7 : CMD ["./hello-app"]
---> Running in 3670d034eb56
Removing intermediate container 3670d034eb56
---> 98b79e8fa949
Successfully built 98b79e8fa949
Successfully tagged gcr.io/jyan-51sec-org-test1/hello-app:v1
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ gcloud docker -- push \
> gcr.io/jyan-51sec-org-test1/hello-app:v1
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.
As an alternative, use `gcloud auth configure-docker` to configure `docker` to
use `gcloud` as a credential helper, then use `docker` as you would for non-GCR
registries, e.g. `docker pull gcr.io/project-id/my-image`. Add
`--verbosity=error` to silence this warning: `gcloud docker
--verbosity=error -- pull gcr.io/project-id/my-image`.
See: https://cloud.google.com/container-registry/docs/support/deprecation-notices#gcloud-docker
The push refers to repository [gcr.io/jyan-51sec-org-test1/hello-app]
0ccc2168dd82: Pushed
50644c29ef5a: Layer already exists
v1: digest: sha256:3d99eecd2ae16e5d85be8fe5fb2d0da50bc867b824d07ef3494be51c5635faa7 size: 739
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ kubectl create deployment \
> hello-app \
> --image=gcr.io/jyan-51sec-org-test1/hello-app:v1
deployment.apps/hello-app created
View the application
Expose the cluster
Make the cluster available to the public:
Find your external IP
List the services, and look for the hello-app
service:
Wait until you see an IP address in the External IP
column. This may take a minute. To stop monitoring the services, press Ctrl+C.
Visit your running app
Copy the IP address from the External IP
column.
Open a new web browser tab, and visit your app by connecting to the IP address on port 8080:
http://[external-IP]:8080
Replace [external-IP] with the external IP address copied in the previous step.
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ kubectl expose deployment \
> hello-app \
> --type="LoadBalancer" --port \
> 8080
service/hello-app exposed
jyan@cloudshell:~/examples/guestbook/kubernetes-engine-samples/hello-app (jyan-51sec-org-test1)$ kubectl get service hello-app \
> --watch
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-app LoadBalancer 10.8.1.133 <pending> 8080:30593/TCP 18s
NAME AGE
hello-app 29s
Modifying your cluster
Kubernetes allows you to easily scale or upgrade your application.
Scale your application
Use the following command to scale your application up to four replicas:
Each replica runs independently on the cluster, with the load balancer serving traffic to all of them.
View your application settings
Look at the deployment settings of your cluster with the following commands:
Update the application
In this section, you'll modify your local copy of main.go
, rebuild the application and push the new version.
Modify the application
Use this command to make a text substitution in your local copy of main.go
to make it return a different version number in its output:
Rebuild the application
Publish the application
View the modified application
Wait a minute to give the application image time to update, and then view the modified application at the same address as before:
http://[external-IP]:8080
Inspect your cluster
You can inspect properties of your cluster through the GCP Console.
View cluster details
Click the that you created. This opens the cluster details page.
Delete the cluster
You can continue to use your app, or you can shut down the entire cluster to avoid subsequent charges.
To remove your cluster, select the next to the cluster name and click the button.
Conclusion
Congratulations! You have deployed a simple 'Hello, World' application using Google Kubernetes Engine.