k8s

WordPress on Kubernetes with NFS and External MariaDB/MySQL

📦 WordPress on Kubernetes with NFS and External MariaDB/MySQL

This repository contains Kubernetes manifests to deploy WordPress using:

  • NFS for persistent storage (192.168.88.61:/nfs/wordpress)
  • External MySQL or MariaDB database running in the same Kubernetes cluster

📌 Requirements

  • Kubernetes cluster (v1.24+)
  • Existing NFS path: /nfs/wordpress
  • Running external DB (mariadb or mysql service)
  • Port 30081 open on Node for external access (NodePort)

📁 NFS Server Setup

sudo mkdir -p /nfs/wordpress
sudo chmod -R 777 /nfs/wordpress

🗂 Deployment Files

1. PersistentVolume and PersistentVolumeClaim

# wordpress-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wordpress-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 192.168.88.61
    path: /nfs/wordpress
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi


2. WordPress Secret

# wordpress-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: wordpress-secret
type: Opaque
data:
  db-password: bXktc2VjcmV0LXB3

📌 The value db-password is the base64 encoding of my-secret-pw.
You can generate your own with:

echo -n 'your-password' | base64

3. WordPress Deployment

# wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
        - name: wordpress
          image: wordpress:6.8-php8.3-apache
          env:
            - name: WORDPRESS_DB_HOST
              value: "mariadb"
            - name: WORDPRESS_DB_USER
              value: "root"
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: wordpress-secret
                  key: db-password
            - name: WORDPRESS_DB_NAME
              value: "wordpress"
          ports:
            - containerPort: 80
          volumeMounts:
            - name: wordpress-data
              mountPath: /var/www/html
      volumes:
        - name: wordpress-data
          persistentVolumeClaim:
            claimName: wordpress-pvc

4. WordPress Service

# wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30081

🚀 Deploy WordPress

Apply all manifests in order:

kubectl apply -f wordpress-pv.yaml
kubectl apply -f wordpress-secret.yaml
kubectl apply -f wordpress-deployment.yaml
kubectl apply -f wordpress-service.yaml

🌐 Access WordPress

Visit:

http://<NodeIP>:30081

⚠️ Notes

  • Ensure that your database service (mariadb or mysql) is running and accessible.
  • The WORDPRESS_DB_NAME must match a database created in your MySQL/MariaDB beforehand (e.g., CREATE DATABASE wordpress;)
  • Make sure the secret name and key match exactly.
  • You can adjust the NodePort or switch to Ingress as needed.

📄 License

MIT

0
Uncategorized