How to Create Deployment YAML File in Kubernetes
Complete guide to creating and configuring Kubernetes deployment manifests
Introduction
Kubernetes deployments are declarative configuration files that define how your application containers should run in a cluster. This guide covers the essential structure and fields needed to create a deployment YAML file.
Basic Deployment Structure
A Kubernetes deployment YAML file consists of four main sections:
apiVersion- Kubernetes API versionkind- Resource type (Deployment)metadata- Deployment metadataspec- Deployment specification
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:1.21
ports:
- containerPort: 80
Required Fields Explained
apiVersion
Specifies the Kubernetes API version. For deployments, use:
apiVersion: apps/v1
kind
Defines the resource type. For deployments:
kind: Deployment
metadata
Contains deployment identification and labels:
metadata:
name: my-app-deployment
namespace: default
labels:
app: my-app
environment: production
spec
Defines the desired state of the deployment:
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
# Pod template specification
Complete Deployment Example
A production-ready deployment with common configurations:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-deployment
namespace: production
labels:
app: web-app
version: v1.0.0
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
version: v1.0.0
spec:
containers:
- name: web-app
image: myregistry/web-app:1.0.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
- name: LOG_LEVEL
value: "info"
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
Key Configuration Options
Replicas
Number of pod instances to maintain:
spec:
replicas: 3
Update Strategy
Controls how pods are replaced during updates:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
Resource Limits
Define CPU and memory constraints:
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
Health Checks
Configure liveness and readiness probes:
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
Creating and Applying the Deployment
Steps to create and deploy your YAML file:
Step 1: Create the YAML file
# Save as deployment.yaml
nano deployment.yaml
# or
vim deployment.yaml
Step 2: Validate the YAML
# Dry run to validate
kubectl apply --dry-run=client -f deployment.yaml
# Validate YAML syntax
kubectl apply --validate=true -f deployment.yaml
Step 3: Apply the deployment
# Apply to cluster
kubectl apply -f deployment.yaml
# Check deployment status
kubectl get deployments
kubectl describe deployment web-app-deployment
Best Practices
-
•
Use semantic versioning in image tags and labels for better tracking
-
•
Set resource limits to prevent resource exhaustion and ensure fair scheduling
-
•
Configure health probes for reliable application lifecycle management
-
•
Use ConfigMaps and Secrets for configuration and sensitive data instead of hardcoding
-
•
Define update strategies appropriate for your application's availability requirements
Common Issues and Solutions
Issue: Image pull errors
If your pods fail to start due to image pull errors:
# Check image pull policy
imagePullPolicy: Always # or IfNotPresent
# Verify image exists and is accessible
docker pull your-image:tag
Issue: Pods not starting
Debug pod startup issues:
# Check pod events
kubectl describe pod <pod-name>
# View pod logs
kubectl logs <pod-name>
# Check deployment status
kubectl get deployment -o wide