Mysql kubernetes Deployment

Mysql is one of the most used backend server for data persistence. It can be deployed on kubernetes as a deployment or a StatefulSet. Deploying as Deployment and StatefulSet have pros and cons of the own. Usually Deployment is the way to g for development usecases. Scaleup and Scaledown is not generally supported or recommended. Mysql doesn’t support horizontal scaling without complicated mechanisms. Stateful Sets should be the way to go for production use cases.

Solutions like Vitess which support horizontal scaling of mysql. They should used for production use case. Use recreate strategy rather than rolling update is must when using mysql deployement. It eliminates issues like data loss when we need to do some upgrade on mysql deployment.

Persistent VolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

Mysql deployment

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-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

ClusterIP Service

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None