forked from supabase/benchmarks-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.nix
285 lines (266 loc) · 9.67 KB
/
deploy.nix
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
let
pkgs = import <nixpkgs> {};
region = "us-east-2";
accessKeyId = "default"; ## aws profile
env = {
withNginx = builtins.getEnv "PGRBENCH_WITH_NGINX" == "true";
withUnixSocket = builtins.getEnv "PGRBENCH_WITH_UNIX_SOCKET" == "true";
withSeparatePg = builtins.getEnv "PGRBENCH_SEPARATE_PG" == "true";
withNgnixLBS = builtins.getEnv "PGRBENCH_PGRST_NGNIX_LBS" == "true";
pgInstanceType = builtins.getEnv "PGRBENCH_PG_INSTANCE_TYPE";
pgrstInstanceType = builtins.getEnv "PGRBENCH_PGRST_INSTANCE_TYPE";
pgrstPool = builtins.getEnv "PGRBENCH_PGRST_POOL";
withPgLogging =
pkgs.lib.optionalAttrs (builtins.getEnv "PGRBENCH_PG_LOGGING" == "true") {
logging_collector = "on";
log_directory = "pg_log";
log_filename = "postgresql-%Y-%m-%d.log";
log_statement = "all";
};
};
postgresConfigs = import ../postgresql/postgres.nix;
in {
network.description = "postgrest benchmark";
# Provisioning
resources = {
ec2KeyPairs.pgrstBenchKeyPair = { inherit region accessKeyId; };
# Dedicated VPC
vpc.pgrstBenchVpc = {
inherit region accessKeyId;
name = "pgrst-bench-vpc";
enableDnsSupport = true;
enableDnsHostnames = true;
cidrBlock = "10.0.0.0/24";
};
vpcSubnets.pgrstBenchSubnet = {resources, ...}: {
inherit region accessKeyId;
name = "pgrst-bench-subnet";
zone = "${region}a";
vpcId = resources.vpc.pgrstBenchVpc;
cidrBlock = "10.0.0.0/24";
mapPublicIpOnLaunch = true;
};
vpcInternetGateways.pgrstBenchIG = { resources, ... }: {
inherit region accessKeyId;
name = "pgrst-bench-ig";
vpcId = resources.vpc.pgrstBenchVpc;
};
vpcRouteTables.pgrstBenchRT = { resources, ... }: {
inherit region accessKeyId;
name = "pgrst-bench-rt";
vpcId = resources.vpc.pgrstBenchVpc;
};
vpcRoutes.IGRoute = { resources, ... }: {
inherit region accessKeyId;
routeTableId = resources.vpcRouteTables.pgrstBenchRT;
destinationCidrBlock = "0.0.0.0/0";
gatewayId = resources.vpcInternetGateways.pgrstBenchIG;
};
vpcRouteTableAssociations.pgrstBenchAssoc = { resources, ... }: {
inherit region accessKeyId;
subnetId = resources.vpcSubnets.pgrstBenchSubnet;
routeTableId = resources.vpcRouteTables.pgrstBenchRT;
};
ec2SecurityGroups.pgrstBenchSecGroup = {resources, ...}: {
inherit region accessKeyId;
name = "pgrst-bench-sec-group";
vpcId = resources.vpc.pgrstBenchVpc;
rules = [
{ fromPort = 80; toPort = 80; sourceIp = "0.0.0.0/0"; }
{ fromPort = 22; toPort = 22; sourceIp = "0.0.0.0/0"; }
# protocol -1 allows icmp traffic in addition to tcp/udp
{ protocol = "-1"; fromPort = 0; toPort = 65535; sourceIp = resources.vpcSubnets.pgrstBenchSubnet.cidrBlock; } # For internal access on the VPC
];
};
};
pgrstServer = {nodes, config, resources, ...}: let pgrst = pkgs.callPackage ./pgrst.nix {}; in
{
deployment = {
targetEnv = "ec2";
ec2 = {
inherit region accessKeyId;
instanceType =
if builtins.stringLength env.pgrstInstanceType == 0
then "t3a.nano"
else env.pgrstInstanceType;
associatePublicIpAddress = true;
ebsInitialRootDiskSize = 10;
keyPair = resources.ec2KeyPairs.pgrstBenchKeyPair;
subnetId = resources.vpcSubnets.pgrstBenchSubnet;
securityGroupIds = [resources.ec2SecurityGroups.pgrstBenchSecGroup.name];
};
};
boot.loader.grub.device = pkgs.lib.mkForce "/dev/nvme0n1"; # Fix for https://github.com/NixOS/nixpkgs/issues/62824#issuecomment-516369379
environment.systemPackages = [
pgrst
pkgs.htop
];
services.postgresql = pkgs.lib.mkIf (!env.withSeparatePg) {
enable = true;
package = pkgs.postgresql_12;
authentication = ''
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all ${resources.vpcSubnets.pgrstBenchSubnet.cidrBlock} trust
'';
enableTCPIP = true; # listen_adresses = *
# Tuned according to https://pgtune.leopard.in.ua
settings = builtins.getAttr config.deployment.ec2.instanceType postgresConfigs // env.withPgLogging;
initialScript = ../schemas/chinook/chinook.sql; # Here goes the sample db
};
systemd.services =
let
pgHost =
if env.withSeparatePg then "pg"
else if env.withUnixSocket then ""
else "localhost";
pgrstConf = num : pkgs.writeText "pgrst${num}.conf" ''
db-uri = "postgres://postgres@${pgHost}/postgres"
db-schema = "public"
db-anon-role = "postgres"
db-use-legacy-gucs = false
db-pool = ${if builtins.stringLength env.pgrstPool == 0 then "20" else env.pgrstPool}
db-pool-timeout = 3600
${
if env.withNginx && env.withUnixSocket
then ''
server-unix-socket = "/tmp/pgrst${num}.sock"
server-unix-socket-mode = "777"
''
else if env.withNginx
then ''
server-port = "3000"
''
else ''
server-port = "80"
''
}
jwt-secret = "reallyreallyreallyreallyverysafe"
'';
pgrstService = enabled: num: {
enable = enabled;
description = "postgrest daemon";
after = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pgrst}/bin/postgrest ${pgrstConf num}";
Restart = "always";
};
};
in
{
postgrest1 = pgrstService true "1";
postgrest2 = pgrstService env.withNgnixLBS "2";
};
services.nginx = {
enable = env.withNginx;
config = ''
worker_processes auto;
events {
worker_connections 1024;
}
http {
upstream postgrest {
${
if env.withUnixSocket && env.withNgnixLBS
then ''
server unix:/tmp/pgrst1.sock;
server unix:/tmp/pgrst2.sock;
''
else if env.withUnixSocket
then
''
server unix:/tmp/pgrst1.sock;
''
else ''
server localhost:3000;
''
}
keepalive 64;
keepalive_timeout 120s;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_set_header Connection "";
proxy_set_header Accept-Encoding "";
proxy_http_version 1.1;
proxy_pass http://postgrest/;
}
}
}
'';
};
networking.firewall.allowedTCPPorts = [ 80 5432 ];
networking.hosts = pkgs.lib.optionalAttrs env.withSeparatePg {
"${nodes.pg.config.networking.privateIPv4}" = [ "pg" ];
};
};
client = {nodes, resources, ...}: {
environment.systemPackages = [
pkgs.k6
pkgs.postgresql_12 # only used for getting pgbench, no postgresql is started here
];
deployment = {
targetEnv = "ec2";
ec2 = {
inherit region accessKeyId;
instanceType = "m5a.16xlarge";
associatePublicIpAddress = true;
keyPair = resources.ec2KeyPairs.pgrstBenchKeyPair;
subnetId = resources.vpcSubnets.pgrstBenchSubnet;
securityGroupIds = [resources.ec2SecurityGroups.pgrstBenchSecGroup.name];
};
};
boot.loader.grub.device = pkgs.lib.mkForce "/dev/nvme0n1";
# Tuning from https://k6.io/docs/misc/fine-tuning-os
boot.kernel.sysctl."net.ipv4.tcp_tw_reuse" = 1;
security.pam.loginLimits = [ ## ulimit -n
{ domain = "root"; type = "hard"; item = "nofile"; value = "10000"; }
{ domain = "root"; type = "soft"; item = "nofile"; value = "10000"; }
];
networking.hosts = {
"${nodes.pgrstServer.config.networking.privateIPv4}" = [ "pgrst" ];
} // pkgs.lib.optionalAttrs env.withSeparatePg {
"${nodes.pg.config.networking.privateIPv4}" = [ "pg" ];
};
};
}
// pkgs.lib.optionalAttrs env.withSeparatePg
{
pg = {resources, config, ...}: rec {
deployment = {
targetEnv = "ec2";
ec2 = {
inherit region accessKeyId;
instanceType =
if builtins.stringLength env.pgInstanceType == 0
then "t3a.nano"
else env.pgInstanceType;
associatePublicIpAddress = true;
ebsInitialRootDiskSize = 10;
keyPair = resources.ec2KeyPairs.pgrstBenchKeyPair;
subnetId = resources.vpcSubnets.pgrstBenchSubnet;
securityGroupIds = [resources.ec2SecurityGroups.pgrstBenchSecGroup.name];
};
};
boot.loader.grub.device = pkgs.lib.mkForce "/dev/nvme0n1"; # Fix for https://github.com/NixOS/nixpkgs/issues/62824#issuecomment-516369379
services.postgresql = {
enable = true;
package = pkgs.postgresql_12;
authentication = ''
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all ${resources.vpcSubnets.pgrstBenchSubnet.cidrBlock} trust
'';
enableTCPIP = true; # listen_adresses = *
# Tuned according to https://pgtune.leopard.in.ua
settings = builtins.getAttr config.deployment.ec2.instanceType postgresConfigs // env.withPgLogging;
initialScript = ../schemas/chinook/chinook.sql; # Here goes the sample db
};
networking.firewall.allowedTCPPorts = [ 5432 ];
};
}