This is the multi-page printable view of this section. .
Task & Tutorial
- 1: Nginx: Expose Web Service
- 2: Docker: Container Support & Proxy
- 3: Use PostgreSQL as Grafana Backend
- 4: Use PostgreSQL as Ansible Inventory CMDB
- 5: Use PG as Prometheus Backend
- 6: Bind a L2 VIP to Node Cluster with Keepalived
- 7: Bind a L2 VIP to PostgreSQL Primary with VIP-Manager
1 - Nginx: Expose Web Service
Pigsty will install Nginx on INFRA Node, as a Web service proxy.
Nginx is the access entry for all WebUI services of Pigsty, and it defaults to the use the 80/443 port on INFRA nodes.
Pigsty provides a global parameter infra_portal to configure Nginx proxy rules and corresponding upstream services.
If you access Nginx directly through the ip:port, it will route to h.pigsty, which is the Pigsty homepage (/www/ directory, served as software repo).
Because Nginx provides multiple services through the same port, it must be distinguished by the domain name (HOST header by the browser). Therefore, by default, Nginx only exposes services with the domain parameter.
And Pigsty will expose grafana, prometheus, and alertmanager services by default in addition to the home server.
How to configure nginx upstream?
Pigsty has a built-in configuration template demo.yml, could be used as a reference, and also exposes some Web services in addition to the default services.
Each record in infra_portal is a key-value pair, where the key is the name of the service, and the value is a dictionary.
Currently, there are four available configuration items in the configuration dictionary:
endpoint: REQUIRED, specifies the address of the upstream service, which can beIP:PORTorDOMAIN:PORT.- In this parameter, you can use the placeholder
${admin_ip}, and Pigsty will fill in the value ofadmin_ip.
- In this parameter, you can use the placeholder
domain: OPTIONAL, specifies the domain name of the proxy. If not filled in, Nginx will not expose this service.- For services that need to know the endpoint address but do not want to expose them (such as Loki, Blackbox Exporter), you can leave the
domainblank.
- For services that need to know the endpoint address but do not want to expose them (such as Loki, Blackbox Exporter), you can leave the
scheme: OPTIONAL, specifies the protocol (http/https) when forwarding, leave it blank to default to http.- For services that require HTTPS access (such as the MinIO management interface), you need to specify
scheme: https.
- For services that require HTTPS access (such as the MinIO management interface), you need to specify
websocket: OPTIONAL, specifies whether to enable WebSocket, leave it blank to default to off.- Services that require WebSocket (such as Grafana, Jupyter) need to be set to
trueto work properly.
- Services that require WebSocket (such as Grafana, Jupyter) need to be set to
If you need to add a new Web service exposed by Nginx, you need to add the corresponding record in the infra_portal parameter in the pigsty.yml file, and then execute the playbook to take effect:
To avoid service interruption, you can also execute the following tasks separately:
Or the simple way:
Nginx related configuration parameters are located at: Parameters: INFRA - NGINX
How to access via domain names?
Nginx distinguishes between different services using the domain name in the HOST header set by the browser. Thus, by default, except for the software repository, you need to access services via domain name.
You can directly access these services via IP address + port, but we recommend accessing all components through Nginx on ports 80/443 using domain names.
When accessing the Pigsty WebUI via domain name, you need to configure DNS resolution or modify the local /etc/hosts file for static resolution. There are several typical methods:
- If your service needs to be publicly accessible, you should resolve the internet domain name via a DNS provider (Cloudflare, Aliyun DNS, etc.). Note that in this case, you usually need to modify the Pigsty
infra_portalparameter, as the default*.pigstyis not suitable for public use. - If your service needs to be shared within an office network, you should resolve the internal domain name via an internal DNS provider (company internal DNS server) and point it to the IP of the Nginx server. You can request the network administrator to add the appropriate resolution records in the internal DNS server, or ask the system users to manually configure static DNS resolution records.
- If your service is only for personal use or a few users (e.g., DBA), you can ask these users to use static domain name resolution records. On Linux/MacOS systems, modify the
/etc/hostsfile (requires sudo permissions) orC:\Windows\System32\drivers\etc\hosts(Windows) file.
We recommend ordinary single-machine users use the third method, adding the following resolution records on the machine used to access the web system via a browser:
The IP address here is the public IP address where the Pigsty service is installed, and then you can access Pigsty subsystems in the browser via a domain like: http://g.pigsty.
Other web services and custom domains can be added similarly. For example, the following are possible domain resolution records for the Pigsty sandbox demo:
How to access via HTTPS?
If nginx_sslmode is set to enabled or enforced, you can trust self-signed ca: files/pki/ca/ca.crt to use https in your browser.
Pigsty will generate self-signed certs for Nginx, if you wish to access via HTTPS without “Warning”, here are some options:
- Apply & add real certs from trusted CA: such as Let’s Encrypt
- Trust your generated CA crt as root ca in your OS and browser
- Type
thisisunsafein Chrome will supress the warning
You can access these web UI directly via IP + port. While the common best practice would be access them through Nginx and distinguish via domain names. You’ll need configure DNS records, or use the local static records (/etc/hosts) for that.
How to access Pigsty Web UI by domain name?
There are several options:
- Resolve internet domain names through a DNS service provider, suitable for systems accessible from the public internet.
- Configure internal network DNS server resolution records for internal domain name resolution.
- Modify the local machine’s
/etc/hostsfile to add static resolution records. (For Windows, it’s located at:)
We recommend the third method for common users. On the machine (which runs the browser), add the following record into /etc/hosts (sudo required) or C:\Windows\System32\drivers\etc\hosts in Windows:
You have to use the external IP address of the node here.
How to configure server side domain names?
The server-side domain name is configured with Nginx. If you want to replace the default domain name, simply enter the domain you wish to use in the parameter infra_portal. When you access the Grafana monitoring homepage via http://g.pigsty, it is actually accessed through the Nginx proxy to Grafana’s WebUI:
2 - Docker: Container Support & Proxy
Pigsty has a DOCKER module, which provides a set of playbooks to install and manage Docker on the target nodes.
This document will guide you through how to enable Docker support in Pigsty, and how to configure a proxy server for DockerHub.
Install Docker
To install docker on specified nodes, you can use the docker.yml playbook:
That’s it.
Proxy 101
Assuming you have a working HTTP(s) proxy server, if you wish to connect to docker hub or other registry sites via the proxy server:
You proxy service should provide you with something like
http://<ip|domain>:<port>|https://[user]:[pass]@<ip|domain>:<port>
For example, if you have a proxy server configuring like this:
You can check the proxy server by using the curl command, for example:
How to configure proxy for Docker Daemon?
If you wish to use a proxy server when Docker pulls images, you should specify the proxy_env parameter in the global variables of the pigsty.yml configuration file:
And when the Docker playbook is executed, these configurations will be rendered as proxy configurations in /etc/docker/daemon.json:
Please note that Docker Daemon does not use the
all_proxyparameter
If you wish to manually specify a proxy server, you can directly modify the proxies configuration in /etc/docker/daemon.json;
Or you can modify the service definition in /lib/systemd/system/docker.service (Debian/Ubuntu) and /usr/lib/systemd/system/docker.service to add environment variable declarations in the [Service] section
And restart dockerd service to take effect:
How to use other registry?
You can specify other registry sites in the docker_registry_mirrors parameter.
It may look like this:
You can also log in to other mirror sites, such as quay.io, by executing:
3 - Use PostgreSQL as Grafana Backend
Grafana uses SQLite for its metadata by default. Pigsty v2.7 can provision a PostgreSQL user and database for Grafana, after which Grafana can be pointed at that database manually.
The old grafana_database and grafana_pgurl Pigsty parameters were already
obsolete in v2.7.0. The release’s
grafana.ini.j2
does not manage a PostgreSQL backend URL, so re-running the grafana_config
task can overwrite a manual edit. Keep the change under your own configuration
management.
Provision the database
Add the user and database to a PGSQL cluster definition:
For an existing pg-meta cluster, create them with the v2.7 helper scripts:
For a new dedicated cluster, define the same objects in its inventory and run:
The helpers correspond to
pgsql-user.yml,
pgsql-db.yml, and
pgsql.yml.
Configure Grafana
Back up the existing Grafana metadata before changing backends. Then edit the
[database] section of /etc/grafana/grafana.ini:
Restart Grafana after the connection has been verified:
Grafana performs its own metadata migrations when it starts. Pointing it at a new empty database does not copy dashboards or data sources from the previous SQLite database.
Restore dashboards and data sources
Pigsty v2.7 keeps dashboard definitions and the loader in
files/grafana:
The loader reads GRAFANA_ENDPOINT, GRAFANA_USERNAME, and
GRAFANA_PASSWORD when non-default credentials are required.
Register the databases declared in pg_databases as Grafana PostgreSQL data
sources with the v2.7 task:
This calls
roles/pgsql/tasks/register_grafana.yml,
which uses the monitor user and skips a database only when its
register_datasource flag is false.
4 - Use PostgreSQL as Ansible Inventory CMDB
Pigsty v2.7 normally reads its Ansible inventory from pigsty.yml. The release
also ships three scripts that can load that inventory into the pigsty schema
of the meta database and switch Ansible to a generated dynamic inventory.
The implementation in this page is tied to the v2.7.0 objects
bin/inventory_load,
bin/inventory_cmdb,
and bin/inventory_conf.
Prerequisites
The CMDB schema is created from
files/cmdb.sql
when the default pg-meta cluster is provisioned. Complete infra.yml and the
pg-meta database initialization first, then verify that the meta service is
reachable.
By default, ansible.cfg
contains:
Load the YAML inventory
bin/inventory_load reads a YAML inventory and replaces the rows in
pigsty.group, pigsty.host, pigsty.group_var, pigsty.host_var, and
pigsty.global_var. Back up or review the target CMDB before loading a new
inventory.
The script accepts -p|--path for the YAML file and -d|--data for the
PostgreSQL connection string. Its defaults come from PIGSTY_HOME and
METADB_URL.
Switch inventory source
Switch Ansible to the CMDB-backed inventory.sh:
bin/inventory_cmdb writes inventory.sh, makes it executable, and changes
the inventory entry in ansible.cfg. The generated script queries
pigsty.inventory through the meta PostgreSQL service.
Switch back to the static pigsty.yml inventory with:
These scripts only change the inventory source. Continue to edit and validate the source configuration deliberately; loading a new YAML file is not an automatic two-way synchronization mechanism.
5 - Use PG as Prometheus Backend
It is not recommended to use PostgreSQL as a backend for Prometheus, but it is a good opportunity to understand the Pigsty deployment system.
Postgres Preparation
Create the database and user:
Check the connection string:
Promscale Configuration
Install promscale package:
If the package is not available in the default repository, you can download it directly:
Edit the promscale configuration file /etc/sysconfig/promscale.conf
Launch promscale service, it will create schema in prometheus database.
Prometheus Configuration
Prometheus can use Remote Write/ Remote Read to store metrics in Postgres via Promscale.
Edit the Prometheus configuration file:
Add the following configuration to the remote_write and remote_read sections:
Metrics are loaded into Postgres after Prometheus is restarted.
6 - Bind a L2 VIP to Node Cluster with Keepalived
You can bind an optional L2 VIP on a node cluster with vip_enabled.
7 - Bind a L2 VIP to PostgreSQL Primary with VIP-Manager
You can define an OPTIONAL L2 VIP on a PostgreSQL cluster, provided that all nodes in the cluster are in the same L2 network.
This VIP works on Master-Backup mode and always points to the node where the primary instance of the database cluster is located.
This VIP is managed by the VIP-Manager, which reads the Leader Key written by Patroni from DCS (etcd) to determine whether it is the master.
Enable VIP
Define pg_vip_enabled parameter as true in the cluster level:
Beware that pg_vip_address must be a valid IP address with subnet and available in the current L2 network.
Beware that pg_vip_interface must be a valid network interface name and should be the same as the one using IPv4 address in the inventory.
If the network interface name is different among cluster members, users should explicitly specify the pg_vip_interface parameter for each instance, for example:
To refresh the VIP configuration and restart the VIP-Manager, use the following command: