Skip to content

Commit

Permalink
Merge pull request #3719 from mukundansundar/k8s-multi-app-run
Browse files Browse the repository at this point in the history
Adding k8s fields for Multi-App run for dev/test environment
  • Loading branch information
hhunter-ms authored Sep 25, 2023
2 parents b57960d + b30b9d2 commit 70e3a5e
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ description: Run multiple applications with one CLI command
---

{{% alert title="Note" color="primary" %}}
Multi-App Run is currently a preview feature only supported in Linux/MacOS.
Multi-App Run for **Kubernetes** is currently a preview feature.
{{% /alert %}}

Let's say you want to run several applications locally to test them together, similar to a production scenario. With a local Kubernetes cluster, you'd be able to do this with helm/deployment YAML files. You'd also have to build them as containers and set up Kubernetes, which can add some complexity.
Let's say you want to run several applications locally to test them together, similar to a production scenario. Multi-App Run allows you to start and stop a set of applications simultaneously, either:
- Locally/self-hosted with processes, or
- By building container images and deploying to a Kubernetes cluster
- You can use a local Kubernetes cluster (KiND) or one deploy to a Cloud (AKS, EKS, and GKE).

Instead, you simply want to run them as local executables in self-hosted mode. However, self-hosted mode requires you to:
The Multi-App Run template file describes how to start multiple applications as if you had run many separate CLI `run` commands. By default, this template file is called `dapr.yaml`.

- Run multiple `dapr run` commands
- Keep track of all ports opened (you cannot have duplicate ports for different applications).
- Remember the resources folders and configuration files that each application refers to.
- Recall all of the additional flags you used to tweak the `dapr run` command behavior (`--app-health-check-path`, `--dapr-grpc-port`, `--unix-domain-socket`, etc.)
{{< tabs Self-hosted Kubernetes>}}

With Multi-App Run, you can start multiple applications in self-hosted mode using a single `dapr run -f` command using a template file. The template file describes how to start multiple applications as if you had run many separate CLI `run`commands. By default, this template file is called `dapr.yaml`.
{{% codetab %}}
<!--selfhosted-->

## Multi-App Run template file

When you execute `dapr run -f .`, it uses the multi-app template file (named `dapr.yaml`) present in the current directory to run all the applications.
When you execute `dapr run -f .`, it starts the multi-app template file (named `dapr.yaml`) present in the current directory to run all the applications.

You can name template file with preferred name other than the default. For example `dapr run -f ./<your-preferred-file-name>.yaml`.

Expand Down Expand Up @@ -71,9 +72,9 @@ The run template provides two log destination fields for each application and it

1. `appLogDestination` : This field configures the log destination for the application. The possible values are `console`, `file` and `fileAndConsole`. The default value is `fileAndConsole` where application logs are written to both console and to a file by default.

2. `daprdLogDestination` : This field configures the log destination for the `daprd` process. The possible values are `console`, `file` and `fileAndConsole`. The default value is `file` where the `daprd` logs are written to a file by default.
1. `daprdLogDestination` : This field configures the log destination for the `daprd` process. The possible values are `console`, `file` and `fileAndConsole`. The default value is `file` where the `daprd` logs are written to a file by default.

#### Log file format
### Log file format

Logs for application and `daprd` are captured in separate files. These log files are created automatically under `.dapr/logs` directory under each application directory (`appDirPath` in the template). These log file names follow the pattern seen below:

Expand All @@ -82,14 +83,90 @@ Logs for application and `daprd` are captured in separate files. These log files

Even if you've decided to rename your resources folder to something other than `.dapr`, the log files are written only to the `.dapr/logs` folder (created in the application directory).


## Watch the demo

Watch [this video for an overview on Multi-App Run](https://youtu.be/s1p9MNl4VGo?t=2456):

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/s1p9MNl4VGo?start=2456" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

## Multi-App Run template file

When you execute `dapr run -k -f .` or `dapr run -k -f dapr.yaml`, the applications defined in the `dapr.yaml` Multi-App Run template file starts in Kubernetes default namespace.

> **Note:** Currently, the Multi-App Run template can only start applications in the default Kubernetes namespace.

The necessary default service and deployment definitions for Kubernetes are generated within the `.dapr/deploy` folder for each app in the `dapr.yaml` template.

If the `createService` field is set to `true` in the `dapr.yaml` template for an app, then the `service.yaml` file is generated in the `.dapr/deploy` folder of the app.

Otherwise, only the `deployment.yaml` file is generated for each app that has the `containerImage` field set.

The files `service.yaml` and `deployment.yaml` are used to deploy the applications in `default` namespace in Kubernetes. This feature is specifically targeted only for running multiple apps in a dev/test environment in Kubernetes.

You can name the template file with any preferred name other than the default. For example:

```bash
dapr run -k -f ./<your-preferred-file-name>.yaml
```

The following example includes some of the template properties you can customize for your applications. In the example, you can simultaneously launch 2 applications with app IDs of `nodeapp` and `pythonapp`.

```yaml
version: 1
common:
apps:
- appID: nodeapp
appDirPath: ./nodeapp/
appPort: 3000
containerImage: ghcr.io/dapr/samples/hello-k8s-node:latest
createService: true
env:
APP_PORT: 3000
- appID: pythonapp
appDirPath: ./pythonapp/
containerImage: ghcr.io/dapr/samples/hello-k8s-python:latest
```

> **Note:**
> - If the `containerImage` field is not specified, `dapr run -k -f` produces an error.
> - The `createService` field defines a basic service in Kubernetes (ClusterIP or LoadBalancer) that targets the `--app-port` specified in the template. If `createService` isn't specified, the application is not accessible from outside the cluster.

For a more in-depth example and explanation of the template properties, see [Multi-app template]({{< ref multi-app-template.md >}}).

## Logs

The run template provides two log destination fields for each application and its associated daprd process:

1. `appLogDestination` : This field configures the log destination for the application. The possible values are `console`, `file` and `fileAndConsole`. The default value is `fileAndConsole` where application logs are written to both console and to a file by default.

2. `daprdLogDestination` : This field configures the log destination for the `daprd` process. The possible values are `console`, `file` and `fileAndConsole`. The default value is `file` where the `daprd` logs are written to a file by default.

### Log file format

Logs for application and `daprd` are captured in separate files. These log files are created automatically under `.dapr/logs` directory under each application directory (`appDirPath` in the template). These log file names follow the pattern seen below:

- `<appID>_app_<timestamp>.log` (file name format for `app` log)
- `<appID>_daprd_<timestamp>.log` (file name format for `daprd` log)

Even if you've decided to rename your resources folder to something other than `.dapr`, the log files are written only to the `.dapr/logs` folder (created in the application directory).

## Watch the demo

Watch [this video for an overview on Multi-App Run in Kubernetes](https://youtu.be/nWatANwaAik?si=O8XR-TUaiY0gclgO&t=1024):

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/nWatANwaAik?si=O8XR-TUaiY0gclgO&amp;start=1024" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

{{% /codetab %}}

{{< /tabs >}}

## Next steps

- [Learn the Multi-App Run template file structure and its properties]({{< ref multi-app-template.md >}})
- [Try out the Multi-App Run template with the Service Invocation quickstart]({{< ref serviceinvocation-quickstart.md >}})
- [Try out the self-hosted Multi-App Run template with the Service Invocation quickstart]({{< ref serviceinvocation-quickstart.md >}})
- [Try out the Kubernetes Multi-App Run template with the `hello-kubernetes` tutorial](https://github.com/dapr/quickstarts/tree/master/tutorials/hello-kubernetes)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Unpack the Multi-App Run template file and its properties
---

{{% alert title="Note" color="primary" %}}
Multi-App Run is currently a preview feature only supported in Linux/MacOS.
Multi-App Run for **Kubernetes** is currently a preview feature.
{{% /alert %}}

The Multi-App Run template file is a YAML file that you can use to run multiple applications at once. In this guide, you'll learn how to:
Expand All @@ -26,20 +26,53 @@ When you provide a directory path, the CLI will try to locate the Multi-App Run

Execute the following CLI command to read the Multi-App Run template file, named `dapr.yaml` by default:

{{< tabs Self-hosted Kubernetes>}}

{{% codetab %}}
<!--selfhosted-->

```cmd
# the template file needs to be called `dapr.yaml` by default if a directory path is given
dapr run -f <dir_path>
```
{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

```cmd
dapr run -f -k <dir_path>
```
{{% /codetab %}}

{{< /tabs >}}

### Execute by providing a file path

If the Multi-App Run template file is named something other than `dapr.yaml`, then you can provide the relative or absolute file path to the command:

{{< tabs Self-hosted Kubernetes>}}

{{% codetab %}}
<!--selfhosted-->

```cmd
dapr run -f ./path/to/<your-preferred-file-name>.yaml
```

{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

```cmd
dapr run -f -k ./path/to/<your-preferred-file-name>.yaml
```
{{% /codetab %}}

{{< /tabs >}}

## View the started applications

Once the multi-app template is running, you can view the started applications with the following command:
Expand All @@ -52,6 +85,11 @@ dapr list

Stop the multi-app run template anytime with either of the following commands:

{{< tabs Self-hosted Kubernetes>}}

{{% codetab %}}
<!--selfhosted-->

```cmd
# the template file needs to be called `dapr.yaml` by default if a directory path is given
Expand All @@ -63,10 +101,36 @@ or:
dapr stop -f ./path/to/<your-preferred-file-name>.yaml
```

{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

```cmd
# the template file needs to be called `dapr.yaml` by default if a directory path is given
dapr stop -f -k
```
or:

```cmd
dapr stop -f -k ./path/to/<your-preferred-file-name>.yaml
```

{{% /codetab %}}

{{< /tabs >}}


## Template file structure

The Multi-App Run template file can include the following properties. Below is an example template showing two applications that are configured with some of the properties.

{{< tabs Self-hosted Kubernetes>}}

{{% codetab %}}
<!--selfhosted-->

```yaml
version: 1
common: # optional section for variables shared across apps
Expand Down Expand Up @@ -96,19 +160,61 @@ apps:
command: ["./backend"]
```
{{% alert title="Important" color="warning" %}}
The following rules apply for all the paths present in the template file:
- If the path is absolute, it is used as is.
- All relative paths under comman section should be provided relative to the template file path.
- All relative paths under command section should be provided relative to the template file path.
- `appDirPath` under apps section should be provided relative to the template file path.
- All relative paths under app section should be provided relative to the appDirPath.
- All relative paths under app section should be provided relative to the `appDirPath`.

{{% /alert %}}
{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

```yaml
version: 1
common: # optional section for variables shared across apps
env: # any environment variable shared across apps
DEBUG: true
apps:
- appID: webapp # optional
appDirPath: .dapr/webapp/ # REQUIRED
appChannelAddress: 127.0.0.1 # network address where the app listens on. (optional) can be left to default value by convention.
appProtocol: http
appPort: 8080
appHealthCheckPath: "/healthz"
appLogDestination: file # (optional), can be file, console or fileAndConsole. default is fileAndConsole.
daprdLogDestination: file # (optional), can be file, console or fileAndConsole. default is file.
containerImage: ghcr.io/dapr/samples/hello-k8s-node:latest # (optional) URI of the container image to be used when deploying to Kubernetes dev/test environment.
createService: true # (optional) Create a Kubernetes service for the application when deploying to dev/test environment.
- appID: backend # optional
appDirPath: .dapr/backend/ # REQUIRED
appProtocol: grpc
appPort: 3000
unixDomainSocket: "/tmp/test-socket"
env:
- DEBUG: false
```

The following rules apply for all the paths present in the template file:
- If the path is absolute, it is used as is.
- `appDirPath` under apps section should be provided relative to the template file path.
- All relative paths under app section should be provided relative to the `appDirPath`.

{{% /codetab %}}

{{< /tabs >}}

## Template properties

{{< tabs Self-hosted Kubernetes>}}

{{% codetab %}}
<!--selfhosted-->

The properties for the Multi-App Run template align with the `dapr run` CLI flags, [listed in the CLI reference documentation]({{< ref "dapr-run.md#flags" >}}).

{{< table "table table-white table-striped table-bordered" >}}

| Properties | Required | Details | Example |
|--------------------------|:--------:|--------|---------|
Expand Down Expand Up @@ -146,8 +252,66 @@ The properties for the Multi-App Run template align with the `dapr run` CLI flag
| `appLogDestination` | N | Log destination for outputting app logs; Its value can be file, console or fileAndConsole. Default is fileAndConsole | `file`, `console`, `fileAndConsole` |
| `daprdLogDestination` | N | Log destination for outputting daprd logs; Its value can be file, console or fileAndConsole. Default is file | `file`, `console`, `fileAndConsole` |

{{< /table >}}

## Next steps

Watch [this video for an overview on Multi-App Run](https://youtu.be/s1p9MNl4VGo?t=2456):

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/s1p9MNl4VGo?start=2456" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
{{% /codetab %}}

{{% codetab %}}
<!--kubernetes-->

The properties for the Multi-App Run template align with the `dapr run -k` CLI flags, [listed in the CLI reference documentation]({{< ref "dapr-run.md#flags" >}}).

{{< table "table table-white table-striped table-bordered" >}}

| Properties | Required | Details | Example |
|--------------------------|:--------:|--------|---------|
| `appDirPath` | Y | Path to the your application code | `./webapp/`, `./backend/` |
| `appID` | N | Application's app ID. If not provided, will be derived from `appDirPath` | `webapp`, `backend` |
| `appChannelAddress` | N | The network address the application listens on. Can be left to the default value by convention. | `127.0.0.1` | `localhost` |
| `appProtocol` | N | The protocol Dapr uses to talk to the application. | `http`, `grpc` |
| `appPort` | N | The port your application is listening on | `8080`, `3000` |
| `daprHTTPPort` | N | Dapr HTTP port | |
| `daprGRPCPort` | N | Dapr GRPC port | |
| `daprInternalGRPCPort` | N | gRPC port for the Dapr Internal API to listen on; used when parsing the value from a local DNS component | |
| `metricsPort` | N | The port that Dapr sends its metrics information to | |
| `unixDomainSocket` | N | Path to a unix domain socket dir mount. If specified, communication with the Dapr sidecar uses unix domain sockets for lower latency and greater throughput when compared to using TCP ports. Not available on Windows. | `/tmp/test-socket` |
| `profilePort` | N | The port for the profile server to listen on | |
| `enableProfiling` | N | Enable profiling via an HTTP endpoint | |
| `apiListenAddresses` | N | Dapr API listen addresses | |
| `logLevel` | N | The log verbosity. | |
| `appMaxConcurrency` | N | The concurrency level of the application; default is unlimited | |
| `placementHostAddress` | N | | |
| `appSSL` | N | Enable https when Dapr invokes the application | |
| `daprHTTPMaxRequestSize` | N | Max size of the request body in MB. | |
| `daprHTTPReadBufferSize` | N | Max size of the HTTP read buffer in KB. This also limits the maximum size of HTTP headers. The default 4 KB | |
| `enableAppHealthCheck` | N | Enable the app health check on the application | `true`, `false` |
| `appHealthCheckPath` | N | Path to the health check file | `/healthz` |
| `appHealthProbeInterval` | N | Interval to probe for the health of the app in seconds
| |
| `appHealthProbeTimeout` | N | Timeout for app health probes in milliseconds | |
| `appHealthThreshold` | N | Number of consecutive failures for the app to be considered unhealthy | |
| `enableApiLogging` | N | Enable the logging of all API calls from application to Dapr | |
| `env` | N | Map to environment variable; environment variables applied per application will overwrite environment variables shared across applications | `DEBUG`, `DAPR_HOST_ADD` |
| `appLogDestination` | N | Log destination for outputting app logs; Its value can be file, console or fileAndConsole. Default is fileAndConsole | `file`, `console`, `fileAndConsole` |
| `daprdLogDestination` | N | Log destination for outputting daprd logs; Its value can be file, console or fileAndConsole. Default is file | `file`, `console`, `fileAndConsole` |
| `containerImage`| N | URI of the container image to be used when deploying to Kubernetes dev/test environment. | `ghcr.io/dapr/samples/hello-k8s-python:latest`
| `createService`| N | Create a Kubernetes service for the application when deploying to dev/test environment. | `true`, `false` |

{{< /table >}}

## Next steps

Watch [this video for an overview on Multi-App Run in Kubernetes](https://youtu.be/nWatANwaAik?si=O8XR-TUaiY0gclgO&t=1024):

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/nWatANwaAik?si=O8XR-TUaiY0gclgO&amp;start=1024" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

{{% /codetab %}}

{{< /tabs >}}


Loading

0 comments on commit 70e3a5e

Please sign in to comment.