
Strapi is a headless Content Management System (CMS) that enables users to create and manage content across websites, web apps, and mobile platforms. It features an intuitive admin dashboard, enabling users to add, edit, or delete content without technical expertise. Strapi also allows users to define custom content types and data structures, offering greater flexibility in content organization. With seamless integration capabilities, Strapi integrates with front-end frameworks, databases, and third-party services, making it a versatile choice for modern content management.
Strapi is designed to handle large volumes of content and efficiently serve a growing user base, making it an ideal for deployment on a Kubernetes cluster. Kubernetes enhances Strapi’s scalability and supports deployments across multiple environments. Strapi also includes built-in role-based authentication and user permissions, ensuring that only authorized users can access or modify content — adding a layer of security for cluster-based applications.
Designed to handle high content volumes and scale efficiently, Strapi is well-suited for deployment on a Kubernetes cluster. Kubernetes enhances Strapi’s scalability and multi-environment support. With built-in role-based authentication and user permissions, Strapi also ensures secure content access within cluster-based applications.
This article explains how to containerize, deploy, and scale a Strapi application on a Kubernetes Engine.
Before you begin, you should:
A record pointing to the Ingress Load Balancer IP. This article uses strapi.example.com, replace all occurrences with your actual domain.Access your management workstation and add the Node.js repository.
Install Node.js.
Configure Git.
Strapi uses Git for version control. If Git isn't configured, then configure your global username and email.
Set a global username to use with Git.
Set a global email address to use with Git.
Create a new Strapi project using npx.
The above command creates a new Strapi application called strapi-project using the Quickstart installation type. It installs the latest version of the create-strapi-app package, sets up the project structure, and installs all necessary dependencies.
Your output should be similar to the one below:
After the installation is complete, press Ctrl + C to stop the server and configure additional application settings.
In this section, configure your Strapi application to use a PostgreSQL Database, then set up an environment variable in the app's server file. Build the app's docker container image to deploy it on the Kubernetes cluster as described below.
Navigate to the project directory.
Edit the .env file.
Update the following values in the .env file.
Save and close the file.
The above configuration defines the database connection details for the PostgreSQL database. Replace postgres.vultrdb.com, 5432, user, strapi_db, and password with your actual database credentials.
Back up the original config/server.js file.
Re-create the config/server.js file.
Add the following configurations to the file.
Save and close the file.
The above code exports a configuration object that sets the host, port, url, and webhooks settings for the Strapi application. It retrieves values from environment variables.
Define the STRAPI_URL environment variable.
This above command sets the STRAPI_URL environment variable to the local Strapi server address and port.
Install the PostgreSQL library for Node.js to enable the database connection.
Rebuild the application.
The above command compiles the application, incorporating all recent changes.
Start the application in the background.
The above command starts the application in production mode. To verify that the Strapi application is running, visit the following address in your web browser and make sure Strapi web page displays. Replace <SERVER-IP> with your server’s actual IP address:
The Strapi dashboard should appear, displaying the register-admin screen to create the first administrator account.
Create a Dockerfile in the project directory.
Add the following configurations to the file.
Save and close the file.
This Dockerfile:
1337.npm run start.Create a file named .dockerignore.
The .dockerignore file declares a list of files and directories to ignore while building the image.
Add the following content to the file
This prevents the local node_modules/ folder from being copied into the image, letting Docker install dependencies fresh.
Build the Docker image.
Log in to your DockerHub account.
Replace <username> and <password> with your DockerHub username and password. To create a new DockerHub account, visit the DockerHub registry.
Your output should be similar to the one below:
Tag the Docker image with your <username>.
Replace <username> with your DockerHub username.
Push the image to DockerHub.
Replace <username> with your DockerHub username.
Your output should be similar to the one below:
Before deploying the Strapi application, you must configure the Kubernetes cluster by installing the necessary plugins and creating essential resources. This section demonstrates the steps to install the Nginx Ingress Controller, CertManager plugin, and creates a ClusterIssuer resource to issue Let’s Encrypt SSL certificates.
Follow the steps below to set up your cluster.
Add the Nginx Ingress repository.
Update Helm chart repositories.
Create a new ingress-nginx namespace for Nginx Ingress Controller.
Install the Nginx Ingress Controller.
After installation, a Load Balancer is provisioned automatically for your cluster. Verify that the assigned External-IP Address matches the Load Balancer IP in the Kubernetes Engine dashboard.
Your output should be similar to the one below:
The Load Balancer may take several minutes to become ready. If the EXTERNAL-IP field shows as <pending>, wait a few minutes and try again. You can also verify the Load Balancer deployment by opening the cluster page in the Kubernetes portal and checking the linked resources.
Install CertManager.
Output:
Visit the official CertManager releases page to get the latest version.
Get the Load Balancer’s External-IP Address.
Your output should be similar to the one below:
Add an A record for your domain strapi.example.com and point it to the External-IP Address of the Load Balancer.
Create a new manifest named clusterissuer.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a ClusterIssuer resource to issue Let’s Encrypt certificates using the HTTP01 challenge. Replace "hello@example.com" with your actual email address.
Apply the ClusterIssuer resource.
Output:
Verify the ClusterIssuer deployment.
Your output should be similar to the one below:
A READY status of True confirms the successful deployment of the ClusterIssuer resource.
Create a new deployment manifest file strapi-deployment.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a Deployment resource:
strapi-app label.regcred secret resource and authenticate to the DockerHub to pull the container image from private repository. Replace the image <username>/strapi:latest with your actual repository.strapi.example.com with your actual domain pointing to the cluster load balancer.Apply the strapi-deployment.yaml deployment file to deploy the application.
Output:
Verify that the application is deployed.
Your output should be similar to the one below:
Create a new service manifest to expose the application.
Add the following configurations to the file.
Apply the Service.
Output:
Create an Ingress manifest.
Add the following configurations to the file.
Save and close the file.
The above Ingress resource allows external access to the strapi-service resource. It uses the cluster issuer resource letsencrypt-prod to issue a new SSL certificate for domain strapi.example.com and store it as a secret resource strapi-tls.
Apply the Ingress resource.
Output:
Verify the Ingress resource.
Your output should be similar to the one below:
In a web browser, visit the below URL and verify that the log in page displays over HTTPS protocol.
Scaling the Strapi application makes it efficient to handle high traffic loads without downtime or interruptions In this section, you'll scale the Strapi application adjust the number of running replicas.
Increase the number of running replicas.
The above command scales the deployment to 4 replicas.
Output:
Verify the deployment status.
Your output should be similar to the one below:
View the running pods.
Your output should be similar to the one below:
Decrease the number of replicas.
Kubernetes automatically terminates the extra pods and adjusts traffic distribution.
You can test the fault tolerance by manually deleting a pod.
Replace <pod-name> with an existing pod name.
Kubernetes detects the change and automatically creates a new pod. The Ingress resource detects the new pod and starts routing requests without downtime.
You have created an example Strapi application using the Strapi API with a PostgreSQL configuration, then containerized, deployed, and scaled the application on a Kubernetes Engine. You also configured CertManager to issue Let’s Encrypt certificates and set up the Nginx Ingress controller to enable secure external access to the cluster services.
Strapi simplifies content management through an intuitive web interface, eliminating the need for technical expertise. This makes it an good choice for building low-code or no-code backends. For more information, visit the following resources.
0 Comments
Be the first to comment and share your perspective with the community.