Skip to content

Files

Latest commit

e2f9868 · May 25, 2022

History

History
225 lines (158 loc) · 7.45 KB

quickstart-dapr.md

File metadata and controls

225 lines (158 loc) · 7.45 KB
title description author ms.author ms.service ms.topic ms.date ms.custom
Deploy an application with the Dapr cluster extension for Azure Kubernetes Service (AKS) or Arc-enabled Kubernetes
Use the Dapr cluster extension for Azure Kubernetes Service (AKS) or Arc-enabled Kubernetes to deploy an application
nickomang
nickoman
container-service
quickstart
05/03/2022
template-quickstart, mode-other, event-tier1-build-2022

Quickstart: Deploy an application using the Dapr cluster extension for Azure Kubernetes Service (AKS) or Arc-enabled Kubernetes

In this quickstart, you will get familiar with using the Dapr cluster extension in an AKS or Arc-enabled Kubernetes cluster. You will be deploying a hello world example, consisting of a Python application that generates messages and a Node application that consumes and persists them.

Prerequisites

Clone the repository

To obtain the files you'll be using to deploy the sample application, clone the Quickstarts repository and change to the hello-kubernetes directory:

git clone https://github.com/dapr/quickstarts.git
cd quickstarts/hello-kubernetes

Create and configure a state store

Dapr can use a number of different state stores (Redis, Cosmos DB, DynamoDB, Cassandra, etc.) to persist and retrieve state. For this example, we will use Redis.

Create a Redis store

  1. Open the Azure portal to start the Azure Redis Cache creation flow.
  2. Fill out the necessary information
  3. Click “Create” to kickoff deployment of your Redis instance.
  4. Take note of the hostname of your Redis instance, which you can retrieve from the “Overview” in Azure. It should look like xxxxxx.redis.cache.windows.net:6380.
  5. Once your instance is created, you’ll need to grab your access key. Navigate to “Access Keys” under “Settings” and create a Kubernetes secret to store your Redis password:
kubectl create secret generic redis --from-literal=redis-password=<your-redis-password>

Configure the Dapr components

Once your store is created, you will need to add the keys to the redis.yaml file in the deploy directory of the Hello World repository. Replace the redisHost value with your own Redis master address, and the redisPassword with your own Secret. You can learn more here.

You will also need to add the following two lines below redisPassword to enable connection over TLS:

- name: redisPassword
    secretKeyRef:
      name: redis
      key: redis-password
- name: enableTLS
  value: true

Apply the configuration

Apply the redis.yaml file:

kubectl apply -f ./deploy/redis.yaml

And verify that your state store was successfully configured in the output:

component.dapr.io/statestore created

Deploy the Node.js app with the Dapr sidecar

Apply the Node.js app's deployment to your cluster:

kubectl apply -f ./deploy/node.yaml

Note

Kubernetes deployments are asynchronous. This means you'll need to wait for the deployment to complete before moving on to the next steps. You can do so with the following command:

kubectl rollout status deploy/nodeapp

This will deploy the Node.js app to Kubernetes. The Dapr control plane will automatically inject the Dapr sidecar to the Pod. If you take a look at the node.yaml file, you will see how Dapr is enabled for that deployment:

  • dapr.io/enabled: true - this tells the Dapr control plane to inject a sidecar to this deployment.

  • dapr.io/app-id: nodeapp - this assigns a unique ID or name to the Dapr application, so it can be sent messages to and communicated with by other Dapr apps.

To access your service, obtain and make note of the EXTERNAL-IP via kubectl:

kubectl get svc nodeapp

Verify the service

To call the service, run:

curl $EXTERNAL_IP/ports

You should see output similar to the following:

{"DAPR_HTTP_PORT":"3500","DAPR_GRPC_PORT":"50001"}

Next, submit an order to the application:

curl --request POST --data "@sample.json" --header Content-Type:application/json $EXTERNAL_IP/neworder

Confirm the order has been persisted by requesting it:

curl $EXTERNAL_IP/order

You should see output similar to the following:

{ "orderId": "42" }

Tip

This is a good time to get acquainted with the Dapr dashboard- a convenient interface to check status, information and logs of applications running on Dapr. The following command will make it available on http://localhost:8080/:

kubectl port-forward svc/dapr-dashboard -n dapr-system 8080:8080

Deploy the Python app with the Dapr sidecar

Take a quick look at the Python app. Navigate to the Python app directory in the hello-kubernetes quickstart and open app.py.

This is a basic Python app that posts JSON messages to localhost:3500, which is the default listening port for Dapr. You can invoke the Node.js application's neworder endpoint by posting to v1.0/invoke/nodeapp/method/neworder. The message contains some data with an orderId that increments once per second:

n = 0
while True:
    n += 1
    message = {"data": {"orderId": n}}

    try:
        response = requests.post(dapr_url, json=message)
    except Exception as e:
        print(e)

    time.sleep(1)

Deploy the Python app to your Kubernetes cluster:

kubectl apply -f ./deploy/python.yaml

Note

As with above, the following command will wait for the deployment to complete:

kubectl rollout status deploy/pythonapp

Observe messages and confirm persistence

Now that both the Node.js and Python applications are deployed, watch messages come through.

Get the logs of the Node.js app:

kubectl logs --selector=app=node -c node --tail=-1

If the deployments were successful, you should see logs like this:

Got a new order! Order ID: 1
Successfully persisted state
Got a new order! Order ID: 2
Successfully persisted state
Got a new order! Order ID: 3
Successfully persisted state

Call the Node.js app's order endpoint to get the latest order. Grab the external IP address that you saved before and, append "/order" and perform a GET request against it (enter it into your browser, use Postman, or curl it!):

curl $EXTERNAL_IP/order
{"orderID":"42"}

You should see the latest JSON in the response.

Clean up resources

Use the az group delete command to remove the resource group, the cluster, the namespace, and all related resources.

az group delete --name MyResourceGroup

Next steps

After successfully deploying this sample application:

[!div class="nextstepaction"] Learn more about other cluster extensions