Published on

Kubernetes - Containers and Setup

Authors

Following on the blog series, this post will focus on explaining what Containers are and how to set up a simple cluster using Minikube.

Containers

Containers are lightweight, standalone, and executable software packages that include everything needed to run a piece of software, such as code, runtime, system tools, libraries, and settings. Unlike virtual machines, which require full OS installation, containers share the host system's kernel and are much more efficient in terms of resource usage and performance.

Containers also provide consistency across multiple environments, whether in development, staging, or production, making them ideal for microservices architectures. With tools like Docker, containers can be built, deployed, and orchestrated with ease.

In Kubernetes, containers run inside Pods, the smallest deployable units. Each Pod can contain one or more containers, with the containers inside a Pod sharing network and storage resources. This architecture allows for more flexible application management, scaling, and resilience.

Setup

Now it's time for some hands-on practice. I will be using Minikube to setup a few k8s clusters following some of the tutorials from the official documentation.

Minikube is focused on Kubernetes learning and allows deployment in a single node, which make things much easier compared to deploying a k8s cluster in the "traditional way" with one control plane node and one worker node, for instance. It is not intended to production enviroments, because it is limited to a single node and lacks security and networking mechanisms.

Also, to keep track of the tutorials completed, I've created a GitHub repository with a Codespaces configuration that has minikube pre-installed to allow easy testing: https://github.com/andreluciani/k8s-101

Hello Minikube

The first tutorial on the official documentation explains how to deploy a container running a NGINX server that echoes back all the requests.

I've followed all the steps and documented on the GitHub repository. The part that really matters (beside the commands and concepts learned) is that it worked:

@andreluciani ➜ /workspaces/k8s-101 (main) $ curl http://192.168.49.2:30105
NOW: 2024-09-18 21:26:36.275195006 +0000 UTC m=+987.744071180

Learn Kubernetes Basics

The next tutorial is a walkthough that consists of 6 steps:

  1. Create a k8s cluster
  2. Deploy an app
  3. Explore the app
  4. Expose the app publicly
  5. Scale up the app
  6. Update the app

The first 4 steps were pretty similar to the "Hello Minikube" tutorial. But on steps 5 and 6, new operations were introduced: scaling and updating. On the tutorial, the number of replicas (Pods) was changed from 1 to 4, and then re-scaled down to 2. The process was manual but it is possible to configure kubernetes to scale automatically, both horizontally (adding or removing pods) and vertically (adding or removing resources such as memory and CPU). Also, an update operation was done by changing the image used in the pods. Kubernetes allows rolling updates with zero downtime, which can be seen in the pods status below. Notice that througout the process, there's always at least one pod running.

  1. Right after setting the new image, a new pod (sha:77596488f6) is created while the current ones (sha: 68cfbdbb99) are still running:

    NAME                                   READY   STATUS              RESTARTS      AGE
    kubernetes-bootcamp-68cfbdbb99-dcdz9   1/1     Running             0             19m
    kubernetes-bootcamp-68cfbdbb99-w89lw   1/1     Running             1 (95m ago)   4h57m
    kubernetes-bootcamp-77596488f6-7mmbw   0/1     ContainerCreating   0             2s
    
  2. When the new pod is running, one of the old pods can be terminated and another pod with the new image can be created:

    NAME                                   READY   STATUS              RESTARTS      AGE
    kubernetes-bootcamp-68cfbdbb99-dcdz9   1/1     Terminating         0             19m
    kubernetes-bootcamp-68cfbdbb99-w89lw   1/1     Running             1 (95m ago)   4h57m
    kubernetes-bootcamp-77596488f6-2hlxc   0/1     ContainerCreating   0             1s
    kubernetes-bootcamp-77596488f6-7mmbw   1/1     Running             0             4s
    
  3. Now the two pods running have the new image, the old pods can be terminated:

    NAME                                   READY   STATUS        RESTARTS      AGE
    kubernetes-bootcamp-68cfbdbb99-dcdz9   1/1     Terminating   0             19m
    kubernetes-bootcamp-68cfbdbb99-w89lw   1/1     Terminating   1 (95m ago)   4h58m
    kubernetes-bootcamp-77596488f6-2hlxc   1/1     Running       0             4s
    kubernetes-bootcamp-77596488f6-7mmbw   1/1     Running       0             7s
    
  4. Update completed:

    NAME                                   READY   STATUS    RESTARTS   AGE
    kubernetes-bootcamp-77596488f6-2hlxc   1/1     Running   0          3m40s
    kubernetes-bootcamp-77596488f6-7mmbw   1/1     Running   0          3m43s
    

Another operation done was a roll back. First, an image that was not available was set for the deployment. This resulted in a failure on pod creation:

NAME                                   READY   STATUS             RESTARTS   AGE
kubernetes-bootcamp-5b9b4dcdf5-2zdmh   0/1     ImagePullBackOff   0          22s
kubernetes-bootcamp-77596488f6-2hlxc   1/1     Running            0          16m
kubernetes-bootcamp-77596488f6-7mmbw   1/1     Running            0          16m

Then, with the command kubectl rollout undo deployments/kubernetes-bootcamp the deployment wass rolled back and the pods returned to the stable configuration.

Self-Hosted Kubernetes x Kubernetes Cloud Providers

For production enviroments, Kubernetes can be deployed either on-premises or using a cloud provider. It will depend on several factors such as: cost, current infrastructure, release cycle, team capabilities, etc. There is no right answer and the decision should be taken accouting all those factors.

The most common managed Kubernetes cloud providers are:

  • AWS: Elastic Kubernetes Service (EKS)
  • Microsoft Azure: Azure Kubernetes Service (AKS)
  • Google Cloud Platfom: Google Kubernetes Engine (GKE)

Conclusion

In this post, I've explored what containers are, their key role in modern software development, and how they form the foundation of Kubernetes. Using Minikube, I set up a simple cluster and walked through tutorials that introduced basic Kubernetes operations such as deploying applications, scaling them, and performing updates without downtime.

Kubernetes simplifies the orchestration of containerized applications, and while Minikube is great for learning, real-world production environments often rely on managed services like AWS EKS or Google GKE. In future posts, I'll dive deeper into Kubernetes features keeping track of the roadmap I've mentioned on the first post.

References