GitOps and ArgoCD

GitOps and ArgoCD

What is GitOps?

GitOps is a modern approach to software delivery that streamlines the process of deploying and managing applications by leveraging the power of Git and automation tools. At the heart of GitOps is the concept of “declarative infrastructure,” which means that the desired state of the infrastructure is defined in a declarative way, and the system automatically ensures that the actual state matches the desired state.

What is ArgoCD?

ArgoCD is a popular GitOps tool that provides a powerful and flexible platform for managing application deployments on Kubernetes clusters. In this blog, we will explore how ArgoCD works, its key features, and how to get started using it for your own deployments.

How ArgoCD Works

At a high level, ArgoCD works by pulling the desired state of the system from a Git repository and applying it to a Kubernetes cluster. This means that you can use Git as the single source of truth for your entire deployment pipeline, from development to production.

ArgoCD monitors the Git repository for changes, and when a change is detected, it automatically deploys the updated configuration to the cluster. This process is fully automated and requires no manual intervention, which makes it a perfect fit for modern, fast-paced development environments.

One of the key benefits of using ArgoCD is that it provides a declarative way to manage the entire application lifecycle. This means that you can define the desired state of your infrastructure using YAML files, and ArgoCD will automatically ensure that the actual state matches the desired state. This approach is much more efficient and reliable than traditional imperative approaches, which require manual intervention and can be prone to errors.

Key Features of ArgoCD

ArgoCD provides a wide range of features that make it a powerful and flexible tool for managing Kubernetes deployments. Some of the key features of ArgoCD include:

  • Declarative Configuration Management: ArgoCD uses Git as the source of truth for managing the desired state of the system. This allows you to define the desired state of your infrastructure using YAML files, and ArgoCD will automatically apply these changes to the Kubernetes cluster.

  • Automated Synchronization: ArgoCD monitors the Git repository for changes, and when a change is detected, it automatically synchronizes the cluster with the updated configuration.

  • Rollbacks and Version Control: ArgoCD provides built-in support for rollbacks and version control. This allows you to easily revert to a previous version of your deployment if something goes wrong.

  • GitOps Principles: ArgoCD adheres to the core principles of GitOps, which means that it provides a streamlined and automated approach to managing Kubernetes deployments.

Getting Started with ArgoCD

Getting started with ArgoCD is easy. The first step is to install ArgoCD on your Kubernetes cluster. You can do this by following the official installation guide, which provides detailed instructions for installing ArgoCD using various deployment methods.

Once ArgoCD is installed, the next step is to connect it to your Git repository. ArgoCD supports a wide range of Git repositories, including GitHub, GitLab, and Bitbucket.

After connecting your Git repository to ArgoCD, you can start managing your deployments using YAML files. ArgoCD provides a powerful web interface that allows you to easily manage and monitor your deployments.

How to setup ArgoCD

  • Create a namespace for ArgoCD:
apiVersion: v1
kind: Namespace
metadata:
  name: argocd
  • Create a secret to store the admin password for ArgoCD:
apiVersion: v1
kind: Secret
metadata:
  name: argocd-secret
  namespace: argocd
type: Opaque
data:
  admin.password: <base64 encoded password>

Note: Replace with the base64 encoded value of the admin password.

  • Create a ConfigMap to store the ArgoCD configuration:
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  namespace: argocd
data:
  application.instanceLabelKey: app.kubernetes.io/name
  application.instanceLabelValue: argocd
  • Create a ServiceAccount for ArgoCD:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: argocd-sa
  namespace: argocd
  • Create a ClusterRoleBinding to grant ArgoCD permissions to access the Kubernetes API:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: argocd-crb
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: argocd-sa
  namespace: argocd

Note: The cluster-admin role may not be suitable for your environment. Please adjust the role to meet your security requirements.

  • Create a Deployment for ArgoCD:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: argocd-server
  namespace: argocd
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: argocd-server
      app.kubernetes.io/part-of: argocd
  template:
    metadata:
      labels:
        app.kubernetes.io/name: argocd-server
        app.kubernetes.io/part-of: argocd
    spec:
      serviceAccountName: argocd-sa
      containers:
      - name: argocd-server
        image: argoproj/argocd:v2.1.3
        command:
        - argocd-server
        args:
        - --staticassets=/usr/share/argocd/static
        - --insecure
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: argocd-cm
          mountPath: /app/config
        - name: argocd-static-assets
          mountPath: /usr/share/argocd/static
      volumes:
      - name: argocd-cm
        configMap:
          name: argocd-cm
      - name: argocd-static-assets
        configMap:
          name: argocd-static-assets

Note: Adjust the image tag to match the version of ArgoCD that you wish to use.

  • Create a Service for ArgoCD:
apiVersion: v1
kind: Service
metadata:
  name: argocd-server
  namespace: argocd
spec:
  ports:
  - name: http
    port: 80
    target

Examples

An example ArgoCD application manifest for deploying all the YAML files using Kustomize:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
spec:
  destination:
    name: ''
    namespace: default
    server: 'https://kubernetes.default.svc'
  source:
    path: .
    repoURL: 'https://github.com/my-org/my-repo.git'
    targetRevision: HEAD
    kustomize:
      namePrefix: my-app-
      namespace: default
      paths:
        - base
        - overlays/dev
        - overlays/production
      images:
        - name: my-image
          newTag: latest
  project: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
  health:
    readiness:
      path: /healthz
      port: http
    liveness:
      path: /healthz
      port: http
  plugin:
    env:
      - name: MY_ENV_VAR
        value: my-value

In this example, the destination field specifies the Kubernetes cluster where the application should be deployed, while the source field specifies the source code repository and the Kustomize configuration. The kustomize field specifies the name prefix, namespace, and paths for the Kustomize configuration, as well as any image updates that should be made. The syncPolicy field specifies the synchronization policy, including automatic pruning and self-healing. The health field specifies the health check endpoints for the application, while the plugin field specifies any environment variables that should be set for the application.

You would need to create a separate YAML file for each Kubernetes kind that you want to deploy with ArgoCD, and then use the kubectl apply command to create the ArgoCD application for each YAML file. For example, to create the ArgoCD application for the Kubernetes Deployment YAML file, you could run the following command:

kubectl apply -f deployment-app.yaml

Conclusion

In conclusion, GitOps is a powerful and modern approach to software delivery that provides a streamlined and automated way to manage Kubernetes deployments. ArgoCD is a popular GitOps tool that provides a wide range of features for managing Kubernetes deployments, including declarative configuration management, automated synchronization, rollbacks and version control, and adherence to GitOps principles. With ArgoCD, you can easily manage your Kubernetes deployments using YAML files and a powerful web interface, all while ensuring that your infrastructure is always in the desired.