πŸ“ Crafting Kubernetes YAML Manifests – A Beginner’s Blueprint (Day 22)⚑

πŸ“œ Writing Kubernetes YAML Manifests - A Beginner-Friendly Guide πŸš€

Welcome to Day 22 of your Kubernetes journey! 🌟 Today, we’ll explore Kubernetes YAML Manifests, which are used to define and deploy applications inside a Kubernetes cluster.

Think of Kubernetes YAML files as blueprints πŸ—οΈ that tell Kubernetes what to build and how to manage itβ€”just like an architect’s design for a building. Let’s break it down step by step in a simple and structured way! πŸ“‘


πŸ› οΈ What is a Kubernetes YAML Manifest?

A Kubernetes Manifest is a configuration file written in YAML (Yet Another Markup Language) that defines the desired state of a Kubernetes object. Instead of manually creating resources using commands, you write a YAML file and apply it to Kubernetes.

πŸ”₯ Why Use YAML in Kubernetes?

βœ… Human-Readable – Easy to understand for both humans & machines πŸ§‘β€πŸ’»
βœ… Declarative – You describe what you want, and Kubernetes figures out how to do it πŸš€
βœ… Reusable & Portable – Easily share configurations with teams 🌎
βœ… Version Controlled – Store YAML files in Git for collaboration & tracking πŸ“œ


πŸ“„ Basic Structure of a Kubernetes YAML File

A Kubernetes YAML file typically follows this structure:

apiVersion: v1  # API version for the resource
kind: Pod       # Type of resource (Pod, Deployment, Service, etc.)
metadata:
  name: my-pod  # Name of the resource
spec:
  containers:
    - name: my-container
      image: nginx  # Docker image
      ports:
        - containerPort: 80

πŸ“Œ Explanation:

  • apiVersion: Specifies the API version for the Kubernetes object

  • kind: Defines what type of resource this file creates (Pod, Deployment, Service, etc.)

  • metadata: Contains resource details like its name & labels

  • spec: The actual specification of the resource (like what container to run)

πŸ“Œ Analogy:
Think of this as an online food order πŸ•:

  • apiVersion = The version of the food delivery system πŸ“¦

  • kind = The type of order (pizza, burger, sushi) 🍣

  • metadata = Customer details (name, address, phone) πŸ“ž

  • spec = Order details (which food, how much, what toppings) πŸ„


πŸ”Ή Common Kubernetes Resources & Their YAML Manifests

1️⃣ Pod Manifest – The Smallest Deployable Unit

A Pod is the basic unit in Kubernetesβ€”it contains one or more containers that run your application.

apiVersion: v1
kind: Pod
metadata:
  name: web-pod
spec:
  containers:
    - name: web-container
      image: nginx
      ports:
        - containerPort: 80

πŸ“Œ Analogy:
A Pod is like a food truck πŸš›β€”it has everything needed inside (kitchen, ingredients, staff) and can be placed at any location (node) to serve customers (users).


2️⃣ Deployment Manifest – Managing Multiple Pods

A Deployment ensures that a specific number of identical Pods are running at all times. If a Pod crashes, Kubernetes will restart it automatically.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 3  # Runs 3 replicas of the Pod
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-container
          image: nginx
          ports:
            - containerPort: 80

πŸ“Œ Analogy:
A Deployment is like a restaurant chain πŸ½οΈβ€”if one branch (Pod) shuts down, another is ready to take over!


3️⃣ Service Manifest – Exposing Pods to the Network

A Service provides a stable network endpoint for accessing a set of Pods. Without it, Pods would be unreachable from the outside world! 🌍

apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP  # Internal service within the cluster

πŸ“Œ Analogy:
A Service is like a waiter πŸ§‘β€πŸ³ in a restaurantβ€”it ensures that customers (users) get the right food (requests go to the correct Pod).


πŸš€ Additional Kubernetes Resources You Should Know

πŸ—„ ConfigMap – Managing Configuration Separately

Instead of hardcoding configuration inside Pods, store it in a ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database_url: "mysql://db-service:3306"

πŸ“Œ Analogy:
A ConfigMap is like a menu card πŸ“œ in a restaurantβ€”you can change it anytime without modifying the kitchen setup (Pods).


πŸ”‘ Secret – Storing Sensitive Information Securely

Secrets are like ConfigMaps but used for storing passwords, API keys, etc.

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  password: cGFzc3dvcmQ=  # Base64-encoded value

πŸ“Œ Analogy:
A Secret is like a restaurant’s special recipe πŸ²β€”only authorized chefs (services) should have access to it!


βœ… Best Practices for Writing Kubernetes YAML Manifests

πŸ“Œ Follow these tips to avoid common pitfalls:

βœ”οΈ Use meaningful names for resources πŸ“›
βœ”οΈ Keep YAML well-indented to avoid syntax errors πŸ“
βœ”οΈ Use labels & selectors for efficient resource management 🏷️
βœ”οΈ Avoid hardcoding valuesβ€”use ConfigMaps & Secrets πŸ”‘
βœ”οΈ Use Helm Charts 🎩 for complex applications
βœ”οΈ Store YAML files in Git for version control and rollback πŸ”„


🎯 Summary – Key Takeaways

πŸ”Ή YAML Manifests define the desired state of Kubernetes objects
πŸ”Ή Pods are the smallest deployable units (like food trucks πŸš›)
πŸ”Ή Deployments manage multiple Pods (like restaurant chains 🍽️)
πŸ”Ή Services expose Pods (like waiters serving food 🍜)
πŸ”Ή ConfigMaps & Secrets help separate configuration from code


🏁 Next Steps: Try It Yourself!

πŸ’‘ Hands-On Practice:
1️⃣ Create a Pod YAML and deploy it
2️⃣ Convert it into a Deployment for better scalability
3️⃣ Expose it using a Service

πŸš€ Experiment, break things, and learn!

Got questions? Drop them in the comments below! πŸ—¨οΈπŸ’¬

0
Subscribe to my newsletter

Read articles from SRITESH SURANJAN directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SRITESH SURANJAN
SRITESH SURANJAN

πŸš€ Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.