Using Sveltos to Deploy Kubernetes Resources in a Controlled and Orderly Manner
Tags: showdev, kubernetes, opensource, devops
Published: 2023-08-21 13:40:13

When deploying Kubernetes resources in a cluster, it is sometimes necessary to deploy them in a specific order. For example, a CustomResourceDefinition (CRD) must exist before any custom resources of that type can be created.

[Sveltos](https://github.com/projectsveltos/addon-controller) can help you solve this problem by allowing you to specify the order in which Kubernetes resources are deployed.

## ClusterProfile order
A [ClusterProfile](https://raw.githubusercontent.com/projectsveltos/addon-controller/main/api/v1alpha1/clusterprofile_types.go) is a Kubernetes custom resource definition (CRD) that defines the resources that you want to deploy on a set of Kubernetes clusters.

ClusterProfile allows customer to define an order:

- **Using the helmCharts field**: The helmCharts field allows you to specify a list of Helm charts that need to be deployed. Sveltos will deploy the Helm charts in the order that they are listed in this field.

- **Using the policyRefs field**: The policyRefs field allows you to reference a list of ConfigMap and Secret resources whose contents need to be deployed. Sveltos will deploy the resources in the order that they are listed in this field.

Here are some examples:

- The following ClusterProfile will first deploy the Prometheus Helm chart and then the Grafana Helm chart:

```yaml
apiVersion: config.projectsveltos.io/v1alpha1
kind: ClusterProfile
metadata:
  name: prometheus-grafana
spec:
  clusterSelector: env=fv
  syncMode: Continuous
  helmCharts:
  - repositoryURL:    https://prometheus-community.github.io/helm-charts
    repositoryName:   prometheus-community
    chartName:        prometheus-community/prometheus
    chartVersion:     23.4.0
    releaseName:      prometheus
    releaseNamespace: prometheus
    helmChartAction:  Install
  - repositoryURL:    https://grafana.github.io/helm-charts
    repositoryName:   grafana
    chartName:        grafana/grafana
    chartVersion:     6.58.9
    releaseName:      grafana
    releaseNamespace: grafana
    helmChartAction:  Install
```

![Projectsveltos: Helm chart deployment order](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvdjd6klcd5j17ah7tcg.gif)

## Resource Deployment Order with Events

In some cases, it is necessary to deploy Kubernetes resources only after other resources are in a healthy state. For example, a Job that creates a table in a database should not be deployed until the database Deployment is healthy.

Sveltos can help you solve this problem by allowing you to use [events](https://medium.com/p/c2c37c13b190) to control the rollout of your application.

An event is a notification that is sent when a certain condition is met. For example, you could create an event that is sent when the database Deployment becomes healthy.

You can then use this event to trigger the deployment of the Job that creates the table in the database.

By using events, you can ensure that your application is rolled out in a controlled and orderly manner.


![Sveltos: Resource Deployment Order](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yjbdoaasfiny073at5m.png)

In above example Sveltos has been instructed to:

1. Deploy postgresql deployment and service

2. Wait for postgresql deployment to be ready

3. Deploy a Job that creates a table in the DB

4. Wait for Job to be completed

5. Deploy todo-app which can access PostgreSQL deployment

6. Wait for todo-app to be healthy

7. Deploy a Job that adds an entry to database via todo-app


![Projectsveltos: Using Events to control the rollout of your application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oej8fkhtnwb65afeddyp.gif)

All YAMLs for this example can be found in [projectsveltos](https://projectsveltos.github.io/sveltos/manifest_order/) documentation.

## 👏 Support this project
I hope you enjoyed this article! If you did, please check out the GitHub repo for the [project](https://github.com/projectsveltos). The repo contains the code, documentation, and examples, so it’s a great resource for getting started. 

You can also [star 🌟 the project](https://github.com/projectsveltos/addon-controller) if you found it helpful.

Thank you for reading!