Kubernetes mariadb Installation (helm)

by Anish

Posted on Friday Jnauary 11, 2019

Referefce

Introduction

In this article we will learn how to to setup jenkins in kubernetes cluster using helm

Helm: Helm is a tool for managing Kubernetes charts. Charts are packages of pre-configured Kubernetes resources.

  • Let's Begin deploying mariadb using helm in kubernetes , if you are new to helm then download and initialize helm as follows
[email protected]:#  helm init
[email protected]:# kubectl create serviceaccount --namespace kube-system tiller
[email protected]:# kubectl create clusterrolebinding tiller-cluster-rule \
   --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
[email protected]:#  kubectl patch deploy --namespace kube-system tiller-deploy \
   -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
  • Make sure the title-deploy pod is up and running
[email protected]:/home/ansible# kubectl get pods -n kube-system 
NAME                                  READY     STATUS    RESTARTS   AGE
coredns-78fcdf6894-jvmlb              1/1       Running   0          1h
coredns-78fcdf6894-xstbn              1/1       Running   0          1h
etcd-kube-master                      1/1       Running   0          1h
kube-apiserver-kube-master            1/1       Running   0          1h
kube-controller-manager-kube-master   1/1       Running   0          1h
kube-flannel-ds-5gzn9                 1/1       Running   0          1h
kube-flannel-ds-tlc8j                 1/1       Running   0          1h
kube-proxy-kl4fg                      1/1       Running   0          1h
kube-proxy-krt6n                      1/1       Running   0          1h
kube-scheduler-kube-master            1/1       Running   0          1h
tiller-deploy-85744d9bfb-wh98g        1/1       Running   0          1h
  • Once titler pod is up and running, deploying mariadb uses bitnami docker images, for this we need to go and create PersistentVolume and PersistentVolumeClaim

  • Define the PersistentVolume for mariadb-pv where the mariadb data to be stored. The hostPath tells the mysql directory is in /bitnami/mariadb location

[email protected]:# cat mariadb-hostpath.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mariadb-pv
spec:
  capacity: 
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /bitnami/mariadb

Define the PersistentVolume for mariadbslave-pv where the mariadbslave data to be stored. The hostPath tells the mysql directory is in /bitnami/mariadbslave location

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mariadbslave-pv
spec:
  capacity: 
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /bitnami/mariadbslave
  • Create PersistentVolume in kube cluster
[email protected]:# kubectl create -f  mariadb-hostpath.yaml 
persistentvolume/mariadb-pv created

[email protected]:# kubectl create -f  mariadbslave-hostpath.yaml  
persistentvolume/mariadbslave-pv created
  • Check the PersistentVolume is available for use
[email protected]:/home/ansible# kubectl get pv 
NAME              CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                                  STORAGECLASS   REASON    AGE
mariadb-pv        1Gi        RWO,ROX        Retain           Bound     default/data-mariadb-mariadb-slave-0                            6m
mariadbslave-pv   1Gi        RWO,ROX        Retain           Bound     default/mariadb-pvc        
  • Define PersistentVolumeClaim mariadb-pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mariadb-pvc
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

  • Define PersistentVolumeClaim data-mariadb-mariadb-slave-0
[email protected]:# cat mariadbslave-pvc.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: data-mariadb-mariadb-slave-0
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  • Create PersistentVolumeClaim in kube cluster
[email protected]:# kubectl create -f  mariadb-pvc.yaml 
persistentvolumeclaim/mariadb-pvc created

[email protected]:# kubectl create -f  mariadbslave-pvc.yaml 
persistentvolumeclaim/data-mariadb-mariadb-slave-0 created
  • Check the PersistentVolumeClaim is bounded to mysql-pv
[email protected]:# kubectl get pvc
NAME                           STATUS    VOLUME            CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-mariadb-mariadb-slave-0   Bound     mariadb-pv        1Gi        RWO,ROX                       11s
mariadb-pvc                    Bound     mariadbslave-pv   1Gi        RWO,ROX                       25s                      6s
  • mariadb Installation starts here, this is done through helm the kubernetes package manager,
  • Download the mariadb required chart configuration file
[email protected]:# wget https://raw.githubusercontent.com/helm/charts/master/stable/mariadb/values.yaml
  • Edit the file and change to according to your need, for this demo i just change the master existingClaim value
# Enable persistence using an existing PVC
existingClaim: mariadb-pvc

while setting up mariadb cluster the persistence.existingClaim=jenkins-pvc is set which we have created earlier for manual change use the below setting

helm install --name mariadb --set
master.persistence.existingClaim=mariadb-pvc stable/mariadb

[email protected]:# helm install --name mariadb  -f values.yaml stable/mariadb
NAME:   mariadb
LAST DEPLOYED: Tue Jan 15 15:36:17 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1beta1/StatefulSet
NAME                    DESIRED  CURRENT  AGE
mariadb-mariadb-master  1        1        2s
mariadb-mariadb-slave   1        1        2s

==> v1/Pod(related)
NAME                      READY  STATUS             RESTARTS  AGE
mariadb-mariadb-master-0  0/1    ContainerCreating  0         2s
mariadb-mariadb-slave-0   0/1    ContainerCreating  0         2s

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

==> v1/ConfigMap
NAME                    DATA  AGE
mariadb-mariadb-master  1     2s
mariadb-mariadb-slave   1     2s
mariadb-tests           1     2s

==> v1/Service
NAME                   TYPE       CLUSTER-IP     EXTERNAL-IP  PORT(S)   AGE
mariadb                ClusterIP  10.108.173.99  <none>       3306/TCP  2s
mariadb-mariadb-slave  ClusterIP  10.99.252.152  <none>       3306/TCP  2s


NOTES:

Please be patient while the chart is being deployed

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace default -l release=mariadb

Services:

  echo Master: mariadb.default.svc.cluster.local:3306
  echo Slave:  mariadb-mariadb-slave.default.svc.cluster.local:3306

Administrator credentials:

  Username: root
  Password : $(kubectl get secret --namespace default mariadb -o jsonpath="{.data.mariadb-root-password}" | base64 --decode)

To connect to your database:

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

      kubectl run mariadb-client --rm --tty -i --restart='Never' --image  docker.io/bitnami/mariadb:10.1.37 --namespace default --command -- bash

  2. To connect to master service (read/write):

      mysql -h mariadb.default.svc.cluster.local -uroot -p my_database

  3. To connect to slave service (read-only):

      mysql -h mariadb-mariadb-slave.default.svc.cluster.local -uroot -p my_database

To upgrade this helm chart:

  1. Obtain the password as described on the 'Administrator credentials' section and set the 'rootUser.password' parameter as shown below:

      ROOT_PASSWORD=$(kubectl get secret --namespace default mariadb -o jsonpath="{.data.mariadb-root-password}" | base64 --decode)
      helm upgrade mariadb stable/mariadb --set rootUser.password=$ROOT_PASSWORD
  • View the mariadb container pod is up and running
[email protected]:# kubectl get pods --watch
NAME                       READY     STATUS    RESTARTS   AGE
mariadb-mariadb-master-0   1/1       Running   0          1m
mariadb-mariadb-slave-0    0/1       Running   4          1m
mariadb-mariadb-slave-0   1/1       Running   4         2m
  • Once the mariadb POD is up and running , your mariadb is ready to use

  • To get your root password of mariad run

[email protected]:/home/ansible# kubectl get secret --namespace default mariadb -o jsonpath="{.data.mariadb-root-password}" | base64 --decode
AsGt2o7JQ5
  • To connect to master service (read/write) within k8 cluster
I have no [email protected]:/$ mysql -h mariadb.default.svc.cluster.local -uroot -p my_database
Enter password:
**Welcome to the MariaDB monitor.  Commands end with ; or \g.**
**Your MariaDB connection id is 262**
**Server version: 10.1.37-MariaDB Source distribution**
**Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.**
**Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.**
MariaDB [my_database]>
  • To connect to slave service (read-only):
mysql -h mariadb-mariadb-slave.default.svc.cluster.local -uroot -p my_database

All right you have successfully created mariadb master/slave


Video Demo


Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively to support you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Go lang Cryptography for developers
  • Python Cryptography
  • Cryptography for JavaScript Developer


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

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here