0

I want to have 2 mosquitto brokers running on Debian 11, each with it's own configuration. I have one instance installed and running, but don't know how to start second. I'm a newbie so please answear as simply and clearly as possible

Marek J.
  • 1
  • 2

2 Answers2

0

First off: Networking basics: Only one server can listen to the same TCP port on a single IP address.

So, you will need to use two different IP addresses, or configure different ports.

That out of the way: The whole linux container world is mostly used for exactly what you describe: run an arbitrary number of the same kind of service, doing different things, on one Linux machine. You might have heard of Docker, LXC, Kubernetes… those things.

So, it's not very hard to do the same for your use case! Especially since it seems you already know how to set up mosquitto (which I have no clue whatsoever about).

You will need to install some kind of container runner on your debian. Debian 11 comes with podman that works out of the box and doesn't introduce a new daemon or strange dependencies, so let's roll with that.

sudo apt install -y podman

That wasn't very painful :) So, let's make sure that your local user (and not root only) can run containers:

sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 YOURUSERNAMEHERE

Great! Log out and in, and let's test that magic:

podman run -it --rm debian:11

This runs a new container, from the "debian:11" image, interactively, with attached terminal, and removes it after it quits.

Try it! That's a clean debian-in-a-container that you have there. You can run apt update inside, try installing some software inside and so on, without touching your "outer" debian at all. Nice. Type exit, and all the spook is over – no trace of what you've done inside of that container remains.

You probably know where this is going to go: You install mosquitto inside such a container, set it up as you want to, and then connect it to your network.

To achieve that, we'll

  1. Build a container containing exactly the software you need,
  2. but put the configuration files outside, so you can change them without being inside the container
  3. and take that MQTT port and expose it to your network.

Let's first settle on a network port – I assume you'll be using 4444. Any other 65536 > port > 1024 is fine, too.

So, start by making a new directory (I'm assuming your home directory is /home/marek, adjust if necessary):

mkdir -p /home/marek/mosquitto-container/config

Next, we'll let a clean installation of mosquitto put the default configuration files there. For that we'll just make a debian container, make it install mosquitto, and save it to a container image that we can later restart whenever we want. We're using a "volume" there, which maps a directory on your "outer" debian (/home/marek/mosquitto-container/config) to a (potentially different) directory on the "inner" debian (/etc/mosquitto):

podman run --name mareksmosquitto --rm -v /home/marek/mosquitto-container/config:/etc/mosquitto debian:11
[root@124578]# apt-get update; apt-get install -y mosquitto'
[root@124578]# apt-get install -y mosquitto

In a separate terminal, commit the state of that container to an image:

podman commit mareksmosquitto mareksmosquittoimage

Great! What we just did is create an image we can run, using podman run …, of a debian system with mosquitto installed. And we can put any mosquito configuration that we want inside – simply by pointing to a configuration directory using -v /path/on/outer/:/etc/mosquitto when running!

We can now go back to the first terminal and end the container:

[root@124578]# exit

Look inside your /home/marek/mosquitto-container/config! There should be a bunch of configuration files there, including the mosquitto.conf; modify that as you wanted to. Especially, set the port to 4444 (instead of 1883 or 8883)!

After having modified the mosquitto.conf to do what you want, test the whole thing:

podman run -it --rm -v /home/marek/mosquitto-container/config:/etc/mosquitto -p 4444:4444 mareksmosquittoimage /usr/sbin/mosquitto

What has changed compared to before:

  • we now expose port 4444 from inside the container to port 4444 outside the container; -p 4444:4444 does that!
  • we run a container from mareksmosquittoimage, instead of the official debian:11 image.
  • instead of starting a shell inside the container, we go straight ahead and run /usr/sbin/mosquitto inside the new container. So, we run mosquitto.

Guess what you have to do to run, say, 20 further brokers? Right, you just run the above command 20 times, but specify a different configuration directory with that -v option every time, and use a different port with -p.

Marcus Müller
  • 21,602
  • 2
  • 39
  • 54
0

I did it just by adding these 2 lines to /etc/rc.local:

/path/to/mosquitto -d -c config1
/path/to/mosquitto -d -c config2

so everytime my system restarts it runs both mosquitto configurations.

Marek J.
  • 1
  • 2