Scaling Event-Driven Applications Made Easy with Sveltos Cross-Cluster Configuration
Tags: kubernetes, opensource, showdev, eventdriven
Published: 2023-04-04 08:26:04

Event-driven architectures are becoming increasingly popular as a way to build scalable, decoupled, and resilient systems. [Sveltos](https://github.com/projectsveltos) is an open-source to deploy add-ons in tens of #Kubernetes clusters. Sveltos also has a built-in event-driven framework that makes it easy to deploy add-ons on Kubernetes clusters as result of events.

## What is Sveltos?

Sveltos is a powerful open source project that makes managing Kubernetes add-ons a breeze. It automatically discovers [ClusterAPI](https://github.com/kubernetes-sigs/cluster-api) powered clusters and allows you to easily register any other cluster (like GKE). Then, it seamlessly manages Kubernetes add-ons across all your clusters.

But that's not all! Sveltos comes loaded with features like event-driven add-on deployment (using Lua scripts), configuration drift detection, and multi-tenancy to easily manage permissions for tenant admins. You can even preview changes with a dry run and rollback configurations with ease.

## Event driven

Sveltos event-driven framework enables the deployment of add-ons in response to events occurring in managed Kubernetes clusters. Events are defined in the form of Lua scripts, which provide a flexible and powerful way to define complex event conditions.

Add-ons are expressed as templates that define the desired state of the resources that will be created in response to the event. These templates can be instantiated using information from resources in the managed clusters, such as labels or annotations, making it easy to dynamically generate new resources based on specific conditions.

With Sveltos, you can create highly scalable and flexible event-driven systems that can automatically respond to changes in your managed Kubernetes clusters. Whether you're deploying new resources or modifying existing ones, Sveltos makes it easy to manage your infrastructure in a highly automated and efficient way, all while maintaining the reliability and scalability that Kubernetes is known for.

Sveltos event-driven framework is covered [here](https://projectsveltos.github.io/sveltos/addon_event_deployment/).

## Cross-cluster configuration

Sveltos' event-driven framework is designed to also enable communication and coordination between multiple Kubernetes clusters, which makes it ideal for cross-cluster configuration. By leveraging Sveltos' cross-cluster configuration, you can create distributed event-driven systems that span multiple clusters, enabling you to build more complex and scalable applications.

Sveltos by default will deploy add-ons in the very same cluster an event is detected. Sveltos though can also be configured for cross-cluster configuration: watch for events in a cluster and deploy add-ons in a set of different clusters.

[EventBasedAddOn](https://raw.githubusercontent.com/projectsveltos/event-manager/main/api/v1alpha1/eventbasedaddon_types.go) CRD has a field called _destinationClusterSelector_, a Kubernetes label selector. This field is optional and not set by default. In such a case, Sveltos default behavior is to deploy add-ons in the same cluster where the event was detected.

If this field is set, Sveltos behavior will change. When an event is detected in a cluster, add-ons will be deployed in all the clusters matching the label selector destinationClusterSelector.

![Sveltos cross-cluster configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21njkcdo5sld1rm1qzdy.gif)

Imagine you're managing two Kubernetes clusters - one on GKE and another provisioned by ClusterAPI. How do you ensure they're working in harmony? That's where Sveltos comes in. With Sveltos, you can create an _EventSource_ that matches any Service with a load balancer IP, and an _EventBasedAddOn_ that references the EventSource and deploys a selector-less Service and corresponding Endpoints in any cluster matching _destinationClusterSelector_.

In simpler terms, Sveltos allows you to watch for specific events (like load balancer services), and respond to them by automatically deploying new resources across multiple clusters. By defining templates that specify how resources should be instantiated, Sveltos can create new Services and Endpoints on the fly, making it easy to manage complex, distributed systems.

For example, in the GKE cluster, you can create a deployment and a Service of type LoadBalancer. The Service will be assigned an IP address and match the EventSource, triggering Sveltos to deploy the selector-less Service and Endpoints in the other cluster - the cluster-api provisioned cluster. And just like that, a pod in the cluster-api provisioned cluster can now reach the service in the GKE cluster, thanks to Sveltos' cross-cluster configuration.

Here are the YAMLs

```yaml
apiVersion: lib.projectsveltos.io/v1alpha1
kind: EventSource
metadata:
 name: load-balancer-service
spec:
 collectResources: true
 group: ""
 version: "v1"
 kind: "Service"
 script: |
  function evaluate()
    hs = {}
    hs.matching = false
    hs.message = ""
    if obj.status.loadBalancer.ingress ~= nil then
      hs.matching = true
    end
    return hs
  end
```

```yaml
apiVersion: lib.projectsveltos.io/v1alpha1
kind: EventBasedAddOn
metadata:
 name: service-policy
spec:
 sourceClusterSelector: env=production
 destinationClusterSelector: dep=eng
 eventSourceName: load-balancer-service
 oneForEvent: true
 policyRefs:
 - name: service-policy
   namespace: default
   kind: ConfigMap
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: service-policy
  namespace: default
data:
  service.yaml: |
    kind: Service
    apiVersion: v1
    metadata:
      name: external-{{ .Resource.metadata.name }}
      namespace: external
    spec:
      selector: {}
      ports:
        {{ range $port := .Resource.spec.ports }}
        - port: {{ $port.port }}
          protocol: {{ $port.protocol }}
          targetPort: {{ $port.targetPort }}
        {{ end }}
  endpoint.yaml: |
    kind: Endpoints
    apiVersion: v1
    metadata:
       name: external-{{ .Resource.metadata.name }}
       namespace: external
    subsets:
    - addresses:
      - ip: {{ (index .Resource.status.loadBalancer.ingress 0).ip }}
      ports:
        {{ range $port := .Resource.spec.ports }}
        - port: {{ $port.port }}
        {{ end }}
```

Sveltos is a powerful tool for managing distributed Kubernetes clusters and building scalable, reliable applications that can seamlessly span multiple clusters. With Sveltos, you can create event-driven systems that respond automatically to changes in your infrastructure, making it easy to build complex, distributed systems that work together seamlessly.

## Summary

1. Sveltos' event-driven framework can be configured for cross-cluster deployment, allowing resources to be deployed across multiple clusters.
2. An EventSource can be defined to watch for specific events, such as load balancer services in any cluster.
3. An EventBasedAddOn can be configured to respond to those events by deploying new resources in other clusters.
4. Sveltos can use templates to instantiate new resources, making it easy to manage complex distributed systems.
5. With Sveltos, you can build scalable, reliable applications that can seamlessly span multiple clusters, making it a powerful tool for managing Kubernetes infrastructure.


## Contributing to Sveltos

🎉 We welcome and appreciate all contributions to Sveltos! Whether you want to report a bug, suggest a new feature, or simply ask a question, we'd love to hear from you. Here are a few ways to connect with us:

1. Open a bug/feature enhancement on github [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/projectsveltos/sveltos-manager/issues)
2. Chat with us on the Slack in the #projectsveltos channel [![Slack](https://img.shields.io/badge/join%20slack-%23projectsveltos-brighteen)](https://join.slack.com/t/projectsveltos/shared_invite/zt-1hraownbr-W8NTs6LTimxLPB8Erj8Q6Q)
3. [Contact Us](mailto:support@projectsveltos.io)