Best Practices for Writing Kubernetes Manifests
Writing Kubernetes manifests is a crucial part of deploying and managing applications in a Kubernetes cluster. Well-structured and organized manifests can improve maintainability, readability, and collaboration among team members. Here are some best practices to follow when writing Kubernetes manifests.
1. Use Descriptive Names
Use clear and descriptive names for your resources. This helps in understanding the purpose of each resource at a glance. For example, instead of naming a deployment app
, use a name that reflects its function, such as frontend-service
.
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-service
spec:
replicas: 3
...
2. Organize Resources with Labels and Annotations
Use labels and annotations to organize and manage your resources effectively. Labels can be used for grouping and selecting resources, while annotations can store additional metadata.
metadata:
labels:
app: frontend
environment: production
annotations:
description: "This is the frontend service for the application."
3. Use Versioned API
Always use the latest stable version of the Kubernetes API for your resources. This ensures compatibility and access to the latest features and improvements.
apiVersion: apps/v1
kind: Deployment
4. Define Resource Requests and Limits
Specify resource requests and limits for your containers to ensure efficient resource allocation and prevent resource contention. This helps Kubernetes manage resources effectively and maintain application performance.
spec:
containers:
- name: my-container
image: my-image:latest
resources:
requests:
memory: "256Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "1"
5. Use Environment Variables for Configuration
Use environment variables to pass configuration data to your applications. This allows you to change configurations without modifying the container image.
spec:
containers:
- name: my-container
image: my-image:latest
env:
- name: DATABASE_URL
value: "mysql://user:password@hostname:3306/dbname"
6. Implement Health Checks
Define liveness and readiness probes to ensure that your application is running correctly and is ready to serve traffic. This helps Kubernetes manage the lifecycle of your pods effectively.
spec:
containers:
- name: my-container
image: my-image:latest
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
7. Use ConfigMaps and Secrets for Sensitive Data
Store configuration data and sensitive information (like passwords and API keys) in ConfigMaps and Secrets, respectively. This keeps your manifests clean and separates configuration from code.
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
DATABASE_PASSWORD: "cGFzc3dvcmQ=" # base64 encoded value
8. Keep Manifests DRY (Don't Repeat Yourself)
Avoid duplication in your manifests by using tools like Kustomize or Helm. These tools allow you to manage variations of your manifests without duplicating code.
# Example of a Kustomization file
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
configMapGenerator:
- name : my-config
literals:
- key1=value1
- key2=value2
9. Use Comments for Clarity
Add comments to your manifests to explain complex configurations or decisions. This helps other team members (or your future self) understand the reasoning behind certain choices.
# This deployment manages the frontend service
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-service
spec:
replicas: 3
...
10. Validate Your Manifests
Before applying your manifests, validate them using tools like kubectl apply --dry-run=client
or kubeval
. This helps catch syntax errors and ensures that your manifests are well-formed.
kubectl apply -f my-manifest.yaml --dry-run=client
Conclusion
Following these best practices when writing Kubernetes manifests can lead to more maintainable, readable, and efficient configurations. By using descriptive names, organizing resources, implementing health checks, and validating your manifests, you can create a robust deployment strategy that enhances collaboration and reduces errors in your Kubernetes environment.