Create a Kubernetes cluster from the scratch (On CentOS 7 / RHEL 7) and Deploy an application on the cluster and Expose to Internet — Complete Example PART — 02 (Docker)

Content

  • Install Docker (Covered by previous story)
  • Create the cluster (on VM) (Covered by previous story)
  • Build Dockerized application — Covered on this story
  • Deploy the application on the cluster — Covered on this story

Previous story

TODO: Will cover on future stories.

  1. Expose the application to internet through an Ingress
  2. Install and configure Helm
  3. Deploy above application using Helm chart

Docker app source : https://github.com/el173/docker-node-app

Kubernetes deployment example : https://github.com/el173/kubernetes-examples

1.0 Build Dockerized application

Here I’m going to build a nodejs hello world app which is get app configurations as environment variables(Will explain later why we do this) on RHEL 7 (Same VM(s) which was used in my previous story PART-1)

First thing first

1.1 Install Node.js

  • Add Node.js Yum Repository
  • Install Node.js
  • After installing node.js verify and check the installed version

1.2 Create the Node.js app

  • Create a new directory where all the files would live. And create a package.json file that describes app and its dependencies.
  • And edit the file package.json
  • Then run npm install. If you are using npm version 5 or later, this will generate a package-lock.json file which will be copied into the Docker image.
  • After that, create a server.js file that specify the web app using the Express.js framework.

Edit and add following code to server.js file

1.3 Build a Docker image of the app

Note: Before move to the docker image creation, Create a Docker Hub account.

  • Create a empty file called Dockerfile
  • Edit and add below lines to Dockerfile
  • Create a .dockerignore file in the same directory and add following contents to it.

This will prevent your local modules and debug logs from being copied onto your Docker image.

  • Build myApp docker image

Run following command on same directory where the Dockerfile contains.

Note: Replace all the place where el173 with your docker hub user name.

Your image will now be listed by Docker

Then run the docker image

Test the docker image

To verify container created successfully run the below command

Output should like below,

To see the out put of the application

Now all good docker image of our app has been build successfully.

1.4 Push docker image to docker repository (This make easy the app deployment on Kubernetes)

First all, we need to login to our docker account on our app building environment.

Then push your docker image to your Docker Hub repository.

You can find the above example source code form this link

https://github.com/el173/docker-node-app

Now all ready to deploy our app on Kubernetes cluster (Which was created on my PART-1)

2.0 Deploy the application on the cluster

Before moving to the deployment please read this Understanding Kubernetes Objects

First create a separate namespace for our deployment. (It will easy to manage our app)

  • namespace.yaml file

Create namespace

Why we use configmap — It is the most convenient way to store our application configurations in kubernetes cluster.

Read more : https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

  • configmap.yaml file

Create config map

Now all good to deploy our application on the kubernetes cluster.

MUST: Read before do the deployment — https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

  • deployment.yaml file

Make the deployment

Okay, we have successfully deployed our app on the cluster. Now we need to create a service to access our app.

MUST: Read before create the service — https://kubernetes.io/docs/concepts/services-networking/service/

service.yaml

Once you created service. Retrieve all of your resources under node-app namespace, It should like below.

And now all good, You can check whether service is running.

Get service ip (cluster ip only available within the cluster)

Get service port

Then curl and see your application output:

On next story we will look on kubernetes Ingress.

References:

el173 — Focus on BigPicture; Syntax doesn’t matter