This is a demo showing how one could use ZFS snapshots to do blue / green deployments for a Drupal website. In this setup, instead of having a "stage" and "production" environment, two identical "production" environments are set up and swapped between on each deployment. While there is an unavoidable delay in production deployments to sync environments, rollbacks become near-instant. ZFS snapshots are block level and incremental, so they should be much faster than rsync or other tools that have to walk the whole filesystem. This is especially noticeable with large image libraries uploaded into Drupal.
- You'll need Vagrant and Virtualbox to run the virtual machines.
- Run
vagrant up
and wait a few minutes as blue and green are initially set up. - Run
vagrant provision
again for blue to complete setup now that green is running. - Browse to http://blue.local and you should see the Umami demo profile with environment indicator.
- Browse to http://green.local and you'll see an error because the first deployment to green hasn't run yet.
- Run
vagrant ssh blue
, thensudo -i
, and finally/vagrant/deploy.sh
to deploy to green. - Browse to http://green.local and you'll see the Drupal site but in the green environment.
- Log in and make some content changes in green. The site code is in
/var/www/site
along with drush to rundrush -l green.local user:login
. - Run
vagrant ssh green
,sudo -i
,/vagrant/deploy.sh
to send Green's content to blue. - Browse to http://blue.local and you'll see your changes.
See deploy.sh for line-by-line details. In summary:
- A new snapshot of the database and files directory is created.
- mariadb is stopped on the destination environment to close any open database files.
zfs send
is used to send the snapshot to the other environment overssh
.zfs recv
is used to apply the snapshot. Any local changes in the active filesystem are reverted.- mariadb is started again on the destination environment.
- The actual site code isn't synced between the environments. In practice this is not the hard part of blue / green deployments, and leaving this "by hand" leaves the opportunity to try out failure scenarios.
- Likewise,
drush updatedb
/cache:rebuild
are not called automatically. - Read-only mode during deployments.
- A front-end proxy to transparently redirect HTTP requests to the active environment. This can be done with Varnish or a CDN like Fastly.
- Snapshot cleanup. There may not actually be much value in this, given that most Drupal sites are constantly adding, and not deleting content. Note that there can start to be performance issues when listing thousands of ZFS snapshots, so considering this in production is important.