Skip to main content

Chapter 3.2 - YAML Declaration for Pods

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the YAML structure of a Pod
  • Create Pods with kubectl
  • Define resources (CPU, memory)
  • Configure environment variables
  • Mount volumes

YAML Structure of a Pod

Basic Structure

apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
env: production
spec:
containers:
- name: my-container
image: nginx:1.20
ports:
- containerPort: 80

Main Sections

  1. apiVersion: Kubernetes API version
  2. kind: Resource type (Pod)
  3. metadata: Metadata (name, labels, annotations)
  4. spec: Pod specification

Complete Example

apiVersion: v1
kind: Pod
metadata:
name: web-server
namespace: default
labels:
app: web
tier: frontend
version: v1
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: ENV_VAR
value: "production"
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
volumeMounts:
- name: config
mountPath: /etc/nginx/conf.d
volumes:
- name: config
configMap:
name: nginx-config
restartPolicy: Always

Resources (CPU and Memory)

Requests and Limits

resources:
requests: # Guaranteed resources
cpu: "100m" # 0.1 CPU core
memory: "128Mi" # 128 Mebibytes
limits: # Maximum resources
cpu: "500m" # 0.5 CPU core
memory: "512Mi" # 512 Mebibytes

CPU Units:

  • 100m = 0.1 CPU
  • 1 = 1 CPU core
  • 2.5 = 2.5 CPU cores

Memory Units:

  • 128Mi = 128 Mebibytes
  • 1Gi = 1 Gibibyte
  • 500M = 500 Megabytes

Environment Variables

Method 1: Direct

env:
- name: DATABASE_URL
value: "postgresql://localhost:5432/mydb"
- name: LOG_LEVEL
value: "info"

Method 2: From ConfigMap

env:
- name: CONFIG_VALUE
valueFrom:
configMapKeyRef:
name: my-config
key: config-key

Method 3: From Secret

env:
- name: PASSWORD
valueFrom:
secretKeyRef:
name: my-secret
key: password

Volumes

Volume Types

volumes:
# Volume from ConfigMap
- name: config
configMap:
name: my-config

# Volume from Secret
- name: secrets
secret:
secretName: my-secret

# Temporary volume
- name: temp
emptyDir: {}

# Persistent volume
- name: data
persistentVolumeClaim:
claimName: my-pvc

Mounting in the Container

volumeMounts:
- name: config
mountPath: /etc/config
readOnly: true

kubectl Commands

Create a Pod

# From a YAML file
kubectl apply -f pod.yaml

# From the command line
kubectl run my-pod --image=nginx:1.20

# Create and display the YAML
kubectl run my-pod --image=nginx:1.20 --dry-run=client -o yaml > pod.yaml

Manage Pods

# List Pods
kubectl get pods

# View details
kubectl describe pod my-pod

# View logs
kubectl logs my-pod

# Execute a command
kubectl exec -it my-pod -- /bin/bash

# Delete a Pod
kubectl delete pod my-pod

Summary

In this chapter, you learned:

YAML Structure: apiVersion, kind, metadata, spec
Resources: Requests (guaranteed) and Limits (maximum)
Environment Variables: Direct, ConfigMap, Secret
Volumes: ConfigMap, Secret, emptyDir, PVC
Commands: kubectl apply, get, describe, logs, exec, delete


Next Steps

Now that you know how to create Pods:

Chapter 3.3: Labels and Selectors
Lab 3.1: Creating and Managing Pods


Chapter created: December 2024