Kubernetes mysql Installation (helm)

by Anish

Posted on Friday Jnauary 11, 2019

Referefce

Introduction

In this section we will learn how to install and setup mysql in kubernetes cluster using helm

  • First define the PersistentVolume mysql-pv where the mysql data to be stored. The hostPath tells the mysql directory is in /opt/mysql location
root@kube-master:# cat mysqldb-hostpath.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity: 
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /opt/mysql
  • Create PersistentVolume in kube cluster
root@kube-master:/home/ansible# kubectl create -f  mysqldb-hostpath.yaml 
persistentvolume/mysql-pv created
  • Check the PersistentVolume mysql-pv is available for use
root@kube-master:/home/ansible# kubectl get pv 
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGE
mysql-pv   1Gi        RWO,ROX        Retain           Available                                      15s
  • Define PersistentVolumeClaim mysql-pvc
root@kube-master:/home/ansible# cat mysqldb-pvc.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-pvc
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  • create PersistentVolumeClaim mysql-pvc in kube cluster
root@kube-master:/home/ansible# kubectl create -f  mysqldb-pvc.yaml 
persistentvolumeclaim/mysql-pvc created
  • Check the PersistentVolumeClaim mysql-pvc is bounded to mysql-pv
root@kube-master:/home/ansible# kubectl get pvc
NAME        STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pvc   Bound     mysql-pv   1Gi        RWO,ROX                       6s
  • MYSQL Installation starts here, this is done through helm the kubernetes package manager,

helm install --name mysql --set
mysqlRootPassword=rootpassword,mysqlUser=mysql,mysqlPassword=my-password,mysqlDatabase=mydatabase,persistence.existingClaim=mysql-pvc stable/mysql

while setting up mysql cluster the persistence.existingClaim=mysql-pvc is set which we have created earlier.

root@kube-master:# helm install --name mysql --set mysqlRootPassword=rootpassword,mysqlUser=mysql,mysqlPassword=my-password,mysqlDatabase=mydatabase,persistence.existingClaim=mysql-pvc stable/mysql

NAME:   mysql
LAST DEPLOYED: Thu Jan 10 16:18:31 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1beta1/Deployment
NAME   DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE
mysql  1        0        0           0          1s

==> v1/Pod(related)
NAME                    READY  STATUS   RESTARTS  AGE
mysql-7b688448b9-pxml5  0/1    Pending  0         1s

==> v1/Secret
NAME   TYPE    DATA  AGE
mysql  Opaque  2     1s

==> v1/ConfigMap
NAME        DATA  AGE
mysql-test  1     1s

==> v1/Service
NAME   TYPE       CLUSTER-IP     EXTERNAL-IP  PORT(S)   AGE
mysql  ClusterIP  10.100.139.57  <none>       3306/TCP  1s


NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
root@kube-master:# kubectl get pods  --watch
NAME                           READY     STATUS      RESTARTS   AGE
my-sql-mysql-95b597d5b-zd5pm   0/1       Init:0/1    0          5s
ubuntu                         0/1       Completed   0          7m
my-sql-mysql-95b597d5b-zd5pm   0/1       PodInitializing   0         12s
my-sql-mysql-95b597d5b-zd5pm   0/1       Running   0         13s
  • Once the my-sql POD is up and running , your mysql is ready to use
  • To get your root password run

  • root@kube-master:/home/ansible# MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
    root@kube-master:/home/ansible# echo $MYSQL_ROOT_PASSWORD
    rootpassword
    
    • To connect to your database directly from outside the K8s cluster
    kubectl port-forward svc/mysql 3306
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306
    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
    

    Video Demo


    Thanku for reading !!! Give a Share for Support


    Your Support Matters!

    Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!

    Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency




    python Cryptography Topics
    Topics
    For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

    Kubernetes for DevOps

    Hello Dockerfile

    Cryptography for Python Developers

    Cryptography for JavaScript Developers

    Go lang ryptography for Developers

    Here