Quickstart: Deploy an application using the Dapr cluster extension for Azure Kubernetes Service (AKS) or Arc-enabled Kubernetes
In this quickstart, you use the Dapr cluster extension in an AKS or Arc-enabled Kubernetes cluster. You deploy a hello world
example, which consists of a Python application that generates messages and a node application that consumes and persists the messages.
Prerequisites
- An Azure subscription. If you don't have an Azure subscription, you can create a free account.
- Azure CLI or Azure PowerShell installed.
- An AKS or Arc-enabled Kubernetes cluster with the Dapr cluster extension enabled.
Clone the repository
Clone the Dapr quickstarts repository using the
git clone
command.git clone https://github.com/dapr/quickstarts.git
Change to the
hello-kubernetes
directory usingcd
.cd quickstarts/tutorials/hello-kubernetes/
Create and configure a state store
Dapr can use many different state stores, such as Redis, Azure Cosmos DB, DynamoDB, and Cassandra, to persist and retrieve state. For this example, we use Redis.
Create a Redis store
Open the Azure portal to start the Azure Cache for Redis creation flow.
Fill out the necessary information.
Select Create to start the Redis instance deployment.
Take note of the hostname of your Redis instance, which you can retrieve from the Overview section in Azure. The hostname might be similar to the following example:
xxxxxx.redis.cache.windows.net:6380
.Under Settings, navigate to Access keys to get your access keys.
Create a Kubernetes secret to store your Redis password using the
kubectl create secret generic redis
command.kubectl create secret generic redis --from-literal=redis-password=<your-redis-password>
Configure the Dapr components
Once your store is created, you need to add the keys to the redis.yaml
file in the deploy directory of the Hello World repository. You can learn more here.
Replace the
redisHost
value with your own Redis master address.Replace the
redisPassword
with your own Secret.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 using thekubectl apply
command.kubectl apply -f ./deploy/redis.yaml
Verify your state store was successfully configured using the
kubectl get components.redis
command.kubectl get components.redis -o yaml
You should see output similar to the following example output:
component.dapr.io/statestore created
Deploy the Node.js app with the Dapr sidecar
Apply the Node.js app deployment to your cluster using the
kubectl apply
command.kubectl apply -f ./deploy/node.yaml
Note
Kubernetes deployments are asynchronous, which means you 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 deploys the Node.js app to Kubernetes. The Dapr control plane automatically injects the Dapr sidecar to the Pod. If you take a look at the
node.yaml
file, you see how Dapr is enabled for that deployment:dapr.io/enabled: true
: tells the Dapr control plane to inject a sidecar to this deployment.dapr.io/app-id: nodeapp
: assigns a unique ID or name to the Dapr application, so it can be sent messages to and communicated with by other Dapr apps.
Access your service using the
kubectl get svc
command.kubectl get svc nodeapp
Make note of the
EXTERNAL-IP
in the output.
Verify the service
Call the service using
curl
with yourEXTERNAL-IP
.curl $EXTERNAL_IP/ports
You should see output similar to the following example output:
{"DAPR_HTTP_PORT":"3500","DAPR_GRPC_PORT":"50001"}
Submit an order to the application using
curl
.curl --request POST --data "@sample.json" --header Content-Type:application/json $EXTERNAL_IP/neworder
Confirm the order has persisted by requesting it using
curl
.curl $EXTERNAL_IP/order
You should see output similar to the following example output:
{ "orderId": "42" }
Deploy the Python app with the Dapr sidecar
Navigate to the Python app directory in the
hello-kubernetes
quickstart and openapp.py
.This example 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'sneworder
endpoint by posting tov1.0/invoke/nodeapp/method/neworder
. The message contains some data with anorderId
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 using the
kubectl apply
command.kubectl apply -f ./deploy/python.yaml
Note
As with the previous command, you 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/pythonapp
Observe messages and confirm persistence
Now that both the Node.js and Python applications are deployed, you watch messages come through.
Get the logs of the Node.js app using the
kubectl logs
command.kubectl logs --selector=app=node -c node --tail=-1
If the deployments were successful, you should see logs like the following example logs:
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 using
curl
.curl $EXTERNAL_IP/order {"orderID":"42"}
You should see the latest JSON in the response.
Clean up resources
Remove the resource group, cluster, namespace, and all related resources using the az group delete command.
az group delete --name MyResourceGroup
Next steps
Azure Kubernetes Service