Pub/Sub and 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.
In this example, we will use the PubSub sample. Redis installation and the subscribers will be in namespace-a
while the publisher UI will be on namespace-b
. This solution should also work if Redis was installed on another namespace or if we used a managed cloud service like Azure ServiceBus.
The table below shows which resources are deployed to which namespaces:
- on any namespace since Dapr works at the cluster level.
- Checkout and cd into directory for PubSub sample.
Create namespace and switch kubectl to use it.
Now, configure deploy/redis.yaml
, paying attention to the hostname containing namespace-a
.
apiVersion: dapr.io/v1alpha1
kind: Component
namespace: default
spec:
type: pubsub.redis
version: v1
metadata:
- name: "redisHost"
value: "redis-master.namespace-a.svc:6379"
- name: "redisPassword"
Deploy resources to namespace-a
:
kubectl apply -f deploy/node-subscriber.yaml
kubectl apply -f deploy/python-subscriber.yaml
Create namespace and switch kubectl to use it.
Deploy resources to namespace-b
, including the Redis component:
kubectl apply -f deploy/redis.yaml
kubectl apply -f deploy/react-form.yaml
kubectl get service -A
Switch back to namespace-a
:
Find the POD names:
kubectl get pod # Copy POD names and use in the next commands.
Display logs:
kubectl logs node-subscriber-XYZ node-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”.