DEV Community

Cover image for Kubernetes Core Concepts: Building Blocks of Container Orchestration
Emmanuel Oyibo
Emmanuel Oyibo

Posted on • Originally published at emminex.Medium

Kubernetes Core Concepts: Building Blocks of Container Orchestration

Kubernetes has revolutionized how we manage containerized applications. This makes it a must-know for modern software development.

But to truly harness its power, you need to understand its core concepts — Pods, Services, Deployments, ReplicaSets, and Namespaces.

You can think of these concepts as the building blocks of Kubernetes.

In this guide, we’ll break down each of these concepts. We’ll explain their roles and how they interact with each other.

Whether you’re new to Kubernetes or looking to level up your skills, this guide will give you the foundation to master container orchestration.

Pods

You can see Pods as a cozy little home where one or more containers live and work together. It’s the smallest unit you can deploy and manage in Kubernetes.

Image description

Moreover, it’s the foundation on which everything else is built.

Why Use Pods?

Why not just deploy containers directly, you might ask? Well, Pods offer some key benefits:

  • Shared Resources: Containers within a pod share the same network and storage. This makes it easy for them to communicate and access data.

  • Co-location: Pods ensure that closely related containers are always scheduled to run on the same machine. This feature reduces network latency and improves performance.

  • Simplified Management: Kubernetes treats a Pod as a single unit. Hence, it makes managing, scaling, and deploying your application much easier.

Single-Container vs. Multi-Container Pods

Pods can house either a single container or multiple containers that work closely together.

  • Single-Container Pods: The most common type, used for simple applications where a single container is sufficient.

  • Multi-Container Pods: Used for more complex scenarios where multiple containers need to share resources and communicate closely. For example, you might have a Pod with a web server container and a logging container that collects and processes logs from the web server.

Let’s illustrate the concept of Pods using a practical example. Imagine a web application that consists of a frontend and a backend.

A single-container pod might run just the backend service. On the other hand, a multi-container Pod could run both the frontend and a sidecar container for logging.

Here’s what the configuration file for a single-container Pod will look like:

apiVersion: v1
kind: Pod
metadata:
  name: single-container-pod
spec:
  containers:
  - name: my-backend
    image: my-backend-image
Enter fullscreen mode Exit fullscreen mode

Let’s take a look at the configuration file for a multi-container Pod:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: my-frontend
    image: my-frontend-image
  - name: my-logger
    image: my-logger-image
Enter fullscreen mode Exit fullscreen mode

We’ll learn more about Kubernetes configuration files in a future article.

In a nutshell, Pods serve as the fundamental building blocks within Kubernetes. It interacts with other essential components like Deployments, Services, and ReplicaSets.

Services

Imagine your Pods are houses in a neighborhood. While each house may have its own unique address, you need a way for residents to interact and access services offered within the community.

In Kubernetes, Services fulfill this crucial role by providing a stable and discoverable network endpoint for groups of Pods.

What are Services?

A Service in Kubernetes is an abstraction layer that groups together a set of Pods. Services provide a single, consistent point of access to groups of Pods.

Furthermore, Services act as a sort of virtual “front door” for your application, even if the Pods behind it changes or are rescheduled to different nodes within the cluster.

Hence, your application components can communicate easily regardless of their physical location.

Types of Services

Kubernetes offers different types of Services. Each with its own purpose:

  • ClusterIP: The default and most basic type. It gives your Service an IP address that’s only accessible from inside the cluster. This is great for internal communication between different parts of your application.

  • NodePort: If you need to expose your application to the outside world, NodePort opens a specific port on every node (server) in your cluster. Traffic sent to that port is then forwarded to the Pods behind the Service.

  • LoadBalancer: This type creates an external load balancer in your cloud provider’s infrastructure. It assigns a public IP address to your Service, making it accessible from the internet.

  • ExternalName: Sometimes you need to access a service that’s outside your Kubernetes cluster. ExternalName lets you give it a friendly name within your cluster. This makes it easier to reference.

Service Discovery and Load Balancing

Kubernetes Services make life easier in two key ways:

  • Service Discovery: Instead of hardcoding IP addresses, your Pods can simply use the Service name to find and communicate with each other. Kubernetes handles the lookup, so your application stays flexible even if Pods move around.

  • Load Balancing: Services automatically distribute incoming traffic across all the healthy Pods that they manage. This ensures that no single Pod gets overwhelmed and helps your application handle heavy loads.

How Services and Pods Work Together

Services and Pods are like two sides of the same coin. The Service provides the address, and the Pods do the actual work.

When pods are created, updated, or deleted, the Service automatically adjusts its list of available Pods. This keeps things running smoothly.

For instance, let’s say you have a web application running on multiple Pods in a cluster. You can create a Service to expose those Pods. This gives them a single stable address.

When users try to access your website, they’ll hit the Service. It will then distribute their requests across the available Pods, ensuring fast and reliable performance.

Now, let’s take a look at what a ClusterIP Service configuration file looks like:

apiVersion: v1
kind: Service
metadata:
  name: my-clusterip-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
Enter fullscreen mode Exit fullscreen mode

Moreover, a NodePort Service configuration file looks like this:

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30007
Enter fullscreen mode Exit fullscreen mode

In conclusion, Services are the communication backbone of your Kubernetes cluster. It ensures that your applications are always accessible and can handle whatever traffic comes their way.

Deployments

Think of Deployments in Kubernetes as the master plan for your application.

They tell Kubernetes exactly how many copies (or replicas) of your Pods should be running, what software image to use, and how to update them over time. Moreover, all these happens without disrupting your users.

What are Deployments?

Deployments are the key to keeping your applications up-to-date and running smoothly in Kubernetes. They act like a blueprint, defining the desired state of your application, including:

  • Which container image to use: This specifies the exact version of your application code that should be running.

  • How many copies of your Pods to run: This ensures your application can handle the expected load and provides redundancy in case of failures.

  • How to roll out updates: This defines the strategy for updating your application to a new version. This ensures minimal downtime and disruption.

Once you create a Deployment, Kubernetes takes the reins. It ensures the actual state of your application always matches the desired state you’ve defined. It’s like having an autopilot for your application that keeps it running smoothly even as you make changes.

Creating and Managing Deployments

Deployments are created using YAML files that specify the application’s desired state. This includes the number of replicas, the container image, and update strategies.

Kubernetes then manages the Deployment, ensuring the specified number of Pods are running and updating them as needed.

Now, let’s see a Deployment configuration file example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:1.0
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Rolling Updates and Rollbacks

Kubernetes Deployments excel at handling updates without causing downtime for your users.

  • Rolling Updates: Kubernetes updates Pods gradually. It replaces old Pods with new ones according to the defined strategy. This ensures that the application remains available throughout the update process. You can even fine-tune how many Pods are updated at a time and how quickly the rollout should progress.

    Here’s an example configuration file, for a rolling update strategy:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-deployment
    spec:
      replicas: 3
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
          - name: my-container
            image: my-image:2.0
            ports:
            - containerPort: 80
    
  • Rollbacks: If something goes wrong during an update, Kubernetes makes it easy to revert to a previous version of your application with a single command. This acts like a safety net. It allows you to quickly recover from any deployment issues.

Deployments and ReplicaSets

Behind the scenes, Deployments work hand-in-hand with ReplicaSets. When you create a Deployment, it automatically creates a ReplicaSet, which then creates and manages the individual pods.

This then ensures the desired number of Pods are always running, even if some fail. More on ReplicaSet later.

For example, imagine you have a web application running on Kubernetes, and you’ve just developed a shiny new version. You’d simply update your Deployment’s configuration to point to the new container image.

Kubernetes would then take over, gracefully replacing the old Pods with new ones running the latest version of your app. All these without your users noticing a thing.

ReplicaSets

In Kubernetes, a ReplicaSet is like a supervisor constantly checking in on your Pods. You tell it how many copies of a Pod you want running, and it makes sure that’s always the case.

If a Pod crashes or is deleted, the ReplicaSet automatically creates a new one to take its place.

Maintaining the Desired Number of Pods

ReplicaSets are crucial for ensuring your applications are always available and can handle increased traffic:

  • Desired State: You specify how many Pod replicas you want in your Deployment. The ReplicaSet then makes sure that’s exactly what’s running.

  • Self-Healing: If a Pod fails or is deleted, the ReplicaSet detects this and creates a new Pod to replace it. This keeps your application running smoothly.

  • Scaling: If you need to scale your application up or down, you simply update the number of replicas in the Deployment. The ReplicaSet takes care of the rest.

For instance, let’s imagine you have a web application running on Kubernetes. Let’s say you want to ensure there are always three instances of the web server Pod running.

You would create a Deployment with a replica count of 3. The Deployment would then create a ReplicaSet, which in turn would create three Pods.

If one of those pods fails, the ReplicaSet would immediately create a new one. This ensures your website stays up.

Let’s see this example configuration file for a ReplicaSet:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:1.0
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

ReplicaSets and Deployments

ReplicaSets and Deployments work together. When you create or update a Deployment, it tells the ReplicaSet what to do.

The ReplicaSet then handles the actual creation and management of the Pods. This ensures the Deployment’s desired state is always met.

Namespaces

Namespaces are a way to create virtual clusters within your Kubernetes cluster. This means you can have multiple teams, projects, or environments running on the same cluster without them stepping on each other’s toes.

It’s like separate rooms in a big house. Each room has its own purpose, its own furniture, and its own set of rules about who can enter.

Namespace lets you divide your Kubernetes cluster into smaller, organized spaces, each with its own resources and access controls.

Benefits of Namespaces

Namespaces offer several key benefits:

  • No More Name Clashes: Ever had two people with the same name in the same room? It gets confusing fast! Namespaces prevent this by giving each “room” its own naming system. Therefore, you can have a “database” Pod in your development namespace and another “database” Pod in your production namespace without any mix-ups.

  • Access Control: Namespaces make it easy to control who can do what in each part of your cluster. You can set up permissions so that developers can only access resources in the development namespace, while production resources are locked down tight.

  • Resource Allocation: You can set limits on how much CPU, memory, and storage each Namespace can use. This prevents one team or project from hogging all the resources.

  • Logical Separation: Namespaces are great for keeping different environments (like development, testing, and production) separate within the same cluster. This keeps things organized and can save you money by avoiding the need for multiple clusters.

How Namespaces Work with Other Kubernetes Components

Namespaces interact with other Kubernetes components to create a well-organized and secure environment:

  • Pods, Services, and Deployments: These resources live within specific Namespaces, ensuring they don’t clash with resources in other Namespaces.

  • Role-Based Access Control (RBAC): You can set up permissions at the Namespace level, controlling who can do what within each “room.”

  • Resource Quotas: Namespaces let you enforce resource limits, making sure everyone gets their fair share.

Below is a typical Namespace configuration file:

apiVersion: v1
kind: Namespace
metadata:
  name: development
Enter fullscreen mode Exit fullscreen mode

Moreover, the configuration file of a Pod in the above Namespace will look like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: development
spec:
  containers:
  - name: my-container
    image: my-image:1.0
Enter fullscreen mode Exit fullscreen mode

In a nutshell, Namespaces are like virtual fences within your Kubernetes cluster. The provide separation, organization, and security.

They’re an essential tool for managing complex environments and keeping your applications running smoothly.

Declarative Configuration and Desired State Management

Imagine telling your car’s GPS where you want to go, and it figures out the best route, avoids traffic, and even re-routes if there’s an unexpected road closure.

Declarative configuration in Kubernetes operates similarly. You tell it what you want your application to look like (the desired state), and Kubernetes takes care of the rest.

Kubernetes makes sure it stays that way, even in the face of challenges or failures.

Declarative vs. Imperative: Two Ways to Manage Your Applications

There are two main ways to manage software systems:

  • Imperative: You give step-by-step instructions on how to achieve a certain state. It’s like giving someone a recipe and telling them exactly how to cook a dish.

  • Declarative: You describe what the final state should look like, and the system figures out how to get there. It’s like telling someone you want a delicious meal, and they take care of the cooking.

Kubernetes embraces the declarative approach. You use YAML files (a human-readable data serialization format) to define the desired state of your applications. Then, Kubernetes continuously monitors the actual state of your cluster to ensure it matches your specifications.

Benefits of Desired State Management

Kubernetes’ declarative approach offers several advantages:

  • Reproducibility: Your YAML files act as a blueprint for your application. This makes it easy to recreate the same environment across different clusters or even on your local machine.

  • Version Control: You can track changes to your application’s configuration using version control systems like Git. Version control makes it easier to roll back to previous states if needed.

  • Self-Healing: If something goes wrong and your application deviates from the desired state (e.g., a Pod crashes), Kubernetes will automatically take action to bring it back in line.

  • Simplified Management: You don’t need to worry about the low-level details of how to achieve a certain state. Kubernetes takes care of the complex orchestration, leaving you to focus on your application logic.

Let’s look at a simple example of a declarative configuration for a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

This YAML file tells Kubernetes to:

  • Create a Deployment named “my-app”

  • Ensure that 3 replicas of the Pod are running

  • Select Pods with the label “app: my-app”

  • Use the container image “my-app-image:latest” for the Pod

  • Expose port 80 on the container

Kubernetes will then work tirelessly to ensure that this desired state is maintained, even if Pods fail or nodes become unavailable.

Conclusion

Kubernetes core concepts – Pods, Services, Deployments, ReplicaSets, and Namespaces – are the foundation of container orchestration.

Understanding these building blocks is essential for harnessing Kubernetes' power to deploy, scale, and manage applications efficiently.

By mastering these concepts, you'll unlock a world of possibilities for building and managing modern, cloud-native applications.


Thanks for reading! If you found this article helpful (which I bet you did 😉), got a question or spotted an error/typo... do well to leave your feedback in the comment section.

And if you’re feeling generous (which I hope you are 🙂) or want to encourage me, you can put a smile on my face by getting me a cup (or thousand cups) of coffee below. :)

Also, feel free to connect with me via LinkedIn.

Top comments (0)