$30 off During Our Annual Pro Sale. View Details »

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. Auto-scaling PHP applications using Chef and AWS Andy Gale Web

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

    @andygale @hellofutur3 Web Consultancy
  3. Hosting Web Consultancy Virtual machine or cloud instance

  4. 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
  5. Crash!

  6. Hosting Web Consultancy Dedicated machine

  7. Crash!

  8. Separate Database and Web Web Consultancy

  9. Crash!

  10. Horizontal scaling Web Consultancy

  11. Great success!

  12. 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
  13. Web Consultancy

  14. Auto-scaling Web Consultancy • Only pay for capacity we use

    • Handle huge traffic peaks • Automatically recover from failure
  15. Web Consultancy When your traffic increases, our robot will automatically

    create a new web server for you.
  16. • 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
  17. 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
  18. 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
  19. • 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
  20. 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
  21. 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
  22. Web Consultancy Port number, database name, file location etc Template

    A template which you create a file from inserting variables Attribute
  23. 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
  24. Web Consultancy Let’s make a cookbook to install our PHP

    application
  25. Web Consultancy

  26. 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>
  27. Web Consultancy include_recipe "apache2" include_recipe "php" include_recipe "php::module_mysql" include_recipe "apache2::mod_php5"

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

    node['phpapp']['host'] end Our recipe
  29. Web Consultancy gettingstartedwithchef.com

  30. 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
  31. Auto-scaling Web Consultancy • We want to auto-scale quickly •

    Our previous Chef and knife example takes 10 minutes to bootstrap our server
  32. Auto-scaling Web Consultancy https://github.com/hellofuture-cookbooks/hf-lamp hf-lamp • Different recipes for installing

    dependencies and setting up you application configuration
  33. Web Consultancy

  34. Web Consultancy include_recipe "apache2" include_recipe "php" include_recipe "php::module_mysql" include_recipe "apache2::mod_php5"

    Auto-scaling
  35. 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
  36. Web Consultancy

  37. Web Consultancy

  38. Web Consultancy

  39. Web Consultancy

  40. Web Consultancy

  41. Web Consultancy

  42. Web Consultancy

  43. Web Consultancy

  44. Web Consultancy

  45. 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
  46. When to auto-scale Web Consultancy ! • Test things out

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

    of scaling configuration options including scaling up on a schedule
  48. Questions? Web Consultancy