Kubernetes - Building NodeJS Application and deployment - Recreate Strategy
Now we'll build a simple Node.js web application and package it into a container image.
The application will accept HTTP requests and respond with the hostname of the machine it's running in.
root@masterk8s:/kube# mkdir nodejs
root@masterk8s:/kube# cd nodejs/
root@masterk8s:/kube/nodejs# vi app.js
root@masterk8s:/kube/nodejs# cat app.js
const http = require('http');
const os = require('os');
console.log("My server starting and listening on 8080...");
var handler = function(request, response) {
console.log("Received request from " + request.connection.remoteAddress);
response.writeHead(200);
response.end("You've hit " + os.hostname() + "\n");
};
var www = http.createServer(handler);
www.listen(8080);
root@masterk8s:/kube/nodejs#
It starts up an HTTP server on port 8080. The server responds with an HTTP response status code 200 OK and the text "You've hit <hostname>" to every request.
The request handler also logs the client's IP address to the standard output, which we'll need later.
root@masterk8s:/kube# cat Dockerfile
FROM node:7
ADD nodejs/app.js /app.js
ENTRYPOINT ["node", "app.js"]
root@masterk8s:/kube#
root@masterk8s:/kube# docker build . -t rajasekar/node-web-v1
Sending build context to Docker daemon 47.1kB
Step 1/3 : FROM node:7
---> d9aed20b68a4
Step 2/3 : ADD nodejs/app.js /app.js
---> ec9668d794c4
Step 3/3 : ENTRYPOINT ["node", "app.js"]
---> Running in c383f70fb199
Removing intermediate container c383f70fb199
---> 67e44dc71429
Successfully built 67e44dc71429
Successfully tagged rajasekar/node-web-v1:latest
root@masterk8s:/kube#
root@masterk8s:/kube# docker run --name my-app -p 8080:8080 -d 67e44dc71429
7883bf20192fed68d39587831878f6b4b19bd426073dc8b85903e6bb1bb90ade
root@masterk8s:/kube#
root@masterk8s:/kube# docker container ls | grep -i my-app
7883bf20192f 67e44dc71429 "node app.js" 24 seconds ago Up 22 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp my-app
root@masterk8s:/kube#
root@masterk8s:/kube# curl 0.0.0.0:8080
You've hit 7883bf20192f
root@masterk8s:/kube#
Since, I have one worker node. I am going to take backup of this image and restore in worker node.
root@masterk8s:/tmp# docker save rajasekar/node-web-v1 -o /tmp/node-web-v1.tar
root@masterk8s:/tmp# ls -l /tmp/node-web-v1.tar
-rw------- 1 root root 681218048 May 2 21:16 /tmp/node-web-v1.tar
root@masterk8s:/tmp#
On the worker node:
root@worker1:/tmp# docker load < /tmp/node-web-v1.tar
Now, Lets use this image to do a k8s deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-app
spec:
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodejs-v1
image: rajasekar/node-web-v1:latest
imagePullPolicy: Never
ports:
- containerPort: 8080
root@masterk8s:/kube# kubectl get pods
NAME READY STATUS RESTARTS AGE
node-app-565bc4d7dc-xjjwg 1/1 Running 0 5s
root@masterk8s:/kube#
root@masterk8s:/kube# kubectl scale deployment node-app --replicas=3
deployment.apps/node-app scaled
root@masterk8s:/kube# kubectl get pods
NAME READY STATUS RESTARTS AGE
node-app-565bc4d7dc-csf68 0/1 ContainerCreating 0 2s
node-app-565bc4d7dc-xjjwg 1/1 Running 0 29s
node-app-565bc4d7dc-zrvjs 0/1 ContainerCreating 0 2s
root@masterk8s:/kube# kubectl get pods
NAME READY STATUS RESTARTS AGE
node-app-565bc4d7dc-csf68 1/1 Running 0 4s
node-app-565bc4d7dc-xjjwg 1/1 Running 0 31s
node-app-565bc4d7dc-zrvjs 1/1 Running 0 4s
root@masterk8s:/kube#
Lets expose this deployment to a NodePort service.
root@masterk8s:/kube# kubectl expose deployment node-app --type NodePort --port 8080 --target-port 8080
service/node-app exposed
root@masterk8s:/kube# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
node-app NodePort 10.102.49.246 <none> 8080:32253/TCP 3s
root@masterk8s:/kube#
root@masterk8s:/kube# kubectl get ep
NAME ENDPOINTS AGE
node-app 10.244.1.10:8080,10.244.1.11:8080,10.244.1.12:8080 20s
root@masterk8s:/kube#
Lets test by hittig the cluster IP on port 8080:
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-565bc4d7dc-zrvjs
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-565bc4d7dc-xjjwg
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-565bc4d7dc-csf68
root@masterk8s:/kube#
We are able to hit the pods behind the deployment.
Now, I am going to make changes to my image with some modification and create new image. Load the new image [rajasekar/node-web-v2] across the workers.
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-app
spec:
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodejs-v2
image: rajasekar/node-web-v2:latest
imagePullPolicy: Never
ports:
- containerPort: 8080
root@masterk8s:/kube# kubectl get pods
NAME READY STATUS RESTARTS AGE
node-app-565bc4d7dc-csf68 1/1 Terminating 0 23m
node-app-565bc4d7dc-xjjwg 1/1 Terminating 0 23m
node-app-565bc4d7dc-zrvjs 1/1 Terminating 0 23m
node-app-5c97994c4c-7hnsh 1/1 Running 0 27s
node-app-5c97994c4c-t97pk 1/1 Running 0 32s
node-app-5c97994c4c-tkdjg 1/1 Running 0 30s
root@masterk8s:/kube#
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-5c97994c4c-tkdjgThis is a secure link of version 99.99%
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-5c97994c4c-7hnshThis is a secure link of version 99.99%
root@masterk8s:/kube# curl 10.102.49.246:8080
You've hit node-app-5c97994c4c-7hnshThis is a secure link of version 99.99%
root@masterk8s:/kube#
If you looks closely, We have not changed anything other than the container name and image. So, when we deploy this delete V1 pods and V2 pods becomes active.
This deployment is called "RECREATE Deployment".
Comments
Post a Comment