HowTo: Configure Pub/Sub components with multiple namespaces

Use Dapr Pub/Sub with multiple namespaces

In some scenarios, applications can be spread across namespaces and share a queue or topic via PubSub. In this case, the PubSub component must be provisioned on each namespace.

Note

Namespaces are a Dapr concept used for scoping applications and components. This example uses Kubernetes namespaces, however the Dapr component namespace scoping can be used on any supported platform. Read How-To: Scope components to one or more applications for more information on scoping components.

This example uses the PubSub sample. The Redis installation and the subscribers are in namespace-a while the publisher UI is in namespace-b. This solution will also work if Redis is installed on another namespace or if you use a managed cloud service like Azure ServiceBus, AWS SNS/SQS or GCP PubSub.

This is a diagram of the example using namespaces.

Multiple namespaces - 图1

The table below shows which resources are deployed to which namespaces:

Resourcenamespace-anamespace-b
Redis masterX
Redis replicasX
Dapr’s PubSub componentXX
Node subscriberX
Python subscriberX
React UI publisherX

Pre-requisites

Setup namespace-a

Create namespace and switch kubectl to use it.

  1. kubectl create namespace namespace-a
  2. kubectl config set-context --current --namespace=namespace-a

Install Redis (master and slave) on namespace-a, following these instructions.

Now, configure deploy/redis.yaml, paying attention to the hostname containing namespace-a.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: pubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: "redisHost"
  10. value: "redis-master.namespace-a.svc:6379"
  11. - name: "redisPassword"
  12. value: "YOUR_PASSWORD"

Deploy resources to namespace-a:

  1. kubectl apply -f deploy/redis.yaml
  2. kubectl apply -f deploy/node-subscriber.yaml
  3. kubectl apply -f deploy/python-subscriber.yaml

Setup namespace-b

Create namespace and switch kubectl to use it.

  1. kubectl create namespace namespace-b
  2. kubectl config set-context --current --namespace=namespace-b

Deploy resources to namespace-b, including the Redis component:

  1. kubectl apply -f deploy/redis.yaml
  2. kubectl apply -f deploy/react-form.yaml

Now, find the IP address for react-form, open it on your browser and publish messages to each topic (A, B and C).

  1. kubectl get service -A

Confirm subscribers received the messages.

Switch back to namespace-a:

  1. kubectl config set-context --current --namespace=namespace-a

Find the POD names:

  1. kubectl get pod # Copy POD names and use in the next commands.

Display logs:

  1. kubectl logs node-subscriber-XYZ node-subscriber
  2. kubectl logs python-subscriber-XYZ python-subscriber

The messages published on the browser should show in the corresponding subscriber’s logs. The Node.js subscriber receives messages of type “A” and “B”, while the Python subscriber receives messages of type “A” and “C”.

Clean up

  1. kubectl delete -f deploy/redis.yaml --namespace namespace-a
  2. kubectl delete -f deploy/node-subscriber.yaml --namespace namespace-a
  3. kubectl delete -f deploy/python-subscriber.yaml --namespace namespace-a
  4. kubectl delete -f deploy/react-form.yaml --namespace namespace-b
  5. kubectl delete -f deploy/redis.yaml --namespace namespace-b
  6. kubectl config set-context --current --namespace=default
  7. kubectl delete namespace namespace-a
  8. kubectl delete namespace namespace-b

Last modified August 2, 2021 : Fix Java SDK link (#1695) (2c67fd1)