Kubernetes শেখার সময় একটি গুরুত্বপূর্ণ বিষয় হলো YAML।
আগের Tutorials-এ আমরা দেখেছি কীভাবে kubectl Commands ব্যবহার করে Pods এবং Deployments Create করতে হয়।
যেমন:
kubectl run nginx --image=nginx
কিন্তু Real Kubernetes Environment-এ আমরা সবসময় Terminal-এ Commands লিখে Resources Create করি না।
বরং YAML Files ব্যবহার করে Kubernetes Resources-এর Configuration Define করি।
যেমন:
- Pods
- Deployments
- Services
- ConfigMaps
- Secrets
- Volumes
YAML হলো একটি Human-Readable Data Serialization Format, যার মাধ্যমে Structured Configuration Data লেখা যায়।
এই Tutorial-এ আমরা YAML-এর Basic Syntax, Key-Value Pairs, Lists, Dictionaries, Indentation এবং Nested Structures শিখব।
এরপর দেখব Kubernetes কীভাবে YAML Files ব্যবহার করে Resources Create করে।
1. YAML কী?
YAML হলো একটি Data Representation বা Serialization Format।
YAML-এর মাধ্যমে Structured Data সহজে লেখা এবং পড়া যায়।
YAML-এর পূর্ণরূপ:
YAML Ain't Markup Language
YAML অনেকটা JSON এবং XML-এর মতো কাজ করে।
তবে YAML সাধারণত Humans-এর জন্য Read এবং Write করা সহজ।
Example YAML
fruit: apple
vegetable: carrot
liquid: water
meat: chicken
এখানে আমরা চারটি Key-Value Pairs Define করেছি।
Structure
Key Value
| |
▼ ▼
fruit: apple
vegetable: carrot
liquid: water
meat: chicken
এই Structure-কে Key-Value Mapping বলা হয়।
YAML-এর সবচেয়ে Basic Concept হলো Key এবং Value।
2. Key-Value Pairs কী?
YAML-এর সবচেয়ে Simple Structure হলো একটি Key-Value Pair।
Example:
fruit: apple
এখানে:
fruitহলো Key।appleহলো Value।
এর General Syntax:
key: value
আরেকটি Example:
fruit: apple
vegetable: carrot
liquid: water
meat: chicken
এখানে প্রতিটি Line একটি Key-Value Pair।
Colon-এর পরে Space কেন গুরুত্বপূর্ণ?
YAML-এ Key এবং Value আলাদা করার জন্য সাধারণত Colon (:)-এর পরে Space ব্যবহার করতে হয়।
Correct:
fruit: apple
Incorrect Mapping Syntax:
fruit:apple
দ্বিতীয় Example-টি YAML Parser-এর কাছে Mapping-এর পরিবর্তে Plain String হিসেবে Interpret হতে পারে।
তাই Key-Value Mapping লেখার সময় সবসময় Colon-এর পরে Space ব্যবহার করো।
3. YAML Lists / Arrays
ধরো, তোমার কাছে একটি Fruit Category আছে।
এই Category-এর মধ্যে Multiple Fruits রাখতে চাও।
যেমন:
- Apple
- Banana
- Orange
এক্ষেত্রে YAML List ব্যবহার করা যায়।
Example
fruits:
- apple
- banana
- orange
এখানে fruits হলো Key।
আর তার Value হলো একটি List।
Structure
fruits
|
▼
List
|
├── apple
├── banana
└── orange
YAML-এ List Items লেখার জন্য Dash (-) ব্যবহার করা হয়।
আরেকটি Example
vegetables:
- carrot
- broccoli
- spinach
এখানে vegetables-এর মধ্যে তিনটি Values রয়েছে।
Simple Rule: একটি Category-এর মধ্যে Multiple Values রাখতে চাইলে List ব্যবহার করা হয়।
4. YAML Dictionaries / Maps
এবার ধরো, তুমি একটি Banana সম্পর্কে Information Store করতে চাও।
Banana-এর কিছু Properties রয়েছে:
- Calories
- Fat
- Carbs
এক্ষেত্রে Dictionary বা Map ব্যবহার করা হয়।
Example
banana:
calories: 105
fat: 0.4
carbs: 27
এখানে banana হলো একটি Object-এর মতো Mapping।
এর মধ্যে তিনটি Properties রয়েছে।
Structure
banana
|
├── calories: 105
|
├── fat: 0.4
|
└── carbs: 27
এখানে:
| Key | Value |
|---|---|
| calories | 105 |
| fat | 0.4 |
| carbs | 27 |
Simple Rule: একটি Object-এর Multiple Properties Describe করতে চাইলে Dictionary বা Map ব্যবহার করা হয়।
5. YAML Indentation কেন গুরুত্বপূর্ণ?
YAML-এর সবচেয়ে গুরুত্বপূর্ণ Rules-এর একটি হলো Indentation।
YAML Structure বোঝানোর জন্য Spaces ব্যবহার করে।
Python Programming-এর মতো YAML-এও Indentation Structure-এর Meaning Determine করে।
Correct Example
banana:
calories: 105
fat: 0.4
carbs: 27
এখানে calories, fat এবং carbs একই Indentation Level-এ রয়েছে।
তাই তিনটি Properties-ই banana Dictionary-এর অংশ।
Incorrect Example
banana:
calories: 105
fat: 0.4
carbs: 27
এখানে fat এবং carbs ভুলভাবে Indented হয়েছে।
কারণ calories ইতিমধ্যে একটি Direct Value পেয়েছে:
calories: 105
এখন সেই Scalar Value-এর নিচে Nested Properties রাখা যায় না।
সহজভাবে বোঝার জন্য
Correct:
banana
|
├── calories
├── fat
└── carbs
Incorrect:
banana
|
└── calories: 105
|
├── fat
└── carbs
এখানে calories একটি Number, Nested Dictionary নয়।
Important: YAML-এ Indentation পরিবর্তন করলে Data Structure-এর Meaning পরিবর্তিত হতে পারে অথবা Syntax Error তৈরি হতে পারে।
6. YAML-এ Spaces ব্যবহার করতে হয়, Tabs নয়
YAML Indentation-এর জন্য Spaces ব্যবহার করা হয়।
Tabs দিয়ে Indentation করা উচিত নয়।
Correct Example
car:
color: blue
model: mustang
এখানে প্রতিটি Nested Property-এর আগে দুটি Spaces রয়েছে।
সাধারণত YAML Files-এ প্রতি Indentation Level-এ 2 Spaces ব্যবহার করা হয়।
তবে YAML-এ সবসময় Exactly 2 Spaces ব্যবহার করা বাধ্যতামূলক নয়।
মূল বিষয় হলো Indentation Consistent থাকতে হবে।
Example
car:
color: blue
model: mustang
transmission: automatic
এখানে সব Properties একই Level-এ রয়েছে।
Kubernetes Best Practice
Kubernetes YAML Files লেখার সময় 2 Spaces Indentation ব্যবহার করা Common।
এর ফলে Nested Resources পড়া সহজ হয়।
7. Dictionary-এর মধ্যে Dictionary
YAML-এ একটি Dictionary-এর মধ্যে আরেকটি Dictionary রাখা যায়।
এটাকে Nested Dictionary বলা হয়।
ধরো, আমরা একটি Car-এর Information Store করতে চাই।
Car-এর Properties:
- Color
- Model
- Transmission
- Price
কিন্তু Model-এরও আবার Name এবং Year রয়েছে।
Example
car:
color: blue
model:
name: mustang
year: 2020
transmission: automatic
price: 25000
এখানে:
carহলো Main Dictionary।modelহলোcarDictionary-এর মধ্যে আরেকটি Dictionary।nameএবংyearহলোmodelDictionary-এর Properties।
Structure
car
|
├── color: blue
|
├── model
| |
| ├── name: mustang
| |
| └── year: 2020
|
├── transmission: automatic
|
└── price: 25000
এভাবে একটি Object-এর কোনো Property সম্পর্কে আরও Detailed Information রাখা যায়।
Kubernetes YAML Files-এ এই Nested Structure খুব Common।
8. List of Strings
ধরো, তুমি শুধু কয়েকটি Car-এর Names Store করতে চাও।
এক্ষেত্রে List of Strings ব্যবহার করলেই যথেষ্ট।
Example
cars:
- blue mustang
- red corvette
- black tesla
এখানে cars একটি List।
প্রতিটি Item হলো একটি String।
Structure
cars
|
├── blue mustang
|
├── red corvette
|
└── black tesla
এখানে প্রতিটি Car সম্পর্কে Additional Properties নেই।
শুধু String Values রয়েছে।
List of Strings ব্যবহার করা হয় যখন প্রতিটি Item Simple Value হয়।
9. List of Dictionaries
এবার ধরো, আমরা Multiple Cars Store করতে চাই।
কিন্তু প্রতিটি Car-এর আলাদা Properties রয়েছে।
যেমন:
- Color
- Model
- Transmission
- Price
এক্ষেত্রে List of Dictionaries ব্যবহার করা হয়।
Example
cars:
- color: blue
model: mustang
transmission: automatic
price: 25000
- color: red
model: corvette
transmission: manual
price: 55000
এখানে cars একটি List।
কিন্তু List-এর প্রতিটি Item একটি Dictionary।
Structure
cars
|
├── Car 1
| |
| ├── color: blue
| ├── model: mustang
| ├── transmission: automatic
| └── price: 25000
|
└── Car 2
|
├── color: red
├── model: corvette
├── transmission: manual
└── price: 55000
এই Structure Kubernetes-এ খুব Common।
কারণ Kubernetes YAML-এ অনেক সময় Multiple Containers, Ports অথবা Volumes Define করতে হয়।
10. List → Dictionary → Nested Dictionary → List
YAML-এর মাধ্যমে আরও Complex Data Structures Represent করা যায়।
ধরো, আমাদের Multiple Fruits রয়েছে।
প্রতিটি Fruit-এর:
- Name
- Nutrition Information
- Vitamins List
Store করতে হবে।
Example
fruits:
- name: banana
nutrition:
calories: 105
fat: 0.4
carbs: 27
vitamins:
- vitamin B6
- vitamin C
- name: grape
nutrition:
calories: 62
fat: 0.3
carbs: 16
vitamins:
- vitamin C
- vitamin K
এই YAML Structure-এ আমরা একাধিক Data Types ব্যবহার করেছি।
Step-by-Step Breakdown
Step 1: fruits একটি List।
Step 2: List-এর প্রতিটি Item একটি Dictionary।
Step 3: প্রতিটি Dictionary-এর মধ্যে nutrition নামে Nested Dictionary রয়েছে।
Step 4: প্রতিটি Dictionary-এর মধ্যে vitamins নামে একটি List রয়েছে।
Complete Structure
fruits (List)
|
├── Fruit 1 (Dictionary)
| |
| ├── name: banana
| |
| ├── nutrition (Dictionary)
| | |
| | ├── calories: 105
| | ├── fat: 0.4
| | └── carbs: 27
| |
| └── vitamins (List)
| |
| ├── vitamin B6
| └── vitamin C
|
└── Fruit 2 (Dictionary)
|
├── name: grape
|
├── nutrition (Dictionary)
| |
| ├── calories: 62
| ├── fat: 0.3
| └── carbs: 16
|
└── vitamins (List)
|
├── vitamin C
└── vitamin K
এই ধরনের Nested Structure Kubernetes Manifests-এ ব্যবহার করা হয়।
কারণ Kubernetes Resources-এর Configuration অনেক সময় Complex এবং Hierarchical হয়।
11. কখন Dictionary আর কখন List ব্যবহার করব?
এটি YAML-এর সবচেয়ে গুরুত্বপূর্ণ Conceptual Part।
একটি Object Describe করতে Dictionary ব্যবহার করো
Example:
car:
color: blue
model: mustang
price: 25000
এখানে একটি Car-এর Multiple Properties Describe করা হচ্ছে।
তাই Dictionary ব্যবহার করা হয়েছে।
Multiple Items Store করতে List ব্যবহার করো
Example:
cars:
- mustang
- corvette
- tesla
এখানে Multiple Cars-এর Names Store করা হয়েছে।
তাই List ব্যবহার করা হয়েছে।
Multiple Objects with Properties হলে List of Dictionaries
Example:
cars:
- color: blue
model: mustang
price: 25000
- color: red
model: corvette
price: 55000
এখানে Multiple Cars রয়েছে এবং প্রতিটি Car-এর নিজস্ব Properties রয়েছে।
তাই List of Dictionaries ব্যবহার করা হয়েছে।
Quick Comparison
| Requirement | YAML Structure |
|---|---|
| Single Key এবং Value | Key-Value Pair |
| One Object with Properties | Dictionary / Map |
| Multiple Simple Values | List |
| Multiple Objects with Properties | List of Dictionaries |
| Complex Nested Data | Nested Lists এবং Dictionaries |
12. Dictionary Order vs List Order
YAML-এ Dictionary এবং List-এর মধ্যে একটি গুরুত্বপূর্ণ Difference রয়েছে।
Dictionary Order সাধারণত Matter করে না
Example 1:
banana:
calories: 105
fat: 0.4
carbs: 27
Example 2:
banana:
carbs: 27
calories: 105
fat: 0.4
এই দুটি Examples-এর Logical Data একই।
কারণ Dictionary বা Mapping-এর ক্ষেত্রে Properties সাধারণত Key দিয়ে Identify করা হয়, Position দিয়ে নয়।
অর্থাৎ:
calories → 105
fat → 0.4
carbs → 27
Properties-এর Order Change হলেও Mapping-এর Meaning একই থাকে।
List Order Matter করে
Example 1:
fruits:
- apple
- banana
Example 2:
fruits:
- banana
- apple
এই দুটি Lists-এর Items একই হলেও Order আলাদা।
প্রথম List:
1. apple
2. banana
দ্বিতীয় List:
1. banana
2. apple
List হলো Ordered Collection।
তাই List-এর Item Order গুরুত্বপূর্ণ হতে পারে।
Kubernetes-এ কোনো Specific List-এর Order Operationally Important কি না, সেটা সেই Kubernetes Field-এর Semantics-এর ওপর নির্ভর করে।
13. YAML Comments
YAML-এ Comments লেখার জন্য Hash Symbol (#) ব্যবহার করা হয়।
Example:
# This is a comment
fruit: apple
YAML Parser Comments-কে Configuration Data হিসেবে Process করে না।
Comments ব্যবহার করা হয় Configuration-এর কোনো অংশ Explain করার জন্য।
Example
# Application Configuration
app:
name: my-app
# Application Port
port: 8080
# Environment
environment: production
এখানে Comments ব্যবহার করে Configuration-এর Purpose Explain করা হয়েছে।
Kubernetes YAML Files-এ Comments Documentation এবং Readability Improve করতে পারে।
14. Kubernetes-এ YAML কেন গুরুত্বপূর্ণ?
Kubernetes YAML Files ব্যবহার করে Resources-এর Desired State Define করতে পারে।
ধরো, তুমি Kubernetes-এ একটি NGINX Pod Create করতে চাও।
আগের Tutorial-এ আমরা Command ব্যবহার করেছি:
kubectl run nginx --image=nginx
কিন্তু একই Pod YAML Manifest দিয়েও Create করা যায়।
Kubernetes Pod YAML Example
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
এখানে YAML File Kubernetes-কে বলছে:
nginxনামে একটি Pod Create করো এবং তার মধ্যেnginxContainer Image ব্যবহার করে একটি Container Run করো।
YAML Structure Breakdown
| Field | Meaning |
|---|---|
apiVersion | Kubernetes API Version |
kind | কোন Kubernetes Resource Create হবে |
metadata | Resource-এর Information |
name | Resource-এর Name |
spec | Resource-এর Desired Configuration |
containers | Pod-এর Container Definitions |
image | Container Image |
Structure Visualization
Kubernetes YAML
|
├── apiVersion: v1
|
├── kind: Pod
|
├── metadata
| |
| └── name: nginx
|
└── spec
|
└── containers (List)
|
└── Container (Dictionary)
|
├── name: nginx
|
└── image: nginx
এখানে containers একটি List।
List-এর মধ্যে একটি Container Dictionary রয়েছে।
এই কারণেই Kubernetes শেখার আগে YAML Lists এবং Dictionaries বোঝা গুরুত্বপূর্ণ।
YAML File দিয়ে Pod Create করা
উপরের Configuration pod.yaml নামে Save করো।
তারপর:
kubectl apply -f pod.yaml
Pod Check করতে:
kubectl get pods
Detailed Information:
kubectl describe pod nginx
এভাবে Kubernetes YAML Manifest ব্যবহার করে Pod Create করা যায়।
15. Common YAML Mistakes
YAML লেখার সময় কিছু Common Mistakes হয়।
| Mistake | Problem |
|---|---|
Colon (:)-এর পরে Space না দেওয়া | Intended Mapping Parse নাও হতে পারে |
| Wrong Indentation | Structure পরিবর্তিত হয় অথবা Syntax Error হয় |
| Tabs দিয়ে Indentation | YAML Indentation-এ Tabs Allowed নয় |
| List এবং Dictionary ভুলভাবে Mix করা | Invalid বা Unexpected Structure |
| Direct Value-এর নিচে Nested Properties লেখা | Mapping Error হতে পারে |
Mistake 1: Missing Space
Incorrect:
fruit:apple
Correct:
fruit: apple
Mistake 2: Wrong Indentation
Incorrect:
banana:
calories: 105
fat: 0.4
Correct:
banana:
calories: 105
fat: 0.4
Mistake 3: Tabs ব্যবহার করা
YAML Indentation-এর জন্য Spaces ব্যবহার করো।
Correct:
car:
color: blue
model: mustang
Mistake 4: List Structure ভুলভাবে লেখা
Incorrect:
cars:
- blue
- red
যদি উদ্দেশ্য দুটি Strings-এর List Create করা হয়, তাহলে Correct Structure:
cars:
- blue
- red
Mistake 5: Scalar Value-এর নিচে Properties রাখা
Incorrect:
car:
price: 25000
currency: USD
যদি Price-এর মধ্যে Amount এবং Currency রাখতে চাও, তাহলে Nested Dictionary ব্যবহার করতে হবে।
Correct:
car:
price:
amount: 25000
currency: USD
16. YAML-এর Complete Mental Model
YAML বোঝার জন্য তিনটি Fundamental Structures মনে রাখলেই অনেক সহজ হয়ে যায়।
1. Key-Value Pair
fruit: apple
2. Dictionary
banana:
calories: 105
fat: 0.4
carbs: 27
3. List
fruits:
- apple
- banana
- orange
এই তিনটি Structure Combine করে আরও Complex YAML Configurations তৈরি করা যায়।
YAML
|
├── Key-Value Pair
|
├── Dictionary / Map
| |
| └── Nested Dictionary
|
└── List / Array
|
└── Dictionary
|
├── Nested Dictionary
|
└── Nested List
Kubernetes YAML Files-এর বেশিরভাগ Structure এই Basic Concepts-এর ওপর নির্ভর করে।
17. YAML vs JSON vs XML
YAML, JSON এবং XML — তিনটিই Structured Data Represent করতে পারে।
একটি Simple Application Configuration দেখি।
YAML Example
app:
name: my-app
port: 8080
JSON Example
{
"app": {
"name": "my-app",
"port": 8080
}
}
XML Example
<app>
<name>my-app</name>
<port>8080</port>
</app>
তিনটি Formats-এর উদ্দেশ্য একই ধরনের Structured Information Represent করা।
তবে Syntax আলাদা।
| Feature | YAML | JSON | XML |
|---|---|---|---|
| Human Readability | High | High | Moderate |
| Comments | Supported | Standard JSON-এ নেই | Supported |
| Hierarchical Data | Yes | Yes | Yes |
| Lists / Arrays | Yes | Yes | Repeated Elements |
| Kubernetes Manifests | Common | Supported | Standard Manifest Format নয় |
Kubernetes-এর Configuration Files-এ YAML খুব Common, কারণ এটি Nested Structures তুলনামূলকভাবে সহজভাবে Represent করতে পারে।
18. Complete Kubernetes YAML Example
এবার একটু বড় Kubernetes YAML দেখি।
ধরো, আমরা একটি NGINX Deployment Create করতে চাই।
এই Deployment তিনটি NGINX Pods Maintain করবে।
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
এখানে আমরা আগের সব YAML Concepts দেখতে পাচ্ছি।
Key-Value Pairs
kind: Deployment
Dictionary
metadata:
name: nginx-deployment
Nested Dictionary
selector:
matchLabels:
app: nginx
List of Dictionaries
containers:
- name: nginx
image: nginx:latest
Nested List of Dictionaries
ports:
- containerPort: 80
Complete Structure
Deployment
|
├── apiVersion
|
├── kind
|
├── metadata
| |
| └── name
|
└── spec
|
├── replicas
|
├── selector
| |
| └── matchLabels
|
└── template
|
├── metadata
|
└── spec
|
└── containers
|
├── name
|
├── image
|
└── ports
এই Example থেকে বোঝা যায় Kubernetes Manifests-এর মধ্যে YAML Lists, Dictionaries এবং Nested Structures কীভাবে একসঙ্গে ব্যবহার করা হয়।
Kubernetes-এ Apply করা
File deployment.yaml নামে Save করো।
তারপর:
kubectl apply -f deployment.yaml
Deployment Check:
kubectl get deployments
Pods Check:
kubectl get pods
এইভাবে YAML File ব্যবহার করে Kubernetes Resources Create এবং Manage করা যায়।
19. Important YAML Rules — Quick Reference
| Rule | Explanation |
|---|---|
key: value | Basic Key-Value Mapping |
- item | List Item |
| Indentation | Nested Structure Define করে |
| Spaces | Indentation-এর জন্য ব্যবহার করা হয় |
| Tabs | Indentation-এর জন্য ব্যবহার করা যায় না |
| Dictionary | একটি Object-এর Properties Describe করে |
| List | Multiple Items Store করে |
| List of Dictionaries | Multiple Objects with Properties |
# | Comment |
| Dictionary Order | সাধারণত Logical Meaning পরিবর্তন করে না |
| List Order | Ordered Collection হিসেবে Preserve হয় |
20. Final Takeaway — YAML Basics
এই Tutorial-এর মূল উদ্দেশ্য ছিল YAML-এর Basic Data Structures বোঝা।
YAML শেখার জন্য সবচেয়ে গুরুত্বপূর্ণ Concepts হলো:
Key-Value Pairs: একটি Key-এর সঙ্গে একটি Value Associate করে।
Lists: একাধিক Values Store করে।
Dictionaries: একটি Object-এর Properties Describe করে।
Indentation: কোন Data কোন Parent Structure-এর মধ্যে রয়েছে তা Determine করে।
Nested Structures: Lists এবং Dictionaries Combine করে Complex Data Represent করে।
Kubernetes YAML Files এই Fundamental Concepts-এর ওপর ভিত্তি করে তৈরি হয়।
Final Mental Model
YAML
|
├── Key-Value Pairs
|
├── Lists
|
├── Dictionaries
|
├── Nested Structures
|
└── Kubernetes Manifests
|
├── Pods
├── Deployments
├── Services
├── ConfigMaps
├── Secrets
└── Volumes
Final One-Line Summary:
YAML হলো একটি Human-Readable Configuration Format, যেখানে Key-Value Pairs, Lists, Dictionaries এবং Indentation ব্যবহার করে Structured Data লেখা হয়। Kubernetes এই YAML Format ব্যবহার করে Pods, Deployments, Services এবং অন্যান্য Resources-এর Desired State Define ও Manage করতে পারে।
Official Documentation
- YAML Official Website
- YAML Specification
- Kubernetes — Understanding Kubernetes Objects
- Kubernetes — Declarative Object Management
- Kubernetes — Pod Concepts
- Kubernetes — Deployment Concepts
YAML-এর Basic Syntax এবং Structure ভালোভাবে বুঝতে পারলে Kubernetes-এর Configuration Files পড়া, লেখা এবং Troubleshoot করা অনেক সহজ হয়ে যায়।
