Slide 1

Slide 1 text

Auto-scaling PHP applications using Chef and AWS Andy Gale Web Consultancy

Slide 2

Slide 2 text

About me Andy Gale Web Consultant Hello Future http://hellofutu.re ! @andygale @hellofutur3 Web Consultancy

Slide 3

Slide 3 text

Hosting Web Consultancy Virtual machine or cloud instance

Slide 4

Slide 4 text

Traffic Web Consultancy 0 1000000 2000000 3000000 4000000 00:00 01:00 02:00 03:00 04:00 05:00 06:00 07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 22:00 23:00 Visitors

Slide 5

Slide 5 text

Crash!

Slide 6

Slide 6 text

Hosting Web Consultancy Dedicated machine

Slide 7

Slide 7 text

Crash!

Slide 8

Slide 8 text

Separate Database and Web Web Consultancy

Slide 9

Slide 9 text

Crash!

Slide 10

Slide 10 text

Horizontal scaling Web Consultancy

Slide 11

Slide 11 text

Great success!

Slide 12

Slide 12 text

Traffic Web Consultancy 0 1000000 2000000 3000000 4000000 00:00 01:00 02:00 03:00 04:00 05:00 06:00 07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 22:00 23:00 Visitors

Slide 13

Slide 13 text

Web Consultancy

Slide 14

Slide 14 text

Auto-scaling Web Consultancy • Only pay for capacity we use • Handle huge traffic peaks • Automatically recover from failure

Slide 15

Slide 15 text

Web Consultancy When your traffic increases, our robot will automatically create a new web server for you.

Slide 16

Slide 16 text

• Cloud hosting provider • Various services • We’ll be using Elastic Load Balancers, EC2 (Elastic Compute Cloud) and RDS (Relational Database Service) What is AWS? Web Consultancy

Slide 17

Slide 17 text

Auto-scaling Web Consultancy • Create new web servers • Use ELB load balancer • Use Amazon RDS database which will in this example will need to be large enough to handle our traffic peaks

Slide 18

Slide 18 text

Auto-scaling Web Consultancy • We’ll need a way of setting up web servers • And we’ll need a way of installing our code on to them

Slide 19

Slide 19 text

• Configuration management tool • Install packages • Ensure services like Apache are running • Write out configuration files from templates • Restart services when templates change What is Chef? Web Consultancy

Slide 20

Slide 20 text

Web Consultancy Collections of recipes, templates and other configuration attributes to get stuff done. Chunks of Ruby code that describe what stuff we need to do to get that stuff done. Cookbooks Recipes

Slide 21

Slide 21 text

Web Consultancy A virtual machine, cloud server or dedicate server Resource Creates a user, expands a template, installs a package, starts a service etc Node

Slide 22

Slide 22 text

Web Consultancy Port number, database name, file location etc Template A template which you create a file from inserting variables Attribute

Slide 23

Slide 23 text

Web Consultancy package "ntp" do action :install end ! template "/etc/ntp.conf" do source "ntp.conf.erb" owner "root" group "root" mode 0644 notifies :restart, resources(:service => "ntp") end ! service "ntp" do action :start end Install package Create config file from template Define service and start NTP Restart service when config file changes

Slide 24

Slide 24 text

Web Consultancy Let’s make a cookbook to install our PHP application

Slide 25

Slide 25 text

Web Consultancy

Slide 26

Slide 26 text

Web Consultancy Our template ServerName <%= @params[:server_name] %> DocumentRoot <%= @params[:docroot] %> ! > Options FollowSymLinks AllowOverride FileInfo Options AllowOverride All Order allow,deny Allow from all ! Options FollowSymLinks AllowOverride None !

Slide 27

Slide 27 text

Web Consultancy include_recipe "apache2" include_recipe "php" include_recipe "php::module_mysql" include_recipe "apache2::mod_php5" Our recipe

Slide 28

Slide 28 text

Web Consultancy web_app node['phpapp']['host'] do template "phpapp.conf.erb" docroot node['phpapp']['dir'] server_name node['phpapp']['host'] end Our recipe

Slide 29

Slide 29 text

Web Consultancy gettingstartedwithchef.com

Slide 30

Slide 30 text

Web Consultancy $ knife ec2 server create --run-list "role[phpapp]" -I ami-3d160149 --region eu-west-1 -G www,default -x ubuntu -- node-name server01 --identity-file ~/.ssh/sshkey.pem Create EC2 instance

Slide 31

Slide 31 text

Auto-scaling Web Consultancy • We want to auto-scale quickly • Our previous Chef and knife example takes 10 minutes to bootstrap our server

Slide 32

Slide 32 text

Auto-scaling Web Consultancy https://github.com/hellofuture-cookbooks/hf-lamp hf-lamp • Different recipes for installing dependencies and setting up you application configuration

Slide 33

Slide 33 text

Web Consultancy

Slide 34

Slide 34 text

Web Consultancy include_recipe "apache2" include_recipe "php" include_recipe "php::module_mysql" include_recipe "apache2::mod_php5" Auto-scaling

Slide 35

Slide 35 text

Auto-scaling Web Consultancy • chef-solo to create an instance with dependencies installed • Create an AMI image from that instance • Use that instance in you auto-scaling launch configuration

Slide 36

Slide 36 text

Web Consultancy

Slide 37

Slide 37 text

Web Consultancy

Slide 38

Slide 38 text

Web Consultancy

Slide 39

Slide 39 text

Web Consultancy

Slide 40

Slide 40 text

Web Consultancy

Slide 41

Slide 41 text

Web Consultancy

Slide 42

Slide 42 text

Web Consultancy

Slide 43

Slide 43 text

Web Consultancy

Slide 44

Slide 44 text

Web Consultancy

Slide 45

Slide 45 text

When to auto-scale Web Consultancy ! • Create CloudWatch alarms on key metrics such as CPU load • Scale up when a high percentage of total of the metric is being used • Scale down when low percentage of the metric is being used

Slide 46

Slide 46 text

When to auto-scale Web Consultancy ! • Test things out with CloudWatch alarms to begin with • Scale up quickly and scale down slowly

Slide 47

Slide 47 text

Amazon OpsWorks Web Consultancy ! • Chef based • Lots of scaling configuration options including scaling up on a schedule

Slide 48

Slide 48 text

Questions? Web Consultancy