From 5c0ff3ff3d5caa3fc4d4c799628fa5e97aa6189d Mon Sep 17 00:00:00 2001 From: jesperpedersen Date: Wed, 15 May 2019 10:07:50 -0400 Subject: [PATCH] Initial pgexporter image --- pgsql11-pgexporter-centos7/Dockerfile | 55 +++++++++++++++++++ pgsql11-pgexporter-centos7/Makefile | 26 +++++++++ pgsql11-pgexporter-centos7/README.md | 37 +++++++++++++ .../root/usr/bin/run-pgexporter | 48 ++++++++++++++++ pgsql11-primary-centos7/README.md | 4 +- .../root/usr/bin/run-postgresql | 21 +++++++ 6 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 pgsql11-pgexporter-centos7/Dockerfile create mode 100644 pgsql11-pgexporter-centos7/Makefile create mode 100644 pgsql11-pgexporter-centos7/README.md create mode 100755 pgsql11-pgexporter-centos7/root/usr/bin/run-pgexporter diff --git a/pgsql11-pgexporter-centos7/Dockerfile b/pgsql11-pgexporter-centos7/Dockerfile new file mode 100644 index 0000000..aaf92a9 --- /dev/null +++ b/pgsql11-pgexporter-centos7/Dockerfile @@ -0,0 +1,55 @@ +# The MIT License (MIT) +# +# Copyright (c) 2019 Jesper Pedersen +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the Software +# is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# +# pgsql11-pgexporter-centos7 +FROM centos:centos7 + +LABEL maintainer="Jesper Pedersen " + +LABEL summary="PostgreSQL 11 postgres_exporter" \ + description="PostgreSQL 11 postgres_exporter" + +# COPY conf/licenses /licenses + +ENV PGVERSION="11" +ENV PGROOT="/usr/pgsql-${PGVERSION}" + +RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \ + && rpm -Uvh https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm \ + && yum -y install wget tar \ + && yum -y update \ + && yum -y clean all + +COPY root/ / + +RUN mkdir -p /usr/local/bin +RUN cd /tmp \ + && wget https://github.com/wrouesnel/postgres_exporter/releases/download/v0.4.7/postgres_exporter_v0.4.7_linux-amd64.tar.gz \ + && tar xzf postgres_exporter_v0.4.7_linux-amd64.tar.gz \ + && mv postgres_exporter_v0.4.7_linux-amd64/postgres_exporter /usr/local/bin/postgres_exporter \ + && rm -Rf postgres_exporter_v0.4.7_linux-amd64 postgres_exporter_v0.4.7_linux-amd64.tar.gz \ + && chown daemon:daemon /usr/local/bin/postgres_exporter + +EXPOSE 9187 + +USER 2 + +CMD ["/usr/bin/run-pgexporter"] diff --git a/pgsql11-pgexporter-centos7/Makefile b/pgsql11-pgexporter-centos7/Makefile new file mode 100644 index 0000000..ff8174c --- /dev/null +++ b/pgsql11-pgexporter-centos7/Makefile @@ -0,0 +1,26 @@ +# The MIT License (MIT) +# +# Copyright (c) 2019 Jesper Pedersen +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the Software +# is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# +IMAGE_NAME = pgsql11-pgexporter-centos7 + +.PHONY: build +build: + podman build --no-cache -t $(IMAGE_NAME) . diff --git a/pgsql11-pgexporter-centos7/README.md b/pgsql11-pgexporter-centos7/README.md new file mode 100644 index 0000000..dcb94bf --- /dev/null +++ b/pgsql11-pgexporter-centos7/README.md @@ -0,0 +1,37 @@ +# PostgreSQL postgres_exporter + +This project contains the PostgreSQL postgres_exporter image. + +## Getting Started + +```bash +# Create the image +make build + +# Run, and register the container under postgresql-pgexporter +podman run -p 9187:9187 --name postgresql-pgexporter -d -e PG_SERVER_NAME=192.168.1.2 -e PG_SERVER_PORT=5432 -e PG_MONITOR_NAME=monuser -e PG_MONITOR_PASSWORD=monpass pgsql11-pgexporter-centos7 + +# Shell to postgresql- +podman exec -it postgresql-pgexporter /usr/bin/bash + +# Stop the container +podman stop postgresql-pgexporter + +# Start the container +podman start postgresql-pgexporter + +# Remove the container +podman rm postgresql-pgexporter +``` + +## Configuration + +| Property | Default | Unit | Required | Description | +|----------|---------|------|----------|-------------| +| PG_SERVER_NAME | | | Yes | The IP of the PostgreSQL server | +| PG_SERVER_PORT | | | Yes | The port of the PostgreSQL server | +| PG_MONITOR_NAME | | | Yes | The name of the monitor user | +| PG_MONITOR_PASSWORD | | | Yes | The password of the monitor user | + + +Note, that `PG_PASSWORD_ENCRYPTION` needs to be set to `md5` for now. diff --git a/pgsql11-pgexporter-centos7/root/usr/bin/run-pgexporter b/pgsql11-pgexporter-centos7/root/usr/bin/run-pgexporter new file mode 100755 index 0000000..b8b6e73 --- /dev/null +++ b/pgsql11-pgexporter-centos7/root/usr/bin/run-pgexporter @@ -0,0 +1,48 @@ +#!/bin/bash +# The MIT License (MIT) +# +# Copyright (c) 2019 Jesper Pedersen +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the Software +# is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +POSTGRES_EXPORTER_PIDFILE=/tmp/postgres_exporter.pid + +function trap_sigterm() { + kill -SIGINT $(head -1 $POSTGRES_EXPORTER_PIDFILE) +} + +trap 'trap_sigterm' SIGINT SIGTERM + +export PG_SERVER_NAME=${PG_SERVER_NAME} +export PG_SERVER_PORT=${PG_SERVER_PORT} +export PG_MONITOR_NAME=${PG_MONITOR_NAME} +export PG_MONITOR_PASSWORD=${PG_MONITOR_PASSWORD} + +if [ -z "${PG_SERVER_NAME}" ] || [ -z "${PG_SERVER_PORT}" ] || + [ -z "${PG_MONITOR_NAME}" ] || [ -z "${PG_MONITOR_PASSWORD}" ]; then + echo "PG_SERVER_NAME, PG_SERVER_PORT, PG_MONITOR_NAME and PG_MONITOR_PASSWORD needs to be defined." + exit 1 +fi + +export DATA_SOURCE_NAME="postgresql://$PG_MONITOR_NAME:$PG_MONITOR_PASSWORD@$PG_SERVER_NAME:$PG_SERVER_PORT/postgres?sslmode=disable" + +/usr/local/bin/postgres_exporter >> /tmp/postgres_exporter.log 2>&1 & +echo $! > $POSTGRES_EXPORTER_PIDFILE + +wait diff --git a/pgsql11-primary-centos7/README.md b/pgsql11-primary-centos7/README.md index 6900747..459439f 100644 --- a/pgsql11-primary-centos7/README.md +++ b/pgsql11-primary-centos7/README.md @@ -9,7 +9,7 @@ This project contains the PostgreSQL Primary image. make build # Run, and register the container under postgresql-primary -podman run -p 5432:5432 -p 9100:9100 --name postgresql-primary -d -e PG_DATABASE=mydb -e PG_USER_NAME=myuser -e PG_USER_PASSWORD=mypass -e PG_REPLICATION_NAME=repl -e PG_REPLICATION_PASSWORD=replpass -e PG_NETWORK_MASK=all pgsql11-primary-centos7 +podman run -p 5432:5432 -p 9100:9100 --name postgresql-primary -d -e PG_DATABASE=mydb -e PG_USER_NAME=myuser -e PG_USER_PASSWORD=mypass -e PG_REPLICATION_NAME=repl -e PG_REPLICATION_PASSWORD=replpass -e PG_NETWORK_MASK=all -e PG_MONITOR_NAME=monuser -e PG_MONITOR_PASSWORD=monpass pgsql11-primary-centos7 # psql to postgresql-primary psql -h localhost -p 5432 -U myuser mydb @@ -53,6 +53,8 @@ podman rm postgresql-primary | PG_REPLICA3 | replica3 | | | Replication slot name 3 | | PG_REPLICA4 | replica4 | | | Replication slot name 4 | | PG_REPLICA5 | replica5 | | | Replication slot name 5 | +| PG_MONITOR_NAME | | | | The name of the monitor user | +| PG_MONITOR_PASSWORD | | | | The password of the monitor user | ## Volumes diff --git a/pgsql11-primary-centos7/root/usr/bin/run-postgresql b/pgsql11-primary-centos7/root/usr/bin/run-postgresql index 702d278..24139da 100755 --- a/pgsql11-primary-centos7/root/usr/bin/run-postgresql +++ b/pgsql11-primary-centos7/root/usr/bin/run-postgresql @@ -52,6 +52,9 @@ if [ ! -f /pgdata/PG_VERSION ]; then export PG_REPLICA4=${PG_REPLICA4:-replica4} export PG_REPLICA5=${PG_REPLICA5:-replica5} + export PG_MONITOR_NAME=${PG_MONITOR_NAME} + export PG_MONITOR_PASSWORD=${PG_MONITOR_PASSWORD} + if [ -z "${PG_DATABASE}" ] || [ -z "${PG_USER_NAME}" ] || [ -z "${PG_USER_PASSWORD}" ] || [ -z "${PG_REPLICATION_NAME}" ] || [ -z "${PG_REPLICATION_PASSWORD}" ] || @@ -80,11 +83,19 @@ if [ ! -f /pgdata/PG_VERSION ]; then sed -i "s/ssl = off/ssl = on/g" /conf/postgresql.conf sed -i "s/SSL/ssl/g" /conf/pg_hba.conf + if [ -n "${PG_MONITOR_NAME}" ] && [ -n "${PG_MONITOR_PASSWORD}" ]; then + echo "hostssl all $PG_MONITOR_NAME $PG_NETWORK_MASK $PG_PASSWORD_ENCRYPTION" >> /conf/pg_hba.conf + fi + cp /pgconf/root.crt /pgdata cp /pgconf/server.crt /pgdata cp /pgconf/server.key /pgdata else sed -i "s/SSL//g" /conf/pg_hba.conf + + if [ -n "${PG_MONITOR_NAME}" ] && [ -n "${PG_MONITOR_PASSWORD}" ]; then + echo "host all $PG_MONITOR_NAME $PG_NETWORK_MASK $PG_PASSWORD_ENCRYPTION" >> /conf/pg_hba.conf + fi fi cp /conf/postgresql.conf /pgdata/ @@ -101,8 +112,18 @@ if [ ! -f /pgdata/PG_VERSION ]; then sed -i "s/PG_REPLICATION_NAME/$PG_REPLICATION_NAME/g" /conf/setup.sql sed -i "s/PG_REPLICATION_PASSWORD/$PG_REPLICATION_PASSWORD/g" /conf/setup.sql + if [ -n "${PG_MONITOR_NAME}" ] && [ -n "${PG_MONITOR_PASSWORD}" ]; then + sed -i "s/PG_MONITOR_NAME/$PG_MONITOR_NAME/g" /conf/setup_monitor.sql + sed -i "s/PG_MONITOR_PASSWORD/$PG_MONITOR_PASSWORD/g" /conf/setup_monitor.sql + fi + /usr/pgsql-11/bin/pg_ctl -D /pgdata/ start /usr/pgsql-11/bin/psql -q -f /conf/setup.sql postgres + + if [ -n "${PG_MONITOR_NAME}" ] && [ -n "${PG_MONITOR_PASSWORD}" ]; then + /usr/pgsql-11/bin/psql -q -f /conf/setup_monitor.sql postgres + fi + /usr/pgsql-11/bin/psql -q -c "SELECT pg_create_physical_replication_slot('$PG_REPLICA1');" postgres /usr/pgsql-11/bin/psql -q -c "SELECT pg_create_physical_replication_slot('$PG_REPLICA2');" postgres /usr/pgsql-11/bin/psql -q -c "SELECT pg_create_physical_replication_slot('$PG_REPLICA3');" postgres