-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.hcl
305 lines (253 loc) · 7.12 KB
/
postgres.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// postgres database server with primary/replica support
// Docker Image: https://github.com/mutablelogic/docker-postgres
///////////////////////////////////////////////////////////////////////////////
// VARIABLES
variable "dc" {
description = "data centers that the job runs in"
type = list(string)
}
variable "namespace" {
description = "namespace that the job runs in"
type = string
default = "default"
}
variable "hosts" {
description = "host constraint for the job"
type = list(string)
default = []
}
variable "service_provider" {
description = "Service provider, either consul or nomad"
type = string
default = "nomad"
}
variable "service_name" {
description = "Service name"
type = string
default = "postgresql"
}
variable "service_dns" {
description = "Service discovery DNS"
type = list(string)
default = []
}
variable "docker_image" {
description = "Docker image"
type = string
}
variable "docker_always_pull" {
description = "Pull docker image on every job restart"
type = bool
default = false
}
variable "primary" {
description = "Primary database host"
type = string
}
variable "replicas" {
description = "Replica (read-only) database hosts"
type = list(string)
default = []
}
variable "port" {
description = "Port for connections"
type = number
default = 5432
}
variable "database" {
description = "default database"
type = string
default = "default"
}
variable "data" {
description = "Data persistence directory"
type = string
}
variable "root_user" {
description = "root user"
type = string
default = "postgres"
}
variable "root_password" {
description = "root password"
type = string
}
variable "replication_user" {
description = "replication user"
type = string
default = ""
}
variable "replication_password" {
description = "replication password"
type = string
}
variable "databases" {
description = "Additional databases to create, with their passwords"
type = map(string)
default = {}
}
///////////////////////////////////////////////////////////////////////////////
// LOCALS
locals {
data_path = var.data == "" ? "${NOMAD_ALLOC_DIR}/data" : "/var/lib/postgresql/data/pgdata"
replication_slots = [for host in var.replicas : format("replica_%s", host)]
}
///////////////////////////////////////////////////////////////////////////////
// JOB
job "postgres" {
type = "service"
datacenters = var.dc
namespace = var.namespace
update {
min_healthy_time = "10s"
healthy_deadline = "5m"
health_check = "task_states"
}
/////////////////////////////////////////////////////////////////////////////////
// PRIMARY
group "primary" {
constraint {
attribute = node.unique.name
value = var.primary
}
network {
port "postgres" {
static = var.port
to = 5432
}
}
service {
tags = ["postgres", "primary"]
name = format("%s-primary", var.service_name)
port = "postgres"
provider = var.service_provider
}
ephemeral_disk {
migrate = false
}
task "server" {
driver = "docker"
resources {
memory = 2048
}
config {
image = var.docker_image
force_pull = var.docker_always_pull
ports = ["postgres"]
dns_servers = var.service_dns
volumes = compact([
var.data == "" ? "" : format("%s:/var/lib/postgresql/data", var.data)
])
}
//dynamic "env" {
// for_each = var.databases
// content {
// name = format("POSTGRES_PASSWORD_%s", env.key)
// value = env.value
// }
//}
env {
POSTGRES_USER = var.root_user
POSTGRES_PASSWORD = var.root_password
POSTGRES_DB = var.database
PGDATA = local.data_path
POSTGRES_REPLICATION_USER = var.replication_user
POSTGRES_REPLICATION_PASSWORD = var.replication_password
POSTGRES_REPLICATION_SLOT = join(",", local.replication_slots)
POSTGRES_DATABASES = join(",", keys(var.databases))
}
}
}
/////////////////////////////////////////////////////////////////////////////////
// REPLICAS
group "replica" {
count = length(var.replicas)
constraint {
attribute = node.unique.name
operator = "set_contains_any"
value = join(",", var.replicas)
}
constraint {
operator = "distinct_hosts"
value = "true"
}
network {
port "postgres" {
static = var.port
to = 5432
}
}
service {
tags = ["postgres", "replica"]
name = format("%s-replica", var.service_name)
port = "postgres"
provider = var.service_provider
}
ephemeral_disk {
migrate = false
}
task "wait-for-primary" {
lifecycle {
hook = "prestart"
sidecar = false
}
meta {
primary_service_name = format("%s-primary", var.service_name)
}
template {
data = <<-EOH
{{ $primary_service_name := env "NOMAD_META_primary_service_name" }}
{{ range nomadService $primary_service_name -}}
POSTGRES_REPLICATION_PRIMARY="host={{ .Address }} port={{ .Port }}"
{{ end }}
EOH
destination = "tmp/config.env"
env = true
}
driver = "docker"
config {
image = var.docker_image
force_pull = var.docker_always_pull
command = "pg_isready"
args = ["-d", "${POSTGRES_REPLICATION_PRIMARY}", "-t", "60"]
}
}
task "server" {
driver = "docker"
resources {
memory = 512
}
config {
image = var.docker_image
force_pull = var.docker_always_pull
ports = ["postgres"]
dns_servers = var.service_dns
volumes = compact([
var.data == "" ? "" : format("%s:/var/lib/postgresql/data", var.data)
])
}
meta {
primary_service_name = format("%s-primary", var.service_name)
}
template {
data = <<-EOH
{{ $primary_service_name := env "NOMAD_META_primary_service_name" }}
{{ range nomadService $primary_service_name -}}
POSTGRES_REPLICATION_PRIMARY="host={{ .Address }} port={{ .Port }}"
{{ end }}
EOH
destination = "tmp/config.env"
env = true
}
env {
POSTGRES_USER = var.root_user
POSTGRES_PASSWORD = var.root_password
POSTGRES_DB = var.database
PGDATA = local.data_path
POSTGRES_REPLICATION_USER = var.replication_user
POSTGRES_REPLICATION_PASSWORD = var.replication_password
POSTGRES_REPLICATION_SLOT = format("replica_%s", node.unique.name)
}
} // task
} // group
} // job