-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
overview.xml
1914 lines (1576 loc) · 78.1 KB
/
overview.xml
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<chapter xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:id="chap-overview">
<title>Overview</title>
<para>This chapter gives a quick overview of how to use NixOps.</para>
<section><title>Deploying a VirtualBox VM</title>
<para>NixOps deploys machines on the basis of a declarative
description of what those machines should do, and where they should be
deployed to. These descriptions are specified in the <emphasis>Nix
expression language</emphasis> used by the Nix package manager. <xref
linkend="ex-logical.nix" /> shows a minimal specification of a network
consisting of only one logical machine named
<literal>webserver</literal>.</para>
<example xml:id="ex-logical.nix">
<title><filename>trivial.nix</filename>: logical network specification</title>
<programlisting>
{
network.description = "Web server";
webserver =
{ config, pkgs, ... }:
{ services.httpd.enable = true;
services.httpd.adminAddr = "alice@example.org";
services.httpd.documentRoot = "${pkgs.valgrind.doc}/share/doc/valgrind/html";
networking.firewall.allowedTCPPorts = [ 80 ];
};
}
</programlisting>
</example>
<para>This specification consists of a set of top-level attributes
describing logical machines (namely <varname>webserver</varname>) and
meta-information (namely <varname>network.description</varname>).
Each attribute not named <varname>network</varname> describes a
logical machine. The value of each logical machine attribute is a
<emphasis>NixOS configuration module</emphasis>, which describes the
desired configuration of the corresponding machine. Thus, the logical
machine <literal>webserver</literal> should have the Apache
<command>httpd</command> web server running, and its document root
(rather arbitrarily for demonstration purposes) should be the
documentation of the Valgrind package.</para>
<para>To deploy this machine, we also need to provide configuration
options that tell NixOps to what environment it should be deployed.
<xref linkend="ex-physical-vbox.nix" /> specifies that
<literal>webserver</literal> should be deployed as a VirtualBox
instance. Note that for this to work the <literal>vboxnet0</literal> network has to exist - you can add it in the VirtualBox general settings under <emphasis>Networks - Host-only Networks</emphasis> if necessary.
If you are running NixOps in a headless environment, then you should also add the option
<code>deployment.virtualbox.headless = true;</code>
to the configuration. Otherwise, VirtualBox will fail when it tries to open a graphical display on the host's desktop.</para>
<example xml:id="ex-physical-vbox.nix">
<title><filename>trivial-vbox.nix</filename>: VirtualBox physical network specification</title>
<programlisting>
{
webserver =
{ config, pkgs, ... }:
{ deployment.targetEnv = "virtualbox";
deployment.virtualbox.memorySize = 1024; # megabytes
deployment.virtualbox.vcpu = 2; # number of cpus
};
}
</programlisting>
</example>
<para>Before we can deploy the network we need to use the command
<command>nixops create</command> to create a <emphasis>NixOps
deployment</emphasis> that contains any state associated with the
deployment (such as information about instantiated VMs). At creation
time, we need to specify the Nix expressions that constitute the
complete deployment specification. So to create a deployment for
deploying the Apache web server to VirtualBox, we would do:
<screen>
$ nixops create ./trivial.nix ./trivial-vbox.nix -d trivial
33bced96-5f26-11e1-b9d7-9630d48abec1
</screen>
Here <literal>-d trivial</literal> gives the symbolic name
<literal>trivial</literal> to the deployment. Deployments can be
identified in two ways: using the UUID printed by <command>nixops
create</command>, or using the symbolic name you specified at creation
time.</para>
<para>You can print a list of existing deployments using
<command>nixops list</command>:
<screen>
+--------------------------------------+-----------+--------------+------------+------------+
| UUID | Name | Description | # Machines | Type |
+--------------------------------------+-----------+--------------+------------+------------+
| 33bced96-5f26-11e1-b9d7-9630d48abec1 | trivial | Web server | 0 | |
+--------------------------------------+-----------+--------------+------------+------------+
</screen>
</para>
<para>The command <command>nixops info</command> shows the current
deployment state:
<screen>
$ nixops info -d trivial
Network UUID: 33bced96-5f26-11e1-b9d7-9630d48abec1
Network description: Web server
+-----------+--------+------------+-------------+------------+
| Name | Status | Type | Resource Id | IP address |
+-----------+--------+------------+-------------+------------+
| webserver | New | virtualbox | | |
+-----------+--------+------------+-------------+------------+
</screen>
The machine status <literal>New</literal> indicates that the logical
machine <literal>webserver</literal> hasn’t been created yet. The
<option>-d</option> option specifies which deployment to use; you can
use the symbolic name (<literal>-d trivial</literal>) or the UUID
(<literal>-d 33bced96-5f26-11e1-b9d7-9630d48abec1</literal>). You
can also set the the environment variable
<envar>NIXOPS_DEPLOYMENT</envar>.</para>
<para>The actual deployment is done by running <command>nixops
deploy</command>:
<screen>
$ nixops deploy -d trivial
creating VirtualBox VM ‘webserver’...
Virtual machine 'nixops-33bced96-5f26-11e1-b9d7-9630d48abec1-webserver' is created and registered.
Clone hard disk created in format 'VDI'. UUID: 5a0b0771-7e03-4fab-9c2f-e95888b57db3
Waiting for VM "nixops-33bced96-5f26-11e1-b9d7-9630d48abec1-webserver" to power on...
VM "nixops-33bced96-5f26-11e1-b9d7-9630d48abec1-webserver" has been successfully started.
waiting for IP address of ‘webserver’........................... 192.168.56.101
waiting for SSH on ‘webserver’...
building all machine configurations...
building path(s) `/nix/store/ybrny9h744q8i3x026ccfmdav8qnw7pd-nixos-version'
building path(s) `/nix/store/zxw279xhl6l8yl94gnka8aqv1kkcrrd4-os-release'
fetching path `/nix/store/pn43d3llpsm3pc1ywaxccmw8pmzjqgz0-valgrind-3.7.0'...
…
copying closure to machine ‘webserver’...
copying 376 missing paths to ‘root@192.168.56.101’...
importing path `/nix/store/jfcs9xnfbmiwqs224sb0qqsybbfl3sab-linux-headers-2.6.35.14'
…
activating new configuration on machine ‘webserver’...
updating GRUB 2 menu...
activating the configuration...
…
starting new service ‘httpd’...
</screen>
NixOps performs the following steps to do the deployment:
<itemizedlist>
<listitem><para>It creates missing machines. In this case, a
VirtualBox instance for the logical machine
<literal>webserver</literal> is started. NixOps then waits to
obtain its IP address.</para></listitem>
<listitem><para>It builds the NixOS machine configurations locally.
For instance, here Valgrind is built or downloaded because our
machine configuration has a dependency on it.</para></listitem>
<listitem><para>It copies the closure of each machine configuration
to the corresponding machine.</para></listitem>
<listitem><para>It activates the configuration on each machine. For
instance, it starts the <literal>httpd</literal> systemd service on
the <literal>webserver</literal> machine. This is the only step
that has a visible effect; all prior steps do not affect the active
configuration of the machines.</para></listitem>
</itemizedlist>
</para>
<para>The <command>nixops info</command> command will show that a
machine was created:
<screen>
$ nixops info -d trivial
Network UUID: 33bced96-5f26-11e1-b9d7-9630d48abec1
Network description: Web server
+-----------+--------+------------+-----------------------------------------------------+----------------+
| Name | Status | Type | Resource Id | IP address |
+-----------+--------+------------+-----------------------------------------------------+----------------+
| webserver | Up | virtualbox | nixops-33bced96-5f26-11e1-b9d7-9630d48abec1-machine | 192.168.56.101 |
+-----------+--------+------------+-----------------------------------------------------+----------------+
</screen>
</para>
<para>Visit <literal>http://192.168.56.101</literal> in a web browser
should now show the Valgrind documentation. You can also log in to
the virtual machine as <literal>root</literal>:
<screen>
$ nixops ssh -d trivial webserver
connecting to 192.168.56.101...
[root@webserver:~]#
</screen>
The command <command>nixops ssh</command> is a convenience wrapper
around <command>ssh</command> that passes the right IP address and SSH
identity for the specified logical machine. (NixOps automatically
creates a unique SSH key pair for communicating with each VirtualBox
instance.)</para>
<para>Redeployment after making a change to the specification is
simply a matter of running <command>nixops deploy</command> again. If
we do this for the example, NixOps will notice that the
<literal>webserver</literal> machine already exists and that most or
all dependencies are already present, so it won’t create a new
VirtualBox instance or need to build and copy a lot of dependencies.
Thus redeployment typically only takes a few seconds:
<screen>
$ time nixops deploy -d trivial
building all machine configurations...
copying closure to machine ‘webserver’...
activating new configuration on machine ‘webserver’...
real 0m3.700s
</screen>
</para>
<para>If you want to get rid of the virtual machines created by
NixOps, you can run <command>nixops destroy</command>:
<screen>
$ nixops destroy -d trivial
warning: are you sure you want to destroy VirtualBox VM ‘webserver’? (y/N) y
webserver> destroying VirtualBox VM...
webserver> 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
</screen>
You can use the option <option>--confirm</option> to confirm all
questions. This is useful for automated deployment, but potentially
dangerous.</para>
<section><title>Deploying multiple machines</title>
<para>A network consisting of only one logical machine is not very
exciting. <xref linkend="ex-logical-multi.nix" /> shows a network
consisting of three machines: a load balancer (named
<literal>proxy</literal>) that uses Apache’s
<literal>mod_proxy</literal> to do reverse proxying, and two backend
web servers (<literal>backend1</literal> and
<literal>backend2</literal>) that serve the actual content. One
important thing to note is that if you want to refer to another
machine (e.g. in a configuration file), you can use a hostname equal
to the logical name of the machine, as in the line
<programlisting>
BalancerMember http://backend1 retry=0
</programlisting>
This works because NixOps generates a <filename>/etc/hosts</filename>
file that contains entries for all the logical machines in the
network, mapping names to each machine’s IP address. Also note that
because the two backend machines have identical configurations, we can
use a let-binding to define the configuration only once.</para>
<example xml:id="ex-logical-multi.nix">
<title><filename>load-balancer.nix</filename>: logical network specification</title>
<programlisting>
let
backend =
{ config, pkgs, ... }:
{ services.httpd.enable = true;
services.httpd.adminAddr = "alice@example.org";
services.httpd.documentRoot = "${pkgs.valgrind.doc}/share/doc/valgrind/html";
networking.firewall.allowedTCPPorts = [ 80 ];
};
in
{
network.description = "Load balancing network";
proxy =
{ config, pkgs, nodes, ... }:
{ services.httpd.enable = true;
services.httpd.adminAddr = "bob@example.org";
services.httpd.extraModules = ["proxy_balancer" "lbmethod_byrequests"];
services.httpd.extraConfig =
''
<Proxy balancer://cluster>
Allow from all
BalancerMember http://backend1 retry=0
BalancerMember http://backend2 retry=0
</Proxy>
ProxyPass / balancer://cluster/
ProxyPassReverse / balancer://cluster/
'';
networking.firewall.allowedTCPPorts = [ 80 ];
};
backend1 = backend;
backend2 = backend;
}
</programlisting>
</example>
<para>To deploy it, we need a physical specification, shown in <xref
linkend="ex-physical-multi.nix" />. Deployment is as follows:
<screen>
$ nixops create ./load-balancer.nix ./load-balancer-vbox.nix -d load-balancer-vbox
$ nixops deploy -d load-balancer-vbox
</screen>
Note that NixOps creates and deploys the VMs in parallel to speed
things up.</para>
<example xml:id="ex-physical-multi.nix">
<title><filename>load-balancer-vbox.nix</filename>: VirtualBox physical network specification</title>
<programlisting>
let
vbox = { deployment.targetEnv = "virtualbox"; };
in
{ proxy = vbox;
backend1 = vbox;
backend2 = vbox;
}
</programlisting>
</example>
</section>
</section>
<section xml:id="sec-deploying-to-ec2"><title>Deploying to a NixOS machine</title>
<para>To deploy to a machine that is already running NixOS, simply set
<varname>deployment.targetHost</varname> to the IP address or host name of the machine,
and leave <varname>deployment.targetEnv</varname> undefined.
See <xref linkend="ex-physical-nixos.nix" />.
</para>
<example xml:id="ex-physical-nixos.nix">
<title><filename>trivial-nixos.nix</filename>: NixOS target physical network specification</title>
<programlisting>
{
webserver =
{ config, pkgs, ... }:
{ deployment.targetHost = "1.2.3.4";
};
}
</programlisting>
</example>
</section>
<section xml:id="sec-deploying-to-ec2"><title>Deploying to Amazon EC2</title>
<para><xref linkend="ex-physical-multi-ec2.nix" /> shows a physical
specification that deploys the load balancer network to Amazon’s
Elastic Compute Cloud (EC2). It states that the three machines need
to be instantiated in EC2 region <literal>eu-west-1</literal>. It
also specifies a non-machine cloud resource: namely, the EC2 key pair
to be used to access the machine via SSH. (It is possible to use
manually created EC2 key pairs, but it’s easier to let NixOps
provision them.)</para>
<example xml:id="ex-physical-multi-ec2.nix">
<title><filename>load-balancer-ec2.nix</filename>: EC2 physical network specification</title>
<programlisting>
let
region = "eu-west-1";
accessKeyId = "dev"; # symbolic name looked up in ~/.ec2-keys or a ~/.aws/credentials profile name
ec2 =
{ resources, ... }:
{ deployment.targetEnv = "ec2";
deployment.ec2.accessKeyId = accessKeyId;
deployment.ec2.region = region;
deployment.ec2.instanceType = "m1.small";
deployment.ec2.keyPair = resources.ec2KeyPairs.my-key-pair;
};
in
{ proxy = ec2;
backend1 = ec2;
backend2 = ec2;
# Provision an EC2 key pair.
resources.ec2KeyPairs.my-key-pair =
{ inherit region accessKeyId; };
}
</programlisting>
</example>
<para>Deployment is as follows:
<screen>
$ nixops create ./load-balancer.nix ./load-balancer-ec2.nix -d load-balancer-ec2
$ nixops deploy -d load-balancer-ec2
my-key-pair> uploading EC2 key pair ‘charon-8e50b4b5-d7f9-11e2-b91c-23f8eaf468f4-my-key-pair’...
backend1...> creating EC2 instance (AMI ‘ami-8badbdff’, type ‘m1.small’, region ‘eu-west-1’)...
backend2...> creating EC2 instance (AMI ‘ami-8badbdff’, type ‘m1.small’, region ‘eu-west-1’)...
proxy......> creating EC2 instance (AMI ‘ami-8badbdff’, type ‘m1.small’, region ‘eu-west-1’)...
backend2...> waiting for IP address...
<replaceable>...</replaceable>
proxy......> activation finished successfully
backend2...> activation finished successfully
backend1...> activation finished successfully
</screen>
Here NixOps has created an EC2 key pair and started three EBS-backed
instances running the default NixOS AMI. Other than that, deployment
is the same as for VirtualBox: NixOps builds the machine
configurations, copies their closure over to the EC2 instances, and
activates the new configurations.</para>
<para>The command <command>nixops info</command> shows all provisioned
resources, not just machines:
<screen>
$ nixops info -d load-balancer-ec2
<replaceable>...</replaceable>
+-------------+-----------------+----------------------------+---------------------------------------------------------+----------------+
| Name | Status | Type | Resource Id | IP address |
+-------------+-----------------+----------------------------+---------------------------------------------------------+----------------+
| backend1 | Up / Up-to-date | ec2 [eu-west-1a; m1.small] | i-0ec4bc43 | 54.228.61.132 |
| backend2 | Up / Up-to-date | ec2 [eu-west-1a; m1.small] | i-0cc4bc41 | 54.216.26.111 |
| proxy | Up / Up-to-date | ec2 [eu-west-1a; m1.small] | i-08c4bc45 | 54.216.171.138 |
| my-key-pair | Up / Up-to-date | ec2-keypair [eu-west-1] | charon-8e50b4b5-d7f9-11e2-b91c-23f8eaf468f4-my-key-pair | |
+-------------+-----------------+----------------------------+---------------------------------------------------------+----------------+
</screen>
</para>
<para>The resources can be destroyed by running:
<screen>
$ nixops destroy -d load-balancer-ec2
</screen>
This terminates the EC2 instances and deletes the EC2 key pair.</para>
<para>Deployment to EC2 has some prerequisites.
<itemizedlist>
<listitem><para>Obviously, you need an EC2 account.</para></listitem>
<listitem><para>You need to add your AWS access key ID and secret
key to the file <filename>~/.ec2-keys</filename>, as follows:
<programlisting>
AKIAIUTDLWJKSLSJDLDQ Grsjf37cDKKWndklek3jdxnSKE3fkskDLqdldDl/ dev # my AWS development account
</programlisting>
Here <literal>dev</literal> is a symbolic name for the AWS account,
which you can use in
<varname>deployment.ec2.accessKeyId</varname>.</para>
<para>Also you can use a standard way of storing credentials in a
<filename>~/.aws/credentials</filename>:
<programlisting>
[dev]
aws_access_key_id = AKIAIUTDLWJKSLSJDLDQ
aws_secret_access_key = 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
</programlisting>
Profile name <literal>dev</literal> is the same as a previously
mentioned symbolic name which you can set in
<varname>deployment.ec2.accessKeyId</varname>.
It is also possible to use an alternative credentials file by setting
the <envar>AWS_SHARED_CREDENTIALS_FILE</envar> environment variable.</para>
<para>Alternatively, you can set the environment variables
<envar>EC2_ACCESS_KEY</envar> and
<envar>EC2_SECRET_KEY</envar>.</para></listitem>
<listitem><para>If you want to use an SSH key pair created with the
<command>ec2-create-keypair</command> command line tool or the
AWS web interface, set <varname>deployment.ec2.keyPair</varname> to
the name of the key pair, and set
<varname>deployment.ec2.privateKey</varname> to the path of the
private key:
<programlisting>
deployment.ec2.keyPair = "your-key-name";
deployment.ec2.privateKey = "/path/to/your-key-name.pem";</programlisting>
You can leave out <varname>deployment.ec2.privateKey</varname> option
in case the key is findable by SSH through its normal mechanisms (e.g. it is listed in <filename>~/.ssh/config</filename> or was added to the ssh-agent)
</para></listitem>
<listitem><para>You need to ensure that your EC2 security groups are
set up to allow (at the very least) SSH traffic from your network.
By default, NixOps uses the security group
<literal>default</literal>. You can set the option
<varname>deployment.ec2.securityGroups</varname> to use other
security groups:
<programlisting>
deployment.ec2.securityGroups = [ "allow-ssh" "allow-http" ];
</programlisting>
</para></listitem>
<listitem><para>You need to set
<varname>deployment.ec2.region</varname> to the EC2 region you want
to deploy to. Note that key pairs and security groups are
region-specific.</para></listitem>
</itemizedlist>
</para>
</section>
<section xml:id="sec-deploying-to-gce"><title>Deploying to Google Compute Engine</title>
<para><xref linkend="ex-physical-multi-gce.nix" /> shows a physical
specification that deploys the load balancer network to Google Compute
Engine(GCE). It states that the three machines need to be instantiated in GCE region
<literal>europe-west1-b</literal>, based on the unstable branch of NixOS.
It also specifies an alternative load balancer implemented using GCE Forwarding Rule.
</para>
<example xml:id="ex-physical-multi-gce.nix">
<title><filename>load-balancer-gce.nix</filename>: GCE physical network specification</title>
<programlisting>
let
# change this as necessary or wipe and use ENV vars
credentials = {
project = "myproject";
serviceAccount = "000000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
accessKey = "/path/to/user/key.pem";
};
gce = { resources, ...}: {
networking.firewall.allowedTCPPorts = [ 80 ];
deployment.targetEnv = "gce";
deployment.gce = credentials // {
region = "europe-west1-b";
tags = [ "public-http" ];
network = resources.gceNetworks.lb-net;
};
};
in {
# create a network that allows SSH traffic(by default), pings
# and HTTP traffic for machines tagged "public-http"
resources.gceNetworks.lb-net = credentials // {
addressRange = "192.168.4.0/24";
firewall = {
allow-http = {
targetTags = [ "public-http" ];
allowed.tcp = [ 80 ];
};
allow-ping.allowed.icmp = null;
};
};
# by default, health check pings port 80, so we don't have to set anything
resources.gceHTTPHealthChecks.plain-hc = credentials;
resources.gceTargetPools.backends = { resources, nodes, ...}: credentials // {
region = "europe-west1";
healthCheck = resources.gceHTTPHealthChecks.plain-hc;
machines = with nodes; [ backend1 backend2 ];
};
resources.gceForwardingRules.lb = { resources, ...}: credentials // {
protocol = "TCP";
region = "europe-west1";
portRange = "80";
targetPool = resources.gceTargetPools.backends;
description = "Alternative HTTP Load Balancer";
};
proxy = gce;
backend1 = gce;
backend2 = gce;
}
</programlisting>
</example>
<para>Deployment is as follows:
<screen>
$ nixops create ./load-balancer.nix ./load-balancer-gce.nix -d load-balancer-gce
$ nixops deploy -d load-balancer-gce
bootstrap> creating GCE image 'n-588718b8099211e49d39b8e8560f8b58-bootstrap'...
lb-net..> Creating GCE network 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-lb-net'...
plain-hc> creating GCE HTTP health check 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-plain-hc'...
backends> creating GCE target pool 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backends'...
lb-net..> Creating GCE firewall 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-lb-net-allow-ssh'...
lb-net..> Creating GCE firewall 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-lb-net-allow-ping'...
backends> updating the machine list of GCE target pool 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backends'...
lb-net..> Creating GCE firewall 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-lb-net-allow-http'...
proxy....> Creating GCE disk of auto GiB from image 'n-588718b8099211e49d39b8e8560f8b58-bootstrap'...
backend1.> Creating GCE disk of auto GiB from image 'n-588718b8099211e49d39b8e8560f8b58-bootstrap'...
backend2.> Creating GCE disk of auto GiB from image 'n-588718b8099211e49d39b8e8560f8b58-bootstrap'...
lb......> creating GCE forwarding rule 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-lb'...done.
lb......> got IP: 146.148.16.5
backend2> creating GCE machine 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backend2'...
proxy...> creating GCE machine 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-proxy'...
backend1> creating GCE machine 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backend1'...
backend1> got IP: 130.211.95.195
backend2> got IP: 146.148.2.203
proxy...> got IP: 146.148.20.120
backend1> attaching GCE disk 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backend1-root'...
backend1> waiting for SSH....
backend2> attaching GCE disk 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-backend2-root'...
backend2> waiting for SSH...
backend1> .
proxy...> attaching GCE disk 'nixops-588718b8-0992-11e4-9d39-b8e8560f8b58-proxy-root'...
<replaceable>...</replaceable>
proxy......> activation finished successfully
backend2...> activation finished successfully
backend1...> activation finished successfully
</screen>
Here NixOps has created a GCE network, a health check, a load balancer,
a bootstrap image based on the unstable branch of NixOS,
3 root disks for the instances and started three instances running
the default NixOS image. Other than that, deployment
is the same as for VirtualBox: NixOps builds the machine
configurations, copies their closure over to the GCE instances, and
activates the new configurations.</para>
<para>The command <command>nixops info</command> shows all provisioned
resources, not just machines:
<screen>
$ nixops info -d load-balancer-gce
<replaceable>...</replaceable>
+-----------+-----------------+------------------------------------+----------------------------------------------+----------------+
| Name | Status | Type | Resource Id | IP address |
+-----------+-----------------+------------------------------------+----------------------------------------------+----------------+
| backend1 | Up / Up-to-date | gce [europe-west1-b; g1-small] | n-588718b8099211e49d39b8e8560f8b58-backend1 | 146.148.20.120 |
| backend2 | Up / Up-to-date | gce [europe-west1-b; g1-small] | n-588718b8099211e49d39b8e8560f8b58-backend2 | 146.148.31.67 |
| proxy | Up / Up-to-date | gce [europe-west1-b; g1-small] | n-588718b8099211e49d39b8e8560f8b58-proxy | 146.148.2.203 |
| lb | Up / Up-to-date | gce-forwarding-rule [europe-west1] | n-588718b8099211e49d39b8e8560f8b58-lb | 130.211.66.82 |
| plain-hc | Up / Up-to-date | gce-http-health-check [:80/] | n-588718b8099211e49d39b8e8560f8b58-plain-hc | |
| bootstrap | Up / Up-to-date | gce-image | n-588718b8099211e49d39b8e8560f8b58-bootstrap | |
| lb-net | Up / Up-to-date | gce-network [192.168.4.0/24] | n-588718b8099211e49d39b8e8560f8b58-lb-net | |
| backends | Up / Up-to-date | gce-target-pool [europe-west1] | n-588718b8099211e49d39b8e8560f8b58-backends | |
+-----------+-----------------+------------------------------------+----------------------------------------------+----------------+
</screen>
</para>
<para>The resources can be destroyed by running:
<screen>
$ nixops destroy -d load-balancer-gce
</screen>
This terminates the GCE instances and deletes the alternative GCE-based load balancer.</para>
<para>Deployment to GCE has some prerequisites.
<itemizedlist>
<listitem><para>Obviously, you need an GCE service account which you can create from the
<link xlink:href="https://console.developers.google.com/">Developer Console</link>.</para></listitem>
<listitem><para>Once you've created a new GCE service account and downloaded the generated
private key (in the PKCS12 format), you'll need to convert the key to PEM format by running
the following command:
<programlisting>
$ openssl pkcs12 -in pkey.pkcs12 -passin pass:notasecret -nodes -nocerts | openssl rsa -out pkey.pem
</programlisting>
</para></listitem>
<listitem><para>All GCE resources and instances must belong to a GCE project which you can create from the
<link xlink:href="https://console.developers.google.com/">Developer Console</link>. Alternatively,
you could use a project you already have. Several deployments can coexist in a single project and
with manually-created resources, as long as you don't exceed the quotas.</para></listitem>
<listitem><para>You must ensure that the GCE service account you've created has sufficient permissions
to manage resources in the project.</para></listitem>
<listitem><para>You must supply the credentials(project, service account name and path to the key)
via either <varname>*.project</varname>, <varname>*.serviceAccount</varname> and
<varname>*.accessKey</varname> options or <envar>GCE_PROJECT</envar>,
<envar>GCE_SERVICE_ACCOUNT</envar> and <envar>ACCESS_KEY_PATH</envar> environment variables.
Options take precedence over environment variables and are per-resource/-instance.
</para></listitem>
<listitem><para>You need to ensure that GCE firewall is configured correctly.
The <literal>default</literal> GCE network which is created for each project
and to which all instances belong by default, only allows SSH and internal traffic.
Usually, this is not enough and you want to create a network managed by NixOps with
custom firewall settings. By default, the NixOps-managed networks allow SSH traffic
because it is absolutely required to manage the instances. In addition to allowing
traffic based on IP and port ranges, firewall can also selectively enable traffic
for instances with specific tags, such as <literal>public-http</literal> in the
example, which is assigned to the instances you want to receive connections
on port 80.
</para></listitem>
<listitem><para>Many resources are region- and zone-specific, and thus you need
to set <varname>*.region</varname> options where applicable.</para></listitem>
</itemizedlist>
</para>
<para>GCE limitations and quirks to be aware of.
<itemizedlist>
<listitem><para>A bootstrap image needs to be created for each deployment because it
is impossible to create public images. Default bootstrap image specification can be
overriden by defining <literal>resources.gceImages.bootstrap</literal>. Additionally,
the instance's <literal>bootstrapImage</literal> option can be used to specify
an instance-specific bootstrap image.</para></listitem>
<listitem><para>There's no "native" support for starting and stopping instances.
NixOps emulates starting and stoping by creating and tearing down GCE instances,
but preserving the disk contents.</para>
<para>While this mostly just works, GCE ends up charging you a minimum of uptime
(which was 10 minutes at the moment of writing this manual) thus too frequent
start/stop cycling ends up expensive.</para>
<para>Start/stop cycling of an instance which uses an ephemeral IP address often causes
the IP address to change, which breaks certain features such as encrypted tunnels
until repaired by <literal>deploy</literal>.</para>
<para>Another important difference is that NixOps attempts to replicate the last known
state of the instance(attached disks, tags). Thus, if the state was modified
manually (e.g. via gcloud tool), such changes are lost in a start/stop cycle.</para>
<para>Consider rebooting instead which doesn't have these limitations and, in addition, is faster.
</para></listitem>
<listitem><para>Creation, modification and deletion of resources and instances are
not idempotent in GCE.</para>
<para>In practice, this means that if you hit Ctrl+C or an error happens, while NixOps is
creating, destroying or otherwise changing the state of a resource, the state of the
resource expected by NixOps and the actual state may diverge.</para>
<para>Usually, this doesn't cause too much trouble, but a good practice is to follow
each failed or aborted deployment operation with a <literal>deploy --check</literal>
run to detect and fix any state mismatch(es).</para></listitem>
<listitem><para>The instances which are members of target pools need a constantly-running
<literal>configure-forwarding-rules</literal> service, which is enabled by default, and
is not otherwise required.
Substantial RAM savings for a large deployment can be obtained by disabling the service
if it isn't needed.
</para></listitem>
</itemizedlist>
</para>
<para>Migration of resources between zones and putting previously-existing resources
under NixOps control.
<itemizedlist>
<listitem><para>Disks can be migrated by making a snapshot and then initializing
a new NixOps-managed disk from it, possibly, in another zone or region.</para></listitem>
<listitem><para>Migrating an instance to another zone via backup functionality
is currently impossible. It is still possible to create a new instance and migrate
each disk by hand using snapshots.</para></listitem>
<listitem><para>Putting a manually-created static IP resource under NixOps management
is done this way: create a resource to temporarily hold the IP address, such as an instance
or a forwarding rule; delete the static IP resource, which still leaves the IP address
itself under your control thanks to the holding resource; create a new static IP address
<literal>with resources.gceStaticIPs.$NAME.ipAddress</literal> set to the IP address of
the holding resource; delete the holding resource after checking that the static IP resource
has been correctly created and holds the original IP address.
<emphasis>You must practice the migration procedure on a test static IP resource.</emphasis></para>
<para>If by accident or after ignoring the above advice, you lose control of a valuable IP address,
you must act very fast and attempt to create a new static IP resource with
<literal>with resources.gceStaticIPs.$NAME.ipAddress</literal> set to the IP address itself
that you want to regain control over. If you are late and the IP address has been given to
someone else, it still makes sense to repeately try reserving the address because most likely
it is in use as an emphemeral one and thus will become available soon. Needless to say,
you want to avoid a situation like this at all costs.</para>
<para>IP addresses are region-specific and thus most likely can't be migrated to another region.
It is impossible to migrate an IP address to another project without temporarily
losing control over it.</para></listitem>
</itemizedlist>
</para>
</section>
<section xml:id="sec-deploying-to-azure"><title>Deploying to Microsoft Azure</title>
<para>Note: only ARM(Azure Resource Manager) mode is supported by this backend.</para>
<para><xref linkend="ex-physical-multi-azure.nix" /> shows a physical
specification that deploys the load balancer network to Azure along with
the absolute minimum of accessory resources that need to be created to
be able to deploy virtual machines.
It states that the three machines need to be instantiated in azure location
<literal>West US</literal>.
It also specifies an alternative load balancer implemented using a native Azure Load Balancer resource.
</para>
<example xml:id="ex-physical-multi-azure.nix">
<title><filename>load-balancer-azure.nix</filename>: Azure physical network specification</title>
<programlisting>
let
# change this as necessary or delete and use ENV vars
credentials = {
subscriptionId = "00000000-0000-0000-0000-000000000000";
authority = "https://login.windows.net/AUTHORITY.onmicrosoft.com";
user = "user@AUTHORITY.onmicrosoft.com";
password = "**********";
};
azure = { backendAddressPools ? [] }: { resources, ...}: {
deployment.targetEnv = "azure";
deployment.azure = credentials // {
location = "westus";
size = "Standard_A0"; # minimal size that supports load balancing
availabilitySet = resources.azureAvailabilitySets.set;
networkInterfaces.default.backendAddressPools = backendAddressPools;
};
};
azure_backend = {resources, ...}@args:
azure { backendAddressPools = [{loadBalancer = resources.azureLoadBalancers.lb;}]; } args;
in {
resources.azureReservedIPAddresses.lb-ip = credentials // {
location = "West US";
};
resources.azureAvailabilitySets.set = credentials // {
location = "westus";
};
resources.azureLoadBalancers.lb = {resources,...}: credentials // {
location = "westus";
frontendInterfaces.default.publicIpAddress = resources.azureReservedIPAddresses.lb-ip;
loadBalancingRules.web = {
frontendPort = 80;
backendPort = 80;
};
};
proxy = azure {};
backend1 = azure_backend;
backend2 = azure_backend;
}
</programlisting>
</example>
<para>
The deployment proceeds like this:
<screen>
$ nixops create ./load-balancer.nix ./load-balancer-azure.nix -d load-balancer-azure
$ nixops deploy -d load-balancer-azure
<replaceable>...</replaceable>
def-group....................> creating Azure resource group 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-def-group' in westus...
dn-westus....................> creating Azure virtual network 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-dn-westus' in westus...
set..........................> creating Azure availability set 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-set' in westus...
lb-ip........................> creating Azure reserved IP address 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-lb-ip' in West US...
def-storage-westus...........> creating Azure storage '71616e2ec165westus' in westus...
lb-ip........................> reserved IP address: 40.78.67.191
lb...........................> creating Azure load balancer 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-lb' in westus...
def-storage-westus...........> waiting for the storage to settle; this may take several minutes...
def-storage-westus...........> updating BLOB service properties of Azure storage '71616e2ec165westus'...
def-storage-westus...........> updating queue service properties of Azure storage '71616e2ec165westus'...
def-storage-westus...........> updating table service properties of Azure storage '71616e2ec165westus'...
def-storage-westus-vhds......> creating Azure BLOB container 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-vhds' in 71616e2ec165westus...
def-storage-westus-vhds-image> creating Azure BLOB 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-unstable-image.vhd' in nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-vhds...
def-storage-westus-vhds-image> updating properties of Azure BLOB 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-unstable-image.vhd'...
backend2.....................> getting an IP address
proxy........................> getting an IP address
backend1.....................> getting an IP address
backend2.....................> creating a network interface
backend1.....................> creating a network interface
proxy........................> creating a network interface
backend1.....................> creating Azure machine 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-backend1'...
backend2.....................> creating Azure machine 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-backend2'...
proxy........................> creating Azure machine 'nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-proxy'...
<replaceable>...</replaceable>
proxy......> activation finished successfully
backend2...> activation finished successfully
backend1...> activation finished successfully
</screen>
</para>
<para>Here NixOps has created a resource group, storage, container for blobs,
root image blob, availability set, load balancer and started three instances
running the default NixOS image. Other than that, deployment
is the same as for VirtualBox: NixOps builds the machine
configurations, copies their closure over to the Azure instances, and
activates the new configurations.</para>
<para>The command <command>nixops info</command> shows all provisioned
resources, not just machines:
<screen>
$ nixops info -d load-balancer-azure
<replaceable>...</replaceable>
+-------------------------------+-----------------+-------------------------------------+----------------------------------------------------------------+--------------+
| Name | Status | Type | Resource Id | IP address |
+-------------------------------+-----------------+-------------------------------------+----------------------------------------------------------------+--------------+
| backend1 | Up / Up-to-date | azure [westus; Standard_A0] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-backend1 | 40.78.60.145 |
| backend2 | Up / Up-to-date | azure [westus; Standard_A0] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-backend2 | 40.78.58.17 |
| proxy | Up / Up-to-date | azure [westus; Standard_A0] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-proxy | 40.78.59.32 |
| set | Up / Up-to-date | azure-availability-set [westus] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-set | |
| def-storage-westus-vhds-image | Up / Up-to-date | azure-blob | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-unstable-image.vhd | |
| def-storage-westus-vhds | Up / Up-to-date | azure-blob-container | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-vhds | |
| lb | Up / Up-to-date | azure-load-balancer [westus] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-lb | |
| lb-ip | Up / Up-to-date | azure-reserved-ip-address [West US] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-lb-ip | 40.78.67.191 |
| def-group | Up / Up-to-date | azure-resource-group [westus] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-def-group | |
| def-storage-westus | Up / Up-to-date | azure-storage [westus] | 71616e2ec165westus | |
| dn-westus | Up / Up-to-date | azure-virtual-network [westus] | nixops-71616e2e-c165-11e5-b910-b8e8560f8b58-dn-westus | |
+-------------------------------+-----------------+-------------------------------------+----------------------------------------------------------------+--------------+
</screen>
</para>
<para>Opening http://40.78.60.145, http://40.78.58.17, http://40.78.59.32,
or http://40.78.67.191 in a web browser should now show the Nixos homepage.
Also, you can log into any of the machines as <literal>root</literal>:
<screen>
$ nixops ssh -d load-balancer-azure backend1
connecting to 40.78.60.145...
[root@backend1:~]#
</screen>
</para>
<para>The resources can be destroyed by running:
<screen>
$ nixops destroy -d load-balancer-azure
</screen>
This terminates the Azure instances and deletes the alternative native load balancer.</para>
<section><title>Prequisites</title>
<itemizedlist>
<listitem><para>You need Azure credentials to authenticate requests.
The authentication methods supported are user/password and service principal/password.
You need to ensure your Azure account has an Active Directory, and add a user or service principal to it.
Refer to these guides:
<link xlink:href="http://innerdot.com/azure/a-gaffers-guide-to-azure-introduction-and-getting-started">
creating an active directory and users</link> and
<link xlink:href="http://innerdot.com/azure/a-gaffers-guide-to-azure-service-principals-and-applications">
applications and service principals</link>.
</para></listitem>
<listitem><para>You must supply the credentials(subscription ID, authority URL,
user or service principal, password) to your deployments via either
<literal>*.subscriptionId</literal>, <literal>*.authority</literal>, <literal>*.user</literal>,
<literal>*.servicePrincipal</literal> and <literal>*.password</literal>
options or <literal>AZURE_SUBSCRIPTION_ID</literal>, <literal>AZURE_AUTHORITY_URL</literal>,
<literal>AZURE_USER</literal>, <literal>AZURE_SERVICE_PRINCIPAL</literal>
and <literal>AZURE_PASSWORD</literal> environment variables.
Options take precedence over environment variables and are specified per resource/machine.
</para>
<para>You must specify either user or service principal but not both.
If either <literal>*.user</literal> or <literal>*.servicePrincipal</literal> is set,
both <literal>AZURE_USER</literal> and <literal>AZURE_SERVICE_PRINCIPAL</literal>
are ignored for the resource.</para>
<para>Example credentials for user/password authentication:
<programlisting>
credentials = {
subscriptionId = "00000000-0000-0000-0000-000000000000";
authority = "https://login.windows.net/YOURDIRECTORYNAME.onmicrosoft.com";
user = "user@YOURDIRECTORYNAME.onmicrosoft.com";
password = "**********";
};
</programlisting>
</para>
<para>Example credentials for service principal/password authentication:
<programlisting>
credentials = {
subscriptionId = "00000000-0000-0000-0000-000000000000";
authority = "https://login.windows.net/YOURDIRECTORYNAME.onmicrosoft.com";