Local kubernetes Hello World in nodejs with Docker

10/30/2019

I've been following tutorial videos and trying to understand to build a small minimalistic application. The videos I followed are pulling containers from the registries while I'm trying to test, build and deploy everything locally at the moment if possible. Here's my setup.

  1. I've the latest docker installed with Kubernetes enabled on mac OS.

  2. A helloworld NodeJS application running with Docker and Docker Compose

TODO: I'd like to be able to start my instances, let's say 3 in the kubernetes cluster


Dockerfile

FROM node:alpine

COPY package.json package.json
RUN npm install

COPY . .
CMD ["npm", "start"]

docker-compose.yml

version: '3'

services:
    user:
      container_name: users
      build:
        context: ./user
        dockerfile: Dockerfile

Creating a deployment file with the help of this tutorial and it may have problems since I'm merging information both from youtube as well as the web link.

Creating a miminalistic yml file for to be able to get up and running, will study other aspects like readiness and liveness later.


apiVersion: v1
kind: Service
metadata:
  name: user
spec:
  selector:
    app: user
  ports:
    - port: 8080
  type: NodePort

Please review the above yml file for correctness, so the question is what do I do next?

-- AppDeveloper
docker
kubernetes

1 Answer

10/31/2019

The snippets you provide are regrettably insufficient but you have the basics.

I had a Google for you for a tutorial and -- unfortunately -- nothing obvious jumped out. That doesn't mean that there isn't one, just that I didn't find it.

You've got the right idea and there are quite a few levels of technology to understand but, I commend your approach and think we can get you there.

  1. Let's start with a helloworld Node.JS tutorial

https://nodejs.org/en/docs/guides/getting-started-guide/

  1. Then you want to containerize this

https://nodejs.org/de/docs/guides/nodejs-docker-webapp/

For #3 below, the last step here is:

docker build --tag=<your username>/node-web-app .

But, because you're using Kubernetes, you'll want to push this image to a public repo. This is so that, regardless of where your cluster runs, it will be able to access the container image.

Since the example uses DockerHub, let's continue using that:

docker push <your username>/node-web-app

NB There's an implicit https://docker.io/<your username>/node-web-app:latest here

  1. Then you'll need a Kubernetes cluster into which you can deploy your app

My advice is (except microk8s and minikube) don't ever run your own Kubernetes clusters; leave it to a cloud provider.

  1. Now that you have all the pieces, I recommend you just:
kubectl run yourapp \
--image=<your username>/node-web-app:latest \
--port=8080 \
--replicas=1

IIRC kubectl run is deprecated but use it anyway. It will create a Kubernetes Deployment (!) for you with 1 Pod (==replica). Feel free to adjust that value (perhaps --replicas=2) if you wish.

Once you've created a Deployment, you'll want to create a Service to make your app accessible (top of my head) this command is:

kubectl expose deployment/yourapp --type=NodePort

Now you can query the service:

kubectl get services/yourapp

NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
yourapp NodePort   10.152.183.27   <none>        80:32261/TCP   7s

NB The NodePort that's been assigned (in this case!) is :32261 and so I can then interact with the app using curl http://localhost:32261 (localhost because I'm using microk8s).

kubectl is powerful. Another way to determine the NodePort is:

kubectl get service/yourapp \
--output=jsonpath="{.spec.ports[0].nodePort}"

The advantage of the approach of starting from kubectl run is you can then easily determine the Kubernetes configuration that is needed to recreate this Deployment|Service by:

kubectl get deployment/yourapp \
--format=yaml \
> ./yourapp.deployment.yaml

kubectl get service/yourapp \
--format=yaml \
> ./yourapp.service.yaml

These commands will interrogate the cluster, retrieve the configuration for you and pump it into the files. It will include some instance data too but the gist of it shows you what you would need to recreate the deployment. You will need to edit this file.

But, you can test this by first deleting the deployment and the service and then recreating it from the configuration:

kubectl delete deployment/yourapp
kubectl delete service/yourapp

kubectl apply --filename=./yourapp.deployment.yaml
kubectl apply --filename=./yourapp.service.yaml

NB You'll often see multiple resource configurations merged into a single YAML file. This is perfectly valid YAML but you only ever see it used by Kubernetes. The format is:

...
some: yaml
---
...
some: yaml
---

Using this you could merge the yourapp.deployment.yaml and yourapp.service.yaml into a single Kubernetes configuration.

HTH!

-- DazWilkin
Source: StackOverflow