Automatically create multiple applications in Argo CD

In this tutorial, I will show you how to automatically create multiple applications in Argo CD using Argo CD.
43 readers like this.
gears and lightbulb to represent innovation

Opensource.com

In a previous article, I demonstrated how Argo CD makes pull-based GitOps deployments simple. In this tutorial, I’ll show you how to automatically create multiple applications in Argo CD using Argo CD itself.

Since Argo CD’s job is to listen to a repo and apply the Manifest files it finds to the cluster, you can use this approach to configure Argo CD internals as well. In my previous example, I used the GUI to create a sample Nginx application with three replicas. This time, I use the same approach as before, but I create an application from the GUI to deploy three separate applications: app-1, app-2, and app-3.

Configuring our child applications

First, start by creating the Manifest files for your three applications. In my example-assets repository, I have created three applications under argocd/my-apps. All three applications are Nginx with three replicas. Be sure to create each application in its own folder.

Create a YAML file to define the first application and save it as my-apps/app-1/app.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app-1
  labels:
    app: nginx-app-1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app-1
  template:
    metadata:
      labels:
        app: nginx-app-1
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Create another one for your second application and save it as my-apps/app-2/app.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app-2
  labels:
    app: nginx-app-2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app-2
  template:
    metadata:
      labels:
        app: nginx-app-2
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Create a third for your final app and save it as my-apps/app-3/app.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app-3
  labels:
    app: nginx-app-3
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app-3
  template:
    metadata:
      labels:
        app: nginx-app-3
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Now that your Manifest files are ready, you must create Argo CD Applications pointing to those Manifests.

Argo CD can be configured in three different ways: using the GUI, using the CLI, or using Kubernetes Manifest files. In this article, I use the third method.

Create the following Manifest files in a new folder argocd/argo-apps. This is argocd-apps/app-1.yml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app-1
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: argocd
    server: https://kubernetes.default.svc
  project: default
  source:
    path: argocd/my-apps/app-1
    repoURL: https://gitlab.com/ayush-sharma/example-assets.git
    targetRevision: HEAD

This is argocd-apps/app-2.yml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app-2
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: argocd
    server: https://kubernetes.default.svc
  project: default
  source:
    path: argocd/my-apps/app-2
    repoURL: https://gitlab.com/ayush-sharma/example-assets.git
    targetRevision: HEAD

And this is argocd-apps/app-3.yml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app-3
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: argocd
    server: https://kubernetes.default.svc
  project: default
  source:
    path: argocd/my-apps/app-3
    repoURL: https://gitlab.com/ayush-sharma/example-assets.git
    targetRevision: HEAD

As you can see, you are creating a Kubernetes object called Application in the argocd namespace. This object contains the source Git repository and destination server details. Your Applications are pointing to the Nginx manifest files you created earlier.

Configuring our main application

Now you need some way to tell Argo CD how to find your three Nginx applications. Do this by creating yet another Application. This pattern is called the App of Apps pattern, where one Application contains the instructions to deploy multiple child Applications.

Create a new Application from the GUI called my-apps with the following configuration:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-apps
spec:
  destination:
    namespace: default
    server: 'https://kubernetes.default.svc'
  source:
    path: argocd/argocd-apps
    repoURL: 'https://gitlab.com/ayush-sharma/example-assets.git'
    targetRevision: HEAD
  project: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

Once it has been created, my-apps begins syncing in the GUI:

Automating ArgoCD with ArgoCD! - Main app.

Figure 1: Automating ArgoCD with ArgoCD! - Main app.

After the sync is complete, your three Nginx applications appear in the GUI as well:

Automating ArgoCD with ArgoCD! - Dashboard.

Figure 2: Automating ArgoCD with ArgoCD! - Dashboard.

Since you didn't enable AutoSync, manually sync app-1, app-2, and app-3. Once synced, your Nginx replicas are deployed for all three apps.

Automating ArgoCD with ArgoCD! - Deployment.

Figure 3: Automating ArgoCD with ArgoCD! - Deployment.

Conclusion

Mastering the App of Apps pattern is critical to leveraging the full power of Argo CD. This method allows you to manage groups of applications cleanly. For example, deploying Prometheus, Grafana, Loki, and other vital services could be managed by a DevOps Application, while deploying frontend code could be managed by a Frontend Application. Configuring different sync options and repo locations for each gives you precise control over different application groups.

What to read next

Get started with Argo CD

Argo CD is a simple pull-based GitOps deployment tool that syncs Kubernetes manifest files with a cluster for easy, no-nonsense deployments.

https://ayushsharma.in
I am a writer and AWS Solutions Architect. I work with startups and enterprises on Software Engineering, DevOps, SRE, and Cloud Architecture. I write about my experiences on https://ayushsharma.in.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.