How to Create Kubernetes Pods: Day-13

Welcome to the Train Station of Kubernetes!

Imagine you’re at a big train station where lots of trains come and go, carrying passengers to different places. This train station is magical because it organizes all the trains, makes sure they run on time, and helps them reach their destinations safely. This magical station is called Kubernetes.

For a deeper dive into Kubernetes fundamentals, check out our article on Kubernetes Basics.https://mrcloudbook.com/kubernetes-cluster-architecture-day-04/

Let’s Explore the Adventure:

Train Cars Are Like Containers

  • Individual Cars: Each train car carries something special—passengers, luggage, food, or mail.
  • Containers in Computers: In the computer world, a container is like a train car. It’s a small package that holds a part of an application or service, complete with everything it needs to work.

Assembling a Train: Pods Are Like Trains

When you connect train cars together to form a full train, that’s like creating a Pod.

  • Pods Are Groups: A Pod is a group of one or more containers (train cars) that are linked together and work as a team.
  • Working Together: Just as a locomotive, passenger cars, and cargo cars form a full train to transport people and goods, containers in a Pod work together to run applications.

A Story of Pods in the Train Station:

1. Building a Train

  • Gathering Cars: The station collects a locomotive, passenger cars, and a dining car.
  • Assembling the Train: These cars are connected to form a full train—this is your Pod.
  • Inside the Train (Pod):
    • The locomotive powers the train (like the main application).
    • The passenger cars carry people (supporting services).
    • The dining car provides food (additional features).

2. The Kubernetes Station Master

  • Organizing the Tracks: Kubernetes is like the station master who coordinates all the trains, ensuring they run smoothly and on schedule.
  • Assigning Routes: It directs trains to the right tracks, so they don’t collide and reach their destinations efficiently.
  • Maintaining Trains: If a train car breaks or needs servicing, Kubernetes helps replace or repair it promptly.

Getting into More Details:

Pods Share the Same Journey

  • Shared Destination: Containers in a Pod are like train cars heading to the same place; they share the same journey.
  • Communication: They are connected physically, allowing them to work together seamlessly.

Pods Come and Go

  • Scheduled Trips: Trains (Pods) are assembled when needed and disassembled after completing their trips.
  • Flexibility: Kubernetes ensures that trains are created for different routes as needed.

Multiple Pods in the Station

  • Different Trains, Different Pods: Other trains might be freight trains, bullet trains, or local shuttles—each is a Pod with its own containers.
  • Coordinated Traffic: Kubernetes manages all these trains, preventing delays and ensuring safety.

How Kubernetes Helps Even More:

Scaling Up and Down

  • Rush Hour Solutions: During busy times, more trains are needed to carry all the passengers.
  • Automatic Adjustment: Kubernetes can increase the number of Pods (trains) to handle more traffic and reduce them when it’s quieter.

Self-Healing

  • Fixing Issues: If a train car has a problem, Kubernetes notices and helps replace it quickly or directs passengers to another train.
  • Reliability: This ensures that services continue without significant interruptions.

Rolling Updates

  • Upgrading Trains: When new, faster train cars are available, Kubernetes helps update the trains without stopping the entire service.
  • Seamless Experience: Passengers continue their journeys smoothly while improvements are made.

The Life of a Pod:

  • Creation: A Pod is created when there’s a need for a train to go on a journey.
  • Active Service: The Pod does its job by carrying passengers and goods to their destinations.
  • Completion: After the journey, the Pod is dismantled, and resources are prepared for new journeys.

Advanced Fun:

Pods with Multiple Containers

  • Special Train Services: Some trains might have sleeping cars, observation decks, or entertainment cars.
  • Containers with Different Roles: Similarly, a Pod can have multiple containers, each providing different functionalities within the application.

Networking: The Railway System

  • Connecting Stations: Just as trains travel between stations, Pods communicate over a network to interact with other services.
  • Service Discovery: Kubernetes ensures Pods know how to find and communicate with each other, much like a railway network connects various stations.

Why Pods Matter:

Simplifying Complex Operations

  • Easier Management: Managing a whole train is simpler than coordinating each car individually.
  • Efficiency: Pods group containers, making it easier for Kubernetes to schedule and run applications.

Resource Sharing

  • Shared Tracks and Facilities: Containers in a Pod share resources like tracks, power, and crew.
  • Coordination: This shared environment allows for synchronized operation and resource optimization.

Consistent Environment

  • Uniform Conditions: All train cars in a Pod experience the same journey conditions, ensuring reliability.
  • Predictable Performance: Containers in a Pod operate under the same settings, leading to consistent application behavior.

1. Creating a Pod with a Single Container

You can create a simple Pod running a single container using the kubectl run command.

Example: Creating a Pod Named single-container-pod Running an NGINX Container

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: single-container-pod
  name: single-container-pod
spec:
  containers:
  - image: nginx
    name: nginx-container

Use Imperative command

kubectl run single-container-pod --image=nginx
  • Explanation:
    • kubectl run: Command to create a new resource.
    • single-container-pod: The name of the Pod.
    • --image=nginx: Specifies the container image to run inside the Pod.

2. Creating a Pod with Multiple Containers

Creating a multi-container Pod using imperative commands involves generating a Pod definition YAML file, modifying it to add additional containers, and then applying it.

Steps to Create a Multi-Container Pod:

Step 1: Generate a Pod Definition File

Use the kubectl run command with --dry-run and output the configuration into a YAML file.

kubectl run multi-container-pod --image=nginx --dry-run=client -o yaml > multi-container-pod.yaml
  • Explanation:
    • --dry-run=client: Ensures the command doesn’t create the Pod but simulates the creation.
    • -o yaml: Outputs the configuration in YAML format.
    • > multi-container-pod.yaml: Redirects the output to a file named multi-container-pod.yaml.

Step 2: Edit the YAML File to Add Additional Containers

Open the multi-container-pod.yaml file in a text editor and modify the spec.containers section to include additional containers.

Modified multi-container-pod.yaml Example:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: multi-container-pod
  name: multi-container-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
  - image: redis
    name: redis-container
  • Explanation:
    • Under spec.containers, add a new entry for each additional container.
    • Ensure each container has a unique name and specified image.

Step 3: Apply the YAML File to Create the Pod

kubectl apply -f multi-container-pod.yaml
  • Explanation:
    • kubectl apply: Command to apply a configuration to a resource.
    • -f multi-container-pod.yaml: Specifies the file to apply.

3. Creating Pods Using Inline Imperative Commands

While the kubectl run command has limitations for multi-container Pods, you can use kubectl create with inline YAML definitions.

Example: Creating a Multi-Container Pod Using kubectl create with Inline YAML

kubectl create -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: inline-multi-container-pod
spec:
  containers:
  - name: nginx-container
    image: nginx
  - name: redis-container
    image: redis
EOF
  • Explanation:
    • kubectl create -f - tells Kubernetes to create a resource from the inline YAML provided.
    • <<EOF and EOF are used to define a multi-line string in the shell.
    • The YAML content defines a Pod with two containers.

4. Creating Pods with Imperative Commands Using kubectl run and --overrides

Alternatively, you can use the --overrides flag with kubectl run to specify additional Pod configurations.

Example: Creating a Multi-Container Pod Using kubectl run with Overrides

kubectl run overridden-multi-container-pod --image=nginx \
--overrides='
{
  "apiVersion": "v1",
  "spec": {
    "containers": [
      {
        "name": "nginx-container",
        "image": "nginx"
      },
      {
        "name": "redis-container",
        "image": "redis"
      }
    ]
  }
}' --dry-run=client -o yaml | kubectl apply -f -
  • Explanation:
    • --overrides: Allows you to override the default Pod configuration with a JSON snippet.
    • The command outputs the YAML configuration, which is then piped (|) to kubectl apply -f - to create the Pod.

Note: Using --overrides can be complex and is generally not recommended for creating multi-container Pods due to readability issues.
5. Creating Pods with Environment Variables and Commands

You can include environment variables or command arguments when creating Pods imperatively.

Example: Creating a Pod with Environment Variables

kubectl run env-pod --image=nginx --env="ENV=production"

6. Using kubectl Imperative Commands for Pod ManagementGet Details of a Pod

kubectl describe pod single-container-pod

Provides detailed information about the specified Pod.

List All Pods

kubectl get pods

Lists all Pods in the current namespace.

Delete a Pod

kubectl delete pod single-container-pod

For more information on multi-container Pods, see the Kubernetes Documentation on Pods.

mrcloudbook.com avatar

Ajay Kumar Yegireddi is a DevSecOps Engineer and System Administrator, with a passion for sharing real-world DevSecOps projects and tasks. Mr. Cloud Book, provides hands-on tutorials and practical insights to help others master DevSecOps tools and workflows. Content is designed to bridge the gap between development, security, and operations, making complex concepts easy to understand for both beginners and professionals.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *