Skip to main content

Continuos Deployment with Argo CD

Having to run kubectl apply -f every time you want to deploy a new version of your application is not the best way to do it. It's better to have a system that automatically deploys the changes when you push them to a Git repository. This is where Argo CD comes into play.

Architecture​

Components​

There are three main Custom Resource Definitions (CRD) that are used to automatically deploy applications in Kubernetes using Argo CD:

  • Application: represent a group of Kubernetes resources defined by a manifest that can be in a Git repository or a Helm chart.
  • Repository: represent a Git repository that contains the manifests or Helm charts.
  • Project: represent a group of applications that share the same configuration (trusted repositories, permissions, etc.).

We are going to use the same repository for all the applications (https://github.com/AgrospAI/apps-deploy), so it's easier to manage them and reference other deployments to create new ones.

Declarative configuration​

Although you can create the components from the Argo CD UI, it's better to use a declarative configuration to have everything under version control. This will allow us to track changes, rollback, save the state of the cluster, control who can push changes, force to use only pull requests, etc.

All the components are defined inside manifests/default/argo-cd. The structure is the following:

manifests/ # (1)!
├── default/argo-cd/ # (3)!
│ ├── projects/
│ │ ├── project-1/
│ │ │ ├── applications/
│ │ │ │ ├── application-1.yaml
│ │ │ │ └── application-2.yaml
│ │ │ └── project-1.yaml
│ │ └── project-2/
│ │ ├── applications/
│ │ │ └── ...
│ │ └── project-2-yaml
│ ├── repositories/
│ │ ├── repository-a.yaml
│ │ └── repository-b.yaml
│ └── bootstrap.yaml # (4)!
├── project-1/
│ ├── application-1/ # (2)!
│ │ └── *.yaml
│ └── application-2/
│ └── *.yaml
└── project-2/
└── ...
  1. manifests/: contains the configuration for all the applications organized by projects.

  2. manifests/project/application/: contains the manifests files of application that belongs to project.

  3. manifests/default/argo-cd/: contains the declarative setup of Argo CD, so it has manifests for creating the projects, the applications and the repositories. Note that the applications are nested inside the projects to keep everything organized.

  4. manifests/default/argo-cd/bootstrap.yaml: is the root application which references (applies the configurations for creating) all the Argo CD components. Those configurations are manifests/default/argo-cd/**/*.yaml. Note that it also references itself, so it also keeps track of its own changes.

Bootstrap Argo CD​

  1. Install Argo CD manually

  2. Apply custom resources

    kubectly apply -f manifests/system/argo-cd

    Maybe you need to reload Argo CD, if so, run kubectl rollout restart deployment/argo-cd-argocd-server -n argo-cd

  3. Add the repositories

    kubectl apply -f manifests/default/argo-cd/repositories
  4. Bootstrap the projects

    kubectl apply -f manifests/default/argo-cd/bootstrap.yaml

Adding a new repository​

GitHub App​

Install the AgrospAI - Argo CD to access the repository.

  • Repository permissions:
    • Contents: Read and Write
    • Commit statuses: Read and Write

Manifest file​

Create the file manifests/default/argo-cd/repositories/repository.yaml. This is a secret that contains the credentials to access the repository. Example file:

apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
name: repository-apps-deploy
namespace: argo-cd
spec:
secretStoreRef:
kind: ClusterSecretStore
name: infisical-kubernetes-prod

target:
name: repository-apps-deploy
template:
metadata:
labels:
argocd.argoproj.io/secret-type: repository
data:
name: apps-deploy
type: git
url: https://github.com/AgrospAI/apps-deploy.git
githubAppID: '{{ .githubAppID }}'
githubAppInstallationID: '{{ .githubAppInstallationID }}'
githubAppPrivateKey: '{{ .githubAppPrivateKey }}'

data:
- secretKey: githubAppID
remoteRef:
key: GITHUB_APP_ID_ARGO_CD
- secretKey: githubAppInstallationID
remoteRef:
key: GITHUB_APP_INSTALLATION_ID_ARGO_CD
- secretKey: githubAppPrivateKey
remoteRef:
key: GITHUB_APP_PRIVATE_KEY_ARGO_CD

Adding a new project​

Create the folder manifests/default/argo-cd/projects/project and add project.yaml file. This is an example of the content:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: { { project } }
namespace: argo-cd
spec:
description: { { description } }
sourceRepos:
- '*'
destinations:
- namespace: '*'
server: '*'
clusterResourceWhitelist:
- group: '*'
kind: '*'
namespaceResourceWhitelist:
- group: '*'
kind: '*'

Adding a new application​

Create the folder manifests/default/argo-cd/projects/project/applications and add application.yaml file.

Example for applying just a folder with manifests:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: { { application } }
namespace: argo-cd
spec:
project: { { project } }
source:
repoURL: https://github.com/AgrospAI/apps-deploy.git
path: manifests/{{project}}/{{application}}
targetRevision: HEAD
directory:
jsonnet: {}
include: '*.yaml'
destination:
server: https://kubernetes.default.svc
namespace: { { namespace } }
syncPolicy:
automated: {}

Example for applying a Helm chart:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: { { application } }
namespace: argo-cd
spec:
project: { { project } }
sources:
- repoURL: { { chart-repo } }
chart: { { chart } }
targetRevision: { { chart-revision } }
helm:
releaseName: { { release } }
valueFiles:
- $values/manifests/default/argo-cd/projects/{{project}}/applications/{{application}}/values.yaml
- repoURL: https://github.com/AgrospAI/apps-deploy.git
targetRevision: HEAD
ref: values
destination:
server: 'https://kubernetes.default.svc'
namespace: argo-rollouts
syncPolicy:
syncOptions:
- CreateNamespace=true

You can get the chart repo and revision from the helm show chart {repo}/{chart} command.

Automatically sync Argo-CD when pushing on GitHub​

By default Argo CD pulls the changes from the Git repository every 3 minutes. If you want to sync the changes immediately after pushing to the repository, you can use a webhook.

Follow this instructions: https://argo-cd.readthedocs.io/en/stable/operator-manual/webhook/