Wednesday, 22 October 2025

Deploy Drupal with MySQL on Kubernetes: Complete Setup

Our course you can check :-   Udemy course  


Ques:-  

We need to deploy a Drupal application on Kubernetes cluster. Application development team want to setup a fresh Drupal as they will do the installation on their own. Below you can find the requirements, they have shared with us.

1) Configure a persistent volume drupal-mysql-pv with hostPath = /drupal-mysql-data (/drupal-mysql-data directory already exists on the worker Node i.e jump host), 5Gi of storage and ReadWriteOnce access mode.

2) Configure one PersistentVolumeClaim named drupal-mysql-pvc with storage request of 3Gi and ReadWriteOnce access mode.

3) Create a deployment drupal-mysql with 1 replica, use mysql:5.7 image. Mount the claimed PVC at /var/lib/mysql.

4) Create a deployment drupal with 1 replica and use drupal:8.6 image.

4) Create a NodePort type service which should be named as drupal-service and nodePort should be 30095.

5) Create a service drupal-mysql-service to expose mysql deployment on port 3306.

6) Set rest of the configration for deployments, services, secrets etc as per your preferences. At the end you should be able to access the Drupal installation page .


Ans:-

Here is your single Kubernetes YAML manifest that includes all required resources to deploy a fresh Drupal application with a MySQL backend:

This manifest includes:

  • PersistentVolume and PersistentVolumeClaim for MySQL data
  • drupal-mysql Deployment with MySQL 5.7
  • drupal Deployment with Drupal 8.6
  • drupal-service as a NodePort service (port 30095)
  • drupal-mysql-service to expose MySQL internally
raj@jumphost ~$ cat pod.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-mysql-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /drupal-mysql-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal-mysql
  template:
    metadata:
      labels:
        app: drupal-mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: drupalroot
            - name: MYSQL_DATABASE
              value: drupaldb
            - name: MYSQL_USER
              value: drupaluser
            - name: MYSQL_PASSWORD
              value: drupalpass
          volumeMounts:
            - name: mysql-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-storage
          persistentVolumeClaim:
            claimName: drupal-mysql-pvc
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      containers:
        - name: drupal
          image: drupal:8.6
          ports:
            - containerPort: 80
          env:
            - name: DRUPAL_DB_HOST
              value: drupal-mysql-service
            - name: DRUPAL_DB_NAME
              value: drupaldb
            - name: DRUPAL_DB_USER
              value: drupaluser
            - name: DRUPAL_DB_PASSWORD
              value: drupalpass
---
apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: NodePort
  selector:
    app: drupal
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30095
---
apiVersion: v1
kind: Service
metadata:
  name: drupal-mysql-service
spec:
  selector:
    app: drupal-mysql
  ports:
    - port: 3306
      targetPort: 3306

raj@jumphost ~$ kubectl apply -f pod.yaml
persistentvolume/drupal-mysql-pv created
persistentvolumeclaim/drupal-mysql-pvc created
deployment.apps/drupal-mysql created
deployment.apps/drupal created
service/drupal-service created
service/drupal-mysql-service created

raj@jumphost ~$ kubectl get pv,pvc
NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                      STORAGECLASS   REASON   AGE
persistentvolume/drupal-mysql-pv                            5Gi        RWO            Retain           Available                                                      23s
persistentvolume/pvc-08ba2d61-5cd4-4204-8471-67fca87d377a   3Gi        RWO            Delete           Bound       default/drupal-mysql-pvc   standard                18s

NAME                                     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/drupal-mysql-pvc   Bound    pvc-08ba2d61-5cd4-4204-8471-67fca87d377a   3Gi        RWO            standard       23s

raj@jumphost ~$ kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/drupal-f5499f965-xbghv          1/1     Running   0          69s
pod/drupal-mysql-84dccbc887-b4d5s   1/1     Running   0          69s

NAME                           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/drupal-mysql-service   ClusterIP   10.96.29.189   <none>        3306/TCP       69s
service/drupal-service         NodePort    10.96.171.61   <none>        80:30095/TCP   69s
service/kubernetes             ClusterIP   10.96.0.1      <none>        443/TCP        16m

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/drupal         1/1     1            1           69s
deployment.apps/drupal-mysql   1/1     1            1           69s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/drupal-f5499f965          1         1         1       69s
replicaset.apps/drupal-mysql-84dccbc887   1         1         1       69s
raj@jumphost ~$ 


Conclusion:-

In this hands-on tutorial, you'll learn how to deploy a fresh Drupal application backed by MySQL on a Kubernetes cluster. We'll walk through setting up persistent storage, configuring deployments, and exposing services using NodePort. By the end of this course, you'll be able to access the Drupal installation page and understand how to manage stateful applications in Kubernetes.

What you'll learn:

  • Create and bind PersistentVolumes and PersistentVolumeClaims
  • Deploy MySQL and Drupal containers using Kubernetes Deployments
  • Configure environment variables for Drupal-MySQL integration
  • Expose services using NodePort and ClusterIP
  • Access and verify the Drupal installation page
  • Understand Kubernetes resource relationships for stateful apps

No comments:

Post a Comment