Slide 1

Slide 1 text

Immutable Infrastructure at Scale Vik Bhatti Senior Platform Engineer @otaku_coder

Slide 2

Slide 2 text

“Immutable infrastructure is comprised of immutable components that are replaced for every deployment, rather than being updated in-place.” What is immutable infrastructure? Florian Motlik CTO @codeship blog.codeship.com/immutable-infrastructure

Slide 3

Slide 3 text

An immutable infrastructure is powerful

Slide 4

Slide 4 text

But difficult to run without the right tooling

Slide 5

Slide 5 text

Why Ansible? Masterless User Experience

Slide 6

Slide 6 text

A Brief History Zeebox • Smart TV Remote • Social Network Beamly • Content network • Ad Tech • Acquired Oct 2015

Slide 7

Slide 7 text

Live call-to-action

Slide 8

Slide 8 text

But…

Slide 9

Slide 9 text

Cue frantic debugging…

Slide 10

Slide 10 text

How Do We Scale Faster? Autoscaling

Slide 11

Slide 11 text

Configure on start-up Auto Scaling group Amazon S3 bucket ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance

Slide 12

Slide 12 text

Configure on start-up EC2 Instance Auto Scaling group Amazon S3 bucket ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance

Slide 13

Slide 13 text

Configure on start-up EC2 Instance Auto Scaling group Amazon S3 bucket ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance Pull down ansible Playbook on boot (or use ansible tower)

Slide 14

Slide 14 text

Configure on start-up EC2 Instance Auto Scaling group Amazon S3 bucket ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance

Slide 15

Slide 15 text

Preconfigure on image build Auto Scaling group ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance

Slide 16

Slide 16 text

Preconfigure on image build Auto Scaling group ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance Ansible play to bundle all requirements into the image

Slide 17

Slide 17 text

Preconfigure on image build Auto Scaling group ansiblerocks.com Elastic Load Balancer Amazon Route 53 Base OS Machine Image EC2 Instance EC2 Instance EC2 Instance

Slide 18

Slide 18 text

Build Pipeline Build Artifact Provision Image Chroot Install Component Artifact Snapshot Machine Image Test Machine Image Deploy New Image SCM

Slide 19

Slide 19 text

Packer by Hashicorp Building Machine Images

Slide 20

Slide 20 text

Builders create machines and generate images { ‘builders’: [ ‘name’: ‘build-aws-hvm’, ‘type’: ‘amazon-chroot’, ‘source_ami’: ‘ami-1234abcd’, ‘ami_name’: ‘ansiblefest-2016-v1’, ‘ami_virtualization_type’: ‘hvm’, ‘tags’: { ‘component’: ‘ansiblefest’, ‘version’: ‘1.0’ } ] }

Slide 21

Slide 21 text

Provisioners define how to install dependencies { ‘provisioners’: [ ‘type’: ‘ansible-local’, ‘playbook_file’: ‘/tmp/myplay.yml’, ‘extra_arguments’: [‘--extra-vars component=ansiblefest’] ] }

Slide 22

Slide 22 text

Packer takes a template file and executes the build $ packer build –machine-readable /tmp/mytemplate.json

Slide 23

Slide 23 text

Packer build – Runs builders in parallel 12:41:13.168 1453293673,,ui,say,==> hvm: Prevalidating AMI Name... 12:41:13.871 1453293673,,ui,say,==> hvm: Gathering information about this EC2 instance... 12:41:13.922 1453293673,,ui,say,==> paravirtual: Gathering information about this EC2 instance... 12:41:13.983 1453293673,,ui,say,==> hvm: Inspecting the source AMI... 12:41:14.000 1453293673,,ui,say,==> paravirtual: Inspecting the source AMI... 12:41:14.052 1453293674,,ui,say,==> paravirtual: Checking the root device on source AMI... 12:41:14.053 1453293674,,ui,say,==> paravirtual: Creating the root volume... 12:41:14.054 1453293674,,ui,say,==> hvm: Checking the root device on source AMI... 12:41:18.399 1453293678,,ui,say,==> paravirtual: Attaching the root volume to /dev/sdf 12:41:21.182 1453293681,,ui,say,==> hvm: Creating the root volume... 12:41:21.183 1453293681,,ui,say,==> paravirtual: Mounting the root device... 12:41:21.296 1453293681,,ui,say,==> paravirtual: Mounting additional paths within the chroot... 12:41:21.497 1453293681,,ui,message, paravirtual: Mounting: /proc 12:41:21.512 1453293681,,ui,message, paravirtual: Mounting: /sys 12:41:21.570 1453293681,,ui,message, paravirtual: Mounting: /dev 12:41:21.577 1453293681,,ui,message, paravirtual: Mounting: /dev/pts 12:41:21.586 1453293681,,ui,message, paravirtual: Mounting: /proc/sys/fs/binfmt_misc 12:41:21.594 1453293681,,ui,say,==> paravirtual: Copying files from host to chroot... 12:41:21.594 1453293681,,ui,message, paravirtual: /etc/resolv.conf 12:41:21.733 1453293681,,ui,say,==> paravirtual: Provisioning with shell script: /tmp/packer-shell284329197 12:41:25.585 1453293685,,ui,say,==> hvm: Attaching the root volume to /dev/sdh 12:41:30.412 1453293690,,ui,say,==> hvm: Mounting the root device... 12:41:30.615 1453293690,,ui,say,==> hvm: Mounting additional paths within the chroot...

Slide 24

Slide 24 text

Gotcha – AWS chroot Builder Ansible tasks must not leave any processes running, or packer can’t unmount the volume. Remove handler and notify calls from galaxy tasks

Slide 25

Slide 25 text

How do we update service config? Configuration

Slide 26

Slide 26 text

v1 - Set config file to use on boot 1. Write multiple configuration files • For each environment/region 2. Inspect metadata on boot and use the matching config file

Slide 27

Slide 27 text

Use with_items to write multiple config files - name: write config files template: src: myconfig.j2 dest: /etc/{{ item.country }}-{{ item.environment }}-conf.json owner: root mode: 0644 with_items: - { country: ‘uk’, environment: ‘stage’ } - { country: ‘uk’, environment: ‘live’ } - { country: ‘us’, environment: ‘stage’ } - { country: ‘us’, environment: ‘live’ }

Slide 28

Slide 28 text

v2 – Use Service Discovery/Config KV 1. Local agent watches for changes in KV values 2. Writes new config to disk on change 3. Restarts corresponding service Note: config value changes != template changes

Slide 29

Slide 29 text

Machine images should be the same regardless of where they are deployed

Slide 30

Slide 30 text

What about system state? Sidenote

Slide 31

Slide 31 text

State is hard …but can be managed and encapsulated

Slide 32

Slide 32 text

Stateful ‘things’ • Logs • Metrics • Data Storage/Databases • Caches • Filesystem • DNS

Slide 33

Slide 33 text

Conclusions A few things to take away

Slide 34

Slide 34 text

Immutable infrastructure is complex at first

Slide 35

Slide 35 text

But wicked fast to scale up and down when all the tools are in place

Slide 36

Slide 36 text

Immutable infrastructure simplifies change management

Slide 37

Slide 37 text

Packer is great for machine image pipelines

Slide 38

Slide 38 text

Ansible can glue all the pieces together

Slide 39

Slide 39 text

Thanks for listening Questions?

Slide 40

Slide 40 text

LONDON Drury House 34-43 Russell Street London WC2B 5HA NEW YORK CITY 350 Fifth Avenue Suite 1700 New York NY 10018 © Beamly 2016. All Rights Reserved.