Skip to content

Instantly share code, notes, and snippets.

@paskal
Last active June 17, 2024 09:09
Show Gist options
  • Save paskal/48f10a0a584f4849be6b0889ede9262b to your computer and use it in GitHub Desktop.
Save paskal/48f10a0a584f4849be6b0889ede9262b to your computer and use it in GitHub Desktop.
How to set up the Percona Monitoring and Management (PMM) v2 with docker-compose

Set up PMM Server v2

Let's start by creating entry for server in docker-compose.yaml:

version: '2'
# version 2 of docker-compose is not "old" version, it's the actual version,
# see below for explanation:
# https://stackoverflow.com/a/53636006/961092
services:
    # Percona Monitoring and Management server
    pmm-data:
        image: percona/pmm-client:2
        container_name: pmm-data
        hostname: pmm-data
        volumes:
            - /srv
        entrypoint: /bin/true

    pmm-server:
        image: percona/pmm-server:2
        hostname: pmm-server
        container_name: pmm-server
        restart: always


        # logging settings limit used disk space
        logging:
            driver: json-file
            options:
                max-size: "10m"
                max-file: "5"

        ports:
            - "443:443"
        # uncomment expose section in order to proxy requests through another container instead of
        # accessing the container directly
        # expose:
        #     - "443"

        volumes_from:
            - pmm-data

After that, you could do docker compose up -d pmm-server and access it on https://127.0.0.1/graph/ with login credentials admin:admin

Change the password and create new user reporter with Admin rights to use it for client setup on the next step.

Set up PMM Client v2

Set up a connection to a server

No instructions for docker installation of client v2 are available at the moment, so deb install guide was used as a starting point for that guide.

First, create file pmm-agent.yaml with mode 0666 so container user will be able to write to it: touch pmm-agent.yaml && chmod 0666 pmm-agent.yaml

Then make sure your docker-compose.yaml has the following content:

version: '2'
services:
    # Percona Monitoring and Management client
    pmm-client:
        image: percona/pmm-client:2
        # select unique hostname, as it will be used for reporting
        hostname: pmm-client-my-cool-mysql-for-blog
        container_name: pmm-client
        # depends_on section is needed if you're monitoring DB that is running in the docker-compose
        depends_on:
            - mysql-server
        # pmm-agent.yaml will contain credentials and should not be added to git or shared with anyone
        volumes:
            - ./pmm-agent.yaml:/etc/pmm-agent.yaml

        # uncomment the ports section in case your pmm-server is on another host,
        # to see full list of ports you need to expose please see `docker-compose logs pmm-client`
        # after start of the container for lines like that:
        # > Sending status: RUNNING (port 42000). <...>
        # > Sending status: RUNNING (port 42001). <...>
        # ports:
        #     - "42000:42000"
        #     - "42001:42001"

        logging:
            driver: json-file
            options:
                max-size: "10m"
                max-file: "5"

        restart: always
        environment:
            - PMM_AGENT_CONFIG_FILE=/etc/pmm-agent.yaml

Before starting the container we need to generate a configuration for it by connecting to the server for the first time. For that temporary add entrypoint section with the following text in it, replacing pass with the password of the reporter user you created on the previous step.

        # important, if pmm-server is on different host you have to access it directly,
        # accessing it trough proxy like nginx wouldn't work
        entrypoint: pmm-agent setup --server-insecure-tls --server-address=pmm-server:443 --server-username=reporter --server-password='pass'

After that run docker-compose up pmm-client once, check that output doesn't contain errors, and then erase the entrypoint section you just added. Expected output:

Registering pmm-agent on PMM Server...
Registered.
Configuration file /etc/pmm-agent.yaml updated.
Please start pmm-agent: `pmm-agent --config-file=/etc/pmm-agent.yaml`.

Here the initial setup is done, docker-compose up -d pmm-client will start the client with the new settings and it should start reporting itself in the PMM Server interface. It won't send any DB data yet, for that to work you need to set up each DB you want to monitor from this machine (with that client) once.

Sidenote, in case you are running pmm-client on another host than pmm-server, after the configuration file is created to make it work via external IP run docker exec pmm-client /usr/local/percona/pmm2/bin/pmm-admin config external_ip container pmm-client-my-cool-mysql-for-blog with pmm-client name different than one used above to make server use IP you provide instead of an internal one, and remove the old instance from pmm-server GUI afterwards.

Set up connection to MySQL

First, we need to prepare the DB. Requirements section in the official documentation has the details, here is what you need to do:

The second step is creating a MySQL user. For that in the MySQL run following, replacing pass with randomly generated password (for example by running docker exec -it mysql-server mysql -u root -p):

-- @'%' and not @'localhost' because in docker client would be connecting not from the localhost
-- if you want, use '172.17.0.0/255.255.0.0' (172.17.0.0/16) to restrict logging to the docker network
-- if you don't use docker, just stick to 'localhost'
CREATE USER 'pmm'@'%' IDENTIFIED BY 'pass' WITH MAX_USER_CONNECTIONS 10;
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'pmm'@'%';
FLUSH PRIVILEGES;

The last step is to register the DB from the client on the server: execute docker exec -it pmm-client /bin/bash to get a shell in the client container, and then run the following command inside:

pmm-admin add mysql --username=pmm --password 'pass' --host mysql-server  --query-source=perfschema
# in case you're monitoring local DB and want to connect by socket, use that instead:
# pmm-admin add mysql --username=pmm --password 'pass' --socket=/var/path/to/mysql/socket --query-source=perfschema

Contributions

This work is distributed by CC-BY-4.0 license, feel free to alter it in a fork and ping me to update it with your changes.

@paskal
Copy link
Author

paskal commented Apr 1, 2021

Thanks a lot, @PaulJacobs-percona!

@spartan
Copy link

spartan commented Dec 1, 2021

With this setup after a docker-compose down followed by docker-compose up -d I cannot login into pmm-server anymore using the previous user/pass credentials.
I removed the entrypoint on pmm-data after the first docker-compose up -d but for some reason after restart everything is still lost.
There is no info online on how to setup pmm correctly using docker-compose. Yours was the closest I could get. Any thoughts on fixing this issue ?

Copy link

ghost commented Dec 2, 2021

Try asking on http://per.co.na/discord

@ahmad-punch
Copy link

ahmad-punch commented Aug 19, 2023

With this setup after a docker-compose down followed by docker-compose up -d I cannot login into pmm-server anymore using the previous user/pass credentials. I removed the entrypoint on pmm-data after the first docker-compose up -d but for some reason after restart everything is still lost. There is no info online on how to setup pmm correctly using docker-compose. Yours was the closest I could get. Any thoughts on fixing this issue ?

Any update on this?

This happens still. Here's what I've tried so far:

Docker Compose File

pmm-server: image: percona/pmm-server:latest container_name: pmm-server ports: - "80:80" - "443:443" # restart: always entrypoint: /path/in/container/entrypoint.sh volumes: - ./docker/pmm-server-entrypoint.sh:/path/in/container/entrypoint.sh environment: - PMM_USER=${PMM_SERVER_USERNAME} - PMM_PASSWORD=${PMM_SERVER_PASSWORD}

  **Entrypoint.sh file**
  
  `#!/bin/bash
  
  # Reset Grafana admin password
  grafana-cli --homepath /usr/share/grafana admin reset-admin-password ${PMM_PASSWORD}
  
  # Start the main process
  exec /opt/entrypoint.sh "$@"`

Now in the logs, it says that the password changed successfully however when I actually try to login on the PMM server, it says invalid password.

@paskal
Copy link
Author

paskal commented Aug 22, 2023

I think Percona Discord is still the right place to ask these questions, as nobody monitors this page. UPD: nvm, it's gone. Email, I guess?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment