How to Deploy Anything to Kubernetes (Part 1 - Containerizing an Application)
Do we really mean anything? No. Because not everything is capable of being deployed with Kubernetes, and some things are not worth deploying on Kubernetes. Those things are beyond the scope of this writing, but in a future series, we will explore the options available for getting those applications onto a Kubernetes cluster.
Let's get right to it and talk about what you're here for – containerizing an application so you can deploy it on Kubernetes. You can think of a container as a virtual machine. It's more or less a wrapper for your application that contains all the dependencies your application needs to run.
For this step, we are going to put your application in a container. To do this, you will need to have Docker installed. Once Docker is installed, you will write a Dockerfile which tells Docker three things (among others):
- What dependencies your application needs.
- How to build your application.
- How to run your application.
Let's consider a Python application. Here's how we might containerize that application by creating a Dockerfile right beside our python code.
Now Here's that same Dockerfile with comments to explain what's happening.
The end result of running this Dockerfile will be a container image that has all of your dependencies and your code. If you are following along with a different application, you might need to add a command to the Dockerfile to actually build your code.
Great! Now you have a Dockerfile that defines your dependencies. Let's use that to actually create a container image that Kubernetes can use. To test this out locally, you can use the following command to have Docker run your Dockerfile steps:
docker build -t myapp:latest .
If everything ran successfully, you should have a container image that is able to be used. You can confirm that the image exists by running the
docker images command, and you can use
docker run myapp:latest run the container and
docker ps to ensure that the container is running.
In the next article, we will expand on this work to place our container image in a registry which a Kubernetes cluster can pull the image from. Here are the links to the articles in this series:
- Part 1: Containerizing an Application (This Article)
- Part 2: Push Image to Registry
- Part 3: Registry Pull Secrets
- Part 4: Write a Kubernetes Manifest
- Part 5: Deploy An Application
- Part 6: VMs on Kubernetes
If you like this article and want to follow this series as we publish and expand it, subscribe for email alerts!