pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Link Keymetrics inside Docker container with pm2

Open cantado opened this issue 10 years ago • 5 comments

Hi,

i know it's not an issue but i need some info about pm2 and keymetrics. I have a server with multiple applications in docker containers. I use pm2 to start the applications and link them to monitor everything. I want to use the same machine name/server name for the applications with different application names. The applications does not group to one server in keymetrics. Keymetrics does not add the application to the server section. Instead it overwrite the section the whole time.

Is there a way to monitor all application in different docker containers without register all applications with a new machine name? (I already upgrade the keymetrics bucket. But for more application i need a bigger bucket)

My Dockerfile looks like this:

FROM mhart/alpine-node:5.7.1

RUN npm install --production --loglevel info
RUN npm install -g pm2@latest
RUN pm2 update

EXPOSE 4000

CMD [ "npm", "start" ]

The npm start execute a start script which looks like this

#!/bin/sh

cleanup() {
    pm2 link delete $KEYMETRICS_SECRET_KEY $KEYMETRICS_PUBLIC_KEY $KEYMETRICS_SERVER_NAME
    pm2 kill
}

trap cleanup EXIT

# get pm2 link info and save return code (should be 1)
pm2 link info $KEYMETRICS_SECRET_KEY $KEYMETRICS_PUBLIC_KEY $KEYMETRICS_SERVER_NAME
rc=$?

# if return code is not 0 (not connected) link the pm2. otherwise restart the link
if [[ $rc != 0 ]]; then
    pm2 link $KEYMETRICS_SECRET_KEY $KEYMETRICS_PUBLIC_KEY $KEYMETRICS_SERVER_NAME
else
    pm2 link restart $KEYMETRICS_SECRET_KEY $KEYMETRICS_PUBLIC_KEY $KEYMETRICS_SERVER_NAME
fi

# start pm2 and tail logs
pm2 start base.yml --only $KEYMETRICS_APP_NAME
pm2 logs

cantado avatar Jun 29 '16 13:06 cantado

For a side note, doing pm2 link <secret> <public> <name> has the same effect than doing pm2 link restart <secret> <public> <name>

About multiple Docker on the same host, I wonder if it's possible to make the socket file ~/.pm2/rpc.sock or the entire ~/.pm2 folder, available to the docker host. So basically being able to do a pm2 list on the root machine that list processes inside the docker instance.

Unitech avatar Jun 29 '16 14:06 Unitech

I add the ~/.pm2 direcotry to the volume parameter of my docker command. If i start the application with pm2 start app.js it will be listed in the docker container and in the root machine's list. But i get this error immediately:

app-0 (err): Error: ENOENT: no such file or directory, uv_chdir
app-0 (err):     at Error (native)
app-0 (err):     at /root/.nvm/versions/node/v6.2.2/lib/node_modules/pm2/lib/ProcessContainer.js:215:13
app-0 (err):     at /root/.nvm/versions/node/v6.2.2/lib/node_modules/pm2/node_modules/async/lib/async.js:52:16
app-0 (err):     at /root/.nvm/versions/node/v6.2.2/lib/node_modules/pm2/node_modules/async/lib/async.js:1209:30
app-0 (err):     at WriteStream.<anonymous> (/root/.nvm/versions/node/v6.2.2/lib/node_modules/pm2/lib/Utility.js:131:13)
app-0 (err):     at emitOne (events.js:96:13)
app-0 (err):     at WriteStream.emit (events.js:188:7)
app-0 (err):     at WriteStream.<anonymous> (fs.js:1884:10)
app-0 (err):     at FSReqWrap.oncomplete (fs.js:117:15)

I think the pm2 on the root machine tries to change the permissions of a path in the container?!

cantado avatar Jun 30 '16 12:06 cantado

trying to do the same here; monitor multiple docker containers within a single host with a single keymetric bucket. Any update?

malsec-dev avatar Jul 20 '16 21:07 malsec-dev

I'd also like to know how this is possible. We have a large application broken down into 10-15 different microservices, each one gets deployed to it's own docker container and run from a single physical EC2 instance via Docker Cloud. At the moment, the only way I can see to integration keymetrics is for each microservice to register itself as a new host with keymetrics. With the current pricing model that is obviously out of the question.

How can I hook up multiple microservices to a single keymetrics account?

mrfelton avatar Sep 30 '16 06:09 mrfelton

You can share a folder on all container and the host and you set PM2_HOME to this folder for all container but its painfull. You can use our image here and configure it using env variables to automaticly link it to keymetrics. The last thing is that our pricing isn't for microservice since you need to pay for each host (so container unless you use the technique i said above), we plan to adapt it to every use case soon.

vmarchaud avatar Jan 12 '17 17:01 vmarchaud