YAML in Kubernetes: A Complete Configuration Guide
Master the art of writing and managing Kubernetes configurations with YAML. From basic deployments to advanced service configurations, learn everything you need to know about Kubernetes manifests.
Kubernetes YAML Fundamentals
Basic Structure
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
Required Fields
- apiVersion - API version and resource type
- kind - Resource type definition
- metadata - Resource information
- spec - Resource specifications
Common Resources
- Deployments
- Services
- Pods
- ConfigMaps & Secrets
Common Resource Types
Deployment Configuration
Define application deployments
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Service Configuration
Configure network services
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
ConfigMap
Manage application configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_URL: "mongodb://localhost:27017"
API_KEY: "development-key"
ENVIRONMENT: "staging"
Best Practices
Resource Management
- Always specify resource limits
- Use namespaces for organization
- Set appropriate replica counts
- Configure health checks
Security
- Use RBAC for access control
- Implement network policies
- Secure sensitive data in Secrets
- Regular security updates
Maintenance
- Version control your configurations
- Use meaningful labels
- Document configuration changes
- Regular backup strategies
Common Challenges and Solutions
Indentation Errors
Problem: YAML syntax errors due to incorrect indentation
Solution: Use a YAML validator and maintain consistent spacing (2 spaces recommended)
Resource Dependencies
Problem: Resources created in wrong order
Solution: Use proper dependency management and wait conditions
Configuration Drift
Problem: Manual changes causing inconsistency
Solution: Implement GitOps practices and automated deployments
Scaling Issues
Problem: Improper resource allocation during scaling
Solution: Configure HPA and set appropriate resource requests/limits
Advanced Configuration Topics
Multi-Container Pods
- Sidecar patterns
- Init containers
- Shared volumes
- Inter-container communication
Custom Resources
- CRD definition
- Custom controllers
- Operator patterns
- API extensions
Need to Work with Kubernetes Configurations?
Try our free YAML tools to validate and convert your Kubernetes configurations.