0
0
Kubernetesdevops~5 mins

GitOps with ArgoCD in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
GitOps with ArgoCD helps you keep your Kubernetes apps in sync with your Git repository automatically. It solves the problem of manually updating your cluster by watching your Git repo and applying changes for you.
When you want to automatically deploy app updates by just pushing code to Git.
When you want a clear history of all changes made to your Kubernetes apps.
When you want to avoid manual errors by having a single source of truth in Git.
When you want to easily roll back to previous app versions using Git history.
When you want to monitor app health and sync status from a web dashboard.
Config File - argocd-app.yaml
argocd-app.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: example-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/argoproj/argocd-example-apps.git
    targetRevision: HEAD
    path: guestbook
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

This file defines an ArgoCD Application resource.

metadata.name is the app name in ArgoCD.

spec.source tells ArgoCD where your Git repo is and which folder to use.

spec.destination tells ArgoCD which Kubernetes cluster and namespace to deploy to.

spec.syncPolicy.automated enables automatic syncing and cleanup of resources.

Commands
Create a separate namespace for ArgoCD to keep its components organized.
Terminal
kubectl create namespace argocd
Expected OutputExpected
namespace/argocd created
Install ArgoCD components into the cluster in the argocd namespace.
Terminal
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Expected OutputExpected
namespace/argocd created serviceaccount/argocd-server created service/argocd-server created deployment.apps/argocd-server created ... (other resources created)
Create an ArgoCD Application resource that tells ArgoCD which Git repo and path to sync to the cluster.
Terminal
kubectl apply -n argocd -f argocd-app.yaml
Expected OutputExpected
application.argoproj.io/example-app created
Check the status of your ArgoCD applications to see if they are synced and healthy.
Terminal
kubectl get applications -n argocd
Expected OutputExpected
NAME SYNCED HEALTH STATUS example-app Synced Healthy Synced to HEAD
Key Concept

If you remember nothing else from this pattern, remember: ArgoCD watches your Git repo and automatically applies changes to your Kubernetes cluster to keep them in sync.

Common Mistakes
Not creating the argocd namespace before installing ArgoCD.
ArgoCD installation fails or installs in the wrong namespace causing confusion.
Always run 'kubectl create namespace argocd' before installing ArgoCD.
Using incorrect repoURL or path in the Application manifest.
ArgoCD cannot find the Kubernetes manifests to deploy, so sync fails.
Double-check the repo URL and path point to a valid Git repo with Kubernetes manifests.
Not enabling automated sync in the Application spec.
Changes in Git won't automatically apply to the cluster; manual sync needed.
Set 'syncPolicy.automated' with 'prune' and 'selfHeal' true for automatic updates.
Summary
Create the argocd namespace to isolate ArgoCD components.
Install ArgoCD using the official manifest in the argocd namespace.
Define an Application resource pointing to your Git repo and app path.
Check application status to confirm ArgoCD synced your app to the cluster.