Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Auto-scaling PHP applications using Chef and AWS

Andy Gale
February 12, 2014

Auto-scaling PHP applications using Chef and AWS

Given at PHPSW in Bristol on 12th February 2014.

Andy Gale

February 12, 2014
Tweet

More Decks by Andy Gale

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Auto-scaling Web Consultancy • Only pay for capacity we use

    • Handle huge traffic peaks • Automatically recover from failure
  4. • 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
  5. 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
  6. 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
  7. • 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
  8. 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
  9. 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
  10. Web Consultancy Port number, database name, file location etc Template

    A template which you create a file from inserting variables Attribute
  11. 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
  12. Web Consultancy Our template <VirtualHost *:80> ServerName <%= @params[:server_name] %>

    DocumentRoot <%= @params[:docroot] %> ! <Directory <%= @params[:docroot] %>> Options FollowSymLinks AllowOverride FileInfo Options AllowOverride All Order allow,deny Allow from all </Directory> ! <Directory /> Options FollowSymLinks AllowOverride None </Directory> ! </VirtualHost>
  13. 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
  14. Auto-scaling Web Consultancy • We want to auto-scale quickly •

    Our previous Chef and knife example takes 10 minutes to bootstrap our server
  15. 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
  16. 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
  17. When to auto-scale Web Consultancy ! • Test things out

    with CloudWatch alarms to begin with • Scale up quickly and scale down slowly
  18. Amazon OpsWorks Web Consultancy ! • Chef based • Lots

    of scaling configuration options including scaling up on a schedule