From b499209ce3281cc91d56afcae4015312e515ca4f Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Mon, 15 May 2023 18:52:31 +0300 Subject: [PATCH] - Move the Prometheus and grafana configuration in a dedicated directory and docker-compose file. - Add documentation about setting-up prometheus and grafana. --- docker-compose.yml | 21 ----------------- installAndRun.sh | 18 ++++++++------- prometheus/README.md | 23 +++++++++++++++++++ prometheus/docker-compose-prometheus.yml | 18 +++++++++++++++ .../resources => prometheus}/prometheus.yml | 0 shutdownController.sh | 10 ++++---- 6 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 prometheus/README.md create mode 100644 prometheus/docker-compose-prometheus.yml rename {src/main/resources => prometheus}/prometheus.yml (100%) diff --git a/docker-compose.yml b/docker-compose.yml index 92696a6..9670156 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,6 @@ version: '3.3' services: urls_controller: - profiles: ["onlyController", "all"] image: 'pdf_aggregation_service/urls_controller:latest' ports: - '1880:1880' @@ -26,23 +25,3 @@ services: build: dockerfile: ./Dockerfile context: . - - prometheus: - profiles: ["all"] - image: 'prom/prometheus:latest' - ports: - - '9090:9090' - command: '--config.file=/etc/prometheus/config.yml' - volumes: - - './src/main/resources/prometheus.yml:/etc/prometheus/config.yml' - depends_on: - - urls_controller - - grafana: - profiles: ["all"] - image: 'grafana/grafana:latest' - ports: - - '3000:3000' - depends_on: - - urls_controller - - prometheus diff --git a/installAndRun.sh b/installAndRun.sh index 9671184..133eda2 100755 --- a/installAndRun.sh +++ b/installAndRun.sh @@ -49,23 +49,25 @@ if [[ justInstall -eq 0 ]]; then if [[ shouldRunInDocker -eq 1 ]]; then - runPrometheusAndGrafanaContainers=0 + runPrometheusAndGrafanaContainers=0 # This is set here, not through cmd-args. echo -e "\nBuilding the docker image and running the containers..\n" sudo docker --version || handle_error "Docker was not found!" 3 (sudo mkdir -p "$HOME"/tmp/config && sudo cp ./src/main/resources/application.yml "$HOME"/tmp/config) || true # This also replaces an existing "application.yml". sudo mkdir -p "$HOME"/logs || true - if [[ runPrometheusAndGrafanaContainers -eq 1 ]]; then - (sudo docker compose --profile all up --build -d && echo -e "\nThe Urls_Controller, Prometheus and Grafana docker-containers started running.\nWaiting 65 seconds before getting their status..\n") || handle_error "Could not list docker containers!" 4 - else - (sudo docker compose --profile onlyController up --build -d && echo -e "\nThe Urls_Controller docker-container started running.\nWaiting 65 seconds before getting its status..\n") || handle_error "Could not list docker containers!" 4 - fi # Run in "detached mode" -d (in the background). + (sudo docker compose up --build -d && echo -e "\nThe Urls_Controller docker-container started running.\n") || handle_error "Could not build and/or run the 'urls_controller' docker container!" 4 + + if [[ runPrometheusAndGrafanaContainers -eq 1 ]]; then + (sudo docker compose -f prometheus/docker-compose-prometheus.yml up --build -d && echo -e "\nThe Prometheus and Grafana docker-containers started running.\n") || handle_error "Could not build and/or run the 'prometheus' and/or 'grafana' docker containers!" 5 + fi + + echo -e "Waiting 65 seconds before getting the status..\n" sleep 65 - sudo docker ps -a || handle_error "Could not get the status of docker-containers!" 5 # Using -a to get the status of failed containers as well. + sudo docker ps -a || handle_error "Could not get the status of docker-containers!" 6 # Using -a to get the status of failed containers as well. echo -e "\n\nGetting the logs of docker-container \"urlscontroller-urls_controller-1\":\n" - sudo docker logs "$(sudo docker ps -aqf "name=^urlscontroller-urls_controller-1$")" || handle_error "Could not get the logs of docker-container \"urlscontroller-urls_controller-1\"!" 6 # Using "regex anchors" to avoid false-positives. Works even if the container is not running, thus showing the error-log. + sudo docker logs "$(sudo docker ps -aqf "name=^urlscontroller-urls_controller-1$")" || handle_error "Could not get the logs of docker-container \"urlscontroller-urls_controller-1\"!" 7 # Using "regex anchors" to avoid false-positives. Works even if the container is not running, thus showing the error-log. fi else export PATH=/opt/gradle/gradle-${gradleVersion}/bin:$PATH # Make sure the gradle is still accessible (it usually isn't without the "export"). diff --git a/prometheus/README.md b/prometheus/README.md new file mode 100644 index 0000000..d40a6ba --- /dev/null +++ b/prometheus/README.md @@ -0,0 +1,23 @@ +# Visualize Prometheus metrics + +### Requirements +- Run the docker-compose of the Controller. +- Run the docker-compose of prometheus. + + +### Check the metrics +- To check the raw metrics hit this url in your browser: http://:1880/api/actuator/prometheus +- To check the metrics though GUI, with some graphs, hit: http://:9090 + + +### Visualize metrics in Grafana +- Access grafana in: http://:3000/ +- Give the default username and password: "admin" (for both). +- Specify the new password. +- Then, add a "Prometheus" datasource. +- Specify the prometheus url: http://:9090 +- Save the datasource. +- Go to "dashboards", click "import", add the number "12900" in the input and click "load" and "next". +- Then select the "prometheus" datasource and click "import". +- Enjoy the graphs. + diff --git a/prometheus/docker-compose-prometheus.yml b/prometheus/docker-compose-prometheus.yml new file mode 100644 index 0000000..efb5fde --- /dev/null +++ b/prometheus/docker-compose-prometheus.yml @@ -0,0 +1,18 @@ +version: '3.3' + +services: + + prometheus: + image: 'prom/prometheus:latest' + ports: + - '9090:9090' + command: '--config.file=/etc/prometheus/config.yml' + volumes: + - './prometheus.yml:/etc/prometheus/config.yml' + + grafana: + image: 'grafana/grafana:latest' + ports: + - '3000:3000' + depends_on: + - prometheus diff --git a/src/main/resources/prometheus.yml b/prometheus/prometheus.yml similarity index 100% rename from src/main/resources/prometheus.yml rename to prometheus/prometheus.yml diff --git a/shutdownController.sh b/shutdownController.sh index ffaea71..9d71f8e 100755 --- a/shutdownController.sh +++ b/shutdownController.sh @@ -1,8 +1,8 @@ echo "Running compose down.." -sudo docker compose down +sudo docker compose -f ./prometheus/docker-compose-prometheus.yml down +sudo docker compose -f docker-compose.yml -f ./prometheus/docker-compose-prometheus.yml down -echo "Stopping the running container.." -sudo docker stop "$(sudo docker ps -aqf "name=^urlscontroller-urls_controller-1$")" -echo "Removing the container.." -sudo docker rm "$(sudo docker ps -aqf "name=^urlscontroller-urls_controller-1$")" +# In case we need to hard-remove the containers, use the following commands: +#sudo docker stop $(sudo docker ps -aqf "name=^(?:urlscontroller-urls_controller|prometheus-(?:prometheus|grafana))-1$") || true # There may be no active containers +#sudo docker rm $(sudo docker ps -aqf "name=^(?:urlscontroller-urls_controller|prometheus-(?:prometheus|grafana))-1$") || true # All containers may be already removed.