Pods, ReplicaSets & Deployments · Part 1414

Demo — Kubernetes Pods with YAMLStep-by-Step Hands-on Guide

vim দিয়ে pod.yaml লেখা, apiVersion, kind, metadata ও spec সাজানো, Labels দেওয়া, তারপর kubectl create -f দিয়ে Minikube-এ NGINX Pod Create করে get ও describe দিয়ে Check করা।

A vim editor with pod.yaml containing an NGINX Pod with app and tier labels, followed by kubectl create -f pod.yaml and the Pod going from ContainerCreating to Running
FIG. 14 vim pod.yaml → :wq → kubectl create -f pod.yaml → ContainerCreating থেকে Running।

আগের Tutorials-এ আমরা শিখেছি Kubernetes Pod কী, YAML কীভাবে কাজ করে এবং Kubernetes Pod Definition File-এর Structure কেমন হয়।

আমরা আরও দেখেছি কীভাবে kubectl run Command ব্যবহার করে একটি Pod Create করতে হয়।

যেমন:

kubectl run nginx --image=nginx

কিন্তু Real Kubernetes Projects-এ সাধারণত YAML Definition Files ব্যবহার করে Resources Create এবং Manage করা হয়।

কারণ YAML Files Save, Reuse, Edit এবং Git Repository-তে Version Control করা যায়।

এই Demo-তে আমরা pod.yaml নামে একটি YAML Definition File তৈরি করব এবং সেই File ব্যবহার করে Minikube Kubernetes Cluster-এর মধ্যে একটি NGINX Pod Create করব।

1. এই Demo-এর মূল উদ্দেশ্য

এই Demo-এর Goal হলো একটি Kubernetes Pod Definition File তৈরি করা।

File Name:

pod.yaml

এই File-এর মধ্যে আমরা Define করব:

  • কোন Kubernetes Object Create করতে চাই
  • Object-এর Name কী হবে
  • Object-এর Labels কী হবে
  • Pod-এর মধ্যে কোন Container Image Run করবে

তারপর Kubernetes-এ Pod Create করব:

kubectl create -f pod.yaml

অথবা:

kubectl apply -f pod.yaml

Complete Workflow

Create pod.yaml
      |
      ▼
Define Kubernetes Pod
      |
      ▼
Save YAML File
      |
      ▼
kubectl create -f pod.yaml
      |
      ▼
Kubernetes API Server
      |
      ▼
Pod Created
      |
      ▼
NGINX Container Running

2. YAML File কেন ব্যবহার করব?

আগের Tutorial-এ আমরা একটি NGINX Pod Create করেছি।

kubectl run nginx --image=nginx

এই Command Quick Testing-এর জন্য Useful।

কিন্তু এখানে Application-এর Configuration একটি Reusable YAML File হিসেবে Save করা হচ্ছে না।

YAML ব্যবহার করলে আমরা:

  • Configuration Save করতে পারি।
  • একই Configuration বারবার Reuse করতে পারি।
  • Configuration পরে Edit করতে পারি।
  • Git Repository-তে Store করতে পারি।
  • Team Members-এর সঙ্গে Share করতে পারি।
  • Real Kubernetes Deployments-এ ব্যবহার করতে পারি।

Imperative vs YAML Approach

Imperative Approach
        |
        ▼
kubectl run nginx --image=nginx
        |
        ▼
Pod Created
YAML Approach
        |
        ▼
     pod.yaml
        |
        ▼
kubectl create/apply
        |
        ▼
     Pod Created

Real Kubernetes Projects-এর জন্য YAML-based Declarative Configuration সাধারণত বেশি Suitable।

3. YAML File Create করা

এই Demo-তে Linux Environment ব্যবহার করে YAML File Create করা হচ্ছে।

Linux Terminal-এ vim Editor ব্যবহার করা যায়।

Command:

vim pod.yaml

এটি pod.yaml নামে একটি File Open করবে।

File আগে থেকে না থাকলে Vim-এর মাধ্যমে নতুন File Create করা যাবে।

Windows-এ কোন Editor ব্যবহার করা যায়?

Windows ব্যবহার করলে:

  • Notepad
  • Notepad++
  • Visual Studio Code (VS Code)

ব্যবহার করা যায়।

YAML Support আছে এমন Editor ব্যবহার করা ভালো, কারণ এতে Indentation Mistakes সহজে Identify করা যায়।

Vim Editor-এ File Edit করা

vim pod.yaml

Vim Open হলে i Press করে Insert Mode-এ যাও।

এবার YAML Configuration লিখতে পারবে।

4. Kubernetes YAML-এর Four Root-Level Fields

Kubernetes Pod Definition File লেখার সময় চারটি Main Root-Level Fields ব্যবহার করা হয়।

apiVersion:
kind:
metadata:
spec:

এগুলোকে Root-Level Properties বলা হয়।

Pod Definition-এর জন্য:

apiVersion: v1
kind: Pod
metadata:
spec:

এখানে:

FieldMeaning
apiVersionKubernetes API Version
kindKubernetes Object Type
metadataObject-এর Name এবং Labels
specObject-এর Desired Configuration

kind Field Case-Sensitive

Kubernetes Resource Type লেখার সময় Capitalization গুরুত্বপূর্ণ।

Correct:

kind: Pod

Incorrect:

kind: pod

এখানে Pod-এর P Capital হতে হবে।

5. metadata Define করা

metadata Field-এর মধ্যে Pod-এর Information Define করা হয়।

Example:

metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend

এখানে:

name: nginx

Pod-এর Name হবে nginx।

app: nginx

Pod-এর একটি Label হবে app=nginx।

tier: frontend

আরেকটি Label হবে tier=frontend।

Structure

metadata
    |
    ├── name: nginx
    |
    └── labels
          |
          ├── app: nginx
          |
          └── tier: frontend

Labels কেন গুরুত্বপূর্ণ?

Labels ব্যবহার করে Kubernetes Objects Group এবং Select করা যায়।

ধরো, তোমার Application-এর দুটি Layers রয়েছে।

E-Commerce Application
         |
    +----+----+
    |         |
    ▼         ▼
Frontend    Backend
  Pods       Pods

Frontend Pods-এর জন্য:

labels:
  tier: frontend

Backend Pods-এর জন্য:

labels:
  tier: backend

এভাবে Pods-এর Responsibilities অনুযায়ী Labels Define করা যায়।

পরবর্তীতে Kubernetes Services এবং Selectors ব্যবহার করার সময় এই Labels গুরুত্বপূর্ণ হবে।

6. YAML Indentation বোঝা

YAML-এর সবচেয়ে গুরুত্বপূর্ণ Rules-এর একটি হলো Indentation।

Indentation ব্যবহার করে Parent-Child Relationship Define করা হয়।

Correct Example

metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend

এখানে:

  • name এবং labels হলো metadata-এর Children।
  • app এবং tier হলো labels-এর Children।

Indentation Structure

metadata
   |
   ├── name
   |
   └── labels
          |
          ├── app
          |
          └── tier

Important Rules

YAML Indentation-এর জন্য Spaces ব্যবহার করতে হবে, Tabs নয়।

Common Practice হলো প্রতি Indentation Level-এ 2 Spaces ব্যবহার করা।

Incorrect Example

metadata:
  name: nginx
    labels:
      app: nginx

এখানে labels ভুলভাবে name-এর নিচে Nested করা হয়েছে।

কিন্তু name ইতিমধ্যে একটি Direct Value পেয়েছে:

name: nginx

তাই এর নিচে Nested Properties রাখা যাবে না।

7. spec-এর মধ্যে Container Define করা

spec Field-এর মধ্যে Pod-এর Desired Configuration Define করা হয়।

আমাদের Demo-তে Pod-এর মধ্যে একটি NGINX Container Run করব।

Example:

spec:
  containers:
    - name: nginx
      image: nginx

এখানে:

FieldMeaning
specPod Specification
containersContainers-এর List
-List-এর একটি Item
nameContainer Name
imageContainer Image

Structure

spec
 |
 └── containers
         |
         └── Container 1
                |
                ├── name: nginx
                |
                └── image: nginx

containers কেন List?

কারণ একটি Pod-এর মধ্যে Multiple Containers থাকতে পারে।

তাই containers Field একটি List।

containers:
  - name: nginx
    image: nginx

এখানে Dash (-) List-এর একটি Item Indicate করে।

যদিও আমাদের Pod-এর মধ্যে একটি Container রয়েছে, Kubernetes এখনও containers-কে List হিসেবে Expect করে।

8. Complete Kubernetes Pod YAML File

এবার আমাদের Complete pod.yaml File তৈরি করি।

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend
spec:
  containers:
    - name: nginx
      image: nginx

এই YAML Configuration Kubernetes-কে বলছে:

nginx নামে একটি Pod Create করো। Pod-এর Labels হবে app=nginx এবং tier=frontend। Pod-এর মধ্যে nginx Image ব্যবহার করে একটি Container Run করো।

Complete Structure

Kubernetes Pod YAML
         |
         ├── apiVersion: v1
         |
         ├── kind: Pod
         |
         ├── metadata
         |      |
         |      ├── name: nginx
         |      |
         |      └── labels
         |             |
         |             ├── app: nginx
         |             |
         |             └── tier: frontend
         |
         └── spec
                |
                └── containers
                       |
                       └── Container
                              |
                              ├── name: nginx
                              |
                              └── image: nginx

9. Container Image অন্য Registry-তে থাকলে কী হবে?

আমাদের Example-এ Container Image:

image: nginx

এটি সাধারণত Docker Hub-এর Official NGINX Image ব্যবহার করে।

কিন্তু যদি Image অন্য Registry-তে Store করা থাকে, তাহলে Full Registry Path ব্যবহার করতে হবে।

Private Company Registry Example

image: myregistry.example.com/myteam/myapp:v1

GitHub Container Registry Example

image: ghcr.io/myuser/myapp:v1

Docker Hub Official Image

image: nginx

Important: Private Registry ব্যবহার করলে Kubernetes-এর Image Pull Credentials Configure করার প্রয়োজন হতে পারে।

10. একটি Pod-এর মধ্যে Multiple Containers রাখা যায়?

হ্যাঁ।

containers Field List হওয়ার কারণই হলো একটি Pod-এর মধ্যে Multiple Containers Define করা যায়।

Example:

spec:
  containers:
    - name: nginx
      image: nginx

    - name: busybox
      image: busybox

এখানে দুটি Container Definitions রয়েছে।

Pod
 |
 ├── NGINX Container
 |
 └── BusyBox Container

তবে এই Demo-তে আমরা শুধু একটি Container ব্যবহার করব।

কারণ Beginner Level-এ Single-Container Pod বোঝা সহজ।

Note: এই Multi-Container Example শুধু YAML Structure বোঝানোর জন্য। বাস্তবে busybox Container-কে Running রাখতে উপযুক্ত Command বা Process Configure করতে হতে পারে।

11. YAML File Save এবং Check করা

YAML Configuration লেখা শেষ হলে File Save করতে হবে।

এই Demo-তে Vim Editor ব্যবহার করা হয়েছে।

Vim-এ Save and Exit

প্রথমে:

Esc

Press করো।

তারপর:

:wq

লিখে Enter Press করো।

এখানে:

  • w → Write বা Save
  • q → Quit

File Content Check করা

Terminal-এ:

cat pod.yaml

Expected Output:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend
spec:
  containers:
    - name: nginx
      image: nginx

এই Command দিয়ে Confirm করা যায় YAML File-এর Content ঠিকভাবে Save হয়েছে।

12. YAML File দিয়ে Pod Create করা

এখন আমাদের YAML File Ready।

File Name:

pod.yaml

Kubernetes-এ Pod Create করার জন্য:

kubectl create -f pod.yaml

Expected Output:

pod/nginx created

এই Command YAML File Read করে Kubernetes API Server-এ Pod Create করার Request পাঠায়।

kubectl apply ব্যবহার করা

Alternative হিসেবে:

kubectl apply -f pod.yaml

ব্যবহার করা যায়।

একটি নতুন Object-এর ক্ষেত্রে দুটি Commands-ই Resource Create করতে পারে।

kubectl create vs kubectl apply

CommandBasic Meaning
kubectl create -f pod.yamlYAML File থেকে নতুন Object Create করা
kubectl apply -f pod.yamlYAML Configuration অনুযায়ী Object Create বা Update করা

Real Kubernetes Projects-এ kubectl apply Commonly Used হয়।

কারণ YAML File Edit করার পর Configuration আবার Apply করা যায়।

তবে Pod-এর সব Fields Update করা যায় না। কিছু Changes-এর জন্য Pod Recreate করার প্রয়োজন হতে পারে।

এই Demo-তে মূল Command হলো:

kubectl create -f pod.yaml

13. Pod Status Check করা

YAML File দিয়ে Pod Create করার পর Check করো:

kubectl get pods

Pod Create হওয়ার প্রথম দিকে Output হতে পারে:

NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          5s

এখানে Kubernetes Container Start করার Preparation করছে।

কিছু সময় পর:

NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          20s

এখানে:

  • NAME → Pod Name
  • READY → Ready Containers / Total Containers
  • STATUS → Current Pod Status
  • RESTARTS → Container Restart Count
  • AGE → Pod Create হওয়ার পর সময়

ContainerCreating

এই Status দেখালে Kubernetes হয়তো:

  • Container Image Pull করছে।
  • Container Create করছে।
  • Networking Setup করছে।
  • Volumes Mount করছে।

Running

Running Status মানে Pod Running Phase-এ রয়েছে।

আর READY 1/1 দেখালে NGINX Container Ready রয়েছে।

14. kubectl describe দিয়ে Pod Inspect করা

Pod সম্পর্কে Detailed Information দেখতে:

kubectl describe pod nginx

এই Command Pod-এর অনেক Information দেখাবে।

যেমন:

  • Pod Name
  • Labels
  • Node Assignment
  • Container Details
  • Container Image
  • Container Status
  • Pod Events
  • Errors, যদি থাকে

Example Information

Name:         nginx
Namespace:    default
Labels:       app=nginx
              tier=frontend
Node:         minikube/192.168.49.2
Status:       Running

Containers:
  nginx:
    Image:    nginx
    State:    Running

Actual Output-এ আরও অনেক Details থাকতে পারে।

Events Section

kubectl describe Command-এর Events Section-এ Pod-এর Lifecycle-related Events দেখা যায়।

Example:

Pod Scheduled
      |
      ▼
Image Pulled
      |
      ▼
Container Created
      |
      ▼
Container Started

Pod Running না হলে Events Section Troubleshooting-এর জন্য Useful।

15. এই Demo-এর Important Commands

CommandPurpose
vim pod.yamlYAML File Create অথবা Edit করা
cat pod.yamlYAML File-এর Content দেখা
kubectl create -f pod.yamlYAML File থেকে Pod Create করা
kubectl apply -f pod.yamlYAML Configuration Create অথবা Update করা
kubectl get podsPod Status Check করা
kubectl describe pod nginxDetailed Pod Information দেখা

16. Complete Hands-on Demo — All Commands

এই Tutorial-এর Commands একসঙ্গে:

# 1. Check Kubernetes Cluster
kubectl get nodes

# 2. Create YAML File
vim pod.yaml

# 3. Check YAML File
cat pod.yaml

# 4. Create Pod from YAML
kubectl create -f pod.yaml

# 5. Check Pod Status
kubectl get pods

# 6. Inspect Pod Details
kubectl describe pod nginx

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend
spec:
  containers:
    - name: nginx
      image: nginx

এই Commands এবং YAML File ব্যবহার করে একটি Basic Kubernetes NGINX Pod Create ও Inspect করা যায়।

17. Simple Mental Model — YAML File কীভাবে কাজ করে?

Kubernetes YAML File-কে একটি Instruction বা Configuration Document হিসেবে ভাবতে পারো।

Developer
    |
    ▼
  pod.yaml
    |
    ▼
kubectl create -f pod.yaml
    |
    ▼
Kubernetes API Server
    |
    ▼
  Pod Created
    |
    ▼
NGINX Container
    |
    ▼
    Running

এখানে YAML File Kubernetes-কে বলে কী Create করতে হবে।

Four Main Fields

apiVersion
    |
    └── Which Kubernetes API Version?

kind
    |
    └── Which Object Type?

metadata
    |
    └── Name and Labels?

spec
    |
    └── Which Container Image Should Run?

Pod-এর ক্ষেত্রে:

kind: Pod

মানে আমরা একটি Pod Create করতে চাই।

আর:

spec:
  containers:
    - name: nginx
      image: nginx

মানে আমরা সেই Pod-এর মধ্যে একটি NGINX Container Run করতে চাই।

18. Final Takeaway — Kubernetes Pods with YAML

এই Demo-তে আমরা শিখলাম কীভাবে একটি YAML Definition File ব্যবহার করে Kubernetes Pod Create করতে হয়।

প্রথমে pod.yaml নামে একটি File তৈরি করতে হবে।

এর মধ্যে চারটি Main Fields Define করতে হবে:

apiVersion:
kind:
metadata:
spec:

তারপর metadata-এর মধ্যে Pod Name এবং Labels Define করতে হবে।

spec.containers-এর মধ্যে Container Name এবং Image Define করতে হবে।

Complete Example:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend
spec:
  containers:
    - name: nginx
      image: nginx

তারপর Pod Create করতে:

kubectl create -f pod.yaml

অথবা:

kubectl apply -f pod.yaml

Pod Running আছে কি না Check করতে:

kubectl get pods

আর Detailed Information দেখতে:

kubectl describe pod nginx

Final Memory Rule

Write YAML File
       |
       ▼
Define Pod Configuration
       |
       ▼
Save pod.yaml
       |
       ▼
kubectl create -f pod.yaml
       |
       ▼
kubectl get pods
       |
       ▼
kubectl describe pod nginx

Final One-Line Summary:

Kubernetes-এ YAML File দিয়ে Pod Create করার জন্য apiVersion, kind, metadata এবং spec Define করতে হয়। এরপর spec.containers-এর মধ্যে Container Image Configure করে kubectl create -f pod.yaml অথবা kubectl apply -f pod.yaml Command ব্যবহার করে Pod Create করা যায়।

Official Documentation

মূল বিষয়: YAML File-এর মাধ্যমে Kubernetes Resources-এর Configuration Save করা যায় এবং সেই Configuration Reuse, Edit, Share ও Version Control করা যায়। তাই Real Kubernetes Projects-এ YAML-based Resource Management একটি গুরুত্বপূর্ণ Practice।

Was this guide helpful?

Search the archive

Press Esc to close