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.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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
$ sudo rabbitmqctl list_vhosts
Listing vhosts ...
name
/
test
2. Check the username
xxxxxxxxxx
$ sudo rabbitmqctl list_users
Listing users ...
user tags
claudia [test]
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
xxxxxxxxxx
$ sudo rabbitmqctl add_vhost test
3. Create a user using rabbitmqctl
on the new server
xxxxxxxxxx
$ sudo rabbitmqctl add_user claudia claudia
4. Set user permission using rabbitmqctl
on the new server
xxxxxxxxxx
$ sudo rabbitmqctl set_user_tags claudia administrator
$ 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
xxxxxxxxxx
$ sudo rabbitmqctl list_vhosts
Listing vhosts ...
name
/
test
$ sudo rabbitmqctl list_users
Listing users ...
user tags
claudia [test]
guest [administrator]
$ hostname
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
.)
xxxxxxxxxx
$ ls /var/lib/rabbitmq/
mnesia
$ cd /var/lib/rabbitmq/mnesia/
root@claudia:/var/lib/rabbitmq/mnesia# ls
rabbit@claudia rabbit@claudia-feature_flags
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
xxxxxxxxxx
$ 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.
xxxxxxxxxx
$ hostnamectl set-hostname claudia
$ hostname
claudia
5. Restart the RabbitMQ server
xxxxxxxxxx
$ sudo systemctl restart rabbitmq-server
Check the RabbitMQ Setting on the New Server
You can see that the migration is completed successfully:
xxxxxxxxxx
$ rabbitmqctl list_vhosts
Listing vhosts ...
name
/
test
$ rabbitmqctl list_users
Listing users ...
user tags
claudia [test]
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
).
xxxxxxxxxx
%% -------------------------------------------------------------------
%%
%% RABBITMQ_NODENAME
%% Erlang node name.
%% Default: rabbit@
%%
%% RABBITMQ_USE_LONGNAME
%% Flag indicating if long Erlang node names should be used instead
%% of short ones.
%% Default: unset (use short names)
xxxxxxxxxx
%% -------------------------------------------------------------------
%%
%% RABBITMQ_MNESIA_BASE
%% Directory where to create Mnesia directory.
%% Default: (Unix) ${SYS_PREFIX}/var/lib/rabbitmq/mnesia
%% (Windows) ${RABBITMQ_BASE}/db
%%
%% RABBITMQ_MNESIA_DIR
%% Directory where to put Mnesia data.
%% Default: (Unix) ${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}
%% (Windows) ${RABBITMQ_MNESIA_BASE}\${RABBITMQ_NODENAME}-mnesia
mnesia_base_dir(#{from_remote_node := Remote} = Context) ->
case get_prefixed_env_var("RABBITMQ_MNESIA_BASE") of
false when Remote =:= offline ->
update_context(Context, mnesia_base_dir, undefined, default);
false ->
mnesia_base_dir_from_node(Context);
Value ->
Dir = normalize_path(Value),
update_context(Context, mnesia_base_dir, Dir, environment)
end;
mnesia_base_dir(Context) ->
mnesia_base_dir_from_env(Context).
mnesia_base_dir_from_env(Context) ->
case get_prefixed_env_var("RABBITMQ_MNESIA_BASE") of
false ->
Dir = get_default_mnesia_base_dir(Context),
update_context(Context, mnesia_base_dir, Dir, default);
Value ->
Dir = normalize_path(Value),
update_context(Context, mnesia_base_dir, Dir, environment)
end.
...
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.
Opinions expressed by DZone contributors are their own.
Comments