Effortless Local Development: Setting Up Directus CMS on Kubernetes

This post aims to guide web developers through the process of setting up Directus CMS on a local Kubernetes environment. Directus CMS is an open-source, headless content management system ideal for flexible and scalable web applications. Paired with Kubernetes, a powerful container-orchestration system, it forms a robust platform for development. We'll also touch on the role of Minikube, a tool that simplifies running Kubernetes locally.

Getting Started

Before beginning our journey into setting up Directus CMS on Kubernetes, it's essential to have the right tools in place. You’ll need Docker or an alternative like Podman, as well as Minikube. These tools are crucial for creating and managing containerized applications and a local Kubernetes environment. While this post won't cover the installation steps for these tools, you can find detailed instructions at their respective websites:

For Docker, visit Docker Desktop.

To install Minikube, head over to Minikube's official documentation.

As an alternative to Docker Desktop, consider exploring Podman.

Understanding Kubernetes and Minikube

Kubernetes has revolutionized how we deploy and manage containerized applications, offering a level of efficiency and scalability previously unattainable. It orchestrates containers across multiple hosts, automates deployment processes, and manages workloads. Key features include automated scheduling, self-healing capabilities, horizontal scaling, and service discovery and load balancing. Minikube brings the power of Kubernetes to your local machine, providing a lightweight, easy-to-use platform for development and testing.

Deploying PostgreSQL on Kubernetes

Setting up PostgreSQL on Kubernetes involves several steps, each defined by specific configurations in YAML files. We start with a PersistentVolumeClaim (PVC) for data persistence, move to deploying the PostgreSQL container, and finally, define a service to expose it.

PersistentVolumeClaim for PostgreSQL

First, we create a PVC for PostgreSQL:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
  namespace: mynamespace
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

PostgreSQL Deployment

Next, we deploy PostgreSQL:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: mynamespace
spec:
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        env:
        - name: POSTGRES_DB
          value: directus
        - name: POSTGRES_USER
          value: username
        - name: POSTGRES_PASSWORD
          value: mypassword
        ports:
          - containerPort: 5432
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-storage
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pv-claim

PostgreSQL Service

Finally, we define a Service:

Copy code
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: mynamespace
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432

Setting Up Directus CMS

Deploying Directus CMS on Kubernetes follows a similar pattern to PostgreSQL.

PersistentVolumeClaim for Directus

First, a PVC for Directus:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: directus-pv-claim
  namespace: mynamespace
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Directus Deployment

Then, the Directus deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: directus
  namespace: mynamespace
spec:
  selector:
    matchLabels:
      app: directus
  template:
    metadata:
      labels:
        app: directus
    spec:
      containers:
      - name: directus
        image: directus/directus:latest
        env:
        - name: DATABASE_URL
          value: postgresql://username:mypassword@postgres:5432/directus
        - name: KEY
          value: "prod"
        - name: SECRET
          value: "myverysecretkey"
        - name: ADMIN_EMAIL
          value: "dev@admin.com"
        - name: ADMIN_PASSWORD
          value: "123456"
        - name: PUBLIC_URL
          value: "http://localhost:8055"
        - name: CORS_ORIGIN
          value: "http://localhost:5173"
        ports:
        - containerPort: 8055
        volumeMounts:
        - name: directus-storage
          mountPath: /directus/uploads
      volumes:
      - name: directus-storage
        persistentVolumeClaim:
          claimName: directus-pv-claim

Directus Service

And finally, the Directus service:

apiVersion: v1
kind: Service
metadata:
  name: directus
  namespace: mynamespace
spec:
  selector:
    app: directus
  ports:
    - protocol: TCP
      port: 8055
      targetPort: 8055

Integrating Directus with PostgreSQL

Integration is crucial for Directus to function correctly with the PostgreSQL database. This is primarily handled through environment variables in the Directus deployment YAML:

DATABASE_URL: Configured to connect Directus to the PostgreSQL service. It includes the database name, user, and password, as well as the PostgreSQL service name and port. Admin Account Setup: Environment variables like ADMIN_EMAIL and ADMIN_PASSWORD are used to create the initial admin account in Directus.

This integration allows Directus to effectively manage and store content in the PostgreSQL database, ensuring a seamless content management experience.

Accessing Directus via Port Forwarding

Once Directus is successfully deployed, you can access it through port forwarding. This is done using the kubectl port-forward command, which allows you to access Directus from your local machine:

kubectl port-forward service/directus 8055:8055 -n mynamespace

After executing this command, Directus will be accessible at http://localhost:8055. This step is crucial for testing your setup and ensures that Directus is properly configured and operational.

Conclusion

Conclude by summarizing the process and its benefits. Highlight the efficiency and scalability of using Kubernetes for local development with Directus CMS. Encourage readers to experiment with different configurations, adapt the setup to their needs, and share their experiences and feedback.