Skip to content

Commit

Permalink
WIP: use native dapr, then branch out to dapr shared
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fisher <[email protected]>
  • Loading branch information
bacongobbler committed Mar 5, 2024
1 parent 53820c1 commit b93c0b3
Showing 1 changed file with 54 additions and 83 deletions.
137 changes: 54 additions & 83 deletions content/en/docs/spin-operator/tutorials/running-with-dapr.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,86 @@ tags: [Tutorials]
weight: 100
---

## How to Configure Dapr Shared With a SpinApp
## How to Configure Dapr With a Spin app on Kubernetes

In this tutorial we will configure Dapr Shared with a SpinApp.
Dapr is a portable, event-driven runtime that makes it easy for developers to build resilient
applications. It provides a set of building blocks for building applications, including state
management, pub/sub, and service invocation. Dapr applications can be written in any language and
run on any platform.

## Dapr Shared
In this tutorial, we will deploy a Spin app on Kubernetes and configure it to use Dapr for Pub/Sub
communication.

[Why Dapr Shared](https://github.com/dapr-sandbox/dapr-shared?tab=readme-ov-file#why-dapr-shared)?

Dapr Shared is a framework designed to share Dapr sidecars across multiple applications. This approach optimizes resource utilization by allowing applications to share a common set of Dapr sidecars, reducing the overhead of deploying individual sidecars for each application. Dapr Shared extends the Dapr sidecar model with two new deployment strategies: the flexibility to create Dapr Applications using the `daprd` Sidecar either as a Kubernetes `DaemonSet` or a `Deployment`.
## Prerequisites

Opting for `daprd` as a Kubernetes `DaemonSet` ensures that the `daprd` container operates on every Kubernetes Node. This setup significantly minimizes the network distance between applications and Dapr, enhancing performance.
Follow the [guide to run spin-operator](running-locally.md) to install the Spin Operator.

Alternatively, deploying Dapr Shared as a Kubernetes `Deployment` allows the Kubernetes scheduler to determine the specific node where the Dapr Shared instance will operate. This method offers a balanced distribution of resources and workload management across the Kubernetes environment.
## Installing Dapr

## Prerequisites
In this post, we will use Dapr v1.13.0, which you should see as

Please see the [Go](./prerequisites.md#go), [Docker](./prerequisites.md#docker), [Kubectl](./prerequisites.md#kubectl), [k3d](./prerequisites.md#k3d) and [Helm](./prerequisites.md#helm) sections in the [Prerequisites](./prerequisites.md) page and fulfill those prerequisite requirements before continuing.
```sh
dapr --version
CLI version: 1.12.0
Runtime version: n/a
```

## Fetch Spin Operator (Source Code)
Initialize Dapr on your cluster with the following command:

Clone the Spin Operator repository:
```sh
⌛ Making the jump to hyperspace...
ℹ️ Note: To install Dapr using Helm, see here: https://docs.dapr.io/getting-started/install-dapr-kubernetes/#install-with-helm-advanced

```bash
git clone https://github.com/spinkube/spin-operator.git
ℹ️ Container images will be pulled from Docker Hub
✅ Deploying the Dapr control plane with latest version to your cluster...
✅ Deploying the Dapr dashboard with latest version to your cluster...
✅ Success! Dapr has been installed to namespace dapr-system. To verify, run `dapr status -k' in your terminal. To get started, go here: https://aka.ms/dapr-getting-started
```
Change into the Spin Operator directory:
Check the status of Dapr with the following command:
```bash
cd spin-operator
```sh
dapr status -k
NAME NAMESPACE HEALTHY STATUS REPLICAS VERSION AGE CREATED
dapr-placement-server dapr-system True Running 1 1.12.5 15s 2024-03-05 14:21.49
dapr-operator dapr-system True Running 1 1.12.5 15s 2024-03-05 14:21.49
dapr-sentry dapr-system True Running 1 1.12.5 15s 2024-03-05 14:21.49
dapr-dashboard dapr-system True Running 1 0.14.0 14s 2024-03-05 14:21.50
dapr-sidecar-injector dapr-system True Running 1 1.12.5 15s 2024-03-05 14:21.49
```
## Setting Up Kubernetes Cluster
Run the following command to create a Kubernetes k3d cluster that has [the containerd-wasm-shims](https://github.com/deislabs/containerd-wasm-shims) pre-requisites installed:
```bash
k3d cluster create wasm-cluster --image ghcr.io/deislabs/containerd-wasm-shims/examples/k3d:v0.10.0 -p "8081:80@loadbalancer" --agents 2
```
## Running the Sample Application
Run the following command to create the Runtime Class:
Now we will run the sample application with Dapr Shared. To do this, we will need to create a
`SpinApp` resource that references the Dapr Shared instance.
```bash
```yaml
kubectl apply -f - <<EOF
apiVersion: node.k8s.io/v1
kind: RuntimeClass
apiVersion: core.spinoperator.dev/v1
kind: SpinApp
metadata:
name: wasmtime-spin-v2
handler: spin
name: my-spinapp
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "my-spinapp"
dapr.io/app-port: "80"
dapr.io/enable-api-logging: "true"
spec:
image: "ghcr.io/thangchung/dapr-labs/product-api-spin:1.0.1"
executor: containerd-shim-spin
replicas: 1
EOF
```
## Installation With Make

Run the following command to install the Custom Resource Definition (CRD) into the cluster:

```bash
make install
```

Run the following command to run the Spin Operator locally:

```bash
make run
```

## Running the Sample Application

Run the following command, in a different terminal window:
```bash
kubectl create -f ./config/samples/simple.yaml
```

## Dapr Shared Instance

For each application service that needs to talk to the Dapr APIs we need to deploy a new Dapr Shared instance. Each instance will have a one-to-one relationship with Dapr Application IDs.

The `shared.appId` is a configuration element used in Dapr that specifies the unique identifier for a Dapr application. The `shared.remoteURL` and `shared.remotePort` are the reachable URL and Port. Creating a Dapr Shared instance can be done via the `helm` command, as per the example below:

```bash
helm install my-shared-instance \
oci://registry-1.docker.io/daprio/dapr-shared-chart \
--set shared.appId=<DAPR_APP_ID> \
--set shared.remoteURL=<REMOTE_URL> \
--set shared.remotePort=<REMOTE_PORT>
```

For example:
<!-- dapr_host = { default = "http://localhost" }
dapr_port = { default = "5003" }
dapr_app_name = { default = "myapp" }
dapr_pubsub_topic = { default = "orders" } -->
```bash
helm install my-shared-instance \
oci://registry-1.docker.io/daprio/dapr-shared-chart \
--set shared.appId=simple-spinapp \
--set shared.remoteURL=simple-spinapp \
--set shared.remotePort=8083
```

The above helm command will return something similar to the following:

```bash
Pulled: registry-1.docker.io/daprio/dapr-shared-chart:0.0.12
Digest: sha256:f63a5936f43294aa1aacb8695c06d43efd5f1672a336a954b4a15990bcfb8112
NAME: my-shared-instance
LAST DEPLOYED: Fri Jan 19 15:59:33 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
kubectl create -f ./config/samples/simple.yaml
```

> Note: You can [Customize Dapr Shared using Helm values](https://github.com/dapr-sandbox/dapr-shared?tab=readme-ov-file#customize-dapr-shared).

0 comments on commit b93c0b3

Please sign in to comment.