关注微信公众号
第一手干货与资讯
加入官方微信群
获取免费技术支持
[RabbitMQ ] RabbitMQ is a messaging broker that transports messages between data producers and data consumers. Data producers can be just about any application, host, or device that emits data that needs to be consumed by other applications for aggregation, processing, or analysis. RabbitMQ is easy to set up, use, and maintain. It can be scaled to handle large numbers of messages between many different data producers and consumers in a variety of application use cases. Rancher provides a container platform that eases managing clusters of container-based applications. It works natively with Kubernetes, Mesos, and Docker Swarm, as well as its own container orchestration and scheduling tool, which is called Cattle. In this article, we are going to discuss setting up a simple RabbitMQ installation in a container using Rancher Server. Connecting to the RabbitMQ management interface, as well as sending and receiving your first message, will be covered.
For the purposes of this tutorial, I assume you already have a Rancher Server instance set up, and that you have added at least one host to it where you can deploy containers. If you haven’t already done this, Rancher provides excellent documentation in their Quick Start Guide that explains how to set up Rancher Server. If you’re not familiar with Rancher Server at all, consider watching the latest Rancher Overview Training on YouTube before proceeding with the Rancher installation. This will help you to understand certain key concepts about Rancher. It’s all right if the only host you have added to Rancher Server is that which has the Rancher Server container running on it. [Rancher normally recommends adding containers in the context of a new service or ][via Rancher catalog (which includes scaling capabilities)][, in which case launching RabbitMQ is as simple as selecting its entry from the community catalog within Rancher. However, in some cases it is useful to launch just a single container for RabbitMQ, without clustering. ]Only one RabbitMQ container will be deployed during this demonstration. For this article, I used a host with 2 vCPU, 4GB of RAM, and 20GB of disk space to ensure enough system level resources to host both the Rancher Server and RabbitMQ containers. The Rancher Server and RabbitMQ containers are both instantiated on the same host. You will also need a Linux workstation or other host with a web browser to connect to the Rancher Server UI, as well as to the RabbitMQ web management interface. In addition, your workstation host will need Python 2.6 or later, but not Python 3. Free eBook: Comparing Kubernetes, Mesos, and Docker Swarm For the workstation, I used a Macbook running OS X El Capitan, with Chrome as the browser and Python 2.6 installed to connect to Rancher Server and RabbitMQ. While performing the steps described in this article, a network or Internet connection with TCP ports 5672, 8080, and 15672 will need to be accessible from your workstation to the host running Rancher and RabbitMQ. Ideally this would be running on a private network, but it could be done over the Internet if you restrict access to only trusted hosts such as your workstation. Note that it is not recommended to leave the above ports open to the Internet. When initially configuring Rancher Server and RabbitMQ, they start up in an insecure manner, and they must be locked down before exposing these services to the Internet.
If you have all of the prerequisites in place, you should have a host with a running Rancher Server container to which you can connect from your workstation using the management URL on port 8080, as well as TCP ports 5672, 15672. Now you’re ready to set up your first RabbitMQ container using Rancher via the following steps:
http://<SERVER_IP>:8080
<SERVER_IP>
Your RabbitMQ container is now up and running. The next step is to configure and test it.
Now that your container is running, you’ll want to take a moment to create a secure login that you can use to connect to RabbitMQ. To do this, follow these steps:
http://<SERVER_IP>:15672
<SERVER_IP>
The next step will be to test connecting to the RabbitMQ container to verify that everything is working as expected. First, be sure you have Python 2.6 or later (not Python 3) installed on your workstation.
http://<SERVER_IP>:15672/cli.
rabbitmqadmin
Open a terminal window, and from the command line, input the following:
rabbitmqadmin -H -u -p list vhosts
Replace SERVER_IP, USERNAME and PASSWORD with the correct parameters you’ve already set prior to this. You should see output similar to the following:
SERVER_IP
USERNAME
PASSWORD
+------+----------+ | name | messages | +------+----------+ | / | | +------+----------+
Now, you’ll create a queue into which you can put your first message:
rabbitmqadmin -H -u -p declare queue name=test durable=false
You should see:
queue declared
Send a message with this command:
rabbitmqadm in -H -u -p publish exchange=amq.default routing_key=test payload="hello, world"
Message published
Retrieve your message:
rabbitmqadmin -H -u -p get queue=test requeue=false
You should see output similar to the following:
+-------------+----------+---------------+--------------+---------------+------------------+-----------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +-------------+----------+---------------+--------------+---------------+------------------+------------+-------------+ | test | | 0 | hello, world | 12 | string | | False | +-------------+----------+---------------+--------------+---------------+------------------+------------+-------------+
You’ve now sent and retrieved your first message using RabbitMQ.
For help using rabbitmqadmin, see the RabbitMQ Management CLI documentation, or you can type rabbitmqadmin --help from the CLI.
In this article, we’ve learned how to set up a simple RabbitMQ installation in a container using Rancher Server, as well as how to connect to the RabbitMQ management interface and send and receive messages using rabbitmqadmin. If you’re interested in proceeding further with your new RabbitMQ installation, or trying out RabbitMQ in general, you might consider trying out the new RabbitMQ Tutorials. Steve Tidwell has been working in the tech industry for over two decades, and has done everything from end-user support to scaling a global data ingestion and analysis platform to handle data analysis for some of the largest streaming events on the Web. He is currently Lead Architect for a well-known tech news site, where he plots to take over the world with cloud based technologies from his corner of the office.