Craft in K8S

Dot All 2022

Who dis?

Matt Gray

Founder of Servd

Formerly, Craft CMS Developer

K8s

=

Kubernetes

Craft in K8s

Why Should I Care?

Enterprise adoption

75%

Are Using K8s

Up from 30% in 2019

Gartner Research 2022

400%

Increase in K8s Job Postings

Since 2020

Projects for Organisations will increasingly be deployed in-house K8s clusters.

Craft in K8s

Why Should I Care?

Managed Solutions

Access to K8s is getting easier

🤔

Craft in K8s

Why Should I Care?

Best Practices

K8s (tries to) force the use of best practices in application design

From 12 Factor Apps:

  • Environment stored config
  • External backing services
  • Build, release, run
  • Stateless processes
  • Port binding
  • Disposability
  • Log event streams

Why So Popular?

Back in 2019, Docker started to become widely adopted for packaging and running applications.

Everything started getting containerised.

Why So Popular?

Enterprises were no exception.

Large companies had thousands of apps packaged as containers.

Why So Popular?

K8s acts as an abstraction over server resources.

Scheduling - Networking - Storage - Resources
Scaling (servers and containers) - Deployments - Server Upgrades (kinda)

1. Containers

K8s organises containers.

Our first step is to make sure we use them.

Then export them as 'images'.

2. Ephemeral Filesystems

Containers have their own filesystems.

Containers restart often.

Each time, their filesystem resets to match the state.

Anything we write to the filesystem at run-time will be lost, unless we prepare for it.

  1. Don't write anything you need to keep to the filesystem
  2. Use a Persistent Volume - but not if you want to be able to horizontally scale

Outside of Assets, Craft is kind to us.

It has been built to work well in this type of environment.

Just set `allowUpdates => false`.

Plugins... not so much.

3. cpresources

The one assumption Craft/Yii does make...

That both PHP and nginx are using the same filesystem and can share cpresources files.

?

- admin-cp.js

web/cpresources/

web/cpresources/

3. cpresources

The one assumption Craft/Yii does make...

That both PHP and nginx are using the same filesystem and can share cpresources files.

?

- admin-cp.js

web/cpresources/

web/cpresources/

3. cpresources

The one assumption Craft/Yii does make...

That both PHP and nginx are using the same filesystem and can share cpresources files.

- admin-cp.js

web/cpresources/

web/cpresources/

Pod

EmptyDir Volume (Ephemeral)

4. Logs

Remember ephemeral filesystems? Things on the filesystem don't last long!

Logs should go to stdout or stderr.

K8s will collect and organise them.

The k8s ops team might provide some way for you to export or access them.

CRAFT_STREAM_LOG=true

5. Sessions and Cache

Craft's default cache and session handlers use the filesystem.

Uh oh, ephemeral filesystems + load balancing...

Super easy to deploy to k8s.

Super easy to configure in Craft.

But watch out for a yii2-redis bug that we ran into:

https://servd.host/blog/craft-yii-and-redis-session-absentee

6. Image Resizing

When deploying to k8s we need to define our expected CPU and memory usages, so k8s can schedule our containers.

Processing images using ImageMagick or GD requires a lot of memory!

Save yourself a headache. Use an external service: Imgix, ServerlessSharp

We have two options:

  1. Set resources to match what we expect PHP to use.
    K8s will kill the container when it processes an image and uses much more than expected.
  2. Set resources to match the max required to resize an image.
    K8s will reserve way more resources than the application is likely to require. Especially when scaling horizontally. Ops team unhappy :(

In conclusion

Running Craft on k8s takes a bit of planning.

But it's do-able.

  1. Build your containers
  2. Plan and test for an ephemeral filesystem
  3. Use a pod + localdir volume for cpresources
  4. Logs go to stdout
  5. Redis for sessions and caches
  6. Try not to resize images directly within containers

Now you're ready to deploy, scale, and play nicely with all the benefits k8s offers.

le fin

Questions / Comments?

#craft-in-k8s

 

matt@servd.host

Craft in k8s

By Matt

Craft in k8s

  • 401