top of page

Node.js, Docker and Kubernetes

My aim is to get a basic understanding of Node.js, Docker and Kubernetes and the way to do that is to build something combing all three. So this is what we are going to do:

  • Step 1: Build a simple Node.js app

  • Step 2: Containerize the app using Docker

  • Step 3: Create a Kubernates cluster in AWS EC2 instance

  • Step 4: Deploy to a Kubernetes Cluster

  • Step 5: Expose and scale

  • Step 6: Happy Days! Time to celebrate :)

Step 1: Build a simple Node.js

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications.

  • Install Node.js

  • https://nodejs.org/en/

  • We will display "Hello world" in a web browser. I have recreated the hello world app from Node.js docs

  • Run the web server

  • node helloWorld.js

  • Navigate to localhost:3000 and you should see the message "Hello world"

Step 2: Containerize the app using Docker

  • Install Docker for windows

  • https://www.docker.com/products/docker-desktop

  • Once installation is complete, start docker

  • Run the following commands in any terminal to see more information about the installed docker

  • docker info

  • create a DOCKERFILE with the following details:

  • In the terminal where the DOCKERFILE is located, run the following command:

  • docker build -t hello-world .

  • We now have a docker image

  • You can explore all images by executing

  • docker images

  • Create an account on docker hub

  • Tag your docker image with your docker username/ID

  • docker tag hello-world <<docker_username>>/hello-world

  • Push the image to docker hub. Make sure that you are signed into docker desktop with your docker id.

  • docker push <<docker_username>>/hello-world

  • hello-world docker image is now on docker-hub

  • verify that its working by starting the first docker container:

  • docker run --name server-container -p 3000:3000 -d <<docker_username>>/hello-world

  • To view a list of all containers:

  • docker ps

Step 3: Create a Kubernates cluster in AWS EC2 instance

In AWS there are three ways to run kubernetes and I am going to attempt the EC2 path:

  • Install AWS CLI

  • Configuring the AWS CLI

  • Create an AWS account if you don't already have you

  • Log into the AWS Management Console

  • Add a new IAM user

  • Note down the access key ID and secret access key.

  • Use the access key ID and secret key to configure the AWS CLI

  • ​aws configure

  • enter the values when prompted

  • enter the region name according to your preferred region. I chose ap-southeast-2

  • Install Kubectl

  • The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters.

  • docker for windows installs its owns version of kubectl. check version with

  • ​​kubectl version

  • Install Kops for windows

  • kops helps you create, destroy, upgrade and maintain production-grade, highly available, Kubernetes clusters from the command line

  • https://github.com/kubernetes/kops#installing

  • ​Now that we have our tools ready, lets create the kubernetes cluster in AWS. To achieve this we need an S3 bucket to store kops configuration, a route 53 domain and a new ssh key. Lets go and create these.

  • Create S3 bucket by executing the following in AWS CLI

  • ​​aws s3api create-bucket --bucket hello-world-kops-state-store --region ap-southeast-2 --create-bucket-configuration LocationConstraint=ap-southeast-2

  • Enable verisioning on s3 bucket

  • aws s3api put-bucket-versioning --bucket hello-world-kops-state-store --versioning-configuration Status=Enabled

  • Create a route 53 domain

  • aws route53 create-hosted-zone --name k8s.local --caller-reference 1

  • Generate a new ssh key locally

  • ssh-keygen -t rsa -C "mouna1619@gmail.com"

  • Create a new ssh public key called admin and create a secret

  • kops create secret --state s3://hello-world-kops-state-store --name k8s.local sshpublickey admin -i id_rsa.pub

  • Now we are ready to create the cluster configuration

  • kops create cluster --node-count=2 --node-size=a1.medium --zones=ap-southeast-2a --name=k8s.local --state=s3://hello-world-kops-state-store

  • To find all the zones in a region in aws:

  • aws ec2 describe-availability-zones --region ap-southeast-2

  • This will create a cluster with name k8s.local

  • Once this is done, you can update the cluster

  • kops update cluster k8s.local --yes --state=s3://hello-world-kops-state-store

  • It does take some time for the infrastructure to be ready. You can validate the cluster by:

  • kops validate cluster --state s3://hello-world-kops-state-store

Step 4: Deploy to a Kubernetes Cluster

Step 5: Expose and scale

Troubleshooting

1. If your nodes fails to start and you see a validation errors message like this:

  • check the status of each node by:

  • kubectl --namespace=kube-system describe pod <<pod_name>>

  • kubectl --namespace=kube-system describe pod kube-dns-57dd96bb49-kvlws

Resources:

  • AWS CLI

  • https://docs.aws.amazon.com/cli/latest/userguide/install-windows.html#install-msi-on-windows

  • https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html?sc_icampaign=docs-cli-config&sc_iplace=docs-bottom

  • ​Kubernetes and kops

  • http://christopher5106.github.io/continous/deployment/2016/05/02/deploy-instantly-from-your-host-to-AWS-EC2-and-Google-Cloud-with-kubernetes.html

  • https://github.com/kubernetes/kops#installing

  • https://medium.com/@jandavid.staerk/why-kubernetes-is-awesome-9f7ff0186996

  • https://kubernetes.io/docs/setup/production-environment/tools/kops/

  • https://medium.com/containermind/how-to-create-a-kubernetes-cluster-on-aws-in-few-minutes-89dda10354f4

  • ​SSH keys

  • https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html

You Might Also Like: