TranslateProject/sources/tech/20210812 Automatically create multiple applications in Argo CD.md
DarkSun 8b16dfcc40 选题[tech]: 20210812 Automatically create multiple applications in Argo CD
sources/tech/20210812 Automatically create multiple applications in Argo CD.md
2021-08-13 05:03:50 +08:00

7.9 KiB
Raw Blame History

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. gears and lightbulb to represent innovation

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

Since Argo CDs 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.


via: https://opensource.com/article/21/7/automating-argo-cd

作者:Ayush Sharma 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出