-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
797 lines (683 loc) · 31.9 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Deploying with Docker | William Durand</title>
<link rel="stylesheet" href="reveal.js/css/reveal.min.css">
<link rel="stylesheet" href="css/theme.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="fontawesome/css/font-awesome.min.css">
<link rel="stylesheet" href="css/custom.css">
<script>
if (window.location.search.match(/print-pdf/gi)) {
document.write('<link rel="stylesheet" href="css/pdf.css" type="text/css">');
}
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<img src="images/symfony-tn.png" id="logo">
<div class="reveal">
<div class="slides">
<section>
<h2>Deploying with<img src="images/docker.png" class="no-border"></h2>
<p><small><em>William Durand - April 17th, 2015</em></small></p>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Docker In A Nutshell
</script>
</section>
<section data-markdown>
<script type="text/template">
### Containers
<br>
A **container** is an **operating-system level
virtualization** method that provides a completely
**isolated environment**, simulating a closed
system running on a single host.
<br>
_chroot, OpenVZ, Jail, LXC, etc._
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### Containers vs VMs
<br>
![](images/container_vs_vm.jpg)
</script>
</section>
<section data-markdown>
<script type="text/template">
### Docker
<br>
* Developed by Docker.inc (formerly DotCloud)
* Open platform for developing, shipping,<br>and running applications
* Use [libcontainer](http://blog.docker.com/2014/03/docker-0-9-introducing-execution-drivers-and-libcontainer/)
as execution driver
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### Docker's Architecture
<br>
![](images/architecture.svg)
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### [Try It!](https://www.docker.com/tryit/)
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
## The Plan
<br>
* **Simple** solution to deploy my application
* **Idempotent** and **reliable** process
* **No** need for rollbacks
* Targetting **zero** downtime
* Single host
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Methodology
</script>
</section>
<section data-markdown>
<script type="text/template">
### Twelve Factors
<br>
* **One codebase** tracked in revision control,<br>many deploys
* Store **config in the environment**
* Keep development, staging, and production<br>as **similar** as possible
<br>
<small>
[12factor.net](http://12factor.net/)
</small>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Infrastructure
</script>
</section>
<section data-markdown>
<script type="text/template">
### Services
<br>
* Reverse proxy: [Hipache](https://github.com/hipache/hipache) (+ [Redis](http://redis.io/))
* Backend: [Nginx](http://nginx.org/) + [HHVM](http://hhvm.com/) + **application**
* Sessions are stored into [memcached](http://memcached.org/)
* Database (MySQL)
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### Overall Architecture
<br>
![](images/architecture-sl.png)
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### Application's `fig.yml`
<br>
``` yaml
web:
build: .
# No support for `env-file` yet...
# See: https://github.com/docker/fig/pull/665
environment:
- SYMFONY_ENV=dev
- SYMFONY_DEBUG=1
- SYMFONY__MEMCACHED_HOST=memcached
- SYMFONY__MEMCACHED_PORT=11211
- SYMFONY__SESSION_MEMCACHED_PREFIX=session
- SYMFONY__SESSION_MEMCACHED_EXPIRE=604800
links:
- memcached
- database
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### Data Only Containers
<br>
Data, logs, etc. MUST live in **data only containers**:
``` yaml
# fig.yml
redis:
image: redis
volumes_from:
- dataredis
dataredis:
image: busybox
volumes:
- /data
```
Wanna backup? <i class="fa fa-smile-o"></i>
```
docker run --rm \
--volumes-from dataredis \
-v $(pwd):/backup \
busybox \
tar cvf /backup/dataredis.tgz /data
```
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### Application's `Dockerfile`
<br>
```
FROM debian:wheezy
MAINTAINER William Durand <[email protected]>
ENV DEBIAN_FRONTEND noninteractive
# Let APT know about HHVM repo...
RUN apt-get update -y
RUN apt-get install -y nginx hhvm supervisor
# Configure Nginx, HHVM, Supervisor here...
ADD . /app
WORKDIR /app
EXPOSE 80
CMD [ "/usr/bin/supervisord" ]
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### DNS
<br>
`example.org` <i class="fa fa-arrow-right"></i> `www.example.org`
<br>
Configured at the **DNS** level. Faster than making
<br>this redirection using the load balancer.
</script>
</section>
<section data-markdown>
<script type="text/template">
### SSL
<br>
Hipache listens on ports `80` and `443`.
<br>
Patched version of Hipache to redirect<br>HTTP traffic to HTTPS.
Sort of SSL offloading.
<br>
<i class="fa fa-github"></i> [willdurand/docker-hipache](https://github.com/willdurand/docker-hipache)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Deploy
</script>
</section>
<section data-markdown>
<script type="text/template">
### 1. Build Artifact
<br>
```
git archive --format tar --prefix myapp/ <SHA> | (cd /tmp && tar xf -)
```
<br>
**Protip:** use a Docker container to run [build tools](https://github.com/willdurand/docker-buildtools):
```
cd /tmp/myapp
docker run --rm -v `pwd`:/srv buildtools composer install
docker run --rm -v `pwd`:/srv buildtools bundle install
docker run --rm -v `pwd`:/srv buildtools bower install
rm <USELESS/TEMP FILES>
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 2. Build Container (From Artifact)
<br>
```
docker build -t myapp:latest .
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 3. Push It
<br>
```
docker push
```
<br>
<i class="fa fa-info-circle"></i> Or whatever that makes the image
<br>available on the production server.
</script>
</section>
<section data-markdown>
<script type="text/template">
### 4. Retrieve Running Containers
<br>
```
RUNNING_CIDS=$(docker ps | grep web | cut -d ' ' -f 1)
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 5. Run The New Container
<br>
```
fig run -d --no-deps --rm web
```
<br>
<i class="fa fa-warning"></i> Booting the container may take a few seconds, wait.
</script>
</section>
<section data-markdown>
<script type="text/template">
### 6. Register It As Backend
<br>
```
IP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' <CID>)
```
```
fig run --rm rediscli rpush frontend:www.example.com "http://$IP:80"
```
<br>
```
# fig.yml
rediscli:
image: redis
entrypoint: [ redis-cli, -h, redis ]
links:
- redis
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 7. Unregister Previous Backends
<br>
For each container id `CID` in `$RUNNING_CIDS`, do:
```
IP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' <CID>)
fig run --rm rediscli lrem frontend:www.example.com 0 "http://$IP:80"
```
<br>
<i class="fa fa-warning"></i> New backend is up and serves requests. However,
previous backends may still process requests. Wait.
</script>
</section>
<section data-markdown>
<script type="text/template">
### 8. Stop Previous Containers
<br>
For each container id `CID` in `$RUNNING_CIDS`, do:
```
docker stop <CID>
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 9. Tag A Release (And Profit!)
<br>
![](images/profit.gif)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Database Migrations
</script>
</section>
<section data-markdown>
<script type="text/template">
### This Is *My* Vision
<br>
* Use [Phinx](https://phinx.org/) (or
[Flyway](http://flywaydb.org/)) to write your
migration scripts
* Put you migration scripts **under version control**
* **Test** your migration scripts
* Applying your migration scripts should be done
asynchronously (i.e. not tied to a deployment)
* This is *tricky* (i.e. not easy)
* **You need a plan**!
</script>
</section>
<section data-markdown>
<script type="text/template">
### Adding A New Column/Table
<br>
1. Prepare your migration **before** modifying your code
2. Apply your migration
3. Update your code
</script>
</section>
<section data-markdown>
<script type="text/template">
### Modifying A Column Type
<br>
1. Create a new column with the new type as a migration
2. Apply your migration
3. Create a migration to populate the new column
4. Apply your second migration
5. Update your code to use this new column (your code
should deal with both columns)
6. Rename the first column as "deprecated", and the new
one using the original name
7. Remove hacks introduced in 5
</script>
</section>
<section data-markdown>
<script type="text/template">
### Renaming A Column/Table
<br>
1. Create a new column or table with the new name
rather than "renaming" it
2. Apply your migration
3. Update your code to use the new column/table *but*
keep using the former
4. Create a migration to populate the new column/table
5. Apply your migration
6. Update your code to use the new column/table
6. Rename the former column/table as "deprecated"
7. Remove hacks introduced in 3
</script>
</section>
<section data-markdown>
<script type="text/template">
### Deleting A Column/Table
<br>
Rename it rather than deleting it. Wait for a
**maintenance party** to delete `*_deprecated`
<br>columns and/or tables.
<br>
**Protip:** when marking a column/table as
**deprecated**, add the date too:
`_deprecated_2015_01_01`.
</script>
</section>
<section data-markdown>
<script type="text/template">
### Making Changes On The Data
<br>
Avoid it as long as you can, but
sometimes,<br>there are no other alternatives.
This implies a **maintenance** mode: **be careful!**
<br>
1. Put your service in maintenance mode
2. Apply your changes
3. Put your service back online
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Benchmarking
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
![](images/boom.png)
Like [Apache Bench](http://httpd.apache.org/docs/2.2/programs/ab.html), but **better!**
<br>
<i class="fa fa-github"></i> [tarekziade/boom](https://github.com/tarekziade/boom)
</script>
</section>
<section data-markdown>
<script type="text/template">
### 1. Run `boom`
<br>
```
boom https://www.example.org/ -n 2000 -c 50
Server Software: nginx/1.2.1
Running GET https://www.example.org/
Running 2000 times per 50 workers.
[====================================================>.] 99% Done
-------- Results --------
Successful calls 2000
Total time 137.1827 s
Average 6.7114 s
Fastest 2.3701 s
Slowest 9.4738 s
Amplitude 7.1037 s
-------- Status codes --------
Code 200 2000 times.
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 2. Deploy
<br>
```
fab deploy --hide=running,stdout,stderr
---> Deploying branch "awesome-feature"
---> Building artifact
---> Artifact id is: "build-8fe47fc"
---> Pushing artifact to the server
---> Building container
---> Finding running containers
---> Starting new container
---> Waiting while container is booting
---> Registering new backend to Hipache
---> Unregistering the previous backends from Hipache
---> Waiting before stopping the previous backends
Done.
```
<br>
<i class="fa fa-info-circle"></i> I use [Fabric](http://www.fabfile.org/).
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
### 3. Monitor
<br>
**E**lasticsearch, **L**ogstash, **K**ibana
<br>+<br>
Logstash Forwarder (_lumberjack_)
<br>
<i class="fa fa-github"></i> [willdurand/docker-elk](https://github.com/willdurand/docker-elk)
<i class="fa fa-github"></i> [willdurand/docker-logstash-forwarder](https://github.com/willdurand/docker-logstash-forwarder)
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
![](images/kibana.png)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Testing
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
![](images/logo-wercker.png)
</script>
</section>
<section data-markdown>
<script type="text/template">
### `wercker.yml`
<br>
```
box: wercker-labs/docker
build:
steps:
- script:
name: install Fig
code: |
curl -L https://.../fig-`uname -s`-`uname -m` > fig ; chmod +x fig
sudo mv fig /usr/local/bin/fig
- ...
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 1. Build Artifact
<br>
```
- script:
name: build artifact
code: |
FAB_DIST_DIR="$WERCKER_OUTPUT_DIR" \
bin/fabric build:commit="$WERCKER_GIT_COMMIT"
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 2. Build a Docker Image
<br>
```
- script:
name: build image
code: |
cd "$WERCKER_OUTPUT_DIR/build-$WERCKER_GIT_COMMIT"
fig -f "$WERCKER_SOURCE_DIR/fig.yml.dist" build web
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 3. Run a Container
<br>
```
- script:
name: run container
code: |
cd "$WERCKER_OUTPUT_DIR/build-$WERCKER_GIT_COMMIT"
fig -f "$WERCKER_SOURCE_DIR/fig.yml.dist" run -d web
sleep 20
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### 4. Run Tests \o/
<br>
```
- script:
name: run test suite
code: |
export IP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}'\
"$(docker ps -q | head -n 1)")
echo "IP address is: $IP" | tee "$WERCKER_REPORT_MESSAGE_FILE"
bin/test
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### Smoke Testing
<br>
``` ruby
require "httparty"
describe "The web server" do
before(:all) do
ip = ENV["IP"] or 'localhost'
@response = HTTParty.get(
"http://#{ip}:80",
:basic_auth => { :username => "privatedemo", :password => "please" }
)
end
it "should return a 200 status code" do
@response.code.should == 200
end
end
```
[RSpec](http://rspec.info/) + [httparty](https://github.com/jnunemaker/httparty) = ♥
</script>
</section>
<section data-markdown class="no-border">
<script type="text/template">
![](images/wercker.png)
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
## Thank You.
### Questions?
<br>
<br>
<br>
<ul class="me">
<!--
<li>
<p><i class="fa fa-comments-o"></i><a href="https://joind.in/XXX" title="joindin">joind.in/XXX</a></p>
</li>
-->
<li>
<p><i class="fa fa-globe"></i><a href="http://williamdurand.fr" title="website">williamdurand.fr</a></p>
</li>
<li>
<p><i class="fa fa-github"></i><a href="https://github.com/willdurand" title="github">github.com/willdurand</a></p>
</li>
<li>
<p><i class="fa fa-twitter"></i><a href="https://twitter.com/couac" title="twitter">twitter.com/couac</a></p>
</li>
</ul>
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Bonus
</script>
</section>
<section data-markdown>
<script type="text/template">
### Fig & Environments
<br>
> Keep development, staging, and production as **similar** as possible<br><small><br>X. Dev/prod parity — 12 Factors</small>
<br>
``` yaml
# fig.yml.dist
web:
# In PRODUCTION, the line below MUST be COMMENTED:
build: .
# In PRODUCTION, the line below must be UNCOMMENTED:
# image: myapp
# ...
```
</script>
</section>
</section>
</div>
</div>
<script type="text/javascript" src="reveal.js/lib/js/head.min.js"></script>
<script type="text/javascript" src="reveal.js/js/reveal.min.js"></script>
<script type="text/javascript">
Reveal.initialize({
slideNumber: !window.location.search.match(/print-pdf/gi),
history: true,
theme: Reveal.getQueryHash().theme,
transition: Reveal.getQueryHash().transition || 'concave',
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'js/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
]
});
</script>
</body>
</html>