What You Will Learn
Beginner
- How the declarative model works
- YAML manifest structure
- kubectl apply vs create
Kubernetes is declarative: you describe the desired state in YAML, and K8s works to make the actual state match it. If a Pod dies, K8s creates a new one to match the desired count.
YAML Manifest Structure
pod.yamlyaml
apiVersion: v1
kind: Pod
metadata:
name: my-app
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80| Field | What it does |
|---|---|
apiVersion | K8s API version (v1, apps/v1, etc.) |
kind | Object type (Pod, Deployment, Service) |
metadata | Name, labels, annotations |
spec | Desired state (containers, ports, volumes) |
Applying YAML
Terminalbash
# Apply (create or update)
kubectl apply -f pod.yaml
# Create (fails if already exists)
kubectl create -f pod.yaml
# Delete
kubectl delete -f pod.yaml
# View the Pod
kubectl get podsapply vs create
Use kubectl apply (declarative - creates or updates). Avoid kubectl create (imperative - fails if object exists). apply is the K8s way.Practical Exercise
Create pod.yaml with the example above
Run: kubectl apply -f pod.yaml
Check: kubectl get pods
Run: kubectl describe pod my-app
Clean up: kubectl delete -f pod.yaml
Key Takeaways
- K8s is declarative: describe desired state in YAML, K8s makes it happen.
- YAML has 4 sections: apiVersion, kind, metadata, spec.
- kubectl apply -f file.yaml: create or update.
- kubectl delete -f file.yaml: remove.
- Always use apply (declarative), not create (imperative).
Comments
Comments
Post a Comment