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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Trending

  • How to Build and Optimize AI Models for Real-World Applications
  • Introduction to Retrieval Augmented Generation (RAG)
  • Engineering LLMOps: Building Robust CI/CD Pipelines for LLM Applications on Google Cloud
  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  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
21.9K 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
[email protected] 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

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook