问题
Has anybody successfully run mosquitto on the openshift online version in a free account?
There are two SO questions relevant but not really solve all the questions:
- "How to connect to my MQTT Broker in Openshift". It runs JBoss Fuse and needs either the SNI feature on the server or the port-forwarding on the client.
- "How can I access socket through Openshift". The author mentions running mosquitto but there is no details as to how.
Stuffs that have been done so far:
- I have run a python websocket server thus to have verified openshift does support websocket nicely though the DIY cartridge.
- I have also compiled mosquitto and run it locally with a sample off github.
What is the exact steps to put mosquitto to run on the openshift from here?
回答1:
Finally I got it working on openshift, though with a simplest configuration. Here is the key steps:
- Use an openshift
DIY
cartridge to get the websocket support which exposes external port8000
and that is routed to internal port8080
on an virtual IP address specified byOPENSHIFT_DIY_IP
. - Compile mosquitto with all the following features turned off in
config.mk
just to avoid installing lot of packages to openshift:WITH_TLS
,WITH_TLS_PSK
,WITH_THREADING
,WITH_BRIDGE
,WITH_SRV
,WITH_UUID
,WITH_DOCS
,WITH_SOCKS
. - Modify the
Makefile
of mosquitto to comment out the command to install fromDOCSDIR
. This seems to be a minor bug in mosquitto because if theWITH_DOCS
is turned off it should not try to install fromDOCSDIR
. - Turn on
WITH_WEBSOCKETS
inconfig.mk
. - Modify the file
src/webockets.c
. In the functionmosq_websockets_init()
at about line 625, add a line ofinfo.iface = listener->host;
, below the line which isinfo.user = user;
. This is to make mosquitto able to bind to the virtual IP address in the openshift DIY gear. By default mosquitto only allow you to bind to0.0.0.0
for any IP address. This seems to be a disconnection between mosquitto and libwebosockets. - Install
libwebsockets
andlibwebsockets-devel
packages. - Run
make
to build mosquitto. - Lets assume you want to install mosquitto to
../mosrun
directory. Create the directory. Runmake prefix= DESTDIR=$(pwd)/../mosrun install
. - Delete all folders under
../mosrun
exceptsbin
.cd ../mosrun/sbin
andcp /usr/lib/libwebsockets.so.7 .
. Adjust the source path of your stock libwebsockets lib. - Grab all the files from
https://github.com/jpmens/simple-mqtt-websocket-example
. Put them into yourmosrun/sbin/simpleweb
directory. - Modify
mosrun/sbin/simpleweb/config.js
to use your openshift DIY domain name, and port 8000. This is where the javascript connects to your DIY server from a browser. - Create a mosquitto configuration file
mosrun/sbin/mosquitto.conf
which contains four lines:listener 8080 <OPENSHIFT_DIY_IP>
,protocol websockets
,http_dir simpleweb
, andconnection_messages
. Use your actual DIY virtual IP address in place of<OPENSHIFT_DIY_OP>
. - Copy the whole tree to your openshift DIY gear.
- Browse with a browser to your DIY domain from a browser so you'll see the default server page.
- Modify openshift
.openshift/action_hooks/start
and.../stop
scripts to comment out the existing commands. Push to openshift. Now you should not be able to connect to the default server but the gear is running. - Go into your mosquitto
mosrun/sbin
directory. - Run
LD_LIBRARY_PATH=. ./mosquitto -c mosquitto.conf -v
to start mosquitto. - Browse to your
diy-yourdomain.rhcloud.com:8000/index.html
. You shall see the mqtt websockets test page copied from jpmen's github repo.
The next step would be to move the tree into DIY git tree. Modify the start and stop hooks to start and stop your mosquitto instead of the default server. And a script running on the openshift gear to create the config.js
and mosquitto.conf
from the actual openshift environment variables.
Tow posts are very helpful to guide you through the steps of building mosquitto: the blog by jpman, and the blog by Jeremy Gooch.
来源:https://stackoverflow.com/questions/37383603/how-to-run-mosquitto-mqtt-broker-on-openshift-online