Introduction
2021 saw GitOps emerge as a leading practice for Kubernetes deployments, with ArgoCD becoming the de facto tool for implementation. Let’s explore how to implement GitOps effectively using ArgoCD in Kubernetes.
What is GitOps?
GitOps is a declarative approach to Kubernetes cluster management and application delivery where:
- Git repository is the single source of truth
- Desired state is described in YAML manifests
- Changes are automatically synchronized
- Drift detection and remediation are automated
Setting Up ArgoCD
1. Installation
# Install ArgoCD in k8s cluster
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/v2.1.0/manifests/install.yaml
2. Basic Application Deployment
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/my-org/my-app.git
targetRevision: HEAD
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: my-app
syncPolicy:
automated:
prune: true
selfHeal: true
GitOps Best Practices
1. Repository Structure
├── base
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
├── overlays
│ ├── development
│ │ └── kustomization.yaml
│ └── production
│ └── kustomization.yaml
2. Environment Management with Kustomize
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
patches:
- path: production-values.yaml
Advanced Features
1. Sync Strategies
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
2. Health Checks
spec:
health:
healthCheckPath: /health
healthCheckTimeout: 60s
healthyThreshold: 1
unhealthyThreshold: 3
Security Considerations
1. RBAC Configuration
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: argocd-role
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
2. Secrets Management
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
name: mysecret
spec:
encryptedData:
API_KEY: AgBy8hCK8...
Monitoring and Observability
1. Prometheus Integration
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: argocd-metrics
spec:
selector:
matchLabels:
app.kubernetes.io/name: argocd-metrics
endpoints:
- port: metrics
2. Logging Configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
data:
logging.level: debug
logging.format: json
Performance Optimization
1. Resource Management
spec:
template:
spec:
containers:
- name: application
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
2. Sync Wave Control
metadata:
annotations:
argocd.argoproj.io/sync-wave: "2"
Disaster Recovery
1. Backup Configuration
apiVersion: velero.io/v1
kind: Backup
metadata:
name: argocd-backup
spec:
includedNamespaces:
- argocd
storageLocation: default
volumeSnapshotLocations:
- default
Best Practices for Production
- Version Control:
- Use semantic versioning
- Tag releases properly
- Maintain changelog
- Application Structure:
- Separate config from code
- Use Helm or Kustomize
- Implement progressive delivery
- Security:
- Implement RBAC
- Use sealed secrets
- Regular security audits
- Monitoring:
- Set up alerts
- Monitor sync status
- Track deployment metrics
Common Challenges and Solutions
- Multi-Cluster Management:
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: multi-cluster
spec:
destinations:
- namespace: '*'
server: https://cluster1.example.com
- namespace: '*'
server: https://cluster2.example.com
2. Private Repository Access:
apiVersion: v1
kind: Secret
metadata:
name: private-repo
namespace: argocd
stringData:
url: https://github.com/my-org/private-repo
password: <token>
79 words
Humanize AI
Conclusion
GitOps with ArgoCD in 2021 marked a radical shift in organizations’ way of handling their Kubernetes deployments. The key takeaways include:
- Improved security through declarative configuration
- Better auditing and compliance
- Automated drift detection and correction
- Simplified rollback procedures
- Enhanced collaboration through Git workflows
For organizations looking to implement GitOps:
- Start with a simple application
- Gradually expand to more complex scenarios
- Implement proper security measures
- Set up comprehensive monitoring
- Train teams on GitOps practices
The future of Kubernetes deployments is increasingly GitOps-driven, and the tooling and best practices will be led by ArgoCD.
Hope you enjoyed the post.
Cheers
Ramasankar Molleti
