Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to use service binding with RabbitMQ

November 3, 2021
Andy Sadler
Related topics:
Developer toolsKubernetesMicroservicesOperators
Related products:
Red Hat OpenShift

    The Kubernetes ecosystem has inconsistent ways to expose Secrets to applications in order to allow them to connect to services. Many service providers have their own bespoke methods of binding an application to their services, which can slow down development teams considerably.

    The Service Binding Operator remedies this by managing the binding process. This article walks through a simple example of service binding in action using the open source RabbitMQ message broker.

    How Service Binding Operator manages the binding process

    When you request a binding, the Service Binding Operator looks at information stored within the custom resource and its corresponding custom resource definition. This information tells the Service Binding Operator the proper method for binding the application. The Service Binding Operator then projects the binding method into the application's container through environment variables or files mounted within the container.

    To learn more about other features of the Service Binding Operator and its integration with other products, read our release announcement Announcing Service Binding Operator 1.0 GA.

    About the example

    Let's say you have two Kubernetes services, producer and consumer, that talk to a RabbitMQ instance using the Advanced Message Queuing Protocol (AMQP). The producer periodically produces data that the consumer reads and acts on. For the sake of this demonstration, the consumer's action is simply to print whatever it receives to the standard output (stdout).

    Prerequisites

    First, install the RabbitMQ Operator:

    $ kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
    

    Next, install the Operator Lifecycle Manager (OLM), a prerequisite for the Service Binding Operator:

    $ curl -sL https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.19.1/install.sh | bash -s v0.19.1
    

    Note: Yes, running curl ... | bash isn't the best security. If this is a concern for you, save the installation script to a location in your filesystem and execute the script there after inspecting its contents.

     

    Finally, you'll also need to install the Service Binding Operator itself: bash $ kubectl apply -f https://operatorhub.io/install/service-binding-operator.yaml ## Deploy the application on Kubernetes Next, you'll want to have the producer and consumer running on the Kubernetes cluster. For convenience, I've authored two containers that provide this functionality; their sources can be found in my GitHub repository. The Service Binding Operator can operate against deployments, and deployments make the most sense for running the applications in this example. You can deploy them with the following commands: bash $ kubectl create deployment producer --image=quay.io/ansadler/rabbitmq-producer:1.0 $ kubectl create deployment consumer --image=quay.io/ansadler/rabbitmq-consumer:1.0 You'll also need a RabbitMQ cluster to run them against: bash apiVersion: rabbitmq.com/v1beta1 kind: RabbitmqCluster metadata: name: rabbitmq spec: service: type: ClusterIP Now, if you inspect the container logs for the consumer (which monitors the consumer process's stdout), you'll see something similar to this: bash $ kubectl logs consumer-deployment-f877cffb6-p9sks Error: 0: $RABBITMQCLUSTER_HOST not defined Location: src/consumer.rs:16 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets.

    Note: The pod whose logs you will be inspecting will not be the same as this example, since the name of the pod that runs our applications will be different every time the deployment is changed. You can retrieve the name of the pod using the following command:

     

    $ kubectl get pods --selector=app=consumer

    If you inspect the logs for the producer as well, you'll see that it throws a similar error. This happens because you haven't bound your RabbitMQ cluster to the producer and consumer, so neither of them knows where to find it. Let's fix that.

    Bind the services together with ServiceBindings

    If you were not using the Service Binding Operator, you would need to tell both the producer and the consumer how to connect to the RabbitMQ instance. This would require distributing at least the following information to these applications:

    • The hostname of the RabbitMQ instance.
    • The port that the RabbitMQ instance is listening on.
    • Authentication credentials (such as username and password).

    This in turn would require you to expose your secrets to your applications, either by having the applications directly fetch that information from Kubernetes's API or by projecting that information into your applications yourself. Both of these methods are rather intrusive toward the applications, and it stands to reason that the process could be automated. And that's where the Service Binding Operator comes in.

    To bind your applications and services together, the Service Binding Operator introduces a new custom resource called ServiceBinding, which represents the binding between an application and a service. In this particular example, the bindings for our producer and consumer applications look like this:

    ---
    apiVersion: binding.operators.coreos.com/v1alpha1
    kind: ServiceBinding
    metadata:
      name: servicebinding-consumer
    spec:
      bindAsFiles: false
      services:
      - group: rabbitmq.com
        version: v1beta1
        kind: RabbitmqCluster
        name: rabbitmq
      application:
        name: consumer-deployment
        version: v1
        group: apps
        resource: deployments
    ---
    apiVersion: binding.operators.coreos.com/v1alpha1
    kind: ServiceBinding
    metadata:
      name: servicebinding-producer
    spec:
      bindAsFiles: false
      services:
      - group: rabbitmq.com
        version: v1beta1
        kind: RabbitmqCluster
        name: rabbitmq
      application:
        name: producer-deployment
        version: v1
        group: apps
        resource: deployments
    ---
    

    Note: If you are running this against an Operator not already supported by the Service Binding Operator (see our README for a list of supported Operators), you will to give Service Binding Operator permission to read from this service according to the rules of role-based access control (RBAC). You can read more about how to grant this permission that in our documentation.

     

    Now, if you inspect the logs of your consumer deployment, you'll see that producer has been sending messages to it. You should see something similar to the following:

    $ kubectl logs consumer-deployment-6f48dbfb7d-5dsgd
    connecting to: amqp://default_user_7Jba_ZP7NKD-UjJK8AQ:HIhVZ4a_6Xm60Z7bmbEDADDpwr2e_tch@rabbitmq.default.svc:5672
    Waiting for messages, press Ctrl-C to exit.
    (  0) Received [hello, world!]
    (  1) Received [hello, world!]
    (  2) Received [hello, world!]
    (  3) Received [hello, world!]
    (  4) Received [hello, world!]
    (  5) Received [hello, world!]
    (  6) Received [hello, world!]
    (  7) Received [hello, world!]
    (  8) Received [hello, world!]
    (  9) Received [hello, world!]
    ( 10) Received [hello, world!]
    ( 11) Received [hello, world!]
    ( 12) Received [hello, world!]
    

    producer says something similar:

    $ kubectl logs producer-deployment-6d8d55949d-8qd9c
    connecting to: amqp://default_user_7Jba_ZP7NKD-UjJK8AQ:HIhVZ4a_6Xm60Z7bmbEDADDpwr2e_tch@rabbitmq.default.svc:5672
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    sending [hello, world!] to queue hello
    

    Resources

    To learn more about the Service Binding Operator, check out the following resources:

    • Service Binding Operator on GitHub
    • Service Binding Operator documentation
    • Materials used in this article
    Last updated: September 20, 2023

    Related Posts

    • Announcing Service Binding Operator 1.0 GA

    • Connect Node.js applications to Red Hat OpenShift Streams for Apache Kafka with Service Binding

    • Introducing the Service Binding Operator

    • Service Binding Operator: The Operator in action

    • Deploying the Mosquitto MQTT message broker on Red Hat OpenShift, Part 1

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.