|
| 1 | +--- |
| 2 | +title: Selective Deployments in Spin |
| 3 | +description: Learn how to deploy a subset of components from your SpinApp using Selective Deployments. |
| 4 | +date: 2024-11-12 |
| 5 | +categories: [Spin Operator] |
| 6 | +tags: [Tutorials] |
| 7 | +weight: 10 |
| 8 | +aliases: |
| 9 | +- /docs/spin-operator/tutorials/selective-deployments |
| 10 | +--- |
| 11 | + |
| 12 | +This article explains how to selectively deploy a subset of components from your Spin App using Selective Deployments. You will learn how to: |
| 13 | + |
| 14 | +- Scaffold a Specific Component from a Spin Application into a Custom Resource |
| 15 | +- Run a Selective Deployment |
| 16 | + |
| 17 | +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). |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +For this tutorial, you’ll need: |
| 22 | + |
| 23 | +- [kubectl](https://kubernetes.io/docs/tasks/tools/) - the Kubernetes CLI |
| 24 | +- Kubernetes cluster with the Spin Operator v0.4 and Containerd Spin Shim v0.17 - follow the [Quickstart](../install/quickstart.md) if needed |
| 25 | +- `spin kube` plugin v0.3 - follow [Installing the `spin kube` plugin](../install/spin-kube-plugin.md) if needed |
| 26 | + |
| 27 | +## Scaffold a Specific Component from a Spin Application into a Custom Resource |
| 28 | + |
| 29 | +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: |
| 30 | + |
| 31 | +- `Hello` component triggered by the `/hi` route |
| 32 | +- `Goodbye` component triggered by the `/bye` route |
| 33 | + |
| 34 | +```yaml |
| 35 | +spin_manifest_version = 2 |
| 36 | + |
| 37 | +[application] |
| 38 | +name = "salutations" |
| 39 | +version = "0.1.0" |
| 40 | +authors = ["Kate Goldenring <[email protected]>"] |
| 41 | +description = "An app that gives salutations" |
| 42 | + |
| 43 | +[[trigger.http]] |
| 44 | +route = "/hi" |
| 45 | +component = "hello" |
| 46 | + |
| 47 | +[component.hello] |
| 48 | +source = "../hello-world/main.wasm" |
| 49 | +allowed_outbound_hosts = [] |
| 50 | +[component.hello.build] |
| 51 | +command = "cd ../hello-world && tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go" |
| 52 | +watch = ["**/*.go", "go.mod"] |
| 53 | + |
| 54 | +[[trigger.http]] |
| 55 | +route = "/bye" |
| 56 | +component = "goodbye" |
| 57 | + |
| 58 | +[component.goodbye] |
| 59 | +source = "main.wasm" |
| 60 | +allowed_outbound_hosts = [] |
| 61 | +[component.goodbye.build] |
| 62 | +command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go" |
| 63 | +watch = ["**/*.go", "go.mod"] |
| 64 | +``` |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +> Note that if you had an Spin application with more than two components, you could choose to deploy multiple components selectively. |
| 69 | +
|
| 70 | +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: |
| 71 | + |
| 72 | +```bash |
| 73 | +spin kube scaffold --from ghcr.io/spinkube/spin-operator/salutations:20241105-223428-g4da3171 --component hello --replicas 1 --out spinapp.yaml |
| 74 | +``` |
| 75 | + |
| 76 | +Now if we take a look at our `spinapp.yaml`, we should see that only the hello component will be deployed via Selective Deployments: |
| 77 | + |
| 78 | +```yaml |
| 79 | +apiVersion: core.spinkube.dev/v1alpha1 |
| 80 | +kind: SpinApp |
| 81 | +metadata: |
| 82 | + name: salutations |
| 83 | +spec: |
| 84 | + image: "ghcr.io/spinkube/spin-operator/salutations:20241105-223428-g4da3171" |
| 85 | + executor: containerd-shim-spin |
| 86 | + replicas: 1 |
| 87 | + components: |
| 88 | + - hello |
| 89 | +``` |
| 90 | +
|
| 91 | +## Run a Selective Deployment |
| 92 | +
|
| 93 | +Now you can deploy your app using `kubectl` as you normally would: |
| 94 | + |
| 95 | +```bash |
| 96 | +# Deploy the spinapp.yaml using kubectl |
| 97 | +kubectl apply -f spinapp.yaml |
| 98 | +spinapp.core.spinkube.dev/salutations created |
| 99 | +``` |
| 100 | + |
| 101 | +We can test that only our `hello` component is running by port-forwarding its service. |
| 102 | + |
| 103 | +```bash |
| 104 | +kubectl port-forward svc/salutations 8083:80 |
| 105 | +``` |
| 106 | + |
| 107 | +Now let’s call the `/hi` route in a seperate terminal: |
| 108 | + |
| 109 | +```bash |
| 110 | +curl localhost:8083/hi |
| 111 | +``` |
| 112 | + |
| 113 | +If the hello component is running correctly, we should see a response of "Hello Fermyon!": |
| 114 | + |
| 115 | +```bash |
| 116 | +Hello Fermyon! |
| 117 | +``` |
| 118 | + |
| 119 | +Next, let’s try the `/bye` route. This should return nothing, confirming that only the `hello` component was deployed: |
| 120 | + |
| 121 | +```bash |
| 122 | +curl localhost:8083/bye |
| 123 | +``` |
| 124 | + |
| 125 | +There you have it! You selectively deployed a subset of your Spin application to SpinKube with no modifications to your source code. This approach lets you easily deploy only the components you need, which can improve efficiency in environments where only specific services are required. |
0 commit comments