k8 the straightforward series: Pt1 — Preps

Shailyn Ortiz
Arionkoder Engineering
4 min readAug 25, 2019

Why another Kubernetes series?

Browsing thru the internet for a quick Kubernetes example, basically, a deployment + svc, some of the tutorials indeed had a deployment but exposed the service manually which isn’t the way we really do things, others used Nginx serving a hello world which is ok to start but then they get you into Nginx config and again, too far from what we need. So I decided to pull out my laptop and start this series!

TLDR:

  • Create a dummy python flask API
  • Create a Dockerfile
  • Install MiniKube (because there must be k8 in each post, I think)

To deploy an app what do we need? An app. Let’s make one.

Python API:

In this section I assume you have python installed, if not download it.

Create a file calledrequirements.txt and add the following lines to it:

flask
flask-restful

Create a file called app.py and add the following to it:

  1. Install the dependencies:pip install -r requirements.txt
  2. Execute the project: python app.py

3. Open your browser and go to the highlighted URL

The output would look like this.

Ok, now we have our API running! so, what’s next?

Docker:

Here we will be going back and forth with the app until we get to a desired state, I didn’t do it in the last section because those changes only make sense on Docker

create a file called Dockerfile , capital D is a must:

FROM python:3.7WORKDIR /app/COPY . .RUN pip install -r requirements.txtCMD python app.py

The above Dockerfile uses python:3.7 as the base, copy all the files over, install the dependencies and tell Docker to start the app when the container is run.

Build the image: docker build -t k8-the-straightforward-way:1 .

Run the app: docker run --publish 5000:5000 k8-the-straightforward-way:1

Our app is now in Docker, producing the same output. Nevertheless it doesn’t work. See bellow

Fixing the issues

Our app is no longer available, if we take a step back and remember that Docker is a way of virtualization and that it treats containers as separate machines (where it applies), which means that 127.0.0.1 (A.K.A localhost) is no longer our 127.0.0.1, our app is listening at the container localhost thus making it unavailable.

How do we fix that?

Let’s change our app host to 0.0.0.0 so it listens in all the interfaces (network-ish knowledge)

Let’s also app the expose instruction to our Dockerfile to let everyone know what ports is our app using

FROM python:3.7WORKDIR /app/COPY . .RUN pip install -r requirements.txtEXPOSE 5000CMD python app.py

Re-build the docker image: docker build -t k8-the-straightforward-way:1 .
Re-run the docker image: docker run --publish 5000:5000 k8-the-straightforward-way:1

Now our app is fully running on Docker.

Minikube

What is Minikube?

According to the docs:

Minikube implements a local Kubernetes cluster on macOS, Linux, and Windows. minikube’s primary goals are to be the best tool for local Kubernetes application development and to support all Kubernetes features that fit.

In other words, Minikube is the easiest way to install a cluster for testing purposes, we will be using it to test our app before pushing to the cloud. So go ahead and install it. And let’s continue on the next chapter.

--

--

Shailyn Ortiz
Arionkoder Engineering

Senior QA Engineer / Devops Engineer. — Docker/kubernetes preacher. — backend and automation — open source contributor