Building DevSecOps solutions using AWS, Terraform and Kubernetes

Creating a Shopify App using Laravel Nova and k8s in 2 days

  • 1st November 2020

This is a broad strokes article on the methods I used to create a high quality Shopify App quickly, while not sacrificing on quality.

Technologies Used

  • Laravel
    Laravel
  • Laravel Nova
    Laravel Nova
  • Maria DB
    Galera Custer
  • Redis
    Redis
  • GitLab
    GitLab CI
  • Kubernetes
    Kubernetes
  • DigitalOcean
    Digital Ocean

Laravel Nova

For the core framework I used laravel, so I could utilise existing community packages for handling the oauth handshakes to speed up development.

Admin panel designs are increasingly becoming a solved problem, and cover a wide array of common use cases out of the box. Bootstrapping my shopify app with a $200 admin panel easily saved weeks of development time.

Redis / Galera

I used Redis for caching/sessions and Galera for the application database. If this was a production app I would of favoured using managed databases, however the shopify app itself is low traffic so there was plenty of resource available to use pre-existing helm packages for these.

Running a multi-master galera cluster on an application with a low level of writes gives a high level of availability.

Kubernetes

Kubernetes sped up development significantly, and made setting up redis and mariadb clusters effortless in minutes.

Want a MariaDB / Galera cluster?

helm install my-release bitnami/mariadb-galera

Want a Redis cluster?

helm install my-release bitnami/redis-cluster

In the end I opted for 3 laravel pods, 3 mariadb pods and 3 redis pods. This gives incredibly high availability, with room to scale by adding additional nodes.

$ kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
cg-laravel-95c7995c9-6xp82           1/1     Running   0          36h
cg-laravel-95c7995c9-c2d7q           1/1     Running   0          36h
cg-laravel-95c7995c9-xw9jw           1/1     Running   0          36h
galera-cg-laravel-mariadb-galera-0   1/1     Running   0          36h
galera-cg-laravel-mariadb-galera-1   1/1     Running   0          36h
galera-cg-laravel-mariadb-galera-2   1/1     Running   0          36h
redis-single-cf-laravel-master-0     1/1     Running   0          36h
redis-single-cf-laravel-slave-0      1/1     Running   0          36h
redis-single-cf-laravel-slave-1      1/1     Running   0          36h

The only single point of failure currently is the master redis node, deleting that pod will cause 1 minute of downtime. A simple range of health checks keep all the pods in line and where they can't self heal, they quickly remove themselves from the cluster to allow a smooth handling of failure.

GitLab CI / GitOps

AWS CodePipeline works great, but GitLab CI is 10x quicker to work with so I always recommend that on projects where you are not tied to a single cloud for any compliance reasons.

GitOps is easy, and you can achieve it with k8s and a few extra lines in your .gitlab-ci.yml file.

deploy:
stage: deploy
dependencies:
    - build
image:
    name: bitnami/kubectl:1.17.14
    entrypoint: [""]
before_script:
    - kubectl version --kubeconfig ./kube-config
script:
    - kubectl apply -f manifest.yaml --kubeconfig ./kube-config
    - kubectl apply -f manifest-galera.yaml --kubeconfig ./kube-config
    - kubectl apply -f manifest-redis.yaml --kubeconfig ./kube-config
    - kubectl rollout restart deploy cg-laravel --kubeconfig ./kube-config
only:
    - master

If you're looking to use GitOps, while still taking advantage of helm, then you can run helm commands with --dry-run --debug parameters to output a manifest file that can slot into your CI pipeline.

Hosting

Initially this started in AWS, but their entry cost of creating an EKS cluster with Fargate was starting at $70/month which was outwith with the acceptable budget for this project. So DigitalOcean is a close second, and treats k8s as a first class citizen. The infrastructure costs $25/month to run two nodes with a private container registry.

Total Costs

Laravel Nova: $200 one-off cost

Hosting: $25/month

Upgrades will be needed to Laravel approximately every 6 months

Rhuaridh

Please get in touch through my socials if you would like to ask any questions - I am always happy to speak tech!