Skip to main content
graphwiz.aigraphwiz.ai
← Back to Posts

Kubernetes GitOps with ArgoCD: A Practical Guide

DevOpsKubernetes
kubernetesgitopsargocdcontinuous-deployment

Kubernetes GitOps with ArgoCD: A Practical Guide

GitOps transforms infrastructure management by making Git the single source of truth. ArgoCD implements GitOps for Kubernetes with a powerful UI and automation capabilities.

Why GitOps?

Traditional CI/CD pipelines push changes to clusters. GitOps inverts this:

  • Pull-based: Clusters pull their desired state from Git
  • Declarative: Describe what you want, not how to get there
  • Versioned: Every change is tracked in Git
  • Auditable: Full history of who changed what and when

ArgoCD Setup

Installation

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
```text

### Access the UI

```bash
kubectl port-forward svc/argocd-server -n argocd 8080:443
```text

## Application Configuration

### Basic Application

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/org/my-app
    targetRevision: main
    path: k8s
  destination:
    server: https://kubernetes.default.svc
    namespace: my-app
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
```text

### Multi-Cluster Setup

```yaml
spec:
  destination:
    server: https://cluster-2.example.com
    namespace: production
```text

## Sync Strategies

### Automatic Sync

```yaml
syncPolicy:
  automated:
    prune: true      # Delete resources not in Git
    selfHeal: true    # Revert manual changes
```text

### Manual Sync with Hooks

```yaml
hooks:
  - name: PreSync
    type: PreSync
    command: ["sh", "-c", "run-migrations.sh"]
```text

## Rollback Strategy

ArgoCD makes rollbacks trivial:

```bash
# List history
argocd app history my-app

# Rollback to previous
argocd app rollback my-app 1
```text

## Best Practices

1. **One app per repository** or use monorepo with clear paths
2. **Separate environments** via branches or kustomize overlays
3. **Use ApplicationSets** for multi-cluster deployments
4. **Enable notifications** for sync failures
5. **Implement RBAC** for team access control

## Conclusion

ArgoCD brings declarative, version-controlled deployments to Kubernetes. Start simple, add complexity as needed, and always test rollbacks.