Please enable JavaScript.
Coggle requires JavaScript to display documents.
Phase 7 - YAML and kubectl top - Coggle Diagram
Phase 7 - YAML and kubectl top
Phase 7 - Kubernetes YAML
What is YAML
Full Form
YAML Ain't Markup Language
Definition
Human-readable data serialization language.
Kubernetes uses YAML files to describe the desired state of objects.
Purpose
Store Infrastructure as Code (IaC)
Version Control with Git
Deploy Applications
Configure Infrastructure
Automation with CI/CD
File Extensions
.yaml
.yml
Kubernetes Reads YAML
kubectl apply -f deployment.yaml
kube-apiserver validates YAML
Object stored in etcd
Scheduler and Controllers make actual state equal desired state
YAML Syntax
Key Value Pair
name: nginx
Lists
containers:
nginx
busybox
Dictionary (Map)
metadata:
name: nginx
labels:
app: nginx
Indentation
Spaces only
Never use Tab
Usually 2 spaces
Comments
String
"hello"
hello
Number
replicas: 3
Boolean
true
false
Null
null
Kubernetes Object
Definition
Every resource inside Kubernetes is called an Object.
Examples
Pod
Deployment
Service
ConfigMap
Secret
Namespace
Job
CronJob
PersistentVolume
Ingress
Object Life Cycle
YAML Written
kubectl apply
API Server
Validation
etcd Database
Scheduler
Controller
Running Object
Basic Kubernetes YAML Structure
apiVersion
Definition
Kubernetes API version.
Examples
apps/v1
v1
batch/v1
Command
kubectl api-resources
kubectl explain deployment
kind
Definition
Type of Kubernetes Object.
Examples
Pod
Deployment
Service
Namespace
ConfigMap
metadata
Definition
Identity of the Object.
Contains
name
namespace
labels
annotations
ownerReferences
finalizers
spec
Definition
Desired State
Written by Administrator
Examples
Number of replicas
Container image
CPU
Memory
Ports
Volumes
status
Definition
Current State
Written by Kubernetes
Never manually edit
Examples
Running
Pending
Failed
Ready
Metadata
Definition
Information that identifies an object.
Fields
name
Unique name
Example
nginx-deployment
namespace
Logical separation
Default
kube-system
production
development
uid
Unique ID
Automatically generated
creationTimestamp
Creation time
labels
Used for grouping and selection
annotations
Additional information
ownerReferences
Parent child relationship
finalizers
Safe deletion
Commands
kubectl get deployment
kubectl describe deployment nginx
kubectl get deployment -o yaml
Labels
Definition
Key value pairs for grouping Kubernetes objects.
Purpose
Organize resources
Service Discovery
Monitoring
Logging
Automation
Deployment Selection
Example
app: nginx
environment: production
team: devops
Enterprise Labels
app
version
owner
department
environment
tier
business-unit
Commands
kubectl get pods --show-labels
kubectl label pod nginx app=frontend
kubectl get pods -l app=frontend
kubectl get pods -l environment=production
Annotations
Definition
Store metadata not used for selection.
Difference from Labels
Labels identify objects.
Annotations describe objects.
Examples
build number
Git commit
Jenkins URL
Documentation
Contact Person
Commands
kubectl annotate pod nginx owner=Bilal
kubectl describe pod nginx
Spec
Definition
Desired State written by Administrator.
Contains
replicas
containers
image
resources
volumes
ports
affinity
nodeSelector
tolerations
Enterprise Examples
CPU Limits
Memory Limits
Persistent Storage
Security Context
Rolling Update
Commands
kubectl explain deployment.spec
kubectl explain deployment.spec.template.spec.containers
Status
Definition
Actual State maintained by Kubernetes.
Examples
Ready Replicas
Available Replicas
Restart Count
Conditions
Node Assigned
Commands
kubectl get deployment
kubectl describe deployment
kubectl get pod
kubectl get pod -o yaml
Selectors
Definition
Mechanism used to select objects using Labels.
Used By
Deployment
ReplicaSet
Service
NetworkPolicy
Match Labels
app=nginx
Match Expressions
In
NotIn
Exists
DoesNotExist
Commands
kubectl get pods -l app=nginx
kubectl get pods -l environment=production
Template
Definition
Blueprint used to create Pods.
Path
spec
template
metadata
spec
Contains
Pod Labels
Containers
Images
Environment Variables
Volumes
Commands
kubectl explain deployment.spec.template
Owner References
Definition
Parent Child Relationship.
Example
Deployment
ReplicaSet
Pod
Automatic Cleanup
Delete Deployment
ReplicaSet Deleted
Pods Deleted
Commands
kubectl get pod -o yaml
kubectl describe pod
Finalizers
Definition
Protection mechanism before deletion.
Purpose
Backup
Cleanup
Cloud Resource Deletion
Storage Cleanup
Enterprise Uses
Persistent Volumes
Databases
Cloud Load Balancers
Commands
kubectl get pod -o yaml
kubectl edit pod nginx
kubectl delete pod nginx
YAML Validation
Validate Syntax
kubectl apply --dry-run=client -f deployment.yaml
Server Validation
kubectl apply --dry-run=server -f deployment.yaml
Explain Fields
kubectl explain deployment
kubectl explain deployment.spec
kubectl explain deployment.metadata
kubectl explain deployment.spec.template.spec
Output YAML
kubectl get deployment nginx -o yaml
Common File Locations
Home Directory
~/k8s
~/kubernetes
Enterprise Repository
infrastructure
kubernetes
production
staging
development
Manifest Types
deployment.yaml
service.yaml
ingress.yaml
configmap.yaml
secret.yaml
namespace.yaml
pvc.yaml
networkpolicy.yaml
Enterprise Best Practices
One Resource Per File
Use Git
Use Labels
Use Namespaces
Use Resource Limits
Never Edit Status
Validate Before Apply
Keep Secrets Separate
Use Comments
Follow Naming Standards
kubectl top Enterprise Level
Definition
Displays real-time CPU and Memory usage of Nodes and Pods.
Uses Metrics Server.
Similar to Linux top command.
Full Form
kubectl
Kubernetes Command Line Tool
top
Resource Usage Display
Requirements
Metrics Server Installed
Verify
kubectl top nodes
kubectl top pods
Check Metrics Server
kubectl get deployment metrics-server -n kube-system
Architecture
Pod
kubelet
Metrics Server
API Server
kubectl top
Node Monitoring
Command
kubectl top nodes
Shows
Node Name
CPU Usage
CPU Percentage
Memory Usage
Memory Percentage
Pod Monitoring
Command
kubectl top pods
Shows
Pod Name
CPU Usage
Memory Usage
Namespace Monitoring
Command
kubectl top pods -n production
kubectl top pods -n kube-system
All Namespaces
Command
kubectl top pods --all-namespaces
Sort Usage
Linux CPU
kubectl top pods --all-namespaces | sort -k3 -hr
Linux Memory
kubectl top pods --all-namespaces | sort -k4 -hr
Container Monitoring
Command
kubectl top pod nginx --containers
Resource Units
CPU
m
Millicores
1000m
One CPU Core
Memory
Ki
Kibibyte
Mi
Mebibyte
Gi
Gibibyte
Enterprise Use Cases
Capacity Planning
Performance Monitoring
Troubleshooting
Detect Memory Leaks
CPU Bottleneck Analysis
Resource Optimization
Cost Optimization
Autoscaling Verification
Related Commands
kubectl get nodes
kubectl get pods
kubectl describe node
kubectl describe pod
kubectl logs
kubectl get events
kubectl top nodes
kubectl top pods
kubectl top pod POD_NAME --containers
kubectl get --raw /metrics
Difference
Linux top
Shows Processes
OS Level
Individual PID
kubectl top
Shows Pods
Shows Nodes
Kubernetes Level
Uses Metrics Server
Troubleshooting
Error
Metrics API not available
Verify
kubectl get deployment metrics-server -n kube-system
Check Pods
kubectl get pods -n kube-system
Logs
kubectl logs deployment/metrics-server -n kube-system
Enterprise Monitoring Stack
kubectl top
Basic Resource Monitoring
Metrics Server
Resource Metrics
Prometheus
Time Series Metrics
Grafana
Dashboards
Alertmanager
Alerts
Kubernetes Dashboard
Cluster Visualization
Learning Order
YAML Basics
Kubernetes Objects
apiVersion
kind
metadata
labels
annotations
spec
status
selectors
templates
ownerReferences
finalizers
YAML Validation
kubectl explain
kubectl apply
kubectl top
Enterprise Monitoring
GitOps
Production Deployments