# Rollouts

A rollout replaces a set of running instances with new instances, for example to deploy a new version of your app.
The `unikraft` CLI creates the new instances, stops the old instances, and deletes the old instances when the new instances run.

## Starting a rollout

Add `--rollout` to [`unikraft instance create`](/cli/unikraft/instances/create) or [`unikraft run`](/cli/unikraft/run):

```bash
unikraft instance create \
  --metro fra \
  --image <my-org>/my-app:v2 \
  --service my-service \
  --autostart \
  --rollout
```

`unikraft instance create` needs `--autostart`.
`unikraft run` starts instances by default, so it doesn't need the flag.

By default, the rollout replaces the instances of the [service group](/platform/services) in `--service`, one at a time.
For each old instance, the CLI creates and starts a new instance.
When the new instance runs, the CLI [drains](/platform/instances#drain-timeout) the connections of the old instance and stops it.
At the end, the CLI deletes the old instances.

The rollout creates one new instance for each old instance, and gives each new instance a generated name.
The service group sends traffic to the old and the new instances while the rollout runs, so the service stays available.

If no instance matches, the CLI creates the new instance without a rollout.
Add `--dry-run` to see the instances that a rollout replaces, in the `replaces` field.

## Options

`--rollout` takes options as comma-separated `<key>=<value>` pairs:

| Option | Default | Description |
|--------|---------|-------------|
| `type` | `rolling` | `rolling` replaces the instances one batch at a time, and `recreate` stops all old instances before the new instances start. |
| `by` | `service` | `service` selects the instances of the service group in `--service`, and `tags` selects the instances that have all tags of the new instance. |
| `target` | All | The service group, or the subset of the tags of the new instance, that selects the old instances. |
| `batch` | `1` | The number of instances that a `rolling` rollout replaces at a time. |
| `healthy-after` | `0` | The time that each new batch must keep running before the rollout continues, for example `30s`. |
| `drain-timeout` | `30s` | The maximum time that an old instance has to finish its connections before it stops, or `0` to stop it without a drain. |

A drain ends when the old instance has no more connections, so most stops take less time than `drain-timeout`.
`batch` doesn't work with `type=recreate`.

### Replacing all instances at once

Use `type=recreate` when the old and the new instances can't run at the same time, for example when they need sole access to a [volume](/platform/volumes):

```bash
unikraft instance create \
  --metro fra \
  --image <my-org>/my-db:v2 \
  --service my-db \
  --autostart \
  --rollout=type=recreate,healthy-after=1m
```

The service doesn't answer requests between the stop of the old instances and the start of the new instances.

### Selecting instances by tag

With `by=tags`, the rollout replaces the instances that have all the tags of the new instance, or the tags in `target`:

```bash
unikraft instance create \
  --metro fra \
  --image <my-org>/my-app:v2 \
  --tag app-web \
  --tag v2 \
  --autostart \
  --rollout=by=tags,target=app-web,batch=2
```

This replaces all instances with the [tag](/platform/tagging) `app-web`, two at a time.
`by=tags` needs `--metro` and at least one `--tag`.

## Health checks

The CLI waits until each new batch runs.
With `healthy-after`, it then waits for the given time, and checks again that the new instances still run.
The rollout fails if a new instance stops, restarts, or disappears before the rollout ends.
Each step of a rollout has a time limit of 15 minutes.

## Failed and interrupted rollouts

If a step fails, or you interrupt the CLI with `Ctrl+C`, the CLI undoes the rollout.
It starts the old instances that it stopped, and deletes the new instances.
If the old instances don't start again, the CLI keeps the new instances and names them in the error.

The CLI runs the rollout itself.
If the CLI ends without the chance to undo the rollout, for example because its machine shuts down, the old and the new instances stay.
Check the instances of the service group, and delete the instances that you don't need.

## Limitations

- `--rollout` doesn't work with `--name` or `--replicas`.
- `by=service` needs an existing service group in `--service`.
- A service group with [autoscale](/features/autoscale) can't have a rollout.

## Learn more

* [Load balancing](/features/load-balancing): how a service group spreads traffic across its instances.
* [Service groups](/platform/services): create the service group that a rollout replaces the instances of.
* [Instances](/platform/instances#drain-timeout): how an instance drains its connections before it stops.
* The [`unikraft instance create`](/cli/unikraft/instances/create) reference.
