DEV Community

Cover image for How to Easily Deploy a Drupal Instance on Kubernetes
Jeysson Aly Contreras
Jeysson Aly Contreras

Posted on

How to Easily Deploy a Drupal Instance on Kubernetes

A practical guide demonstrating how to deploy and manage a Drupal content management system on a Kubernetes cluster. This tutorial covers container orchestration basics, setting up necessary Kubernetes resources, and streamlining the deployment process to run Drupal efficiently in a containerized environment.

Table of Contents

Preparing Your Kubernetes Environment

Setting Up Your Kubernetes Cluster

To begin deploying a Drupal instance on Kubernetes, you need a fully operational Kubernetes cluster. This involves setting up a cluster using tools like Minikube for local development or leveraging cloud providers like AWS, GCP, or Azure for production environments. It's essential to ensure that kubectl, the Kubernetes command-line tool, is installed and configured to interact with your cluster. A simple command to verify your setup is kubectl cluster-info, which provides details about your cluster's components. If you encounter issues, ensure your kubeconfig file is correctly set up, as it contains the necessary credentials and configurations. A common problem is network connectivity, so check your firewall settings if you cannot connect to the cluster. Tools like Lens or K9s can provide a GUI to help manage your cluster more effectively. [Image: Kubernetes Cluster Dashboard] This image could display a typical Kubernetes dashboard, offering insights into the cluster's health and resources. Here's a basic command to start Minikube: minikube start --cpus=4 --memory=8192mb. Adjust resources based on your machine's capabilities.

Installing the Drupal Operator

The Drupal Operator simplifies managing Drupal instances within Kubernetes. To install, apply the operator YAML file using kubectl apply -fhttps://raw.githubusercontent.com/geerlingguy/drupal-operator/master/deploy/drupal-operator.yaml. This command deploys the operator, enabling you to create and manage Drupal instances effortlessly. The operator uses Ansible to automate tasks, ensuring consistent and reliable deployments. Once installed, you can verify its deployment by running kubectl get pods -n default, which lists all pods in the default namespace. If the operator pod isn't running, check the logs with kubectl logs <pod-name> for troubleshooting. [Image: Drupal Operator Deployment] This image could show the operator's pod running within the Kubernetes dashboard. The operator requires certain permissions, so ensure your Kubernetes role-based access control (RBAC) is configured correctly.

Configuring Persistent Storage

Kubernetes containers are stateless by default, meaning data isn't preserved after a restart. To maintain Drupal data, configure persistent storage using PersistentVolume (PV) and PersistentVolumeClaim (PVC). Define a PV in a YAML file, specifying storage size and access modes. Here's a basic example of a PV configuration:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
Enter fullscreen mode Exit fullscreen mode

Ensure the path specified in hostPath exists on your node. After creating the PV, define a PVC to request storage from this volume. The PVC binds to the PV, allowing your Drupal pods to use the storage. Use kubectl apply -f <filename>.yaml to create these resources in your cluster.

Deploying Drupal on Kubernetes

Creating a Drupal Instance

With the operator and storage in place, create a Drupal instance using a custom resource definition (CRD). Start by defining a YAML file, my-drupal-site.yml, specifying the Drupal version and image. Here’s a sample configuration:

apiVersion: drupal.drupal.org/v1alpha1
kind: Drupal
metadata:
  name: my-drupal-site
  namespace: default
spec:
  drupal_image: 'drupal:8.8-apache'
Enter fullscreen mode Exit fullscreen mode

Apply this configuration with kubectl apply -f my-drupal-site.yml. The operator will handle the deployment, creating the necessary pods and services. Monitor the deployment with kubectl get pods to ensure the Drupal pod is running. If the pod fails, check the logs for errors and verify the image name and tag. [Image: Drupal Pod Running] This image could show the status of the Drupal pod within the Kubernetes dashboard.

Exposing Your Drupal Site

To access your Drupal site externally, expose the service using a Kubernetes Service of type LoadBalancer or NodePort. Define a service YAML file that routes traffic to your Drupal pods. Here’s an example configuration for a LoadBalancer service:

apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: my-drupal-site
Enter fullscreen mode Exit fullscreen mode

Apply this configuration with kubectl apply -f drupal-service.yml. The service will assign an external IP, making your Drupal site accessible over the internet. Use kubectl get svc to retrieve the external IP address. If using a cloud provider, ensure your account supports LoadBalancer services. [Image: LoadBalancer Service] This image could show the external IP address assigned to the Drupal service.

Securing Your Deployment

Security is crucial for any web application, including Drupal. Start by ensuring your Kubernetes cluster is secure, using network policies to restrict traffic. Implement SSL/TLS to encrypt data in transit by integrating a service like Let's Encrypt. Use Ingress resources to manage SSL certificates and route traffic securely. Here’s a basic Ingress configuration:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: drupal-ingress
spec:
  rules:
    - host: my-drupal-site.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: drupal-service
                port:
                  number: 80
Enter fullscreen mode Exit fullscreen mode

Apply this with kubectl apply -f drupal-ingress.yml. Ensure DNS records point to your Ingress controller's external IP. [Image: Ingress Controller] This image could depict the Ingress setup for secure traffic routing.

Managing Your Drupal Instance

Scaling Your Deployment

Kubernetes excels at scaling applications to handle varying loads. Use Horizontal Pod Autoscaler (HPA) to adjust the number of Drupal pods based on CPU utilization. Define an HPA resource that targets your Drupal deployment. Here’s an example configuration:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: drupal-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-drupal-site
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50
Enter fullscreen mode Exit fullscreen mode

Apply this with kubectl apply -f drupal-hpa.yml. Monitor scaling events with kubectl get hpa. Ensure your cluster has sufficient resources to accommodate additional pods. [Image: Horizontal Pod Autoscaler] This image could show the scaling metrics for your Drupal deployment.

Monitoring and Logging

Effective monitoring and logging are vital for maintaining application health. Use tools like Prometheus and Grafana to collect and visualize metrics from your Drupal pods. Deploy Fluentd or Logstash to aggregate logs for analysis and troubleshooting. Integrate with Kubernetes metrics server for real-time insights. Here’s a basic configuration for Prometheus:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: drupal-monitor
spec:
  selector:
    matchLabels:
      app: my-drupal-site
  endpoints:
    - port: web
      interval: 30s
Enter fullscreen mode Exit fullscreen mode

Apply this with kubectl apply -f drupal-monitor.yml. Access Grafana dashboards to visualize performance metrics. [Image: Monitoring Dashboard] This image could show a Grafana dashboard with Drupal metrics.

Updating and Maintaining Your Instance

Regular updates and maintenance are crucial for security and performance. Use Kubernetes rolling updates to deploy new versions of Drupal without downtime. Update your deployment YAML file with the new image tag and apply it. Here’s an example command for updating the deployment:

kubectl set image deployment/my-drupal-site drupal=drupal:9.0-apache
Enter fullscreen mode Exit fullscreen mode

Monitor the rollout status with kubectl rollout status deployment/my-drupal-site. If issues arise, rollback to the previous version using kubectl rollout undo deployment/my-drupal-site. Regularly check for updates to both Drupal and the operator. [Image: Rolling Update Process] This image could show the rolling update process in action.

Conclusion

Deploying a Drupal instance on Kubernetes offers flexibility, scalability, and resilience for your web applications. By leveraging Kubernetes' powerful orchestration capabilities, you can efficiently manage your Drupal deployments, ensuring high availability and performance. Remember to secure your deployment, monitor performance, and scale resources as needed. If you encounter challenges, our support team is available 24/7 to assist you. Start optimizing your Drupal deployment today and experience the benefits of containerization and orchestration.

Reference Description Options

  1. "Learn how to deploy a Drupal instance on Kubernetes with our step-by-step guide. Scale effortlessly and ensure high availability."
  2. "Deploy Drupal on Kubernetes: A comprehensive guide to setup, manage, and scale your CMS efficiently."
  3. "Optimize your Drupal deployment on Kubernetes with our expert guide. Secure, scale, and manage with ease."
  4. "Step into the future of CMS deployment with Drupal on Kubernetes. Learn how to scale and secure your site today."
  5. "Discover the power of Kubernetes for Drupal deployments. Our guide covers setup, scaling, and security."

[1] https://medium.com/containerum/how-to-easily-deploy-a-drupal-8-instance-on-kubernetes-b90acc7786b7 "How to easily deploy a Drupal instance on Kubernetes"

[2] https://www.jeffgeerling.com/blog/2019/running-drupal-kubernetes-docker-production "Running Drupal in Kubernetes with Docker in production"

[3] https://medium.com/@Initlab/a-drop-in-the-sea-running-drupal-on-kubernetes-ce4a56ae2ae0 "A Drop in the Sea: Running Drupal on Kubernetes - Initlab - Medium"

[4] https://bobcares.com/blog/kubernetes-drupal-deployment/ "Kubernetes Drupal Deployment Guide"

[5] https://www.reddit.com/r/kubernetes/comments/8m4ws0/noob_where_to_run_database_migrations/ "Reddit - Dive into anything"

[6] https://github.com/geerlingguy/drupal-operator "GitHub - geerlingguy/drupal-operator: Drupal Operator for Kubernetes, built with Ansible and the Operator SDK."

[7] https://www.reddit.com/r/kubernetes/comments/oo8c3s/where_to_play_with_k8_free_or_cheap/ "Reddit - Dive into anything"

[8] https://stackoverflow.com/questions/41192053/cron-jobs-in-kubernetes-connect-to-existing-pod-execute-script "Cron Jobs in Kubernetes - connect to existing Pod, execute script"

[9] https://blogit.michelin.io/statefull-application-on-kubernetes/ "Drupal on Kubernetes (a.k.a stateful application)"

[10] https://alejandromoreno.medium.com/deploying-your-ddev-containers-in-digitalocean-or-aws-with-kubernetes-507df41b4816 "Deploying your DDEV containers in digitalocean (or aws) with kubernetes"

Top comments (0)