DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action
  • Javac and Java Katas, Part 2: Module Path

Trending

  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • AI Agents: A New Era for Integration Professionals
  • Securing the Future: Best Practices for Privacy and Data Governance in LLMOps
  • System Coexistence: Bridging Legacy and Modern Architecture
  1. DZone
  2. Coding
  3. Languages
  4. The Best Way To Migrate a RabbitMQ Server

The Best Way To Migrate a RabbitMQ Server

Instead of repetitive tasks, I discovered a relationship between RabbitMQ settings and a hostname, which ultimately decreased repetition when migrating servers.

By 
Juhee Kang user avatar
Juhee Kang
·
Updated Mar. 27, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
20.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

As the userbase of my company increased over time, I realized it was necessary to apply auto-scaling to my servers. It required server migration. Migrating RabbitMQ settings meant a lot of repetitive tasks to ensure a smooth migration process; rather than perform this, I discovered a relationship between the RabbitMQ settings and the hostname, which ultimately decreased repetition.

In this article, I'll explain how to migrate RabbitMQ using the hostname.


A Basic Way To Migrate RabbitMQ (Using RabbitMQCTL)

In order to migrate RabbitMQ to the same existing server setting, you need to manually set each of the settings (such as vhost, username, permission, etc.) from the existing server. For example, there is a RabbitMQ server consisted of the settings which the Vhost is test and username is Claudia. If you migrate this server, you will go through the following steps.


Check the Existing Server RabbitMQ Setting

1. Check the vhost

Shell
x
 
1
$ sudo rabbitmqctl list_vhosts
2
Listing vhosts ...
3
name
4
/
5
test


2. Check the username

Shell
xxxxxxxxxx
1
 
1
$ sudo rabbitmqctl list_users 
2
Listing users ... 
3
user tags 
4
claudia [test] 
5
guest [administrator]


Try To Migrate To the RabbitMQ Server

1. Install the RabbitMQ server on a new server

2. Create a vhost using rabbitmqctl on the new server

Shell
xxxxxxxxxx
1
 
1
$ sudo rabbitmqctl add_vhost test


3. Create a user using rabbitmqctl on the new server

Shell
xxxxxxxxxx
1
 
1
$ sudo rabbitmqctl add_user claudia claudia


4. Set user permission using rabbitmqctl on the new server

Shell
xxxxxxxxxx
1
 
1
$ sudo rabbitmqctl set_user_tags claudia administrator 
2
$ sudo rabbitmqctl set_permissions -p test claudia “.*” “.*” “.*”


It is easy to migrate the RabbitMQ server using this way when the server has some vhost, user, etc... But if the server has many settings, it is inefficient to migrate this way because it will take a lot of time. (Maybe it requires repetition according to the settings.)

Therefore, I will introduce a new migration way without repetition that works.


A New Way To Migrate RabbitMQ Using Copy and Setting Hostname

For using the new way, we need to check the setting files. You can find the setting files on /var/lib/rabbitmq. Thus, let's start a migration using these files.

Check the Existing Server RabbitMQ Setting

Shell
xxxxxxxxxx
1
12
 
1
$ sudo rabbitmqctl list_vhosts
2
Listing vhosts ...
3
name
4
/
5
test
6
$ sudo rabbitmqctl list_users
7
Listing users ...
8
user tags
9
claudia [test]
10
guest [administrator]
11
$ hostname
12
claudia


Try To Migrate the RabbitMQ Server

1. Find the setting files on the existing server

You can find the setting files according to the following command. (Tip: the path of RabbitMQ setting files is /var/lib/rabbitmq.)

Shell
xxxxxxxxxx
1
 
1
$ ls /var/lib/rabbitmq/
2
mnesia
3
$ cd /var/lib/rabbitmq/mnesia/
4
root@claudia:/var/lib/rabbitmq/mnesia# ls
5
rabbit@claudia rabbit@claudia-feature_flags
6
rabbit@claudia.pid rabbit@claudia-plugins-expand

2. Install the RabbitMQ server on a new server.

3. Copy the setting file of the existing server to /var/lib/rabbitmq/mnesia on the new server

Shell
xxxxxxxxxx
1
 
1
$ scp -r /var/lib/rabbitmq/mnesia <new-server-ip>:/var/lib/rabbitmq/mnesia


4. Change the hostname

If the hostname is not equal to the past one, the new server will not work properly.

Shell
xxxxxxxxxx
1
 
1
$ hostnamectl set-hostname claudia 
2
$ hostname 
3
claudia


5. Restart the RabbitMQ server

Shell
xxxxxxxxxx
1
 
1
$ sudo systemctl restart rabbitmq-server


Check the RabbitMQ Setting on the New Server

You can see that the migration is completed successfully:

Shell
xxxxxxxxxx
1
10
 
1
$ rabbitmqctl list_vhosts
2
Listing vhosts ...
3
name
4
/
5
test
6
$ rabbitmqctl list_users
7
Listing users ...
8
user tags
9
claudia [test]
10
guest [administrator]

With this method, no matter how many settings you have, it is easy to complete the migration.


How Is This Possible?

When starting the RabbitMQ server, it reads the environment files from the base directory according to rabbitMQ_NODENAME. Let's check the initialization environment process of the rabbitmq-server code, which is a function(get_prefixed_env_var).

rabbit_env.erl#L464-473

Erlang
 




xxxxxxxxxx
1
10


 
1
%% -------------------------------------------------------------------
2
%%
3
%% RABBITMQ_NODENAME
4
%%   Erlang node name.
5
%%   Default: rabbit@
6
%%
7
%% RABBITMQ_USE_LONGNAME
8
%%   Flag indicating if long Erlang node names should be used instead
9
%%   of short ones.
10
%%   Default: unset (use short names)



rabbit_env.erl#L744-L836

Erlang
 




xxxxxxxxxx
1
37


 
1
%% -------------------------------------------------------------------
2
%%
3
%% RABBITMQ_MNESIA_BASE
4
%%   Directory where to create Mnesia directory.
5
%%   Default: (Unix) ${SYS_PREFIX}/var/lib/rabbitmq/mnesia
6
%%         (Windows) ${RABBITMQ_BASE}/db
7
%%
8
%% RABBITMQ_MNESIA_DIR
9
%%   Directory where to put Mnesia data.
10
%%   Default: (Unix) ${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}
11
%%         (Windows) ${RABBITMQ_MNESIA_BASE}\${RABBITMQ_NODENAME}-mnesia
12
 
          
13
 
          
14
mnesia_base_dir(#{from_remote_node := Remote} = Context) ->
15
    case get_prefixed_env_var("RABBITMQ_MNESIA_BASE") of
16
        false when Remote =:= offline ->
17
            update_context(Context, mnesia_base_dir, undefined, default);
18
        false ->
19
            mnesia_base_dir_from_node(Context);
20
        Value ->
21
            Dir = normalize_path(Value),
22
            update_context(Context, mnesia_base_dir, Dir, environment)
23
    end;
24
mnesia_base_dir(Context) ->
25
    mnesia_base_dir_from_env(Context).
26
 
          
27
 
          
28
mnesia_base_dir_from_env(Context) ->
29
    case get_prefixed_env_var("RABBITMQ_MNESIA_BASE") of
30
        false ->
31
            Dir = get_default_mnesia_base_dir(Context),
32
            update_context(Context, mnesia_base_dir, Dir, default);
33
        Value ->
34
            Dir = normalize_path(Value),
35
            update_context(Context, mnesia_base_dir, Dir, environment)
36
    end.
37
...



This means that if you copy the existing setup file format to the base directory /var/lib/RabbitMQ/mnesia, the setup will be applied as it is. In addition, you can see this type of setup files such as rabbit@<hostname>-<option> because of the default value of RabbitMQ_NODENAME(rabbit@<hostname>).

Therefore, if you have a hostname that is not the same as the existing server, the server will not run normally.


Conclusion

There are many different ways to migrate RabbitMQ, but it is difficult to find a way to do it easily and quickly. If you need to migrate RabbitMQ with complex settings, I recommend you use this hostname and the setup file copy method. Maybe using hostname makes for a more easy time to migrate RabbitMQ.

shell

Opinions expressed by DZone contributors are their own.

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action
  • Javac and Java Katas, Part 2: Module Path

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!