alpha
Login
or
Join now
lass.pds.lassul.us
/
superconfig
Star
0
Fork
1
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
This repository has no description
Star
0
Fork
1
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
1
Pipelines
configs.consul: add recover helper
author
lassulus
date
1 year ago
(Jul 9, 2025, 8:56 PM CEST)
commit
1b8bbb72
1b8bbb721dc7032154fc3ddcfe8d052099330237
parent
8255b0b0
8255b0b03e5350e8ace41432c0fa168277e95097
+143
-1
1 changed file
Expand all
Collapse all
Unified
Split
2configs
consul.nix
+143
-1
2configs/consul.nix
View file
Reviewed
···
1
1
-
{ config, lib, ... }:
1
1
+
{
2
2
+
config,
3
3
+
lib,
4
4
+
pkgs,
5
5
+
...
6
6
+
}:
2
7
{
3
8
services.consul = {
4
9
enable = true;
···
17
22
18
23
# try to fix random lock loss on leader reelection
19
24
retry_interval = "3s";
25
25
+
26
26
+
# Autopilot configuration for better automatic failure handling
27
27
+
autopilot = {
28
28
+
# Automatically clean up dead servers
29
29
+
cleanup_dead_servers = true;
30
30
+
# How long a server must be stable before promoting
31
31
+
server_stabilization_time = "10s";
32
32
+
# Only requires 3 servers minimum instead of all configured servers
33
33
+
min_quorum = 3;
34
34
+
};
35
35
+
36
36
+
# Performance tuning for faster leader elections
37
37
+
performance = {
38
38
+
# Reduce raft timing for faster recovery
39
39
+
raft_multiplier = 1;
40
40
+
};
41
41
+
42
42
+
# Leave on terminate for cleaner shutdowns
43
43
+
leave_on_terminate = true;
44
44
+
45
45
+
# Disable leave intent to prevent stuck "leaving" state
46
46
+
disable_leave_intent = true;
20
47
};
21
48
};
49
49
+
50
50
+
# Add systemd override for easier recovery
51
51
+
systemd.services.consul = {
52
52
+
# Add pre-start script to clean up potential issues
53
53
+
preStart = ''
54
54
+
# Remove any stale peers.json from failed recovery attempts
55
55
+
rm -f /var/lib/consul/raft/peers.json
56
56
+
57
57
+
# Ensure proper permissions
58
58
+
chown -R consul:consul /var/lib/consul
59
59
+
'';
60
60
+
61
61
+
# Add recovery commands to service
62
62
+
serviceConfig = {
63
63
+
# Allow consul to restart on failure
64
64
+
Restart = "on-failure";
65
65
+
RestartSec = "5s";
66
66
+
StartLimitBurst = 3;
67
67
+
StartLimitInterval = "60s";
68
68
+
};
69
69
+
};
70
70
+
71
71
+
# Add recovery helper script
72
72
+
environment.systemPackages = [
73
73
+
(pkgs.writeScriptBin "consul-recover" ''
74
74
+
#!${pkgs.stdenv.shell}
75
75
+
set -e
76
76
+
77
77
+
echo "Consul Recovery Tool"
78
78
+
echo "==================="
79
79
+
echo ""
80
80
+
81
81
+
case "''${1:-}" in
82
82
+
force-leave)
83
83
+
if [ -z "''$2" ]; then
84
84
+
echo "Usage: consul-recover force-leave <node-name>"
85
85
+
exit 1
86
86
+
fi
87
87
+
echo "Force removing node ''$2 from cluster..."
88
88
+
consul force-leave "''$2"
89
89
+
;;
90
90
+
91
91
+
reset-cluster)
92
92
+
echo "WARNING: This will completely reset the local consul cluster data!"
93
93
+
echo "Only use this if the cluster is completely broken."
94
94
+
read -p "Are you sure? (yes/no): " confirm
95
95
+
if [ "''$confirm" = "yes" ]; then
96
96
+
echo "Stopping consul..."
97
97
+
systemctl stop consul
98
98
+
echo "Clearing consul data..."
99
99
+
rm -rf /var/lib/consul/*
100
100
+
echo "Starting consul..."
101
101
+
systemctl start consul
102
102
+
echo "Reset complete. This node will rejoin or form a new cluster."
103
103
+
else
104
104
+
echo "Cancelled."
105
105
+
fi
106
106
+
;;
107
107
+
108
108
+
bootstrap-single)
109
109
+
echo "WARNING: This will force this node to bootstrap as a single-node cluster!"
110
110
+
echo "Only use this in emergency recovery scenarios."
111
111
+
read -p "Are you sure? (yes/no): " confirm
112
112
+
if [ "''$confirm" = "yes" ]; then
113
113
+
echo "Stopping consul..."
114
114
+
systemctl stop consul
115
115
+
echo "Clearing consul data..."
116
116
+
rm -rf /var/lib/consul/*
117
117
+
echo "Creating temporary bootstrap configuration..."
118
118
+
cat /etc/consul.json | ${pkgs.jq}/bin/jq '.bootstrap_expect = 1' > /tmp/consul-bootstrap.json
119
119
+
echo "Starting consul in bootstrap mode..."
120
120
+
consul agent -server -data-dir=/var/lib/consul -config-file=/tmp/consul-bootstrap.json -config-file=/etc/consul-addrs.json -config-dir=/etc/consul.d &
121
121
+
CONSUL_PID=$!
122
122
+
sleep 10
123
123
+
echo "Checking cluster status..."
124
124
+
if consul operator raft list-peers; then
125
125
+
echo "Bootstrap successful! Restarting with normal configuration..."
126
126
+
kill $CONSUL_PID
127
127
+
systemctl start consul
128
128
+
else
129
129
+
echo "Bootstrap failed!"
130
130
+
kill $CONSUL_PID 2>/dev/null || true
131
131
+
exit 1
132
132
+
fi
133
133
+
else
134
134
+
echo "Cancelled."
135
135
+
fi
136
136
+
;;
137
137
+
138
138
+
status)
139
139
+
echo "Consul Cluster Status:"
140
140
+
echo "====================="
141
141
+
echo ""
142
142
+
echo "Members:"
143
143
+
consul members || echo "Failed to get members (no leader?)"
144
144
+
echo ""
145
145
+
echo "Raft Peers:"
146
146
+
consul operator raft list-peers || echo "Failed to get raft peers (no leader?)"
147
147
+
echo ""
148
148
+
echo "Service Status:"
149
149
+
systemctl status consul --no-pager | head -20
150
150
+
;;
151
151
+
152
152
+
*)
153
153
+
echo "Usage: consul-recover <command>"
154
154
+
echo ""
155
155
+
echo "Commands:"
156
156
+
echo " status - Show cluster status"
157
157
+
echo " force-leave <node> - Force remove a node from the cluster"
158
158
+
echo " reset-cluster - Completely reset local consul data"
159
159
+
echo " bootstrap-single - Emergency: Bootstrap as single-node cluster"
160
160
+
;;
161
161
+
esac
162
162
+
'')
163
163
+
];
22
164
23
165
environment.etc."consul.d/testservice.json".text = builtins.toJSON {
24
166
service = {