Define etcd cluster at different size
This is the multi-page printable view of this section. .
ETCD
ETCD is a distributed, reliable key-value store for the most critical data of a distributed system. etcd is used as DCS (Distributed configuration storage) for patroni, the PostgreSQL HA Agent for config management and leader election.
In short, PGSQL depends on a global ETCD module, and ETCD depends on NODE module to work properly (use the node CA).
Customize etcd components with 10 parameters
Adding / Removing etcd members, refresh endpoints...
Ansible playbooks that can be used in etcd module
Dashboards, metrics, record & alerting rules.
Frequently Asked Questions about etcd module
1 - Configure
You have to define the etcd cluster in the config inventory before deploying it.
Usually you can choose an etcd cluster with:
- One Node, no high availability, just the functionality of etcd, suitable for dev, test & demo purpose.
- Three Nodes, basic high availability, tolerate one node failure, suitable for medium production env.
- Five Nodes, better high availability, tolerate two-node failure, suitable for large production env.
Use even number of etcd nodes is meaningless, and more than five nodes is not common.
One Node
Define the group etcd in the inventory, It will create a singleton etcd instance.
This line exists almost in all single-node config template, where the placeholder IP address 10.10.10.10 will be replaced with the current admin node IP.
The only necessary parameters are etcd_seq and etcd_cluster, which uniquely identify the cluster and each instance.
Three Nodes
Three-node etcd cluster is quite common, which tolerates one-node failure, suitable for most cases.
The trio and safe config templates use a three-node etcd cluster, as shown below:
Five Nodes
Five nodes etcd cluster can tolerate two node failure, suitable for large prod env.
There’s a five-node etcd cluster example in the prod template:
You can use even more nodes, but 3 or 5 nodes are recommended.
Use odd number for cluster size, like 1, 3, 5, 7, …
Etcd Usage
These are the services that currently use Etcd:
- patroni: Use etcd as a consensus backend for PostgreSQL HA
- vip-manager: Read leader info from Etcd to bind an optional L2 VIP on the PostgreSQL cluster
You’ll have to reload etcd config after any permanent change to the etcd cluster members.
e.g, update patroni reference to etcd endpoints:
e.g., update vip-manager reference to etcd endpoints (if you are using PGSQL L2 VIP):
2 - Parameter
There are 12 parameters about the ETCD module.
ETCD: 9 Parameters:
| Parameter | Type | Level | Comment |
|---|---|---|---|
etcd_seq |
int | I | etcd instance identifier, REQUIRED |
etcd_cluster |
string | C | etcd cluster & group name, etcd by default |
etcd_learner |
bool | I | prevent purging running etcd instance? |
etcd_data |
path | C | etcd data directory, /data/etcd by default |
etcd_port |
port | C | etcd client port, 2379 by default |
etcd_peer_port |
port | C | etcd peer port, 2380 by default |
etcd_init |
enum | C | etcd initial cluster state, new or existing |
etcd_election_timeout |
int | C | etcd election timeout, 1000ms by default |
etcd_heartbeat_interval |
int | C | etcd heartbeat interval, 100ms by default |
ETCD_REMOVE: 3 Parameters:
| Parameter | Type | Level | Comment |
|---|---|---|---|
etcd_safeguard |
bool | G/C/A | prevent purging running etcd instance? |
etcd_rm_data |
bool | G/C/A | remove etcd data during removal? (default: true) |
etcd_rm_pkg |
bool | G/C/A | uninstall etcd packages during removal? (default: false) |
Defaults
The default parameters are defined in roles/etcd/defaults/main.yml
Additional removal parameters are defined in roles/etcd_remove/defaults/main.yml
ETCD_REMOVE Parameters
etcd_seq
name: etcd_seq, type: int, level: I
etcd instance identifier, REQUIRED
no default value, you have to specify it explicitly. Here is a 3-node etcd cluster example:
etcd_cluster
name: etcd_cluster, type: string, level: C
etcd cluster & group name, etcd by default
default values: etcd, which is a fixed group name, can be useful when you want to use deployed some extra etcd clusters
etcd_learner
name: etcd_learner, type: bool, level: I
Init etcd instance as learner? default value is false
When set to true, the etcd instance will be init as leaner, so it will not be able to vote in the etcd cluster.
You can promote it to a full member later with etcdctl member promote command.
etcd_data
name: etcd_data, type: path, level: C
etcd data directory, /data/etcd by default
etcd_port
name: etcd_port, type: port, level: C
etcd client port, 2379 by default
etcd_peer_port
name: etcd_peer_port, type: port, level: C
etcd peer port, 2380 by default
etcd_init
name: etcd_init, type: enum, level: C
etcd initial cluster state, new or existing
default values: new, which will create a standalone new etcd cluster.
The value existing is used when trying to append new member to existing etcd cluster.
etcd_election_timeout
name: etcd_election_timeout, type: int, level: C
etcd election timeout, 1000 (ms) by default
etcd_heartbeat_interval
name: etcd_heartbeat_interval, type: int, level: C
etcd heartbeat interval, 100 (ms) by default
ETCD_REMOVE
This section contains parameters for the etcd_remove role,
which are action flags for the etcd-rm.yml playbook.
etcd_safeguard
name: etcd_safeguard, type: bool, level: G/C/A
prevent purging etcd instance? default value is false
If enabled, running etcd instance will not be purged by etcd-rm.yml playbook.
etcd_rm_data
name: etcd_rm_data, type: bool, level: G/C/A
remove etcd data during removal? default value is true
When enabled, the etcd-rm.yml playbook will remove etcd data directories and configuration files during cluster or member removal.
etcd_rm_pkg
name: etcd_rm_pkg, type: bool, level: G/C/A
uninstall etcd packages during removal? default value is false
When enabled, the etcd-rm.yml playbook will uninstall etcd packages during cluster or member removal.
3 - Administration
Here are some administration SOP for etcd:
- Create Cluster
- Remove Cluster
- CLI Environment
- Reload Config
- Management Scripts
- Append Member
- Remove Member
Check ETCD: FAQ for more questions.
Create Cluster
To create an etcd cluster, define the etcd cluster in inventory first:
Then run the etcd.yml playbook.
If there’s an existing etcd cluster, this playbook will update the config and restart all the etcd instances
Pigsty has a safeguard mechanism to prevent accidental purge. etcd_safeguard is false by default,
For provisioned etcd cluster in prod env, you can enable safeguard to prevent accidental clean.
Since Pigsty v3.6+, the etcd.yml playbook and etcd role are focused solely on cluster installation and member addition. All removal operations have been moved to the dedicated etcd-rm.yml playbook using the etcd_remove role.
Remove Cluster
To remove an existing etcd cluster, you can use the dedicated etcd-rm.yml:
If the etcd_safeguard is set to true, the playbook will abort.
CLI Environment
Pigsty use etcd v3 API by default. (v2 support is dropped since v3.6.0)
Here’s an example of client environment config.
You can do CRUD with the following commands after setting up the envs:
Reload Config
In case of permanent etcd cluster membership changes, You’ll have to refresh the 4 etcd endpoints references:
- config file of existing etcd members and client env var
- patroni dcs endpoint config
- vip-manager dcs endpoint config
To refresh etcd config file /etc/etcd/etcd.conf on existing members and client env vars:
Update patroni reference to etcd endpoints:
Update vip-manager reference to etcd endpoints (if you are using PGSQL L2 VIP):
Append Member
ETCD Reference: Add a member
Pigsty can perform etcd cluster expansion with bin/etcd-add script or the etcd.yml playbook.
You can add new members to existing etcd cluster in 5 steps:
- issue
etcdctl member addcommand to tell existing cluster that a new member is coming (use learner mode) - update inventory group
etcdwith new instance - init the new member with
etcd_init=existing, to join the existing cluster rather than create a new one (VERY IMPORTANT) - promote the new member from leaner to follower
- update etcd endpoints reference with reload-config
Manual Approach
Automated Approach (Recommended)
Use the bin/etcd-add script to simplify the process:
The etcd-add script will:
- Validate IP addresses
- Execute the etcd.yml playbook with appropriate parameters
- Provide safety warnings and countdown timers
- Guide you through post-operation configuration updates
Remove Member
To remove a member from existing etcd cluster, you have two approaches:
Automated Approach (Recommended)
Use the bin/etcd-rm script for simplified removal:
Or use the dedicated removal playbook:
Manual Approach
For manual removal, it usually takes 3 steps:
- remove/uncomment it from inventory and reload config
- remove it with
etcdctl member remove <server_id>command and kick it out of the cluster - use the etcd-rm.yml playbook to clean up the instance
Removal Parameters
The etcd_remove role supports several configuration options:
etcd_safeguard=true: Prevents accidental removaletcd_rm_data=true: Removes etcd data directories (default: true)etcd_rm_pkg=false: Uninstalls etcd packages (default: false)
Example with custom parameters:
4 - Playbook
There’s a built-in playbook: etcd.yml for etcd cluster installation.
etcd.yml
To create a new etcd cluster, run the following playbook:
Here are available sub tasks:
etcd_assert: generate etcd identityetcd_install: install etcd rpm packagesetcd_dir: create etcd data & conf diretcd_config: generate etcd configetcd_conf: generate etcd main configetcd_cert: generate etcd ssl cert
etcd_launch: launch etcd serviceetcd_register: register etcd to prometheus
If you want to append a new member to an existing etcd cluster,
you have to add it to the config inventory, and run the playbook with etcd_init = existing
against the new member:
It’s usually OK to re-run the playbook, it will update the etcd cluster config and restart etcd instances.
Since Pigsty v3.6+, the etcd.yml playbook no longer has cluster removal capabilities. Use the dedicated etcd-rm.yml playbook and etcd_remove role for etcd cluster removal operations.
etcd-rm.yml
To remove the etcd cluster, run the following playbook:
Here are available sub tasks:
etcd_safeguard: check safeguard and abort if enabledprometheus: remove etcd targets registration from prometheusetcd_leave: try graceful leaving etcd cluster before purgeetcd_stop: stop and disable etcd service with systemdetcd_data: remove etcd data (disable withetcd_rm_data=false)etcd_pkg: uninstall etcd packages (enable withetcd_rm_pkg=true)
To remove a member from an existing etcd cluster, you can run the playbook
The removal playbook uses the new etcd_remove role with configurable parameters:
etcd_safeguard: Prevents accidental removal when set totrueetcd_rm_data: Controls whether ETCD data is deleted (trueby default)etcd_rm_pkg: Controls whether ETCD packages are uninstalled (falseby default)
5 - Monitor
Dashboards
The ETCD module provides a monitoring dashboard: Etcd Overview.
ETCD Overview: Overview of the ETCD cluster
This dashboard provides key information about the ETCD status, with the most notable being ETCD Aliveness, which displays the overall service status of the ETCD cluster.
Red bands indicate periods when instances are unavailable, while the blue-gray bands below show when the entire cluster is unavailable.
Alert Rules
Pigsty provides the following two alert rules for the INFRA module:
| Alert Rule | Description | Severity |
|---|---|---|
EtcdServerDown |
Etcd node down, critical alert | Critical |
EtcdNoLeader |
Etcd cluster has no leader, critical alert | Critical |
EtcdQuotaFull |
Etcd quota usage exceeds 90%, warning | Warning |
EtcdNetworkPeerRTSlow |
Etcd network latency is slow, notice | Notice |
EtcdWalFsyncSlow |
Etcd disk fsync is slow, notice | Notice |
You can modify or add new etcd alert rules in files/prometheus/rules/etcd.yml.
6 - FAQ
What is the role of the etcd in pigsty?
etcd is a distributed, reliable key-value store used to store the most critical config / consensus data in the deployment. Pigsty uses etcd as the DCS (Distributed Configuration Store) service for Patroni, which will store the high availability status information of the PostgreSQL cluster.
How many etcd instances should I choose?
If more than (include) half of the etcd instances are down, the etcd cluster, and its service will be unavailable.
For example, a 3-node etcd cluster can tolerate at most one node failure, and the other two nodes can still work normally; while a 5-node etcd cluster can tolerate 2 node failures.
Beware that the learner instances in the etcd cluster do not count in the member number. So in a 3-node etcd cluster, if there is a learner instance, the actual member count is 2, so no node failure can be tolerated.
It is advisable to choose an odd number of etcd instances to avoid split-brain scenarios. It is recommended to use 3 or 5 nodes for the production environment.
What is the impact of etcd failure?
If etcd cluster is unavailable, it will affect the control plane of Pigsty, but not the data plane — the existing PostgreSQL cluster will continue to serve, but admin operations through Patroni will not work.
During etcd failure, PostgreSQL HA is unable to perform automatic failover; and most of the Patroni operations will be blocked, such as edit-config, restart, switchover, etc… Admin tasks through Ansible playbooks are usually not affected by etcd failure, such as create database, create user, reload HBA and Service, etc…, and you can always operate the PostgreSQL cluster directly to achieve most of the patroni functions.
Beware that the above description is only applicable to newer versions of Patroni (>=3.0, Pigsty >= 2.0). If you are using an older version of Patroni (<3.0, corresponding to Pigsty version 1.x), etcd / consul failure will cause a serious impact:
All PostgreSQL clusters will be demoted and reject write requests, and etcd failure will be amplified as a global PostgreSQL failure. After Patroni 3.0’s DCS Failsafe feature, this situation has been significantly improved.
What data is stored in the etcd cluster?
etcd is only used for PostgreSQL HA consensus in Pigsty, no other data is stored in etcd by default.
These consensus data are managed by Patroni, and when these data are lost in etcd, Patroni will automatically rebuild them.
Thus, by default, the etcd in Pigsty can be regarded as a “stateless service” that is disposable, which brings great convenience to maintenance work.
If you use etcd for other purposes, such as storing metadata for Kubernetes, or storing other data, you need to back up the etcd data yourself and restore the data after the etcd cluster is restored.
How to recover from etcd failure?
Since etcd is disposable in Pigsty, you can quickly stop the bleeding by “restarting” or “redeploying” etcd in case of failure.
To Restart the etcd cluster, you can use the following Ansible command (or systemctl restart etcd):
To Reset the etcd cluster, you can run this playbook, it will nuke the etcd cluster and redeploy it:
Beware that if you use etcd to store other data, don’t forget to backup etcd data before nuking the etcd cluster.
Is any maintenance work for etcd cluster?
In short: do not use all the quota of etcd.
etcd has a default quota for database size of 2GB, if your etcd database size exceeds this limit, etcd will reject write requests. Meanwhile, as etcd’s data model illustrates, each write will generate a new version (a.k.a. revision), so if your etcd cluster writes frequently, even with very few keys, the etcd database size may continue to grow, and may fail when it reaches the quota limit.
You can achieve this by Auto Compact, Manual Compact, Defragmentation, and Quota Increase, etc., please refer to the etcd official maintenance guide.
Pigsty has auto compact enabled by default since v2.6, so you usually don’t have to worry about etcd full. For versions before v2.6, we strongly recommend enabling etcd’s auto compact feature in the production environment.
Fill etcd may lead to PostgreSQL failure!
For Pigsty v2.0 - v2.5 users, we strongly recommend upgrading to a newer version, or following the instructions below to enable etcd auto compaction!
How to enable etcd auto compaction?
If you are using an earlier version of Pigsty (v2.0 - v2.5), we strongly recommend that you enable etcd’s auto compaction feature in the production environment.
Edit the etcd config template in roles/etcd/templates/etcd.conf with these 3 new lines:
You can set all the PostgreSQL cluster to maintenance mode and then redeploy the etcd cluster with ./etcd.yml to apply the these changes.
It will increase the etcd default quota from 2 GiB to 16 GiB, and ensure that only the most recent day’s write history is retained, avoiding the infinite growth of the etcd database size.
Where does the PostgreSQL HA data store in etcd?
Patroni will use the pg_namespace (default is /pg) as the prefix for all metadata keys in etcd, followed by the PostgreSQL cluster name.
For example, a PG cluster named pg-meta, its metadata keys will be stored under /pg/pg-meta, which may look like this:
How to use existing external etcd cluster?
The hard-coded group, etcd, will be used as DCS servers for PGSQL. You can initialize them with etcd.yml or assume it is an existing external etcd cluster.
To use an existing external etcd cluster, define them as usual and make sure your current etcd cluster certificate is signed by the same CA as your self-signed CA for PGSQL.
How to add a new member to the existing etcd cluster?
Check Add a member to etcd cluster
How to remove a member from an existing etcd cluster?
Check Remove member from etcd cluster