Multiple openhab2 instances on the same host

Good morning everybody.

I’d like to heve several oh2 instances oh2 same host.
This will allow to save money for cloud hosted solutions.

I’ve managed to run OH2 on a different port. Here is my ‘openhab2_8091.service’ file.
But trying to run different OH2 seems not possible, they interfere each other.
would it be possible in some way now or in future ?
Thanks

[Unit]
Description=Starts and stops the openHAB Home Automation Bus
Documentation=http://www.openhab.org
Wants=network-online.target
After=network-online.target

[Service]
EnvironmentFile=/etc/default/openhab2
Environment=OPENHAB_HTTP_PORT=8091
Environment=OPENHAB_CONF_DIR="/etc/openhab2_8091"
PIDFile=/var/run/opnhab2_8091.pid
User=openhab
Group=openhab
WorkingDirectory=/usr/share/openhab2
#PermissionsStartOnly=true
#ExecStartPre=/usr/share/openhab/bin/setpermissions.sh
ExecStart=/usr/share/openhab2/start.sh server
ExecStop=/usr/share/openhab2/runtime/karaf/bin/stop

Shutdown delay in seconds, before process is tried to be killed with KILL (if configured)

TimeoutStopSec=120
Restart=on-failure

[Install]
WantedBy=multi-user.target
root@rufus:/usr/lib/systemd/system

Hi Markus,

out of curiosity, why would you have 2 openhab instances of the same configuration running on the same machine?

I understand two instances for redundancy, yet no when they run on the same machine.

So please enlighten me, why are you doing this?

yves

Just one host to maintein to pay for when you have several unrelated regulation needs.
Using one OH there is the risk of conflicts.
On the other end using several host just for that is a waste of resources.
Hope this clarifies.

not sure I understand this. Could you give an example?

Suppose you want to control 4 different apartments of the same building.
You do not want apartments interfere each-other.
Each apartment will need its won rules and UI.

I don’t think you need for openhabds to do that. If you just have 4 sitemaps on the same openhab, that will do the trick (I think.)

yves

There are other ways to handle this with one OH instance. But putting that aside, are the conflicts strictly ports? Or are you seeing errors with accessing hardware (e.g. zwave dongle, GPIO pins, etc)?

If just ports, keep in mind that there are at least three ports and all three must be different. There is the HTTP (defaults to 8080), HTTPS (defaults to 8443) and Console Ports (in OH 1 defaults to 5555, not sure in OH 2). There might be additional ports in OH 2 I’m not aware of.

Only one instance of OH can access the same hardware device at a time. There is no way to share these devices across multiple running programs.

Given this is your goal, I strongly recommend looking into running OH in Docker containers. You can then map the default ports to whatever you want in the startup of the Docker container itself. This will provide an even stronger separation between the multiple instances of OH and it should improve management, maintenance, and upgrade of each instance.

For example, you can download from DockerHub or create your own OH 2 image. Then all you need are some openhab.service scripts (one for each instance) which starts a container with a separate name and different port mappings and volume mappings (if you want to preserve the data generated by the container). With this approach, you can update/upgrade the software on the one OH image then just rebuild the containers and everything is upgraded all at once.

I can elaborate or answer questions if desired. I’m slowly moving most of my services over to this approach more as a means to source control my service’s configuration than separation but so far I’m exceptionally happy with Docker. I’ll post an example Dockerfile and systemd service script for one of my services below.

With this script when I want to upgrade the software or build the initial image I just run docker build -t rich/mosquitto . which will recreate the image from scratch. Every time the systemd service runs, it creates a new container from this image (i.e. starts fresh). I put the config and logs on my host machine and map those to the container to preserve the data I want to preserve when the container is rebuilt.

If I wanted to run multiple mosquitto brokers, I would just need to create a copy of mosquitto.service, map the ports to a different number and the volumes to a different path and voila I have two independent services running on the same machine without conflicts.

The problem is he can’t control access to the different sitemaps on a per user basis (unless he sets up a reverse proxy which manages the user authentication and authorization).

Mosquitto Dockerfile

FROM ubuntu:latest
MAINTAINER Rich Koshak <rlkoshak@gmail.com>
LABEL multi.version="1.0" multi.notes="First attempt at building an Image"
RUN useradd --system --uid 1064 -M --shell /usr/sbin/nologin mosquitto \
    && apt-get -q update \
    && apt-get -q -y install mosquitto
EXPOSE 8883 1883
USER mosquitto
ENTRYPOINT ["/usr/sbin/mosquitto", "-c"]
CMD ["/etc/mosquitto/conf.d/mosquitto.conf"]

mosquitto.service

[Unit]
Description=Mosquitto
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker run --name=%n -p 8883:8883 -p 1883:1883 -v /opt/mosquitto/etc:/etc/mosquitto -v /opt/mosquitto/logs:/var/log -v /opt/mosquitto/lib:/var/lib/mosquitto rich/mosquitto
ExecStop=/usr/bin/docker stop -t 2 %n ; /usr/bin/docker rm -f %n

[Install]
WantedBy=multi-user.target

Thanks Rich and Yves for your interesting answers.

I’ve no hardware conflict problems because I’d like to talk MQTT to ny devices.
The problem is just how to manage different sites on one host.
Could it make sense to have multiple rule/script/site files on the same OH2 instance ?
About ports on OH2 I’m able to run 2 instances on different HTTP and HTTS pots but, despite of this, I’m still seeing this in the opehab.log.

2016-08-08 21:16:33.130 [WARN ] [org.apache.karaf.shell.ssh.Activator] - Exception caught while starting SSH server
java.net.BindException: Address already in use

The 2 UIs seems to be working anyway on the 2 HTTP ports.

I’m not familiar with Dockers, it sound very interesting and your example Rich is really of great help. I guess that using Docker is not for free… isn’t it ?

Thanks ! ciao

That is because there is also a console port that openHAB opens up. Those need to be different too.

Completely free and open source and included in the app repo for most Linux distros. You can pay for higher levels of support or to host your containers on their cloud but for most home use none of that is needed.

You can install on Ubuntu or raspbian with a simple sudo apt-get install docker. Docker Hub has a ton

Docker Hub has tons of pre built Docker images you can download (the official one is here) if you just want to download and start running.

Personally I like to build the images myself as I will then know everything that went into it. But for some services like gogs and zoneminder I use one posted to Docker Hub. I gave up trying to get them to work.

oh , I did not know that. I thought it wasposisble to have multiple sitemaps for the same house, just a limit view for different people, and I always assume that it also ment different security on that.
that is a bummer.