Skip to content

Commit

Permalink
Initial pgexporter image
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed May 15, 2019
1 parent 6135dc0 commit 5c0ff3f
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 1 deletion.
55 changes: 55 additions & 0 deletions pgsql11-pgexporter-centos7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Jesper Pedersen <[email protected]>
#
# 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 <[email protected]>"

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"]
26 changes: 26 additions & 0 deletions pgsql11-pgexporter-centos7/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Jesper Pedersen <[email protected]>
#
# 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) .
37 changes: 37 additions & 0 deletions pgsql11-pgexporter-centos7/README.md
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 48 additions & 0 deletions pgsql11-pgexporter-centos7/root/usr/bin/run-pgexporter
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2019 Jesper Pedersen <[email protected]>
#
# 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
4 changes: 3 additions & 1 deletion pgsql11-primary-centos7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions pgsql11-primary-centos7/root/usr/bin/run-postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -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}" ] ||
Expand Down Expand Up @@ -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/
Expand All @@ -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
Expand Down

0 comments on commit 5c0ff3f

Please sign in to comment.