/ HOWTO

Poniendo a correr un WEB en NodeJS con Kubernetes.



Creamos una app simple en Nodejs (server web que devuelve un “Hola Dockertips!” )

# vi node server.js
var http = require('http');

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hola Dockertips!');
};
var www = http.createServer(handleRequest);
www.listen(8888);

Lo probamos con un

# node server.js

y accedemos mediante un navegador a

* http://127.0.0.1:8888

Crearemos un nuevo contenedor basándonos en uno de Node V. 6.9.2

# vi Dockerfile
FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js

Construimos la imágen.

# docker build -t hello-node:v1 .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM node:6.9.2
6.9.2: Pulling from library/node
75a822cd7888: Pull complete
57de64c72267: Pull complete
4306be1e8943: Pull complete
871436ab7225: Pull complete
0110c26a367a: Pull complete
1f04fe713f1b: Pull complete
ac7c0b5fb553: Pull complete
Digest: sha256:2e95be60faf429d6c97d928c762cb36f1940f4456ce4bd33fbdc34de94a5e043
Status: Downloaded newer image for node:6.9.2
 ---> faaadb4aaf9b
Step 2/4 : EXPOSE 8080
 ---> Running in f0e8e3226440
 ---> dea00f0cc475
Removing intermediate container f0e8e3226440
Step 3/4 : COPY server.js .
 ---> 567d0c6d3366
Removing intermediate container e78804e31889
Step 4/4 : CMD node server.js
 ---> Running in 769d68dde466
 ---> 8540ce4f93a8
Removing intermediate container 769d68dde466
Successfully built 8540ce4f93a8
Successfully tagged hello-node:v1

Nuestra imágen mide 655 MB incluyendo nuestro servidor web en Nodejs.

# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
node                                                   6.9.2               faaadb4aaf9b        12 months ago       655MB

Ponemos en marcha el contenedor.

# kubectl run hello-node --image=hello-node:v1 --port=8080
deployment "hello-node" created

Vemos que está en funcionamiento.

# docker stats
CONTAINER ID        NAME                                                                                                          CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
05f57d09e86b        k8s_hello-node_hello-node-75ddf8bc9d-dqtmp_default_d2a49e4c-e5a4-11e7-ac4c-08002789f4ab_0                     0.00%               8.664MiB / 1.953GiB   0.43%               1.18kB / 0B         0B / 0B             7

Como continuamos con el despliegue anterior, vemos dos contenedores corriendo.

# kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node            1         1         1            1           1m
kubernetes-bootcamp   1         1         1            1           51m
# kubectl get pods
NAME                                   READY     STATUS    RESTARTS   AGE
hello-node-75ddf8bc9d-dqtmp            1/1       Running   0          2m
kubernetes-bootcamp-6db74b9f76-6jkwt   1/1       Running   0          51m

Desde la interfaz web podemos ver MiniKube funcionando.

GRAFICO7: kube7

GRAFICO8: kube8

Ahora vemos los eventos

# kubectl get events
LAST SEEN   FIRST SEEN   COUNT     NAME                                                    KIND         SUBOBJECT                              TYPE      REASON                         SOURCE                  MESSAGE
2m          2m           1         hello-node-75ddf8bc9d-dqtmp.15020e52ad2f7329            Pod                                                 Normal    Scheduled                      default-scheduler       Successfully assigned hello-node-75ddf8bc9d-dqtmp to minikube
2m          2m           1         hello-node-75ddf8bc9d-dqtmp.15020e52b96f2d36            Pod                                                 Normal    SuccessfulMountVolume          kubelet, minikube       MountVolume.SetUp succeeded for volume "default-token-668vt"
2m          2m           1         hello-node-75ddf8bc9d-dqtmp.15020e52f56a8e75            Pod          spec.containers{hello-node}            Normal    Pulled                         kubelet, minikube       Container image "hello-node:v1" already present on machine
2m          2m           1         hello-node-75ddf8bc9d-dqtmp.15020e5304ac6090            Pod          spec.containers{hello-node}            Normal    Created                        kubelet, minikube       Created container
2m          2m           1         hello-node-75ddf8bc9d-dqtmp.15020e53140df2b5            Pod          spec.containers{hello-node}            Normal    Started                        kubelet, minikube       Started container
2m          2m           1         hello-node-75ddf8bc9d.15020e52ab3eb298                  ReplicaSet                                          Normal    SuccessfulCreate               replicaset-controller   Created pod: hello-node-75ddf8bc9d-dqtmp
2m          2m           1         hello-node.15020e52a83372ad                             Deployment                                          Normal    ScalingReplicaSet

Para que el servidor pueda dar servicio a la red lo publicamos.

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

Podemos acceder al servidor de MiniKube son un shell.

# minikube ssh
            _         _ ( )           ( )
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ uname -a
Linux minikube 4.9.13 #1 SMP Thu Oct 19 17:14:00 UTC 2017 x86_64 GNU/Linux
$ exit
logout
# kubectl get services
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.101.211.128   <pending>     8080:30676/TCP   1m

Este comando nos abrirá una ventana en un navegador donde nos correrá el WEB. ( http://192.168.99.100:30676/ )

# minikube service hello-node

Plugins/Addons que se pueden activar o desactivar en MiniKube.

# minikube addons list
- default-storageclass: enabled
- storage-provisioner: enabled
- efk: disabled
- ingress: disabled
- registry-creds: disabled
- addon-manager: enabled
- dashboard: enabled
- coredns: disabled
- kube-dns: enabled
- heapster: disabled
- registry: disabled

Para parar y borrar, hacemos.

# kubectl delete service hello-node
# kubectl delete deployment hello-node
# minikube stop
Stopping local Kubernetes cluster...

Este comando borrará la máquina virtual completa.

# -- minikube delete

Subscríbete y recibirás los últimos artículos semanalmente en tu email.