Over the last few weeks we’ve looked at cloud computing and some of the technologies that support it, like virtual machines, hypervisors and containers. This week we’ll have a closer look at Docker and Kubernetes.
Docker and Kubernetes are the two technologies that are most often referred to when talking about containers. They are independent technologies and can be used with or without each other. They are complementary inasmuch as Docker technology is mainly used to build containers, and Kubernetes is used to manage and run multiple containers.
-
Docker is used before and during the deployment of application containers. Docker automates the building and deployment of containers. It assists the continuous integration/development (CI/CD) process.
-
Kubernetes is used after container deployment for automated scheduling and management of containers. Kubernetes manages clusters of multiple containers.
Container Revision
Remember that a container is a packaging of an executable software unit. It contains an application along with all the libraries and other dependencies it needs to run. Container do not include a copy of a guest operating system. Containers prevent the common problem of “I don’t know why it doesn’t run here; it runs just fine on my computer”. Because the application is packaged with everything it needs, it’s guaranteed to run anywhere, whether on a laptop, a desktop computer, a mainframe, or in the cloud.
An application can consist of many processes, such as front-end components, the back-end application logic processing, the database persistence layer, REST and/or SOAP web services, etc. Each process can be packaged in a separate container. This allows an application to be easily scaled up or down as demand changes. Because containers are small, they are very fast to spin up when extra containers are needed for processes under demand. If a bug is found in one container, it’s easy to run the rest of the application continuously across the other containers while the separate container with the buggy code is being updated.
A best practice is to install only one process per container. This allows us to easily replace components while other unrelated components are running. This facilitates scalability. Small, lightweight containers are also ideal for microservices architectures, where applications are made up of smaller, loosely coupled, independent services.
Obviously, if there are many containers running at the same time, it becomes essential to have some controlling engine/node to manage the container interactions. This mechanism is called container orchestration.
Docker
Docker is an open-source containerization platform. Docker provides the tools that allow developers to quickly and easily package applications into small, isolated containers. One of the reasons behind Docker’s success is the portability of Docker containers. They can run on any computer whether it’s a desktop, a mainframe or somewhere in the cloud.
Some of the advantages of running an application in a Docker container are:
- The application executes in the same environment no matter where it is, so always runs identically.
- The application just works because all the dependencies are packaged together.
- A container is very fast to start up. It can start up in seconds versus minutes for a full VM (virtual machine).
- Containers are less resource hungry than VMs.
- Applications can be sandboxed from each other.
The Docker Engine is the runtime environment that allows developers to build and run containers.
Docker images are defined using a Dockerfile. This is a text file containing a list of instructions to package an application into an image. It specifies everything needed to build a Docker container image, such as network configurations, port addresses, file locations, etc. The Docker Engine runs this list of commands to assemble the image. When the image is run, it becomes one instance (or multiple instances) of the container.
Docker images are built on top of other pre-built images. It’s possible to build a Docker image from scratch, but they are mostly pull from common repositories. The Docker Hub repository is the best place to find prebuilt images. Using the Docker CLI (command line interface) tool we can push and pull images to and from registries.
Docker has over 50 CLI commands and countless options and child commands. Some of the common commands used to create and run containers are the following:
docker build
: builds a new Docker image from a Dockerfile and other dependencies.docker create
: creates a new Docker image from an existing image. This creates a writeable container layer over the image.docker run
: identical todocker create
, except it also runs the container after creation.docker exec
: used to execute a new command inside an already running container.
Docker has a tool called Compose for defining and running multi-container applications. It creates a YAML file to specify which services are included in the application. Containers can be deployed and run with a single command via the Docker CLI.
Container Orchestration
Docker works well with smaller applications, but large enterprise applications can involve a huge number of containers — sometimes hundreds or even thousands. This becomes overwhelming for the IT teams that have to manage them. That’s where container orchestration comes in.
Container orchestration products provide services such as:
- Automated deployment.
- Automated rollouts and rollbacks.
- Auto-scaling under load.
- Load balancing between containers.
- Exposing endpoints and service discovery.
- Monitoring, logging and analytics.
Docker has its own orchestration tool, Docker Swarm, but the most popular and robust option is Kubernetes.
Kubernetes
Kubernetes is an open-source container orchestration platform. Like a conductor leading an orchestra and keeping the musicians together, Kubernetes schedules and automates the deployment and management of containerized applications. Kubernetes is Greek for “helmsman”, “pilot”, or “governor”. It sometimes abbreviated to K8S (as in 8 letters between the K and the S in the word).
The Kubernetes Engine is installed across a number of physical or virtual servers to create a Kubernetes cluster. Multiple containers operate together in a cluster. The cluster includes a master node that schedules the work for the rest of the containers (worker nodes) in the cluster. The master node determines where to host containers and how to put them together.
Features of Kubernetes include:
- Automated deployment: Kubernetes schedules and automates container deployment across multiple nodes.
- Service discovery and load balancing: Kubernetes exposes a container on the network and uses load balancing to ensure high availability.
- Auto-scaling: Kubernetes automatically starts up new containers to handle heavy loads.
- Self-healing: Kubernetes restarts, replaces or reschedules containers when they fail or if nodes die. It kills containers that don’t respond to specified health checks.
- Automated rollouts and rollbacks: It rolls out application changes and rolls back those changes if something goes wrong.
Kubernetes can run and manage containers built with the Open Container Initiative (OCI), which is Docker’s own image format. Later versions of Docker have built-in integration with Kubernetes.
Docker Swarm
Docker Swarm is another open-source container orchestration platform. Docker Swarm provides native support for orchestrating clusters of Docker Engines. A Swarm cluster consists of Swarm manager nodes (that orchestrate and manage the cluster) and worker nodes (that execute tasks as directed by the manager nodes). All of these nodes are deployed on Docker Engines.
Some of the differences between Kubernetes and Docker Swarm include:
Kubernetes | Docker Swarm |
---|---|
Complex installation | Easier installation |
More powerful | Limited functionality |
High learning curve | Easier to learn and use |
Supports auto-scaling | Manual scaling |
Built-in monitoring | Monitoring with third-party tools |
Manual load balancing | Automatic load balancing |
Needs separate CLI tool | Integrates with Docker CLI |
What’s Next?
You should now have a better understanding of containers and container orchestration.
If you’d like to do more reading, you can go to the home pages of Docker and Kubernetes
Wikipedia also has good articles on Docker and Kubernetes.
IBM has some excellent resources at the IBM Cloud Learn Hub.
In the next post we’ll have a look at microservices.
Don’t forget to share your comments and experiences.