With the constant rise in the number of internet users, the demand for creating scalable web apps is at an all-time high. Gone are the days when businesses would run their servers on self-managed hardware.
The advent of cloud computing has shifted the gears toward the cloud-native model of software development. Cloud-native is more than just using a cloud provider to serve apps — it's an all-in-one approach that takes full advantage of the cloud computing model.
In a cloud-native deployment, the application is microservice-oriented and every part of the application is hosted. Containers are ordered to ensure that they are scheduled in a way that optimizes resource usage, which means that the application performs well and is balanced against the user's load. This approach is different from the traditional monolithic application that has to be scaled and maintained manually.
Docker is an open platform for developing, distributing, and running applications. Docker allows you to decouple applications from your infrastructure so you can distribute software quickly.
Kubernetes is a container orchestration system used for automated deployment, scaling, and management of containers. Kubernetes makes running a containerized application easy. Multiple worker machines are combined to form a cluster, and each worker machine hosts pools that run different application-related containers. If you're new to Kubernetes, the official tutorials are a great place to start.
To follow along with this tutorial, you need to have the following:
Strapi is an open-source headless CMS. It’s 100% JavaScript and fully customizable. To see firsthand how it performs, Strapi provides an official demo app called FoodAdvisor that can be previewed on Strapi’s website or deployed locally or in the cloud.
The FoodAdvisor app shows a listing of different restaurants and serves as a demo of the capabilities of Strapi.
Content management systems (CMS) have been around for a long time. Traditionally, these were monolithic applications serving content, images, HTML, CSS, and JavaScript from a single location. It makes it difficult to reuse content because the content is bound to the view.
A headless CMS that takes a different approach and separates the content from the view. In headless CMS, content is served through an API used by multiple clients responsible for rendering the content. This separation is ideal for cloud-native architectures because individual components can be contained and scaled independently of each other. In this tutorial, you'll learn how to leverage Kubernetes to deploy the FoodAdvisor app.
To scaffold a new Strapi project is very simple and works precisely as installing a new frontend framework. We are going to start by running the following commands and testing them out in our default browser.
npx create-strapi-app strapi-api --quickstart
# OR
yarn create strapi-app strapi-api --quickstart
The command above will scaffold a new Strapi project in the directory you specified.
Next, run yarn build
to build your app and yarn develop
to run the new project if it doesn't start automatically.
The last command will open a new tab with a page to register your new admin of the system. Go ahead and fill out the form and click on the submit button to create a new Admin.
Start the minikube cluster by executing the following command:
minikube start
Minkube will automatically configure kubectl to communicate with this local cluster, so no manual configuration is necessary. Check if minikube is running with this command:
minikube status
Once the Minikube cluster is up and running, you can proceed to deploy the FoodAdvisor app with Kubernetes.
As the first step, you need to clone the FoodAdvisor GitHub repo. This tutorial will be using the version v4
of the FoodAdvisor app.
Clone the repo, navigate to the foodadvisor directory, and checkout to the v4
branch:
git clone https://github.com/strapi/foodadvisor.git
cd foodadvisor
git checkout v4
In the repo, you'll see two directories: api
and client
. The api
directory holds the backend Strapi server as well as the demo data. The client
directory contains the frontend written in Next.js. The server uses a local SQLite file as the database, so you don't need to set up a separate database to run this app.
After cloning the repo, you will prepare the Docker images for the backend and the frontend. You will need a Docker Hub account to push the images. If you do not wish to create an account, feel free to skip to deploying the backend and use the images I have already prepared.
After creating a Docker Hub account, log in with your username and password by running the docker login
command.
In the api
directory, create the following Dockerfile
:
1 FROM node:16
2
3 WORKDIR /app
4
5 ADD ./package.json /app
6
7 ADD ./yarn.lock /app
8
9 RUN yarn install --frozen-lockfile
10
11 ADD . /app
12
13 RUN yarn seed
14
15 EXPOSE 1337
16
17 CMD ["yarn", "develop"]
Edit the .env.example
file and set the CLIENT_URL
variable to http://foodadvisor.client
. Then, in the same directory, run the following commands:
1 docker build -t <your-docker-hub-username>/foodadvisor-api .
2 docker image push <your-docker-hub-username>/foodadvisor-api:latest
Don’t forget to substitute your Docker Hub username. Now, create the following Dockerfile in the client
directory:
1 FROM node:16
2
3 WORKDIR /app
4
5 ADD ./package.json /app
6
7 ADD ./yarn.lock /app
8
9 ADD ./.env.development /app
10
11 RUN yarn install --frozen-lockfile
12
13 ADD . /app
14
15 EXPOSE 3000
16
17 CMD ["yarn", "dev"]
Edit the .env.development
file and set the NEXT_PUBLIC_API_URL
to http://foodadvisor.backend
and run the following commands, substituting your Docker Hub username of course:
1 docker build -t <your-docker-hub-username>/foodadvisor-client .
2 docker image push <your-docker-hub-username>/foodadvisor-client:latest
With the Docker images ready, you can start deploying the backend to your Kubernetes cluster. The kubectl
command will be used to handle communication with the cluster. kubectl
takes a YAML file, which declares the configuration, and they’re applied to the cluster.
Save the following configuration in deploy-backend.yaml
:
1 apiVersion: apps/v1
2 kind: Deployment
3 metadata:
4 name: foodadvisor-backend
5 spec:
6 selector:
7 matchLabels:
8 app: foodadvisor
9 component: backend
10 template:
11 metadata:
12 labels:
13 app: foodadvisor
14 component: backend
15 spec:
16 containers:
17 - name: foodadvisor-api
18 image: faithkovi/foodadvisor-api
19 ports:
20 - containerPort: 1337
If you created your own Docker images in the previous step, change faithkovi/foodadvisor-api
to <your-docker-hub-username>/foodadvisor-api
.
The YAML file sets up a Deployment named foodadvisor-backend
. The deployment creates a Pod, which runs one container with your specified Docker image.
Now apply the changes:
1 kubectl apply -f deploy-backend.yaml
You can verify the deployment by running the following command:
kubectl get deployments
The backend is now running inside the cluster, but it is isolated from the outside world by default. To be able to send requests to the server from outside the cluster, you need to expose appropriate ports. In this case, port 1337 since this is the port Strapi listens to.
1 kubectl expose deployment foodadvisor-backend --port 1337
This will create a Service, which you can verify by running the following command:
1 kubectl get services foodadvisor-backend
The next step is to deploy the frontend and connect it to the backend. Save the following in deploy-frontend.yaml
:
1 apiVersion: apps/v1
2 kind: Deployment
3 metadata:
4 name: foodadvisor-frontend
5 spec:
6 selector:
7 matchLabels:
8 app: foodadvisor
9 component: frontend
10 template:
11 metadata:
12 labels:
13 app: foodadvisor
14 component: frontend
15 spec:
16 containers:
17 - name: foodadvisor-client
18 image: faithkovi/foodadvisor-client
19 ports:
20 - containerPort: 3000
Again, if you created your own Docker image, replace faithkovi/foodadvisor-client
with <your-docker-hub-username>/foodadvisor-client
.
Like the backend, this YAML creates a deployment that contains a pod that runs the specified Docker image. Apply the changes to deploy the frontend:
1 kubectl apply -f deploy-frontend.yaml
Verify the deployment by running the command below:
1 kubectl get deployments
Similar to the backend, you need to expose port 3000 so that you can use your browser to reach the frontend.
1 kubectl expose deployment foodadvisor-frontend --port 3000
Verify the service by running kubectl get services foodadvisor-frontend
.
Finally, you'll need to add an Ingress to your cluster. The job of the ingress is to route the incoming requests to the frontend or the backend, depending on the URL you use.
The following ingress rule will route all requests made to http://foodadvisor.backend
to the backend service you created earlier, and all requests made to http://foodadvisor.client
to the frontend service.
1 apiVersion: networking.k8s.io/v1
2 kind: Ingress
3 metadata:
4 name: main-ingress
5 spec:
6 rules:
7 - host: foodadvisor.backend
8 http:
9 paths:
10 - path: /
11 pathType: Prefix
12 backend:
13 service:
14 name: foodadvisor-backend
15 port:
16 number: 1337
17 - host: foodadvisor.client
18 http:
19 paths:
20 - path: /
21 pathType: Prefix
22 backend:
23 service:
24 name: foodadvisor-frontend
25 port:
26 number: 3000
Save the YAML to ingress.yaml
. If this is the first time you’re using ingress on Minikube, you need to enable the addon:
1 minikube addons enable ingress
Verify that the NGINX Ingress controller is running:
1 kubectl get pods -n ingress-nginx
You need to run this step only once. Finally, apply the ingress:
1 kubectl apply -f ingress.yaml
The last step is to make the hostnames http://foodadvisor.backend
and http://foodadvisor.client
known to your operating system so that you can open them with your browser. For that, you need to know the IP address of the minikube cluster:
1 minikube ip
Edit /etc/hosts
and add the following lines at the end. Substitute the appropriate IP address from the previous output:
1 192.168.49.2 foodadvisor.client
2 192.168.49.2 foodadvisor.backend
With these changes in place, requests to http://foodadvisor.backend
and http://foodadvisor.client
will be sent to your minikube cluster where the ingress controller will pick them up and route them to their appropriate destinations.
Visit http://foodadvisor.backend/admin
to access your Admin panel. Since this is the first time, you’ll be prompted to create an admin user.
Once you’ve created an admin user, you can access the Strapi dashboard and explore the sample data that’s been prepopulated.
To access the frontend, visit http://foodadvisor.client
. You should see the homepage of the Foodadvisor app.
Congratulations, you have successfully deployed Foodadvisor with Kubernetes.
The cloud-native approach will undoubtedly become the de facto standard for application development in the near future. This approach eliminates the need for a monolithic application and instead splits the application into scalable, performant microservices. The result is an application that is easy to develop and manage that is difficult to achieve with traditional deployments.
In this tutorial, we showed how easy it is to get started with Kubernetes to deploy the official Strapi demo app. If you're interested in this, visit Strapi website to learn more about Headless CMS. You can find the project in the Github repo and the docker images for the client and api in the docker repository.
I am self-motivated and purpose-driven, always curious and eager to learn new skills. I am also enthusiastic about cloud security.