Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Kubernetes for Developers

You're reading from  Kubernetes for Developers

Product type Book
Published in Apr 2018
Publisher Packt
ISBN-13 9781788834759
Pages 374 pages
Edition 1st Edition
Languages
Author (1):
Joseph Heck Joseph Heck
Profile icon Joseph Heck

Table of Contents (16) Chapters

Title Page
Packt Upsell
Contributors
Preface
1. Setting Up Kubernetes for Development 2. Packaging Your Code to Run in Kubernetes 3. Interacting with Your Code in Kubernetes 4. Declarative Infrastructure 5. Pod and Container Lifecycles 6. Background Processing in Kubernetes 7. Monitoring and Metrics 8. Logging and Tracing 9. Integration Testing 10. Troubleshooting Common Problems and Next Steps 1. Other Books You May Enjoy Index

Chapter 2. Packaging Your Code to Run in Kubernetes

n this chapter, we will dig into the first thing you need to enable to use Kubernetes: getting your software into a container. We will review what containers are, how you store and share images, and how to build a container. The chapter then continues with two examples, one in Python, and another in Node.js, which step you through how to take simple example code from those languages to build containers, and run them within Kubernetes. The sections of this chapter are:

  • Container images
  • Making your own container
  • Python example—making a container image
  • Node.js example—making a container image
  • Tagging your container images

Container images


The first step for using Kubernetes is getting your software into a container. Docker is the easiest way to create these containers, and it is a fairly simple process. Let's take a moment to look at an existing container image to understand what choices you will need to make when creating your own containers:

docker pull docker.io/jocatalin/kubernetes-bootcamp:v1

First, you'll see it pulling down a list of files with arcane IDs. You'll see them updating in parallel, as it tries to grab these as they're available:

v1: Pulling from jocatalin/kubernetes-bootcamp
5c90d4a2d1a8: Downloading  3.145MB/51.35MB
ab30c63719b1: Downloading  3.931MB/18.55MB
29d0bc1e8c52: Download complete
d4fe0dc68927: Downloading  2.896MB/13.67MB
dfa9e924f957: Waiting

And when the downloads are complete, the output will update to say extracting, and finally pull complete:

v1: Pulling from jocatalin/kubernetes-bootcamp
5c90d4a2d1a8: Pull complete
ab30c63719b1: Pull complete
29d0bc1e8c52: Pull complete
d4fe0dc68927...

Making your first container


Making a container is something easily done with the Docker software and the docker build command. This command uses a manifest that details how to create the container, called a Dockerfile.

Let's start with the simplest possible container. Create a file called a Dockerfile and add this to it:

FROM alpine
CMD ["/bin/sh", "-c", "echo 'hello world'"]

And then, invoke build:

docker build .

If you see a response like this:

"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

Then you are either missing the . in the command, or ran the command in a directory different from where you created the Dockerfile. The . is telling docker where to find Dockerfile (. meaning] in this current directory).

What you should see is some output akin to the following:

Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
latest: Pulling from library/alpine
88286f41530e: Pull complete...

Example – Python/Flask container image


To walk through the details of how to use Kubernetes, I have created two sample applications that you can download, or replicate to follow along and try out these commands. The first of these is a very simple Python application using the Flask library. The sample application is directly from the Flask documentation (http://flask.pocoo.org/docs/0.12/).

You can download a copy of this code from GitHub at https://github.com/kubernetes-for-developers/kfd-flask/tree/first_container. Since we will evolve these files, the code referenced here is available at the first_container tag. If you want to use Git to get these files, you can run the following commands:

git clone https://github.com/kubernetes-for-developers/kfd-flask

Then, go into the repository and check out the tag: 

cd kfd-flask
git checkout tags/first_container

Let's start with looking at the contents of a Dockerfile, which define what gets built into a container and how that happens.

The goals we have...

Example – Node.js/Express container image


This example follows the same pattern as the Python example, a simple Node.js application built with the Express library to walk through the details of how to use Kubernetes. If you are more familiar with JavaScript development, this example may be more meaningful. The sample application is directly from the Express documentation (https://expressjs.com/en/starter/generator.html).

You can get a download a copy of this code from GitHub at https://github.com/kubernetes-for-developers/kfd-nodejs/tree/first_container. Since we will evolve these files, the code referenced here is available at the first_container tag. If you want to use Git to retrieve these files, you can do so using the following commands:

git clone https://github.com/kubernetes-for-developers/kfd-nodejs
cd kfd-nodejs
git checkout tags/first_container

Like the Python example, we will start with the Dockerfile. As a reminder, this is what defines what gets built into a container, and how...

Tagging your container images


Using the :latest tag on Docker images is incredibly convenient, but it can easily lead to confusion as to what exactly is running. If you do use :latest, then it is a very good idea to also tell Kubernetes to always attempt to pull a new image when loading the container. We will see how to set this in Chapter 4, Declarative Infrastructure, when we talk about declaratively defining our applications.

An alternative is to make explicit tags, building with a tag, and also using docker tag to tag the image as latest for the convenience factor, but maintaining specific tags within the declarations that you check in to source control. For this example, the tag chosen is 0.2.0, using semantic versioning to represent a value to use with the container, and matched to a git tag as well.

The steps that were used while making this example were:

git tag 0.2.0
docker build -t quay.io/kubernetes-for-developers/nodejs:0.2.0 .
git push origin master --tags
docker push quay.io/kubernetes...

Summary


In this chapter, we reviewed what makes up a container, how to store and share containers on the internet, and some of the commands you can use to create your own containers. We then used that knowledge to walk through an example in Python and another in Node.js, creating simple web-based services in both, building those into container images, and running them within Kubernetes. In our next chapter, we will dive deeper into how to interact with your code once it's been packaged into a container and will explore tips for taking full advantage of containers and Kubernetes during your development.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Kubernetes for Developers
Published in: Apr 2018 Publisher: Packt ISBN-13: 9781788834759
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}