Practical Annotation Patterns
Annotation Patterns for Different Scenarios
1. Resource Management Annotations
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
annotations:
## Ownership and contact information
"owner": "[email protected]"
"contact": "[email protected]"
## Deployment tracking
"deployment/timestamp": "2023-06-15T10:30:00Z"
"deployment/version": "1.2.3"
2. CI/CD Integration Annotations
## Annotate deployment with CI/CD metadata
kubectl annotate deployment web-app \
"ci.labex.io/pipeline-id"="build-123" \
"ci.labex.io/commit-hash"="a1b2c3d4" \
"ci.labex.io/build-time"="2023-06-15T14:45:00Z"
Annotation Patterns Workflow
graph TD
A[Resource Creation] --> B{Annotation Strategy}
B -->|Management| C[Ownership Annotations]
B -->|Tracking| D[Version and Metadata Annotations]
B -->|Integration| E[Tool-Specific Annotations]
Common Annotation Pattern Categories
Category |
Purpose |
Example Annotations |
Ownership |
Track responsible teams |
owner , contact |
Versioning |
Track deployment versions |
version , build-number |
External Tools |
Integration metadata |
monitoring , ci/cd |
Documentation |
Additional context |
description , notes |
3. Monitoring and Observability Annotations
## Add monitoring configuration annotations
kubectl annotate deployment web-app \
"monitoring.labex.io/enabled"="true" \
"monitoring.labex.io/alert-level"="critical" \
"monitoring.labex.io/dashboard"="web-app-metrics"
4. Custom Resource Extension Annotations
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: database
annotations:
## Custom backup strategy
"backup.labex.io/strategy": "weekly"
"backup.labex.io/retention": "3"
## Performance tuning
"performance.labex.io/max-connections": "100"
Advanced Annotation Patterns
Namespace-Level Annotations
## Apply annotations at namespace level
kubectl annotate namespace production \
"environment"="production" \
"managed-by"="platform-team"
Dynamic Annotation Management
## Use scripts for dynamic annotation management
for pod in $(kubectl get pods -l app=web -o names); do
kubectl annotate $pod "last-scaled"=$(date +%Y-%m-%d)
done
Best Practices for Annotation Patterns
- Use consistent and meaningful annotation keys
- Avoid storing sensitive information
- Keep annotations concise and descriptive
- Leverage annotations for cross-tool integration
- Document custom annotation meanings
Annotation Pattern Validation
## Validate annotations
kubectl get deployments -o jsonpath='{.items[*].metadata.annotations}'
## Filter resources by specific annotations
kubectl get pods -l owner=devops-team
By implementing these practical annotation patterns, Kubernetes users can enhance resource management, improve tracking, and enable better integration across different tools and platforms.