How to build scalable, modular, and event-driven applications using Sveltos and Kubernetes

Karan Singh Avatar

 

In modern application development, microservices architecture is becoming increasingly popular due to its scalability, reliability, and agility. However, managing microservices can be complex, and it requires a platform that can handle containerized applications, like Kubernetes. Sveltos is a microservices framework that simplifies the development of microservices and can be easily deployed on Kubernetes. In this blog, we will explore Sveltos with Kubernetes, how it works, how to install it, how to deploy add-ons using event-based triggers, and how to implement multi-tenancy.

How it works

Sveltos is a microservices framework that allows developers to build scalable, modular, and event-driven applications. It follows the principles of the Reactive Manifesto, which focuses on building systems that are responsive, resilient, elastic, and message-driven.

Sveltos uses a command and event-driven architecture. Commands are requests that are sent to a service to perform an action, while events are notifications that are sent when something happens in the system. Services in Sveltos are built as independent microservices that communicate with each other using events.

Sveltos provides a set of core components, including a service registry, event bus, and command bus, to handle the communication between services. The service registry keeps track of all the services registered with Sveltos, while the event bus and command bus handle the communication between services.

Features List

  1. Kubernetes add-on distribution across multiple clusters;
  2. Event driven framework to deploy add-ons as response to events in managed clusters. Event can be defined in the form of Lua script. Add-ons can be expressed as template and instantiated using information from resources in the managed clusters;
  3. Sveltos’ event driven framework can also be configured for cross-cluster configuration;
  4. Sveltos extensions expand Sveltos by incorporating your own controller to dynamically generate the necessary information required for deploying addons via an automated process;
  5. Configuration drift detection;
  6. Notification: Sveltos can be configured to send notifications (Kubernetes event and Slack, Webex messages) when for instance all add-ons are deployed in a cluster;
  7. Addons can be represented as templates and instantiated using values from management cluster resources. Sveltos can fetch any resource in the management cluster provided that the appropriate RBAC permissions are granted;
  8. Multi-tenancy allowing platform admin to easily grant permissions to tenant admins and have Sveltos enforces those;
  9. Dry run to preview effect of a change;
  10. Kubernetes cluster classification and automatic label management based on cluster runtime states;
  11. Techsupport: collect tech support from managed clusters;
  12. Configuration snapshots, diff and rollback: freeze your policy configurations in time, compare changes between snapshots, and roll back or forward to any saved snapshot.

Installation

To install Sveltos on Kubernetes, we need to follow the below steps:

Step 1: Create a Kubernetes cluster

To create a Kubernetes cluster, we can use any cloud provider like Amazon Web Services, Google Cloud Platform, or Microsoft Azure. We can also use a local Kubernetes installation like minikube or kind.

Step 2: Install Sveltos

To install Sveltos, we can use the Helm package manager. Helm is a popular package manager for Kubernetes that allows us to manage Kubernetes applications and their dependencies.
To install Sveltos using Helm, we can use the following command:

helm install sveltos sveltos/sveltos

This will install Sveltos with all its dependencies on the Kubernetes cluster.

Event-Based Add-On Deployment:

First, we will create a Kubernetes deployment YAML file for the Sveltos add-on:

apiVersion: projectdveltos.io/v1alpha1
kind: Sveltos
metadata:
  name: sveltos-addon
spec:
  image: your-image-name:tag
  replicas: 1
  env:
    - name: MY_VAR
      value: my_value
  triggers:
    - name: trigger-1
      event-type: my-event-type
      selector:
        matchLabels:
          app: my-app
      action:
        method: POST
        url: http://my-service/my-endpoint
        body: {"message": "Hello from Sveltos!"}

In this YAML file, we define a Sveltos add-on with the following properties:

  • image: the Docker image containing the Sveltos add-on.
  • replicas: the number of replicas to deploy.
  • env: environment variables to pass to the Sveltos add-on container.
  • triggers: a list of triggers that define when the add-on should be activated.

In this example, we define one trigger with the following properties:

  • name: a unique name for the trigger.
  • event-type: the type of event to trigger the add-on.
  • selector: a Kubernetes selector that specifies which resources the trigger should be applied to.
  • action: the action to perform when the trigger is activated.

In this example, the action is to perform an HTTP POST request to a service with a JSON payload containing a message.

Next, we will create a Kubernetes service YAML file to expose the add-on:

apiVersion: v1
kind: Service
metadata:
  name: sveltos-addon
spec:
  selector:
    app: sveltos-addon
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

In this YAML file, we define a Kubernetes service with the following properties:

  • selector: a Kubernetes selector that specifies which pods to target with the service.
  • ports: the ports to expose on the service.

In this example, we expose port 80 on the service and target port 8080 on the Sveltos add-on container.

To deploy the Sveltos add-on, we can apply both YAML files using the kubectl apply command:

kubectl apply -f sveltos-addon.yaml
kubectl apply -f sveltos-addon-service.yaml

This will deploy the Sveltos add-on with one replica and expose it on port 80. The add-on will be triggered when an event of type my-event-type is detected on a resource with the label app=my-app.

Note that you will need to have the Sveltos controller installed on your Kubernetes cluster in order for the projectdveltos.io/v1alpha1 API version to be available. You can install the controller using the instructions provided in the Sveltos documentation.

Multi-Tenancy

In Svelto, multi-tenancy is achieved using the Tenant and Namespace resources. A Tenant is a logical grouping of resources within a Kubernetes cluster, while a Namespace is a Kubernetes construct that provides logical isolation between resources.

Here’s an example of how you can define a Tenant and a Namespace using the Tenant and Namespace resources in Svelto’s API:

apiVersion: projectdveltos.io/v1alpha1
kind: Tenant
metadata:
  name: example-tenant
spec:
  namespacePrefix: exmpl
---
apiVersion: projectdveltos.io/v1alpha1
kind: Namespace
metadata:
  name: exmpl-namespace
  annotations:
    tenant: example-tenant
spec:
  tenantRef:
    name: example-tenant

In this example, we define a Tenant named example-tenant with a namespace prefix of exmpl. We also define a Namespace named exmpl-namespace with a reference to the example-tenant tenant. The annotations field is used to store metadata about the namespace, such as the tenant to which it belongs.
Once you have defined your Tenant and Namespace, you can use them to deploy your application. Here’s an example of how you can define a Kubernetes deployment for a multi-tenant application

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: example-container
          image: example-image
          env:
            - name: MY_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace

In this example, we define a Kubernetes deployment with a single replica. The selector field is used to match the deployment to a specific Namespace. The template field is used to define the pod template for the deployment, including the container image and environment variables.

The environment variable MY_NAMESPACE is set to the namespace of the pod, which is automatically injected by Kubernetes. This allows the application to determine which namespace it is running in, and therefore which tenant it belongs to.

Conclusion

Overall, Sveltos is a powerful tool for deploying and managing applications on Kubernetes. Its event-based add-on deployment system and support for multi-tenancy make it a great choice for organizations that need to manage a large number of applications across multiple tenants. By following the installation and configuration steps outlined above, you can start using Sveltos to deploy and manage your own Kubernetes applications with ease.

Leave a Reply

Your email address will not be published. Required fields are marked *