Benjamin Kušen
January 6, 2024

Simplifying MaC with Crossplane

In this article we are going to explore the concept of Monitoring as Code and dive deep into crossplane, a really powerful Monitoring as code tool.

In today’s fast-paced world of Decops, effectively managing the cloud resources has become critical for maintaining cloud based systems. A popular concept for managing cloud resources is Monitoring as code or MAC. MAC was first introduced in the 2010s and it got popular in 2018 with widespread adoption of Devops. Monitoring as code is often a part of Infrastructure as code policy (Iac) and it helps in defining,observing and managing resources using code.

MaC provides several advantages like it streamlines the monitoring procedures and makes sure that infrastructure related information is automatically updated in the monitoring dashboard. This approach also reduces human error.

What Is Crossplane?

Crossplane is an open source project that provides multi-cloud control planes. It acts as a bridge and connects kubernetes clusters to non-kubernetes resources outside the cluster. This enables devops teams to design APIs that can consume resources outside the kubernetes cluster.

In order to represent external resources as native kubernetes objects it creates custom resource definitions. As a native kubernetes objects commands like kubectl create and kubectl describe can be used. Crossplane allows monitoring of external resources as a Kubernetes Controller. In case of any modification or accidental delete it restores the changes and also recreates the deleted resources.

Crosplane is also referred to as a developer platform as a developer has to only provide the high-level requirements of resources.

How Crossplane Facilitates Monitoring as Code?

Let’s discuss how we can use Crossplane to configure the already existing monitoring setup. By defining monitioring configurations as Kubernetes manifests we unlock the potential of kubernetes. This includes the ability to enforce policies, backup definitions, and integrate monitoring into a GitOps workflow. Crossplane enables developers to manage infrastructure dependencies, monitoring dependencies and application logic all as a single bundle.

In crossplane we can benefit from the control-plane approach of kubernetes. In the crossplane monitoring platform kubernetes can cross check that the desired and actual state to ensure everything is up-to-date. It provides a centralized platform for all the teams to update and access the resources being monitored.

Use-Case: Build a Dashboard for Cloud Services Using Crossplane

Let's assume a scenario in which the platform team needs a dashboard that gets updated every time a new infrastructure is created. First let’s create an abstraction for the development team. The abstraction is nothing but an API that allows other teams to access our monitoring dashboard. Keep in mind that these abstractions are going to be based on the cloud service provider.

For API definition we will be using CompositeResourceDefinition (XRD) with an OpenAPI Schema. Now we can apply this to the kubernetes cluster but make sure that the Crossplane is already installed on that cluster. It will simply create a resource type in the kubernetes cluster with our specified name.

<pre class="codeWrap"><code>#apis/acmebuckets/definition.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
 name: xacmebuckets.storage.example.com
spec:
 group: storage.example.com
 claimNames:
   kind: AcmeBucket
   plural: acmebuckets
 versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
...
                properties:
                  bucketName:
                    description: The Bucket name
                    type: string
</code></pre>

When deployed to a Kubernetes Cluster a Crossplane installation, this will result in the creation of a new Kubernetes resource type called AcmeBucket.

In the next step create a bucket with a specific region and add a monitoring dashboard to it. Here we have used S3 bucket as storage and for monitoring Grafana will be used.

<pre class="codeWrap"><code># apis/acmebuckets/composition.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: xacmebuckets-aws-grafana
spec:
  compositeTypeRef:
    apiVersion: storage.example.com/v1alpha1
    kind: XAcmeBucket
  resources:
  - name: bucket
    base:
      apiVersion: s3.aws.crossplane.io/v1beta1
      kind: Bucket
      spec:

        forProvider:
          acl: public-read-write
          locationConstraint: us-east-1
    patches: ...
-  name: dashboard
    base:
      apiVersion: oss.grafana.crossplane.io/v1alpha1
      kind: Dashboard
      spec:
        spec:
          forProvider:
            configJson: |
              { // Grafana dashboard definition }
</code></pre>

Once the cloud service provider and monitoring tools are set up, developers can just create a new bucket and spin up the monitoring dashboard.

<pre class="codeWrap"><code># Create a custom acme-bucket:
$ kubectl create -n dev-space -f my-bucket.yaml
storage.example.com/acmebucket created

# Check all managed resources that the composition has created:
$ kubectl get managed
NAME                                          READY SYNCED AGE
bucket.s3.aws.crossplane.io/test-bucket-24496 True  True   43s
NAME                                          READY SYNCED AGE
dashboard.oss.grafana.crossplane.io/test-123  True  True   43s

# Get the URL of the Grafana dashboard:
$ kubectl get dashboard.oss.grafana.crossplane.io/test-123 -o yaml | yq .status.atProvider.url
https://random123.grafana.net/d/crossplane/crossplane-test
</code></pre>

This is how simple it is to build and manage monitoring platforms using Crossplane.

Conclusion

Crossplane serves as a powerful tool by facilitating cloud resource management and Monitoring as Code efficient. Its unique feature to represent non-kubernetes resources as kubernetes objects and bridge the gap by connecting kubernetes to external resources is revolutionary. The gist is that Crosplane provides a centralized platform for monitoring cloud resources while maintaining consistency and accuracy throughout the system.

Facing Challenges in Cloud, DevOps, or Security?
Let’s tackle them together!

get free consultation sessions

In case you prefer e-mail first:

Thank you! Your message has been received!
We will contact you shortly.
Oops! Something went wrong while submitting the form.
By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information. If you wish to disable storing cookies, click here.