Introduction
This lab is turning an node.js app into a replicated application running on Kubernetes.
This is taking code that have developed on your machine, turn it into a Docker container image, and then run that image on Google Container Engine.
Here is the architecture diagram:
Create a project & Enable billing
Create a container cluster
Create a docker image and push to google container registry
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~# mkdir hellonode
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~# mv server.js ./hellonode/
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~# cat ./hellonode/server.js
var http = require(‘http’);
var handleRequest = function(request, response) {
response.writeHead(200);
response.end(“Hello World!”);
}
var www = http.createServer(handleRequest);
www.listen(8080);
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# docker build -t gcr.io/lexical-cider-136923/hello-node:v1 .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM node:4.4
4.4: Pulling from library/node
17bd2058e0c6: Pull complete
3f0d3d140ce1: Pull complete
c28cbef85c39: Pull complete
0d3e866c82f3: Pull complete
7e16e2273003: Pull complete
05f9ce51426c: Pull complete
9569bc848268: Pull complete
6078f0e2c35e: Pull complete
82b826d33703: Pull complete
f7ef43765f1e: Pull complete
Digest: sha256:144ebd02c79c0458fbf145a29310a8d9856fc873e7db8c13109b7617c45032c8
Status: Downloaded newer image for node:4.4
—> f7ef43765f1e
Step 2 : EXPOSE 8080
—> Running in d27d0be230c3
—> 84ea4637aa2d
Removing intermediate container d27d0be230c3
Step 3 : COPY server.js .
—> b38353d1bee9
Removing intermediate container 39b8a41c0c89
Step 4 : CMD node server.js
—> Running in 748adf09f479
—> 81d71683c753
Removing intermediate container 748adf09f479
Successfully built 81d71683c753
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# curl http://localhost:8080
Hello World!
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
926923edbef9 gcr.io/lexical-cider-136923/hello-node:v2 “/bin/sh -c ‘node ser” About a minute ago Up About a minute 0.0.0.0:808
0->8080/tcp lonely_mclean
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud auth login
You are running on a Google Compute Engine virtual machine.
It is recommended that you use service accounts for authentication.
You can run:
$ gcloud config set account `ACCOUNT`
to switch accounts if necessary.
Your credentials may be visible to others with access to this
virtual machine. Are you sure you want to authenticate with
your personal account?
Do you want to continue (Y/n)? y
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&prompt=select_account&response_type=code&clien
t_id=32555940559.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2
Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&access_typ
e=offline
Enter verification code: 4/tokLBQ6Du_nCAsqu2Xd58N8dwjPEtoFE6KV-46yCQ-I
Saved Application Default Credentials.
You are now logged in as [robertchen117@gmail.com].
Your current project is [None]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud docker push gcr.io/lexical-cider-136923/hello-node:v2
The push refers to a repository [gcr.io/lexical-cider-136923/hello-node] (len: 1)
892963bc9ede: Pushed
52103d8a29f7: Pushed
84ea4637aa2d: Pushed
82b826d33703: Pushed
05f9ce51426c: Image already exists
7e16e2273003: Image already exists
0d3e866c82f3: Image already exists
c28cbef85c39: Image already exists
17bd2058e0c6: Image already exists
v2: digest: sha256:fe66288f90264b08390446bbd65bd4fd6ee92c5c8741b81a9582ec63ca5da2e7 size: 12996
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud config set compute/zone us-central1-b
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud compute zones list
ERROR: (gcloud.compute.zones.list) The required property [project] is not currently set.
You may set it for your current workspace by running:
$ gcloud config set project VALUE
or it can be set temporarily by the environment variable [CLOUDSDK_CORE_PROJECT]
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud config set project lexical-cider-136923
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud compute zones list
NAME REGION STATUS NEXT_MAINTENANCE TURNDOWN_DATE
asia-east1-c asia-east1 UP
asia-east1-b asia-east1 UP
asia-east1-a asia-east1 UP
europe-west1-d europe-west1 UP
europe-west1-c europe-west1 UP
europe-west1-b europe-west1 UP
us-central1-a us-central1 UP
us-central1-f us-central1 UP
us-central1-c us-central1 UP
us-central1-b us-central1 UP
us-east1-b us-east1 UP
us-east1-d us-east1 UP
us-east1-c us-east1 UP
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud container clusters get-credentials robert-test
Fetching cluster endpoint and auth data.
kubeconfig entry generated for robert-test.
Kubectl run the container
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl run hello-node –image=gcr.io/lexical-cider-136923/hell
o-node:v2 –port=8080
deployment “hello-node” created
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-node 1 1 1 0 19s
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-131304990-mook5 1/1 Running 0 32s
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl cluster-info
Kubernetes master is running at https://23.236.51.69
GLBCDefaultBackend is running at https://23.236.51.69/api/v1/proxy/namespaces/kube-system/services/default-http-backend
Heapster is running at https://23.236.51.69/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://23.236.51.69/api/v1/proxy/namespaces/kube-system/services/kube-dns
kubernetes-dashboard is running at https://23.236.51.69/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl get events
FIRSTSEEN LASTSEEN COUNT NAME KIND SUBOBJECT TYPE REASON
SOURCE MESSAGE
9m 9m 1 gke-robert-test-default-pool-60db0713-4igs Node Normal Starting
{kubelet gke-robert-test-default-pool-60db0713-4igs} Starting kubelet.
9m 8m 10 gke-robert-test-default-pool-60db0713-4igs Node Normal NodeHasSuf
ficientDisk {kubelet gke-robert-test-default-pool-60db0713-4igs} Node gke-robert-test-default-pool-60db0713-4igs status is now: NodeHa
sSufficientDisk
8m 8m 1 gke-robert-test-default-pool-60db0713-4igs Node Normal Registered
Node {controllermanager } Node gke-robert-test-default-pool-60db0713-4igs event: Registered Nod
e gke-robert-test-default-pool-60db0713-4igs in NodeController
…
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl config view
apiVersion: v1
clusters:
– cluster:
certificate-authority-data: REDACTED
server: https://23.236.51.69
name: gke_lexical-cider-136923_us-central1-b_robert-test
contexts:
– context:
cluster: gke_lexical-cider-136923_us-central1-b_robert-test
user: gke_lexical-cider-136923_us-central1-b_robert-test
name: gke_lexical-cider-136923_us-central1-b_robert-test
current-context: gke_lexical-cider-136923_us-central1-b_robert-test
kind: Config
preferences: {}
users:
– name: gke_lexical-cider-136923_us-central1-b_robert-test
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
password: CFFdJIhKPiSuhsIv
username: admin
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl expose deployment hello-node –type=”LoadBalancer”
service “hello-node” exposed
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl get services hello-node
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node 10.3.250.79 8080/TCP 19s
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl get services hello-node
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node 10.3.250.79 104.155.158.193 8080/TCP 1m
access the app with external IP
Remove the lab:
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# kubectl delete service,deployment hello-node
service “hello-node” deleted
deployment “hello-node” deleted
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gcloud container clusters delete robert-test
The following clusters will be deleted.
– [robert-test] in [us-central1-b]
Do you want to continue (Y/n)? y
Deleting cluster robert-test…done.
Deleted [https://container.googleapis.com/v1/projects/lexical-cider-136923/zones/us-central1-b/clusters/robert-test].
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gsutil ls
gs://artifacts.lexical-cider-136923.appspot.com/
root@cs-6497-devshell-vm-94e2cda5-5afe-4de8-a0ec-57c992334806-1b:~/hellonode# gsutil rm -r gs://artifacts.lexical-cider-136923.appspot.com/
The post Running app on Kubernetes & Google container engine appeared first on Robert Chen.