DEV Community

Cover image for CKA Full Course 2024: Day 7/40 Pod in Kubernetes Explained
Lloyd Rivers
Lloyd Rivers

Posted on

CKA Full Course 2024: Day 7/40 Pod in Kubernetes Explained

Image description

At the core of Kubernetes is the Pod. Think of a Pod as a wrapper for one or more containers, where these containers share the same network and storage. Pods represent the smallest, most basic deployable objects in Kubernetes, and they typically run a single instance of an application. However, a Pod can contain multiple tightly coupled containers.

Two Ways to Create Pods: Imperative vs Declarative

In Kubernetes, there are two main approaches to creating and managing resources: Imperative and Declarative.

  • Imperative: This method allows you to directly instruct the Kubernetes cluster to perform an action immediately using the kubectl command. It’s quick but may not be suitable for managing large clusters, as it does not retain a record of your infrastructure.

  • Example: Create an nginx Pod with an imperative command:

    kubectl run nginx-pod --image=nginx:latest
    
  • Declarative: This method is more sustainable for large-scale environments. You define your desired state in a YAML file, and Kubernetes works to ensure that the cluster matches that state. This approach is beneficial for version control and team collaboration.

  • Example: Create the same nginx Pod using a YAML file.

    First, create a YAML configuration file like this:

    # The names in this configuration are case-sensitive. 
    # Be sure to use the exact casing as shown.
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:latest
    

    Then, apply it to the cluster with:

    kubectl apply -f nginx-pod.yaml
    

Creating and Managing Pods with YAML

For larger teams or more complex applications, we use declarative YAML files. YAML allows us to define the desired state of our Pods and other Kubernetes resources in a clean, human-readable format. Let's break down the example YAML file mentioned earlier:

  • apiVersion: Specifies the API version (in this case, v1).
  • kind: Defines the type of resource (here, a Pod).
  • metadata: Contains information about the object, such as the name of the Pod.
  • spec: Describes the desired state of the object, including the containers, images, and more.

To create a Pod from this YAML file, save it as mypod.yaml and run:

kubectl apply -f mypod.yaml
Enter fullscreen mode Exit fullscreen mode

To view the status of the Pod:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

If you need to delete the Pod:

kubectl delete -f mypod.yaml
Enter fullscreen mode Exit fullscreen mode

If you need to see the details of the Pod:

kubectl explain pod
Enter fullscreen mode Exit fullscreen mode

YAML Tutorial for Beginners

If you’re new to YAML, don’t worry—it’s a simple, human-readable format used to define objects in Kubernetes. Here’s a breakdown of what you need to know:

  • Indentation matters: YAML relies on spaces (not tabs) to structure data.
  • Key-Value pairs: Similar to JSON but more readable.
  • Lists: Defined using a hyphen (-) followed by the value.

Example:

list_of_fruits:
  - apple
  - banana
  - orange
Enter fullscreen mode Exit fullscreen mode

In Kubernetes, YAML allows you to define objects like Pods, Services, and more.


Summary

In today's post, we covered:

  • What Pods are in Kubernetes and why they are important.
  • The difference between imperative and declarative methods of managing Pods.
  • How to create and manage Pods using both imperative commands and declarative YAML files.

Top 3 kubectl Commands from Today's Post

  1. kubectl run nginx-pod --image=nginx:latest – Create a Pod imperatively.
  2. kubectl apply -f nginx-pod.yaml – Apply a YAML configuration to create a Pod.
  3. kubectl get pods – View all running Pods in the cluster.

Tags and Mentions

@piyushsachdeva
Day 6 video

Top comments (0)