k8s

MySQL Standalone on Kubernetes with NFS

📦 MySQL Standalone on Kubernetes with NFS

This repository contains Kubernetes manifests to deploy a standalone MySQL instance using an external NFS server for persistent storage.

📌 Requirements

  • Kubernetes cluster (v1.24+)
  • NFS Server: 192.168.88.61
  • Exported path: /nfs/mysql with write permission

📁 NFS Server Setup

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

🗂 Deployment Files

1. PersistentVolume and PersistentVolumeClaim

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

2. MySQL Secret

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

📌 root-password is base64-encoded value of my-secret-pw
Generate a new one using:

echo -n 'your-password' | base64

3. MySQL Deployment

# mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.4
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: root-password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-pvc

4. MySQL Service

# mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
  type: ClusterIP

🚀 Deploy All Components

Apply in order:

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

🧪 Test the Connection

kubectl run mysql-client --rm -it --image=mysql:8.4 -- bash
# Inside pod:
mysql -h mysql -u root -pmy-secret-pw

⚠️ Notes

  • ReadWriteOnce may not be optimal for NFS in all CSI environments. Consider ReadWriteMany if your setup supports it.
  • Do not share /nfs/mysql path across multiple MySQL instances simultaneously.
  • Ensure proper export and firewall settings on the NFS server.
  • Always use Secrets for sensitive data such as passwords.

📄 License

MIT

0
Uncategorized