Skip to main content

Lab 002: Deploy MySQL on Kubernetes



Requirements

A new MySQL server needs to be deployed on Kubernetes cluster. The Nautilus DevOps team was working on to gather the requirements. Recently they were able to finalize the requirements and shared them with the team members to start working on it. Below you can find the details:

  1. Create a PersistentVolume mysql-pv, its capacity should be 250Mi, set other parameters as per your preference.

  2. Create a PersistentVolumeClaim to request this PersistentVolume storage. Name it as mysql-pv-claim and request a 250Mi of storage. Set other parameters as per your preference.

  3. Create a deployment named mysql-deployment, use any mysql image as per your preference. Mount the PersistentVolume at mount path /var/lib/mysql.

  4. Create a NodePort type service named mysql and set nodePort to 30007.

  5. Create three secrets:

    • mysql-root-pass with key password and value YUIidhb667
    • mysql-user-pass with keys username (value: kodekloud_top) and password (value: B4zNgHA7Ya)
    • mysql-db-url with key database and value kodekloud_db8
  6. Define environment variables within the container to reference the secrets:

    • MYSQL_ROOT_PASSWORD from mysql-root-pass:password
    • MYSQL_DATABASE from mysql-db-url:database
    • MYSQL_USER from mysql-user-pass:username
    • MYSQL_PASSWORD from mysql-user-pass:password

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Note

This lab demonstrates creating a complete MySQL deployment with persistent storage and secret management on Kubernetes. All components will be deployed to the default namespace.

Prerequisites

  • Access to Kubernetes cluster via kubectl
  • kubectl configured to work with the target cluster
  • Sufficient cluster resources for MySQL deployment with 250Mi storage

Steps

Create the secrets manifest:

# Step 1: Create secrets
apiVersion: v1
kind: Secret
metadata:
name: mysql-root-pass
type: Opaque
stringData:
password: YUIidhb667
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-user-pass
type: Opaque
stringData:
username: kodekloud_top
password: B4zNgHA7Ya
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-db-url
type: Opaque
stringData:
database: kodekloud_db8

Create the volumes manifest for PersistentVolume and PersistentVolumeClaim:

# Step 2: Create PersistentVolume and PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 250Mi
accessModes:
- ReadWriteOnce
hostPath:
path: "/var/lib/mysql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 250Mi

Create the MySQL Deployment and Service manifest:

# Step 3: Create MySQL Deployment and Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
ports:
- name: mysql
containerPort: 3306
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
volumeMounts:
- name: mysql-pv
mountPath: /var/lib/mysql
volumes:
- name: mysql-pv
persistentVolumeClaim:
claimName: mysql-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: NodePort
selector:
app: mysql
ports:
- nodePort: 30007
targetPort: 3306
port: 3306

Apply all manifests:

# Apply secrets
kubectl apply -f secrets.yml

# Apply volumes
kubectl apply -f volumes.yml

# Apply deployment and service
kubectl apply -f mysql.yml

Verification

Verify that all resources are created and running:

# Check secrets
kubectl get secrets

# Check PersistentVolume and PersistentVolumeClaim
kubectl get pv
kubectl get pvc

# Check deployment and service
kubectl get deployment
kubectl get service mysql

# Check all resources
kubectl get all

# Verify environment variables in the pod
kubectl exec deployment/mysql-deployment -- printenv | grep MYSQL_

# Check pod logs
kubectl logs deployment/mysql-deployment

# Test MySQL connectivity (if accessible)
kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-test -- \
mysql -h mysql -p<password> -u<username> -e "SELECT VERSION();"

Resources