Bootstrap FreeKB - ArgoCD - Add Cluster using the CLI
ArgoCD - Add Cluster using the CLI

Updated:   |  ArgoCD articles

If possible, I find it helpful to remove my users /home/<username>/.kube/config file so that I when I log into Kubernetes or OpenShift, a new .kube/config file will be created.

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

There is no option to add a cluster to ArgoCD in the ArgoCD console at Settings > Clusters thus you must add the cluster on the command line.

The following commands must be run on the Kubernetes or OpenShift cluster that you want to add to ArgoCD.

This assumes you have already created the argocd-manager service account, the argocd-manager-role cluster role, and the argocd-manager-role-binding cluster role binding. If not, check out my article create the argocd-manager Service Account.

Let's create a YAML file that will be used to create a secret which will add the cluster to ArgoCD.

apiVersion: v1
kind: Secret
metadata:
  namespace: openshift-gitops <- must be the namespace ArgoCD was installed in
  name: api-dev-openshift-example-com
  labels:
    argocd.argoproj.io/secret-type: cluster
type: Opaque
stringData:
  name: api-dev-openshift-example-com
  server: https://api.dev.openshift.example.com:6443
  config: |
    {
      "bearerToken": "<bearer token>", <- this is the token you made note of a moment ago
      "tlsClientConfig": {
        "insecure": true <- this can be true for SSL - I'm just going with false to simplify this tutorial
      }
    }

 

Let's log into our Kubernetes or OpenShift cluster that ArgoCD is installed on.

oc login -u john.doe api.controller.openshift.example.com:6443

 

The auth can-i command can be used to determine if you have permission to create a Secret.

~]$ oc auth can-i create secret
yes

 

And then use the kubectl (Kubernetes) or oc (OpenShift) apply command to create the secret in the namespace ArgoCD is installed in (usually the argocd or openshift-gitops namespace). The creation of this secret should add the cluster to ArgoCD.

kubectl apply -f cluster.yaml --namespace <the namespace argocd is in - usually argocd or openshift-gitops>

 

There should now be a secret.

~]$ oc get secret my-cluster --namespace <the namespace argocd is in - usually argocd or openshift-gitops>
NAME                                    TYPE     DATA   AGE
api-dev-openshift-example-com           Opaque   3      5d4h

 

And the argocd cluster list command should include the cluster you just added. Awesome! Don't worry that the status is "Unknown" with message "Cluster has no applications and is not being monitored." This simply appears because no application have been created on the newly added cluster. Once an application is created and sync'd on the newly added cluster, the status should update to "Successful." So let's Create Application using the CLI.

~]$ argocd cluster list --grpc-web
SERVER                                      NAME                VERSION  STATUS      MESSAGE                                                  PROJECT
https://api.dev.openshift.example.com:6443  dev-cluster                  Unknown     Cluster has no applications and is not being monitored.
https://kubernetes.default.svc              in-cluster          1.27     Successful

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 934c4e in the box below so that we can be sure you are a human.