Use Dapr to Build Distributed Applications Easily on Kubernetes 🎩
🐳 Introduction
(monitoring the system's health), security, and scalability across various services and runtimes.
Additionally, these applications often interact with or (like databases), and external services (like third-party APIs), necessitating a thorough understanding of specific APIs and SDKs, which adds to the complexity. The need for robust error handling, efficient load balancing, and seamless service discovery further complicates the development process.
In this blog, we will explore how the open-source (publish/subscribe communication), which can significantly reduce the development effort.
By using Dapr’s built-in best practices and patterns, we will also highlight common use cases for Dapr on . You can also learn more about , and (Distributed Application Runtime) is an open-source project designed to simplify the development of microservices. It provides a set of building blocks that address common challenges in building distributed applications, such as service-to-service communication, state management, and pub/sub messaging.
🌟 Main Features of Dapr
API. This API abstracts the complexity and provides built-in retries, timeouts, and error handling. The that allows you to store and retrieve state across different services. This API supports various state stores like Redis, DynamoDB, and Cosmos DB, making it flexible and easy to use.
Publish/Subscribe Messaging:
- For event-driven architectures, Dapr offers a .
Workflow:
- Dapr provides a built-in to handle sensitive information like API keys and database credentials.
Configuration:
- Dapr provides a to provide mutually exclusive access to shared resources from an application.
Cryptography:
- Dapr includes a to manage the scheduling and orchestration of jobs.
By leveraging these features, Dapr simplifies the development of distributed applications, allowing developers to focus on writing business logic rather than dealing with the complexities of distributed systems. This leads to faster development cycles, more reliable applications, and easier maintenance.
❓ Why Do We Need Dapr?
Developing distributed applications involves several challenges:
Complex Communication: Ensuring reliable communication between services can be difficult, especially when dealing with different protocols and error handling.
State Management: Keeping track of state across multiple services requires a consistent and reliable approach.
Event-Driven Architecture: Implementing pub/sub messaging patterns can be complex and requires integration with various message brokers.
External Integrations: Connecting to external systems like databases and third-party APIs often involves writing boilerplate code.
Observability: Monitoring and diagnosing issues in a distributed system requires comprehensive logging, metrics, and tracing.
Security: Ensuring secure communication and managing secrets are essential for protecting your application.
Isolation: Dapr namespacing provides isolation and multi-tenancy across many capabilities, giving greater security. Typically applications and components are deployed to namespaces to provide isolation in a given environment, such as Kubernetes.
Dapr addresses these challenges by providing a set of standardized APIs and components that simplify the development process. By using Dapr, developers can focus on writing business logic instead of dealing with the complexities of distributed systems. This leads to faster development cycles, more reliable applications, and easier maintenance.
For more details,
In this section, we'll demonstrate how to deploy services with unique application IDs, allowing other services to discover and call endpoints using Dapr's service invocation over HTTP.
🆔 Step 1: Choose an ID for Your Service
Dapr allows you to assign a global, unique ID for your app. This ID encapsulates the state for your application, regardless of the number of instances it may have.
🛠️ Step 2: Set an App-ID When Deploying to Kubernetes
In Kubernetes, set the dapr.io/app-id annotation on your pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: default
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "order-processor"
dapr.io/app-port: "6001"
dapr.io/app-protocol: "http" # Use "https" if your app uses TLS
📡 Step 3: Invoke the Service
To invoke an application using Dapr, you can use the invoke API on any Dapr instance. The sidecar programming model encourages each application to interact with its own instance of Dapr. The Dapr sidecars discover and communicate with one another.
Below is an example in Python that leverages Dapr SDKs for service invocation:
import random
from time import sleep
import logging
import requests
import json
base_url = "http://localhost:3500/v1.0/invoke/order-processor/method"
headers = {'Content-Type': 'application/json'}
logging.basicConfig(level=logging.INFO)
while True:
sleep(random.randrange(50, 5000) / 1000)
order_id = random.randint(1, 1000)
order = {'orderId': order_id, 'item': 'laptop', 'quantity': 1}
# Invoke a service
result = requests.post(
url=f'{base_url}/orders',
data=json.dumps(order),
headers=headers
)
logging.info(f'Order requested: {order_id}')
logging.info(f'Result: {result.text}')
🌐 Additional URL Formats
To invoke a GET endpoint:
curl http://localhost:3500/v1.0/invoke/order-processor/method/orders/100
Dapr provides multiple ways to call the service invocation API:
- Change the address in the URL to
localhost:<dapr-http-port>. - Add a
dapr-app-idheader to specify the ID of the target service, or alternatively pass the ID via HTTP Basic Auth:http://dapr-app-id:<service-id>@localhost:3500/path.
For example, the following command:
curl http://localhost:3500/v1.0/invoke/order-processor/method/orders/100
is equivalent to:
curl -H 'dapr-app-id: order-processor' 'http://localhost:3500/orders/100' -X GET
or:
curl 'http://dapr-app-id:order-processor@localhost:3500/orders/100' -X GET
Using CLI:
dapr invoke --app-id order-processor --method orders/100
🔍 Including a Query String in the URL
You can append a query string or a fragment to the end of the URL, and Dapr will pass it through unchanged. For example:
curl 'http://dapr-app-id:order-processor@localhost:3500/orders/100?basket=1234&key=abc' -X GET
🏷️ Using Namespaces
When running on namespace-supported platforms, include the namespace of the target app in the app ID. For example, use order-processor.production.
Invoking the service with a namespace would look like:
curl http://localhost:3500/v1.0/invoke/order-processor.production/method/orders/100 -X GET
This example demonstrates how to use Dapr's service invocation to call services securely and efficiently within a Kubernetes environment. For more details, check the .
dapr init -k --dev
Expected output in a fresh Kubernetes cluster without Dapr installed:
⌛ 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
ℹ️ 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...
✅ Deploying the Dapr Redis with 17.14.5 version to your cluster...
✅ Deploying the Dapr Zipkin with latest version to your cluster...
ℹ️ Applying "statestore" component to Kubernetes "default" namespace.
ℹ️ Applying "pubsub" component to Kubernetes "default" namespace.
ℹ️ Applying "appconfig" zipkin configuration to Kubernetes "default" namespace.
✅ 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
🐍 Deploy the Python App
Next, we deploy the Python app. This is a basic Python app that posts JSON messages to localhost:3500, 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:
import time
import requests
n = 0
dapr_url = "http://localhost:3500/v1.0/invoke/nodeapp/method/neworder"
while True:
n += 1
message = {"data": {"orderId": n}}
try:
response = requests.post(dapr_url, json=message)
print(response.json())
except Exception as e:
print(e)
time.sleep(1)
To deploy the Python app to the Kubernetes cluster:
kubectl apply -f ./deploy/python.yaml
🟢 Deploy the Node.js App
To deploy the Node.js app to Kubernetes, use the following command:
kubectl apply -f ./deploy/node.yaml
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.
dapr.io/enable-api-logging: "true"- This is added tonode.yamlfile by default to see the API logs.
You'll also see the container image that you're deploying. If you want to update the code and deploy a new image, see the Next Steps section.
🔄 Accessing the Kubernetes Service
There are several different ways to access a Kubernetes service depending on which platform you are using. Port forwarding is one consistent way to access a service, whether it is hosted locally or on a cloud Kubernetes provider like AKS.
kubectl port-forward service/nodeapp 8080:80
This will make your service available on http://localhost:8080.
Configure the Redis Statestore Component
Apply the redis.yaml file and observe that your state store was successfully configured!
kubectl apply -f ./deploy/redis.yaml
📜 Viewing Logs
Now that the Node.js and Python applications are deployed, watch messages come through:
Node.js App Logs
Get the logs of the Node.js app:
kubectl logs --selector=app=node -c node --tail=-1
If all went well, you should see logs like this:
Got a new order! Order ID: 1
Successfully persisted state for Order ID: 1
Got a new order! Order ID: 2
Successfully persisted state for Order ID: 2
Got a new order! Order ID: 3
Successfully persisted state for Order ID: 3
API Call Logs
Observe API call logs:
Node.js App API Logs
Get the API call logs of the Node.js app:
kubectl logs --selector=app=node -c daprd --tail=-1
When save state API calls are made, you should see logs similar to this:
time="2024-11-02T22:46:09.82121774Z" level=info method="POST /v1.0/state/statestore" app_id=nodeapp instance=nodeapp-7dd6648dd4-7hpmh scope=dapr.runtime.http-info type=log ver=1.7.2
time="2024-11-02T22:46:10.828764787Z" level=info method="POST /v1.0/state/statestore" app_id=nodeapp instance=nodeapp-7dd6648dd4-7hpmh scope=dapr.runtime.http-info type=log ver=1.7.2
Python App API Logs
Get the API call logs of the Python app:
kubectl logs --selector=app=python -c daprd --tail=-1
time="2024-11-02T02:47:49.972688145Z" level=info method="POST /neworder" app_id=pythonapp instance=pythonapp-545df48d55-jvj52 scope=dapr.runtime.http-info type=log ver=1.7.2
time="2024-11-02T02:47:50.984994545Z" level=info method="POST /neworder" app_id=pythonapp instance=pythonapp-545df48d55-jvj52 scope=dapr.runtime.http-info type=log ver=1.7.2
✅ Confirm Successful Persistence
Call the Node.js app's order endpoint to get the latest order. Grab the external IP address that you saved before, append /order, and perform a GET request against it (enter it into your browser, use Postman, or curl it!):
curl $NODE_APP/order
{"orderID":"42"}
You should see the latest JSON in response!
This will spin down each resource defined by the .yaml files in the deploy directory, including the state component.
Note: This will also delete the state store component. If the
--devflag was used for Dapr init, and you want to use thedapr-dev-redisdeployment as state store, replace theredisHostvalue inside./deploy/redis.yamlwithdapr-dev-redis-master:6379and also thesecretKeyRef,namewithdapr-dev-redis. Then run the commandkubectl apply -f ./deploy/redis.yaml, to apply the file again. This will create astatestoreDapr component pointing todapr-dev-redisdeployment.
For more details, check the .
Happy coding! 🚀
💡 Thank you for Reading !! 🙌🏻😁📃, see you in the next blog.🤘 Until next time 🎉
🚀 Thank you for sticking up till the end. If you have any questions/feedback regarding this blog feel free to connect with me:
♻️ LinkedIn:
The end ✌🏻
🔰 Keep Learning !! Keep Sharing !! 🔰
📅 Stay updated
Subscribe to our newsletter for more insights on AWS cloud computing and containers.
SOCIAL SHARE CARD GENERATOR