Containerize your application in preparation for Kubernetes

Matt
3 min readNov 21, 2020

Part of my Kubernetes series

I’m going to avoid going into an explanation of why this might be useful. Instead I will simply say that it will be required for “container orchestration systems/platforms”.

Ingress is a Kubernetes resource, and Kubernetes is a container orchestration system. Therefore, in order to have an app running in Kubernetes (K8s) we need to “containerize” our example. Strictly speaking this does not need to be docker but it is what I am working with.

For the most part, you will primarily be able to grab the code, copy it over and it should have exactly what you need to run the application in this example. We will create an image and test the running image (called a container) to ensure it works as we expect. I will provide more details below.

Install docker:

I already have this installed so i will defer this to docker’s documentation.

Creating Images:

Again, for this example simply copy the code over it will have exactly what you need to run the application.

The code will consist of:

dockerfile: this is the set of instructions used to generate an image.
build.sh: a script that will build the image (make it executable).
run-container.sh script that will start the container (make is executable).

  1. We execute the docker build script to build it:

cat build.sh:

COMMAND="docker build --tag request-capture ."
echo "Running command: $COMMAND ...."
eval $COMMAND
COMMAND="docker images | grep request-capture"
echo "Listing images generated: $COMMAND ...."
eval $COMMAND

dockerfile:

FROM node:12.18.4-alpine as baseimgENV NODE_ENV=production# get dependancies
RUN apk add --update --no-cache curl -f && \
apk add --update --no-cache bash -f && \
# print version of requirements
node -v && \
npm -v;
FROM baseimgARG NODE_BUILD_VERSION="12.18.4"
ARG PATH_TO_CODE="/Users/walczyk/DAP/devops/INGRESS/deep_dive/mw-test-ingress-micro-service/request-capture"
ENV MICROSERVICENAME="request-capture"
ENV NODE_ENV=production
ENV NODE_BUILD_VERSION=${NODE_BUILD_VERSION}
ENV PATH="/node-v${NODE_BUILD_VERSION}-linux-x64/bin/:${PATH}"
LABEL description="Used to test ingress related changes"USER node
RUN node -v && npm -v
# This prepares the image with the nodejs dependancies:COPY --chown=node "request-capture/package.json" /home/node/$MICROSERVICENAME/
RUN cd /home/node/$MICROSERVICENAME/ && npm install --no-progress --production
# This copies the code into the image:
# This is currently dependant on where the file is .. :( relative to dockerfile hmm
COPY --chown=node $PATH_TO_CODE /home/node/$MICROSERVICENAMEENTRYPOINT ["/bin/bash", "-c", "cd /home/node/$MICROSERVICENAME; node -v ; node $MICROSERVICENAME.js 8081"]

2. We execute the docker run script to run it:

run-container.sh:

#!/bin/bashCOMMAND="docker stop request-capture; docker rm request-capture; docker run -d -p 8084:8081 --name request-capture request-capture:latest"
echo "Running command: $COMMAND ...."
eval $COMMAND

Here, on my laptop i’m not using port 8084, but the app is hardcoded to run with 8081. So we map my machines 8084 port to the docker containers 8081 port. The app runs in the container so it only ever sees what the container will tell it and doesn’t actually know it is being mapped.

Ultimately, this will also be reachable and testable as before on with a curl against 8084 or 8081 (if you are on the container).

3. Let’s test this now:

Similar to how we did this locally, we will run a curl test. The difference here is that the port is now going to be 8084 from your machine (outside your container).

The output should be the same as what we did in the test of the application without docker.

Excellent now we can get it onto a Kubernetes cluster in the next step, let’s check it out

--

--

Matt
0 Followers

A curious mind in the tech field. #Question-everything