Skip to content

Instantly share code, notes, and snippets.

@initcron
Last active July 30, 2022 12:24
Show Gist options
  • Save initcron/bf01c24f9c07d5d84bf54a596c40233d to your computer and use it in GitHub Desktop.
Save initcron/bf01c24f9c07d5d84bf54a596c40233d to your computer and use it in GitHub Desktop.
Creating and Deploying a Sample App with Kubernetes

Create Dockerfile for sample Node App

    mkdir dev

    cd dev

    cat > Dockerfile
    
FROM node:4.4
EXPOSE 8080
COPY server.js .
CMD node server.js

[press ^d to exit and save the file]

Validate Dockerfile

cat Dockerfile

Create Node App


cat > 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);

[press ^d to exit and save the file]

Build Image using Dockerfile Created Earlier

docker build -t initcron/node:v1 .

Validate 

docker images

Test Docker Image Create Above

docker run -d -p 9090:8080 --name nodetest initcron/node:v1
curl localhost:9090

docker rm -f nodetest

Launch Kubernetes Pod

kubectl run hello-node --image=initcron/node:v1 --port=8080

Validate

kubectl get deployments
[output]
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   1         1         1            1           9s

kubectl get pods
[output]
NAME                          READY     STATUS    RESTARTS   AGE
hello-node-3739401303-9ltt5   1/1       Running   0          53s

Cluster Info 

kubectl cluster-info

kubectl get events

kubectl config view




Create Service to Allow External Traffic

kubectl expose deployment hello-node --type="LoadBalancer"

kubectl get services
hello-node   10.0.0.50    <pending>     8080/TCP   9m
kubernetes   10.0.0.1     <none>        443/TCP    43m
nginx        10.0.0.28    <none>        80/TCP     38m

Validate 

curl http://10.0.0.50:8080


Scaling

kubectl scale deployment hello-node --replicas=4

kubectl get deployments
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   4         4         4            4           5m

kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
hello-node-3739401303-4b8qp   1/1       Running   0          23s
hello-node-3739401303-9ltt5   1/1       Running   0          6m
hello-node-3739401303-kc2u1   1/1       Running   0          23s
hello-node-3739401303-uavwk   1/1       Running   0          23s

Rolling Updates

Update Node App

cat > server.js

var http = require('http');
var handleRequest = function(request, response) {
  response.writeHead(200);
  response.end("Hello Kubernetes World!");
}
var www = http.createServer(handleRequest);
www.listen(8080);

[^d to exit]


Build Image

docker build -t initcron/node:v2 .


Update Deployment Specs

kubectl edit deployment hello-node


[ Change image to use v2 ] 

Validate

# kubectl get deployments
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   4         5         4            3           8m
 kubectl get  pods
NAME                          READY     STATUS              RESTARTS   AGE
hello-node-3739401303-bcsuz   1/1       Terminating         0          6m
hello-node-3739401303-gyips   1/1       Terminating         0          6m
hello-node-3739401303-zh3az   1/1       Running             0          8m
hello-node-3820797016-4opzh   1/1       Running             0          1m
hello-node-3820797016-9gdzr   0/1       ContainerCreating   0          6s
hello-node-3820797016-x1ql4   1/1       Running             0          1m
hello-node-3820797016-y30fh   0/1       ContainerCreating   0          15s


kubectl get services
AME         CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
hello-node   10.0.0.50    <pending>     8080/TCP   9m
kubernetes   10.0.0.1     <none>        443/TCP    43m
nginx        10.0.0.28    <none>        80/TCP     38m


curl http://10.0.0.50:8080

Tear Down

kubectl delete service,deployment hello-node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment