password
. Below is a sample YAML configuration:apiVersion: v1kind: Secretmetadata:name: postgres-testtype: OpaquestringData:username: postgrespassword: you-guess # Corresponding PostgreSQL password
apiVersion: apps/v1kind: Deploymentmetadata:name: postgres-testnamespace: postgres-testlabels:app: postgresapp.kubernetes.io/name: postgresqlspec:replicas: 1selector:matchLabels:app: postgresapp.kubernetes.io/name: postgresqltemplate:metadata:labels:app: postgresapp.kubernetes.io/name: postgresqlspec:containers:- name: postgres-exporterimage: wrouesnel/postgres_exporter:latestargs:- "--web.listen-address=:9187"- "--log.level=debug"env:- name: DATA_SOURCE_USERvalueFrom:secretKeyRef:name: postgres-testkey: username- name: DATA_SOURCE_PASSvalueFrom:secretKeyRef:name: postgres-testkey: password- name: DATA_SOURCE_URIvalue: "x.x.x.x:5432/postgres?sslmode=disable"ports:- name: http-metricscontainerPort: 9187
Secret
are passed in to the environment variables DATA_SOURCE_USER
and DATA_SOURCE_PASS
, so the username and password cannot be viewed in plaintext. You can also use DATA_SOURCE_USER_FILE
/DATA_SOURCE_PASS_FILE
to read the username and password from the file, or use DATA_SOURCE_NAME
to put them in the connection string, such as postgresql://login:password@hostname:port/dbname
.query
part (after ?
) in the DATA_SOURCE_URI
/DATA_SOURCE_NAME
connection string supports the following parameters (the latest supported parameters listed in Connection String Parameters shall prevail):Parameter | Description |
sslmode | Whether to use SSL. Valid values: |
- disable | Do not use SSL |
- require | Always use (skip verification) |
- verify-ca | Always use (check whether the certificate provided by the server is issued by a trusted CA) |
- verify-full | Always use (check whether the certificate provided by the server is issued by a trusted CA and whether the hostname matches the certificate) |
fallback_application_name | Alternative application_name |
connect_timeout | Maximum connection wait time in seconds. `0` indicates to wait infinitely |
sslcert | Certificate file path. The file data must be in PEM format |
sslkey | Private key file path. The file data must be in PEM format |
sslrootcert | Root certificate file path. The file data must be in PEM format |
Parameter | Description | Environment Variable |
--web.listen-address | Listening address. Default value: :9487 | PG_EXPORTER_WEB_LISTEN_ADDRESS |
--web.telemetry-path | Path under which to expose metrics. Default value: /metrics | PG_EXPORTER_WEB_TELEMETRY_PATH |
--extend.query-path | Path of a YAML file containing custom queries to run. For more information, please see queries.yaml | PG_EXPORTER_EXTEND_QUERY_PATH |
--disable-default-metrics | Uses only metrics supplied from queries.yaml | PG_EXPORTER_DISABLE_DEFAULT_METRICS |
--disable-settings-metrics | Skips scraping pg_settings metrics | PG_EXPORTER_DISABLE_SETTINGS_METRICS |
--auto-discover-databases | Whether to discover the databases in the PostgreSQL instance dynamically | PG_EXPORTER_AUTO_DISCOVER_DATABASES |
--dumpmaps | Prints the internal metric information to help troubleshoot custom queries (do not use it unless for debugging) | - |
--constantLabels | Custom label provided in the format of key=value. Multiple labels are separated with , | PG_EXPORTER_CONSTANT_LABELS |
--exclude-databases | Database to be excluded. It takes effect only if --auto-discover-databases is enabled | PG_EXPORTER_EXCLUDE_DATABASES |
--log.level | Log level. Valid values: debug, info, warn, error, fatal | PG_EXPORTER_LOG_LEVEL |
curl http://exporter:9187/metrics
. You can define a queries.yaml
file to get this metric:queries.yaml
.--extend.query-path
to aggregate the information of the aforementioned Secret and Deployment. The YAML file after aggregation is as shown below:# Note: the following document sample code creates a namespace named `postgres-test`, which is for reference onlyapiVersion: v1kind: Namespacemetadata:name: postgres-test# The following document sample code creates a Secret containing a username and password---apiVersion: v1kind: Secretmetadata:name: postgres-test-secretnamespace: postgres-testtype: OpaquestringData:username: postgrespassword: you-guess# The following document sample code creates a `queries.yaml` file containing custom metrics---apiVersion: v1kind: ConfigMapmetadata:name: postgres-test-configmapnamespace: postgres-testdata:queries.yaml: |pg_postmaster:query: "SELECT pg_postmaster_start_time as start_time_seconds from pg_postmaster_start_time()"master: truemetrics:- start_time_seconds:usage: "GAUGE"description: "Time at which postmaster started"# The following document sample code mounts the Secret and ConfigMap and defines exporter deployment-related parameters such as image---apiVersion: apps/v1kind: Deploymentmetadata:name: postgres-testnamespace: postgres-testlabels:app: postgresapp.kubernetes.io/name: postgresqlspec:replicas: 1selector:matchLabels:app: postgresapp.kubernetes.io/name: postgresqltemplate:metadata:labels:app: postgresapp.kubernetes.io/name: postgresqlspec:containers:- name: postgres-exporterimage: wrouesnel/postgres_exporter:latestargs:- "--web.listen-address=:9187"- "--extend.query-path=/etc/config/queries.yaml"- "--log.level=debug"env:- name: DATA_SOURCE_USERvalueFrom:secretKeyRef:name: postgres-test-secretkey: username- name: DATA_SOURCE_PASSvalueFrom:secretKeyRef:name: postgres-test-secretkey: password- name: DATA_SOURCE_URIvalue: "x.x.x.x:5432/postgres?sslmode=disable"ports:- name: http-metricscontainerPort: 9187volumeMounts:- name: config-volumemountPath: /etc/configvolumes:- name: config-volumeconfigMap:name: postgres-test-configmap
curl http://exporter:9187/metrics
, and you can use the custom queries.yaml
to query the PostgreSQL instance start time as follows:# HELP pg_postmaster_start_time_seconds Time at which postmaster started# TYPE pg_postmaster_start_time_seconds gaugepg_postmaster_start_time_seconds{server="x.x.x.x:5432"} 1.605061592e+09
Pod Monitor
to define a Prometheus scrape task. Below is a sample YAML configuration:apiVersion: monitoring.coreos.com/v1kind: PodMonitormetadata:name: postgres-exporternamespace: cm-prometheusspec:namespaceSelector:matchNames:- postgres-testpodMetricsEndpoints:- interval: 30spath: /metricsport: http-metrics # Port name of the aforementioned exporter containerrelabelings:- action: labeldropregex: __meta_kubernetes_pod_label_(pod_|statefulset_|deployment_|controller_)(.+)- action: replaceregex: (.*)replacement: postgres-xxxxxxsourceLabels:- instancetargetLabel: instanceselector:matchLabels:app: postgres
Was this page helpful?