Knative Serving distributed calculator (#85)

* adding distributed calculator sample with Knative Serving

* adding Knative section to the table in the root readme.md
This commit is contained in:
Stepan 2021-11-25 17:19:23 +02:00 committed by GitHub
parent b4b1553cad
commit a985c90c6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 359 additions and 0 deletions

View File

@ -28,6 +28,7 @@ If you are new to Dapr, you may want to review following resources first:
| [Distributed Calendar](./dapr-distributed-calendar) | Shows use of statestore, pubsub and output binding features of Dapr to roughly create a distributed version of a MVCS architecture application. |
| [Hello Service Fabric](./hello-service-fabric) | Shows use of statestore, pubsub and service invocation in a Service Fabric environment running the Dapr sidecar as a guest executable. |
| [Pub-sub routing](./pub-sub-routing) | Demonstrates how to use Dapr to enable pub-sub applications with message routing. |
| [Distributed Calculator with Knative Serving](./knative-distributed-calculator) | Demonstrates how to use Dapr and Knative Serving using distributed calculator from quickstarts. |
## External samples

View File

@ -0,0 +1,197 @@
# Distributed calculator using Knative Serving
| Attribute | Details |
| ----------------------- | -------------------------- |
| Dapr runtime version | v1.5.0 |
| Knative Serving version | v1.0 |
| Language | Javascript, Python, Go, C# |
| Environment | Kubernetes |
This is a distrubuted calculator application from [Dapr quickstart](https://github.com/dapr/quickstarts/tree/master/distributed-calculator) using Knative Serving (with Kourier) to host React Calculator. This is build as proof-of-concept to show how to use Knative Serving with Dapr.
## Contents
## Prerequisites
This sample requires you to have the following installed on your machine:
- [Dapr CLI](https://github.com/dapr/cli/tree/release-1.5) v1.5.0
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
- An online hoster Kubernetes cluster, such as [AKS](https://docs.dapr.io/operations/hosting/kubernetes/cluster/setup-aks/) or [GKE](https://cloud.google.com/kubernetes-engine/)
Also, unless you have already done so, clone the repository with the samples and `cd` into the right directory:
```bash
git clone https://github.com/dapr/samples.git
cd samples/read-kubernetes-events
```
## Step 1 - Make sure that your kubectl client is working
The first thing you need is an enabled Kubernetes cluster. This sample was tested on fully-fledged cluster.
Once you have that make sure you get a positive response from the following kubectl command
```bash
kubectl get pods
```
This should either have output as `No resources found in default namespace.` or should list the pods running the `default` namesapce.
## Step 2 - Setup Dapr
Follow [instructions](https://docs.dapr.io/getting-started/install-dapr/) to download and install the Dapr CLI and initialize Dapr.
## Step 3 - Setup Knative Serving
> **Note**: Here you can find full [instruction](https://knative.dev/docs/install/serving/install-serving-with-yaml/) of how to install and configure Knative Serving. All the information below in steps 3 and 4 is an excerpt from it which was used and tested.
### Install Knative Serving CRDs
```bash
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.0.0/serving-crds.yaml
```
### Install Knative Serving Core
```bash
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.0.0/serving-core.yaml
```
### Install Knative Kourier - networking layer
```bash
kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.0.0/kourier.yaml
```
### Configure Knative to use Kourier
```bash
kubectl patch configmap/config-network \
--namespace knative-serving \
--type merge \
--patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'
```
### Verify installation
```bash
kubectl get pods -n knative-serving
```
All pods inside `knative-serving` namespace should have `Running` or `Completed` status.
## Step 3 - Configure DNS for Knative
### Fetch the External IP address by running the command
```bash
kubectl --namespace kourier-system get service kourier
```
### Configure DNS
This sample was tested with real DNS. In this case, you need to take External IP address from previous step and add to your DNS wildcard `A` record (e.g. `*.knative.example.com`).
### Direct Knative to use that domain
Please change `knative.example.com` below to your domain.
```bash
kubectl patch configmap/config-domain \
--namespace knative-serving \
--type merge \
--patch '{"data":{"knative.example.com":""}}'
```
## Step 4 - Setup Distributed Calculator
### Install Redis store
Follow [these instructions](https://docs.dapr.io/getting-started/configure-state-pubsub/) to create and configure a Redis store.
Here is a quick excerpt from it using Helm:
```bash
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis
```
### Install Distributed Calculator
Navigate to the deploy directory in this quickstart directory:
```bash
cd deploy
```
Deploy all of your resources:
```bash
kubectl apply -f .
```
> **Note**: This is the same Distributed Calculator from [quickstart](https://github.com/dapr/quickstarts/tree/release-1.5/distributed-calculator) except for the React deployment.
### Verification
Get URL for your React application:
```bash
kubectl get service.serving
```
Example output:
```bash
NAME URL LATESTCREATED LATESTREADY READY REASON
calculator-front-end http://calculator-front-end.default.knative.example.com calculator-front-end-rev1 calculator-front-end-rev1 True
```
Make sure that `READY` is set to `True`. Otherwise, please wait until all the necessary components are configured by Knative. The address in this case is `http://calculator-front-end.default.knative.example.com`.
Navigate to this address with your browser and you should see the distributed calculator. Do some calculations to make sure that all works as expected.
### Behind the scene
By default, Knative will scale to zero its workloads if there is no traffic to them. Wait for a couple minutes and run next command to list all pods in `default` namespace:
```bash
kubectl get pods
```
Example output:
```bash
NAME READY STATUS RESTARTS AGE
addapp-86cfcb8969-mvzs8 2/2 Running 0 2d23h
divideapp-6b94b477f5-58n92 2/2 Running 0 2d23h
multiplyapp-545c4bc54d-n6vrd 2/2 Running 0 2d23h
redis-master-0 1/1 Running 0 2d23h
redis-replicas-0 1/1 Running 0 2d23h
redis-replicas-1 1/1 Running 0 2d23h
redis-replicas-2 1/1 Running 0 2d23h
subtractapp-5c6c6bc4fc-wlbqv 2/2 Running 0 2d23h
```
As you can see there are no `calculator-front-end` pods.
Navigate back to the address with your browser to generate some traffic.
Return back to and list all pods once again:
```bash
NAME READY STATUS RESTARTS AGE
addapp-86cfcb8969-mvzs8 2/2 Running 0 2d23h
calculator-front-end-rev1-deployment-6fd89f78df-6ttr2 3/3 Running 0 6s
divideapp-6b94b477f5-58n92 2/2 Running 0 2d23h
multiplyapp-545c4bc54d-n6vrd 2/2 Running 0 2d23h
redis-master-0 1/1 Running 0 2d23h
redis-replicas-0 1/1 Running 0 2d23h
redis-replicas-1 1/1 Running 0 2d23h
redis-replicas-2 1/1 Running 0 2d23h
subtractapp-5c6c6bc4fc-wlbqv 2/2 Running 0 2d23h
```
As you can see there is `calculator-front-end-rev1-deployment-6fd89f78df-6ttr2` pod with 3 containers running inside: `calculator-front-end`, `queue-proxy` and `daprd`.

View File

@ -0,0 +1,9 @@
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: appconfig
spec:
tracing:
samplingRate: "1"
zipkin:
endpointAddress: "http://zipkin.default.svc.cluster.local:9411/api/v2/spans"

View File

@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: subtractapp
labels:
app: subtract
spec:
replicas: 1
selector:
matchLabels:
app: subtract
template:
metadata:
labels:
app: subtract
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "subtractapp"
dapr.io/app-port: "80"
dapr.io/config: "appconfig"
spec:
containers:
- name: subtract
image: dapriosamples/distributed-calculator-csharp:latest
ports:
- containerPort: 80
imagePullPolicy: Always

View File

@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: addapp
labels:
app: add
spec:
replicas: 1
selector:
matchLabels:
app: add
template:
metadata:
labels:
app: add
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "addapp"
dapr.io/app-port: "6000"
dapr.io/config: "appconfig"
spec:
containers:
- name: add
image: dapriosamples/distributed-calculator-go:latest
ports:
- containerPort: 6000
imagePullPolicy: Always

View File

@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: divideapp
labels:
app: divide
spec:
replicas: 1
selector:
matchLabels:
app: divide
template:
metadata:
labels:
app: divide
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "divideapp"
dapr.io/app-port: "4000"
dapr.io/config: "appconfig"
spec:
containers:
- name: divide
image: dapriosamples/distributed-calculator-node:latest
ports:
- containerPort: 4000
imagePullPolicy: Always

View File

@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: multiplyapp
labels:
app: multiply
spec:
replicas: 1
selector:
matchLabels:
app: multiply
template:
metadata:
labels:
app: multiply
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "multiplyapp"
dapr.io/app-port: "5000"
dapr.io/config: "appconfig"
spec:
containers:
- name: multiply
image: dapriosamples/distributed-calculator-python:latest
ports:
- containerPort: 5000
imagePullPolicy: Always

View File

@ -0,0 +1,23 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: calculator-front-end
spec:
template:
metadata:
# This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
name: calculator-front-end-rev1
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: calculator-front-end
dapr.io/app-port: "8080"
dapr.io/metrics-port: "19090"
dapr.io/config: "appconfig"
dapr.io/log-level: debug
spec:
containers:
- image: dapriosamples/distributed-calculator-react-calculator:latest
name: calculator-front-end
imagePullPolicy: Always
ports:
- containerPort: 8080

View File

@ -0,0 +1,21 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
# These settings will work out of the box if you use `helm install
# bitnami/redis`. If you have your own setup, replace
# `redis-master:6379` with your own Redis master address, and the
# Redis password with your own Secret's name. For more information,
# see https://docs.dapr.io/operations/components/component-secrets .
- name: redisHost
value: redis-master:6379
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
auth:
secretStore: kubernetes