3 steps to containerize and deploy Node.JS apps to Kubernetes
2. Kubernetes-- Deployment (deployment.yaml)
After creating an image using the Dockerfile, we need to deploy a container based on that image. To facilitate that, we can use a deployment file indicating which image to use, the number of instances of the application, etc.
3. Kubernetes-- Services (services.yaml)
By default, the deployed application will not be accessible outside of the Kubernetes cluster. Kubernetes Services provides a way to expose the underlying application to the wider Internet.
However, the easiest way to create a Kubernetes cluster in a local environment is using a tool called Minikube. It enables us to run a single-node Kubernetes cluster in Windows, Linux, or macOS. So let's see how to set up the Minikube software in a Windows environment.
Step 1: Download Minikube executable.
Step 2: Run the Minikube installer.
Step 3: Start the Minikube cluster. Open the command prompt as the administrator and run the "minikube start" command. When starting, users can specify some additional configurations to customize the Minikube Kubernetes cluster. (Ex: Limit the CPU and RAM amount
minikube start --cpus=2 --memory=1800m)
Step 4: Run
kubectl version to validate the installation.
That's it, and now we have a working Kubernetes cluster! So let's go ahead and deploy the application.
test-app, which prints a simple message in the browser.
Step 1: Create and Build the Dockerfile
We will use the above application to create our container image. First, let us create a Dockerfile. We will be using the official Docker image for Node.JS as the base image, and we will select the alpine variant due to its size. Alpine is a lightweight and security-focused Linux distribution that is ideal for containerized applications. The Dockerfile simply states the step-by-step process that is needed to be carried out to build an image.
The next step is to build the Docker image. This build process is analogous to a software build process. Yet, it builds a complete environment eliminating the need for a separate deployment process to configure the application on a server. Simply run the docker build command to build the image.
In the above result, the dot indicates the current directory while the -t parameter relates to a tag value which would be the image name. A successful build process will go through all our specified steps and create a deployable image. As we are building this locally, the image will be saved in the local image repository.
Step 2: Create the Kubernetes Deployment
Next, we need to deploy the image on the Kubernetes cluster we created earlier. Even though we can use terminal commands to deploy the image as a container, the recommended way is to use a deployment file as it will simplify the management of the application through a single file.
Note here that we specify the “imagePullPolicy: Never” to stop Kubernetes from looking for the image in external/online container repositories. The deployment finds the necessary pods to manage using the selector field, which matches the labels defined in the template section.
We can verify if the deployment is successful by checking the number of pods created. It will be the value we defined in the replicas field. (replicas: 3)
Step 3: Create the Kubernetes Service
Since the application is deployed now, the next step is to expose it to the world. We can use a Kubernetes service for that. When defining the service, the most important field is the selector, which is similar to a variable that indicates which Kubernetes pods to expose. Same as in the deployment, this will look for pods with a matching label field called ("app: test-app") and expose them using the service.
However, you can see that the EXTERNAL-IP is indicated as pending in the above output. The reason is that we are running this deployment in a local environment using Minikube. Simply run the Minikube tunnel command to associate a network route that would expose the external IP to the host operating system. (In our example, Windows operating system)
Finally, open up a different command prompt and check the running Kubernetes services. There, you can find an EXTERNAL-IP associated with our service.