Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 62

Deploying Blog with Kubernetes

This lesson is built with resources from youtube.com, learnkube.com and 1 more site.

Hello! Let's dive into Module 10.

In our last lesson, you successfully provisioned a production-grade Kubernetes cluster on AWS using EKS and eksctl. You now have a running, albeit empty, cluster and you know how to connect to it using kubectl. This is a massive step, moving us from a single-machine setup to a distributed cloud environment.

Today, we'll start giving that cluster something to do. Our goal is to tell Kubernetes how to run our Blog service. To do this, we'll learn to write a Deployment manifest—a declarative configuration file that describes the desired state for our application.

MERN Stack Comparison

In your MERN world, you might use a process manager like PM2 to run your Node.js application, ensure it restarts on failure, and scale it across CPU cores. A Kubernetes Deployment serves a similar purpose but on a much grander, cluster-wide scale. It defines which Docker container to run, how many copies (replicas) to maintain for high availability, and how to perform updates. The manifest file we will write is the declarative "source of truth" for how our service should run.



1. The Core of Kubernetes: Desired State

Before we write any code, it's crucial to understand the fundamental principle of Kubernetes: desired state management.

You don't tell Kubernetes how to do things (e.g., "start a container"). Instead, you tell it what you want the end result to be (e.g., "I want 3 instances of my application running at all times"). This is the desired state.

Kubernetes's job is to constantly observe the current state of the cluster and take action to make it match the desired state you've defined.

Kubernetes Desired State Concept
This image illustrates the core concept of desired state. You define the target state (e.g., 2 running containers). Kubernetes compares this to the current state (e.g., only 1 is running) and automatically takes action (launches a new container) to reconcile the difference. This is the basis for self-healing and auto-scaling.

We define this desired state using YAML files, which are called manifests in Kubernetes terminology. The first and most common manifest we'll work with is the Deployment.



2. Anatomy of a Deployment Manifest

A Deployment is a Kubernetes object specifically designed to manage stateless applications. It creates and manages a set of identical Pods. A Pod is the smallest deployable unit in Kubernetes—think of it as a wrapper around one or more of your application containers.

Let's break down the structure of a Deployment manifest.

Kubernetes YAML File Explained - Deployment and Service | Kubernetes Tutorial 19

To get a high-level overview of how Kubernetes configuration files are structured, watch this short explanation from TechWorld with Nana. It clarifies the purpose of the metadata, spec, and auto-generated status fields.

Watch from the beginning to 03:42. Focus on understanding the roles of the three main parts of a Kubernetes configuration file. Don't worry about the Service example for now; we'll cover that in the next lesson.

As you saw, a typical manifest has these top-level fields:

  • apiVersion: Specifies the Kubernetes API version to use for this object. For Deployments, it's apps/v1.
  • kind: The type of object we are defining. Here, it's Deployment.
  • metadata: Data that helps uniquely identify the object, such as its name.
  • spec: This is the most important part—it's where we define the desired state for our Deployment.

Let's dig deeper into the spec for a Deployment.

Developing and deploying Spring Boot microservices on Kubernetes - Defining a Deployment

The article "Developing and deploying Spring Boot microservices on Kubernetes" provides a practical, code-first breakdown of a Deployment manifest. We'll use it to understand the key fields within the spec.

Read the section that breaks down the knote.yaml file, starting from "Now that you know how to look up the documentation..." down to the end of the section. Pay close attention to the explanation of replicas, selector, and the template section.

Based on that reading, here are the crucial fields inside spec:

  1. replicas: A simple integer that tells Kubernetes how many identical Pods to run. If you set this to 3, Kubernetes will ensure three Pods for your application are always running.

  2. selector: This tells the Deployment how to find the Pods it is supposed to manage. It uses labels—key-value pairs you attach to objects. The selector specifies which labels a Pod must have to be managed by this Deployment.

  3. template: This is the blueprint for the Pods that the Deployment will create. It has its own metadata and spec:

    • template.metadata.labels: These are the labels applied to each Pod created from this template. Crucially, these labels must match the selector of the Deployment. This is how the Deployment finds its Pods.
    • template.spec: This defines the actual contents of the Pod, most importantly the containers to run.
      • containers: A list of containers to run inside the Pod. For our simple services, this will just be one container.
        • name: A name for the container (e.g., blog-service).
        • image: The Docker image to pull and run.
        • ports: The network ports to open on the container (e.g., containerPort: 8080 for our Spring Boot app).


3. Hands-On: Deploying the Blog Service

Let's put theory into practice. We'll create a Deployment manifest for our blog-service.

Since we haven't set up our image registry (ECR) yet, we'll use a public, pre-built Spring Boot "hello world" image for now. This lets us focus purely on the Kubernetes part. We'll substitute our actual service image in a later module.

Step 1: Create the Manifest File

In the root of your project, create a new directory called kube. Inside this directory, create a file named blog-service-deployment.yaml.

/blog-platform
|-- /kube
|   |-- blog-service-deployment.yaml
|-- /src
|-- ...

Step 2: Write the Deployment Manifest

Copy the following YAML content into your blog-service-deployment.yaml file.




# kube/blog-service-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog-service-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: blog-service
  template:
    metadata:
      labels:
        app: blog-service
    spec:
      containers:
      - name: blog-service-container
        image: paulbouwer/spring-boot-hello-world:latest
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"

Let's break down two new additions here:

  • resources: This is a critical section for production stability.
    • requests: The amount of CPU and memory that Kubernetes guarantees to allocate to the Pod.
    • limits: The maximum amount of CPU and memory the Pod is allowed to consume. If it exceeds the memory limit, Kubernetes will terminate it (a common "OOMKilled" error).
  • Why is this important? Setting resources prevents a single misbehaving service from consuming all the resources on a node and crashing other services. It also helps Kubernetes make smarter decisions about where to schedule Pods.
Kubernetes Best Practices: Resource Requests and Limits
This diagram shows the trade-offs of setting resource requests and limits. Setting them too low can cause performance issues (CPU throttling) or crashes (Out of Memory), while setting them too high wastes resources. The goal is to find a balance based on your application's actual usage.

Step 3: Apply the Manifest and Verify

Now, use kubectl to send this manifest to your EKS cluster.

  1. Apply the configuration:

    kubectl apply -f kube/blog-service-deployment.yaml
    

    You should see the output: deployment.apps/blog-service-deployment created.

  2. Verify the Deployment: Check that the Deployment object exists and its desired state is being met.

    kubectl get deployments
    

    The output should show blog-service-deployment with 2/2 in the READY column, indicating both replicas are running.

  3. Inspect the Pods: The Deployment creates Pods. Let's see them.

    kubectl get pods
    

    You'll see two Pods with names like blog-service-deployment-xxxxxxxxxx-xxxxx. Their status should be Running.

This workflow—apply, get, describe—is the fundamental feedback loop for working with Kubernetes.

Kubernetes Tutorial | Run & Deploy Spring Boot Application in K8s Cluster using yaml configuration

To see this workflow in action, watch this clip from Java Techie. It demonstrates applying a manifest and then using kubectl get pods and kubectl logs to verify the deployment.

Watch from 08:19 to 10:33. This shows exactly the steps we just took: applying the YAML file, checking the pods that are created, and looking at the logs of a running pod. This is your primary debugging loop.



4. Debugging-First: When Things Go Wrong

Your kubectl get pods command is your first stop. If you see a status other than Running (e.g., ImagePullBackOff, CrashLoopBackOff, Pending), your next step is to use describe.

  • Scenario: You make a typo in the image name. The Pod status will be ImagePullBackOff.
  • Debug Command: kubectl describe pod <your-pod-name>
  • Output: In the Events section at the bottom, you'll see a clear error message like Failed to pull image "..." and Error: image not found. This tells you exactly where the problem is.

Always start with get pods, then describe pod, and finally logs pod (kubectl logs <your-pod-name>) to check for application-level errors.



Conclusion

In this lesson, you moved from having an empty cluster to deploying your first application. You've learned the most fundamental concept in Kubernetes—desired state—and translated that into a practical Deployment manifest.

Key Takeaways:

  • A Kubernetes Deployment describes the desired state for a stateless application.
  • We define this state in a YAML manifest with four key fields: apiVersion, kind, metadata, and spec.
  • The connection between a Deployment and its Pods is made using labels and selectors.
  • The Pod template defines the blueprint for each replica, including the container image and resources.
  • Your core workflow is: write YAML -> kubectl apply -> kubectl get -> kubectl describe.

Preview of the Next Lesson:
Our Pods are running, but they are isolated. They have internal IP addresses that can change at any time, making them inaccessible. In the next lesson, we will write a Service manifest. A Kubernetes Service provides a stable network endpoint (a single, unchanging IP address and DNS name) to access our group of blog-service Pods.