Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/en/docs/topics/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ spin kube deploy --from ttl.sh/hello-spin:24h

spinapp.core.spinkube.dev/hello-spin created
```
> You can deploy a subset of components in your Spin Application using [Selective Deployments](./selective-deployments.md).

## Scaffolding Spin Apps

Expand Down
123 changes: 123 additions & 0 deletions content/en/docs/topics/selective-deployments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Selective Deployments in Spin
description: Learn how to deploy a subset of components from your SpinApp using Selective Deployments.
date: 2024-11-12
categories: [Spin Operator]
tags: [Tutorials]
weight: 10
aliases:
- /docs/spin-operator/tutorials/selective-deployments
---

This article explains how to selectively deploy a subset of components from your Spin App using Selective Deployments. You will learn how to:

- Scaffold a Specific Component from a Spin Application into a Custom Resource
- Run a Selective Deployment

Selective Deployments allow you to control which components within a Spin app are active for a specific instance of the app. With Component Selectors, Spin and SpinKube can declare at runtime which components should be activated, letting you deploy a single, versioned artifact while choosing which parts to enable at startup. This approach separates developer goals (building a well-architected app) from operational needs (optimizing for specific infrastructure).

## Prerequisites

For this tutorial, you’ll need:

- [kubectl](https://kubernetes.io/docs/tasks/tools/) - the Kubernetes CLI
- Kubernetes cluster with the Spin Operator v0.4 and Containerd Spin Shim v0.16 - follow the [Quickstart](../install/quickstart.md) if needed
- `spin kube` plugin - follow [Installing the `spin kube` plugin](../install/spin-kube-plugin.md) if needed

## Scaffold a Specific Component from a Spin Application into a Custom Resource

We’ll use a sample application called "Salutations," which demonstrates greetings via two components, each responding to a unique HTTP route. If we take a look at the [application manifest](https://github.com/spinkube/spin-operator/blob/main/apps/salutations/spin.toml), we’ll see that this Spin application is comprised of two components:

- `Hello` component triggered by the `/hi` route
- `Goodbye` component triggered by the `/bye` route

```yaml
spin_manifest_version = 2

[application]
name = "salutations"
version = "0.1.0"
authors = ["Kate Goldenring <[email protected]>"]
description = "An app that gives salutations"

[[trigger.http]]
route = "/hi"
component = "hello"

[component.hello]
source = "../hello-world/main.wasm"
allowed_outbound_hosts = []
[component.hello.build]
command = "cd ../hello-world && tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
watch = ["**/*.go", "go.mod"]

[[trigger.http]]
route = "/bye"
component = "goodbye"

[component.goodbye]
source = "main.wasm"
allowed_outbound_hosts = []
[component.goodbye.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
watch = ["**/*.go", "go.mod"]
```

With Selective Deployments, you can choose to deploy only specific components without modifying the source code. For this example, we’ll deploy just the `hello` component.

> Note that if you had an Spin application with more than two components, you could choose to deploy multiple components selectively.

To Selectively Deploy, we first need to turn our application into a SpinApp Custom Resource with the `spin kube scaffold` command, using the optional `--component` field to specify which component we’d like to deploy:

```bash
spin kube scaffold --from ghcr.io/spinkube/spin-operator/salutations:20241105-223428-g4da3171 --component hello --replicas 1 --out spinapp.yaml
```

Now if we take a look at our `spinapp.yaml`, we should see that only the hello component will be deployed via Selective Deployments:

```yaml
apiVersion: core.spinkube.dev/v1alpha1
kind: SpinApp
metadata:
name: salutations
spec:
## TODO update image
image: "ghcr.io/spinkube/spin-operator/salutations:20241105-223428-g4da3171"
executor: containerd-shim-spin
replicas: 1
components:
- hello
```

## Run a Selective Deployment

Now you can deploy your app using kubectl apply -f as you normally would:

```bash
# Deploy the spinapp.yaml using kubectl
kubectl apply -f spinapp.yaml
# TODO make sure that's the right domain
spinapp.core.spinkube.dev/salutations created
```

Optionally, let’s test that only our hello component is running. We’ll use port-forwarding to access our service locally:

```bash
kubectl port-forward svc/salutations 8083:80
```

Now let’s call the `/hi` route. If the hello component is running correctly, we should see a response of "hello":

```bash
curl localhost:8083/hi
# TODO include output
```

Next, let’s try the `/bye` route. This should fail, confirming that only the hello component was deployed:

```bash
curl localhost:8083/bye
# TODO include output
```

And there you have it! A subset of your Spin application deployed on SpinKube with no modifications to your source code. This approach lets you quickly deploy only the components you need, which can improve efficiency in environments where only specific services are required.
27 changes: 16 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.