Deploy strapiv4 on Kubernetes EKS
Strapi is a headless CMS that is used to develop websites, mobile applications, eCommerce sites, and APIs. It allows you to create an API without knowing anything about the backend or databases. The system builds APIs based on content models automatically, making it easy to view data in the CMS with Strapi examples.
- Strapi CMS is a free, open-source headless CMS that uses an API to link your frontend to Strapi’s backend.
- It’s a developer-friendly open-source and free-to-use service.
- Strapi is simple to learn and use, and you can get work done in minutes.
- Strapi may be used with React, TezJS, Vue, Nuxt.js, Next.js, Angular, Svelte, Sapper, and Flutter.
Let’s get started.
First spin up a brand new V4 project.
npx create-strapi-app@latest myproject --quickstart`
Note: Replace myproject with the project name
Once this is completed we can press CTRL + C in the terminal to stop the current file, we don’t need to fill in the file as this is using SQLite, and we will be using PostgreSQL
Let’s open the folder now in vscode (In the terminal, you can try to use this nifty quick way of opening it cd myproject && code . which will change to the project directory of strapi and open vscode.)
Note: Replace myproject with the project name
Upgrade required
Add following configurations to config/server.js
file.
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
url: env("PUBLIC_URL", "https://strapi.example.com"),
});
Once, done run command following command to update configuration:
npm run build
Dockerfile
Create a docker file as following:
FROM node:16-alpine
# Installing libvips-dev for sharp Compatibility
RUN apk update && apk add build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY ./package.json ./package-lock.json ./
ENV PATH /opt/node_modules/.bin:$PATH
RUN npm install
WORKDIR /opt/app
COPY ./ .
RUN npm run build
EXPOSE 1337
CMD ["npm", "run", "develop"]
Let’s step through this gibberish of some code for those that have not used Docker 🐳before.
First, we get a node image 16 (We can use :14) if we wanted to use version 14 of node instead.
We are setting an argument as default to develop as we want to not have to provide this every time we run our setup. The ENV is a way to override it if we want to like production
WORKDIR we are creating a folder inside our container 🚀 where our node_modules will live
We copy package.json and yarn.lock (or package-lock.json if you use npm) into our work directory. We do this FIRST as 🐳 docker caches each layer and this will then speed up our build process. Unless the file changes. 📝
We then tell Docker where to look for our node_modules
In case we have some network issues or a bit of slow internet we are setting a large timeout to give it some extra time.
We then run yarn install to install all dependencies.
We then change directories into /opt/app
We then copy the project that we created first into this folder.
Then we run yarn build to build our strapi project.
At the end, we expose the port 1337 and tell Docker to run yarn develop
dockerignore
Create a file called .dockerignore
.tmp/
.cache/
.git/
build/
node_modules/
data/
Now, build image
docker build -t mystrapi:latest
Once image is been created push the image to ECR with proper tags.
EKS Deployment
Connect you the eks cluster using following command:
aws eks --region REGION update-kubeconfig --name CLUSTER-NAME
Env variables
The following environment variables are required in order to run Strapi in a Docker container:
| Variable name | Description |
|-------------------|-------------------------------------------------------|
| NODE_ENV | The environment in which the application is running.
| DATABASE_CLIENT | The database client to use. example. mysql, postgres
| DATABASE_HOST | The database hostname.
|DATABASE_PORT | The database port.
| DATABASE_NAME | The database name.
| DATABASE_USERNAME | The database username.
| DATABASE_PASSWORD | The database password.
| JWT_SECRET | The secret used to sign the JWT for the Users-Permissions plugin.
| ADMIN_JWT_SECRET | The secret used to sign the JWT for the Admin panel.
| APP_KEYS | The secret keys used to sign the session cookies.
kubectl apply -k .
Once, you are done with deployment, apply ingress.yaml
to get domain and cert.
After you finish whole deployment. Add subdomain in Route53
Hosted Zone map LoadBalancer.
PS C:\Users\aman_\Documents\workspace\plasmic> kubectl get ingress -n strapi
NAME CLASS HOSTS ADDRESS PORTS AGE
strapi-ingress <none> strapi.example.com <elb> 80, 443 12m
updated Route53 DNS record.