Matt
Completely unqualified to be an entrepreneur. But it's the taking part that counts, right?
Dot All 2022
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
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:
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.
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:
In conclusion
Running Craft on k8s takes a bit of planning.
But it's do-able.
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
By Matt