Ghost: Building an Instance
I publish journal notes of my cost-effective blog engineering challenge. This time I'll walk you through the cornerstone of my design - compute instance. Bear with me for a few more minutes, and I'll walk you through the chain of decision points to the final solution.
Once again, I start with my mantra: Containers are the most effective way to keep up with the latest software releases. Lucky me: Ghost community publishes Docker images and a few examples of a container-based deployment.
Since I need at least two containers (MySQL and Ghost), I might go with the Podman Compose (I use it with the WSL2 Linux), but Google has made this choice with the standard Container-Optimised OS image. It is a hardened image with a single purpose - safely and effectively running docker containers. Next stop - building the startup script.
Startup Scripts
In broad strokes, a new instance should perform the steps:
- Adjust the instance configuration
- Download the latest backup and configuration files
- Spin up a new container stack
For starters, the Container Optimized OS has Google API, and you can't install it because there is no package manager. It does not allow you to execute custom scripts or applications either. Fortunately, it offers a toolbox utility - containerized app that you can use to access Google Cloud Services. Another feature of this instance - OS takes about 5Gb of the storage device; the rest is mounted as stateful storage. What we can take and use: toolbox container mounts /var location by default, and my extra space for site and database is outside the root partition. There is another essential system update - disable Docker's daemon live restoration. So the first part of my startup script would look like this:
Now, the system is ready for the configuration files. Next, I need to restore: static content, the database dump file, and the stack configuration. With the toolbox in mind, my restoration commands are:
Please note that gsutil runs in the toolbox container, so the /var/ghost folder becomes a /media/root/var/ghost one. An extra step is to adjust my legacy database character set and collation configurations.
Now the whole scene is ready to start up the blog's clone. All I need to do is initialize the swarm and bring up the ghost stack. But before we go through, let's look at the docker stack description. It's very similar to the docker-compose with a few product-specific twists:
There are a few critical things to consider:
- Static content for the Ghost container is mounted as /var/lib/ghost/content. Respectfully, on our side, the content location is /var/ghost/content.
- Set your database password. The "example" one is for illustration purposes only, even though it's unavailable outside the stack.
- Database name 'ghost_dbatabase' is a part of the full database export.
- The /docker-entrypoint-initdb.d/ mapping is for the site content restoration. If you have some complex initial setup - put .sql, .gz, or .sh files, so MySQL will l run them as part of the database initialization.
- Ghost blog connection parameters are derived from MySQL container configuration.
- I used a port 80 mapping since it's only one click in the instance configuration.
When all the commands were executed without a scratch, I combined them in a single shell script and uploaded them to the same storage bucket. This way, I can use it as a startup-script-url parameter for my instance template.
Create an Instance Group Template
Since day one, I mean to use spot instances for my development environment. So let's start with saving startup and shutdown scripts on the same Google Store. You can use startup-script-url and shutdown-script-url attributes with the Google Store files. After some cost/performance estimations, I ended up with the regular, two vCPU instance with the 2GB RAM and the standard persistent disk (50GB is more than enough today). For development purposes, I use the e2-small VM with the container-optimized OS image for the spot instance.
To fit the strict budget limitations, my instance template:
- Uses the e2-small instance as the most suitable low-load shape for the task.
- Use the standard persistent device, not the balanced one (default and more expensive)
- Have a 50Gb allocated size instead of the default 10Gb
The next step is - LoadBalancer, firewall, and instance group configuration.
Previous articles in the series are: