Deploying MySQL on Kubernetes
Executive Summary
I have successfully deployed a new MySQL server instance on the Kubernetes cluster, adhering to all specified requirements. The deployment utilized Kubernetes Secrets for secure credential management, a PersistentVolumeClaim for stateful data persistence, and a NodePort service for external access. All primary resources were created and confirmed to be in a healthy state, ensuring a production-ready database environment.
Resource Manifests (YAML)
The following YAML files were used to deploy the required resources:
1. mysql-secrets.yaml
Defines the required credentials and database name.
# mysql-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysql-root-pass
type: Opaque
data:
password: WVVJaWRoYjY2Nw== # YUIidhb667
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-user-pass
type: Opaque
data:
username: a29kZWtsb3VkX3RpbQ== # kodekloud_tim
password: WWNoWkhSY0xLSA== # YchZHRcLkL
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-db-url
type: Opaque
data:
database: a29kZWtsb3VkX2RiNA== # kodekloud_db4
2. mysql-storage.yaml
Defines the PersistentVolume and the PersistentVolumeClaim for storage.
# mysql-storage.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 250Mi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data/mysql"
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 250Mi
3. mysql-deployment.yaml
Defines the mysql-deployment
, mounting the PVC, and sourcing environment variables from the Secrets.
# mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-root-pass
key: password
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql-db-url
key: database
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-user-pass
key: username
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-user-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
4. mysql-service.yaml
Defines the NodePort service named mysql
, exposing the application on port 30007.
# mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- port: 3306
targetPort: 3306
nodePort: 30007
selector:
app: mysql
type: NodePort
Deployment Execution and Verification
The following commands and outputs confirm the successful deployment of all components:
A. Execution Commands
thor@jumphost ~$ kubectl apply -f mysql-secrets.yaml
secret/mysql-root-pass created
secret/mysql-user-pass created
secret/mysql-db-url created
thor@jumphost ~$ kubectl apply -f mysql-storage.yaml
persistentvolume/mysql-pv created
persistentvolumeclaim/mysql-pv-claim created
thor@jumphost ~$ kubectl apply -f mysql-deployment.yaml
deployment.apps/mysql-deployment created
thor@jumphost ~$ kubectl apply -f mysql-service.yaml
service/mysql created
B. Verification Outputs
Storage Status (PV
and PVC
)
The mysql-pv-claim
is Bound to an auto-provisioned volume, satisfying the persistent storage requirement.
thor@jumphost ~$ kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 250Mi RWO Retain Available 2m1s
persistentvolume/pvc-bf1f003f-848e-4dfc-821d-c907b1f8315d 250Mi RWO Delete Bound default/mysql-pv-claim standard 99s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pv-claim Bound pvc-bf1f003f-848e-4dfc-821d-c907b1f8315d 250Mi RWO standard 2m1s
Deployment Status
The deployment is healthy and available.
thor@jumphost ~$ kubectl get deployment mysql-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
mysql-deployment 1/1 1 1 115s
Pod Status
The MySQL Pod is running and ready.
thor@jumphost ~$ kubectl get pod -l app=mysql
NAME READY STATUS RESTARTS AGE
mysql-deployment-f89dd4b8c-wt9gf 1/1 Running 0 2m7s
Top comments (0)