A Pod is the smallest deployable unit in Kubernetes that can be created and managed. It's a group of one or more containers with shared storage/network resources.
apiVersion: v1
kind: Pod
metadata:
name: my-app
labels:
app: web
tier: frontend
spec:
containers:
- name: web-container
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"apiVersion: v1
kind: Pod
metadata:
name: multi-container-pod
spec:
containers:
- name: web
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: content-sync
image: debian
volumeMounts:
- name: shared-data
mountPath: /content
command: ["/bin/sh"]
args: ["-c", "while true; do date >> /content/index.html; sleep 5; done"]
volumes:
- name: shared-data
emptyDir: {}- Pending
- Running
- Succeeded
- Failed
- Unknown
spec:
containers:
- name: web
image: nginx
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
startupProbe:
httpGet:
path: /startup
port: 80
failureThreshold: 30
periodSeconds: 10A Deployment provides declarative updates for Pods and ReplicaSets, allowing for easy scaling and rolling updates.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.21
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%spec:
strategy:
type: Recreate# Manual scaling
kubectl scale deployment web-deployment --replicas=5
# Autoscaling
kubectl autoscale deployment web-deployment --min=2 --max=5 --cpu-percent=80ReplicaSets ensure that a specified number of pod replicas are running at any given time.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.21spec:
containers:
- name: web
image: nginx
env:
- name: DATABASE_URL
value: "mysql://db:3306/myapp"
- name: API_KEY
valueFrom:
secretKeyRef:
name: app-secrets
key: api-keyapiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database_url: "mysql://db:3306/myapp"
api_endpoint: "https://api.example.com"
---
spec:
containers:
- name: web
envFrom:
- configMapRef:
name: app-configapiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
username: dXNlcm5hbWU= # base64 encoded
password: cGFzc3dvcmQ= # base64 encoded
---
spec:
containers:
- name: web
volumeMounts:
- name: secrets
mountPath: "/etc/secrets"
volumes:
- name: secrets
secret:
secretName: app-secrets- Resource Management
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"- Health Checks
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 3
periodSeconds: 3- Labels and Selectors
metadata:
labels:
app: web
environment: production
version: v1.0.0- Security Context
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000# Get pod details
kubectl describe pod <pod-name>
# Get pod logs
kubectl logs <pod-name>
kubectl logs -f <pod-name> # Follow logs
# Execute commands in pod
kubectl exec -it <pod-name> -- /bin/bash
# Check deployment status
kubectl rollout status deployment/<deployment-name>
# Deployment history
kubectl rollout history deployment/<deployment-name>
# Rollback deployment
kubectl rollout undo deployment/<deployment-name>- Pod in Pending State
kubectl describe pod <pod-name>
# Check for:
# - Resource constraints
# - PVC binding issues
# - Node selector issues- Pod in CrashLoopBackOff
kubectl logs <pod-name>
kubectl describe pod <pod-name>
# Check for:
# - Application crashes
# - Resource limits
# - Configuration issues- Pod in ImagePullBackOff
kubectl describe pod <pod-name>
# Check for:
# - Image name/tag
# - Registry credentials
# - Network connectivity- Deployment Not Progressing
kubectl rollout status deployment/<deployment-name>
kubectl get events --field-selector involvedObject.kind=Deployment
# Check for:
# - Pod scheduling issues
# - Resource constraints
# - Configuration errors