Thursday, July 4, 2019

UBUNTU18+KVM+VAGRANT+KUBERNETES 6) Provisioning local-disk and mariadb-master install as a pod

UBUNTU18+KVM+VAGRANT+KUBERNETES 6) Provisioning local-disk and mariadb-master install as a pod(https://www.youtube.com/watch?v=07subgHDTIM&feature=youtu.be)

HI.!
This article shows how to provision and install mariadb-master using local-disk provisioning.(k8s)
In this case, pod must be running the server(node) that has local-disk(physically).

!!!If you want to know more about local-disk provisioning visit below site!!!
https://kubernetes.io/blog/2019/04/04/kubernetes-1.14-local-persistent-volumes-ga/

Creating mariadb-master server.(storage is local-disk provision)
1)Creating storage class(sc) and persistent volumes for mariadb-master
2) Create service for mariadb-master
3) Creating configmap for maraidb-config cnf for master service.
4) Create Stateful mariadb-master
5) mysql connection test.


1)Creating storage class(sc) and persistent volumes for mariadb-master

[vagrant@kubemaster mariadb-master]$ alias | grep kb
alias kb='kubectl'
[vagrant@kubemaste
[vagrant@kubemaster ~]$ kb get pv
No resources found.
[vagrant@kubemaster ~]$ ls
[vagrant@kubemaster ~]$ mkdir mariadb-master


[vagrant@kubemaster ~]$ cd mariadb-master/
[vagrant@kubemaster mariadb-master]$ ls
[vagrant@kubemaster mariadb-master]$ vi mariadb-master-sc.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: mariadb-master-sc
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

[vagrant@kubemaster mariadb-master]$ kb create -f mariadb-master-sc.yaml


storageclass.storage.k8s.io/mariadb-master-sc created

[vagrant@kubemaster mariadb-master]$ kb describe sc mariadb-master-sc
Name:                  mariadb-master-sc
IsDefaultClass:        No
Annotations:           <none>
Provisioner:           kubernetes.io/no-provisioner
Parameters:            <none>
AllowVolumeExpansion:  <unset>
MountOptions:          <none>
ReclaimPolicy:         Delete
VolumeBindingMode:     WaitForFirstConsumer
Events:                <none>
[vagrant@kubemaster mariadb-master]$

[vagrant@kubemaster mariadb-master]$ vi mariadb-master-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mariadb-master-disk
spec:
  capacity:
    storage: 1Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: mariadb-master-sc
  local:
    path: /db1
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - kubeworker1
[vagrant@kubemaster mariadb-master]$ kb create -f mariadb-master-pv.yaml
persistentvolume/mariadb-master-disk created
[vagrant@kubemaster mariadb-master]$

[vagrant@kubemaster mariadb-master]$ kb get pv
NAME                  CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS        REASON   AGE
mariadb-master-disk   1Gi        RWO            Recycle          Available           mariadb-master-sc            5m57s


2) Create service for mariadb-master
[vagrant@kubemaster mariadb-master]$ vi mariadb-master-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mariadb-master
  labels:
    app: mariadb-master
spec:
  ports:
  - name: mariadb-master
    port: 3306
  clusterIP: None
  selector:
    app: mariadb-master
~                                 

[vagrant@kubemaster mariadb-master]$ kb create -f mariadb-master-svc.yaml
service/mariadb-master created
[vagrant@kubemaster mariadb-master]$ kb get svc
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes       ClusterIP   10.96.0.1    <none>        443/TCP    5d7h
mariadb-master   ClusterIP   None         <none>        3306/TCP   7s

[vagrant@kubemaster mariadb-master]$ kb describe svc mariadb-master
Name:              mariadb-master
Namespace:         default
Labels:            app=mariadb-master
Annotations:       <none>
Selector:          app=mariadb-master
Type:              ClusterIP
IP:                None
Port:              mariadb-master  3306/TCP
TargetPort:        3306/TCP
Endpoints:         <none>
Session Affinity:  None
Events:            <none>

3) Creating configmap for maraidb-config cnf for master service.

[vagrant@kubemaster mariadb-master]$ vi configmap-mariadb-master.cnf
# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf
#
[client]
#  Default is Latin1, if you need UTF-8 set this (also in server section)
# default-character-set = utf8
#
[mysqld]
#master server for replication
log-bin
server_id=1
log-basename=master1
# * Character sets
#  Default is Latin1, if you need UTF-8 set all this (also in client section)
character_set_server   = utf8
collation_server       = utf8_general_ci
#
[mysqld_safe]
skip_log_error
syslog

[mariadb]
# See https://mariadb.com/kb/en/how-to-enable-tokudb-in-mariadb/
# # for instructions how to enable TokuDB
# #
# # See https://mariadb.com/kb/en/tokudb-differences/ for differences
# # between TokuDB in MariaDB and TokuDB from http://www.tokutek.com/
#
# #plugin-load-add=ha_tokudb.so


[vagrant@kubemaster mariadb-master]$ kb create configmap mariadb-master.cf --from-file=configmap-mariadb-master.cnf
configmap/mariadb-master.cf created
[vagrant@kubemaster mariadb-master]$ kb describe configmap mariadb-master.cf
Name:         mariadb-master.cf
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
configmap-mariadb-master.cnf:
----
# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf
#
[client]
#  Default is Latin1, if you need UTF-8 set this (also in server section)
# default-character-set = utf8
#
[mysqld]
#master server for replication
log-bin
server_id=1
log-basename=master1
# * Character sets
#  Default is Latin1, if you need UTF-8 set all this (also in client section)
character_set_server   = utf8
collation_server       = utf8_general_ci
#
[mysqld_safe]
skip_log_error
syslog

[mariadb]
# See https://mariadb.com/kb/en/how-to-enable-tokudb-in-mariadb/
# # for instructions how to enable TokuDB
# #
# # See https://mariadb.com/kb/en/tokudb-differences/ for differences
# # between TokuDB in MariaDB and TokuDB from http://www.tokutek.com/
#
# #plugin-load-add=ha_tokudb.so

Events:  <none>


4) Create Stateful mariadb-master
[vagrant@kubemaster mariadb-master]$ vi kustomization.yaml
ecretGenerator:
- name: mariadb-pass
  literals:
  - password=StrongPass$^^$
- name: rep-user
  literals:
  - rep-user=rep-user
- name: rep-password
  literals:
  - rep-password=Good$^Password!
resources:
  - mariadb-master.yaml


[vagrant@kubemaster mariadb-master]$ vi mariadb-master.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mariadb-master
spec:
  selector:
    matchLabels:
      app: mariadb-master
  serviceName: mariadb-master
  replicas: 1
  template:
    metadata:
      labels:
        app: mariadb-master
    spec:
      containers:
      - name: mariadb-master
        image: ohyoungjooung2/mariadb:10.1.14-master
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
               name: mariadb-pass
               key: password
        - name: MYSQL_REPLICATION_USER
          valueFrom:
            secretKeyRef:
               name: rep-user
               key: rep-user

        - name: MYSQL_REPLICATION_PASSWORD
          valueFrom:
            secretKeyRef:
               name: rep-password
               key: rep-password
        ports:
        - name: mariadb-master
          containerPort: 3306
        volumeMounts:
        - name: mariadb-master-st
          mountPath: /var/lib/mysql
          subPath: mariadb
        - name: mariadb-master-cnf
          mountPath: /etc/mysql/conf.d

        resources:
          requests:
            cpu: 500m
            memory: 1Gi
   
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "localhost","-pStrongPass$^^$","-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1

      volumes:
      - name: mariadb-master-cnf
        configMap:
          name: mariadb-master.cf

       
      
  volumeClaimTemplates:
  - metadata:
      name: mariadb-master-st
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "mariadb-master-sc"
      resources:
        requests:
          storage: 1Gi


[vagrant@kubemaster mariadb-master]$ kb apply -k .
secret/mariadb-pass-hmt2hb8m6g created
secret/rep-password-8bhkm96dbt created
secret/rep-user-bttd79k4t8 created
statefulset.apps/mariadb-master created



[vagrant@kubemaster mariadb-master]$ kb get po
NAME               READY   STATUS              RESTARTS   AGE
mariadb-master-0   0/1     ContainerCreating   0          3s
[vagrant@kubemaster mariadb-master]$ kb get po
NAME               READY   STATUS    RESTARTS   AGE
mariadb-master-0   1/1     Running   0          92s


[vagrant@kubemaster mariadb-master]$ kb describe po mariadb-master-0
Name:           mariadb-master-0
Namespace:      default
Priority:       0
Node:           kubeworker1/192.168.121.6
Start Time:     Thu, 04 Jul 2019 17:43:46 +0000
Labels:         app=mariadb-master
                controller-revision-hash=mariadb-master-5cdc8d5bfc
                statefulset.kubernetes.io/pod-name=mariadb-master-0
Annotations:    <none>
Status:         Running
IP:             10.244.2.3
Controlled By:  StatefulSet/mariadb-master
Containers:
  mariadb-master:
    Container ID:   docker://854f12654c97984d383663a1ad38e691f11cee66c3ae25a2b2ac86f0b95df996
    Image:          ohyoungjooung2/mariadb:10.1.14-master
    Image ID:       docker-pullable://ohyoungjooung2/mariadb@sha256:1a1011fb68a7d94c30aa60a74749c439041522813ab1649c3033cb846946cc75
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 04 Jul 2019 17:43:47 +0000
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:      500m
      memory:   1Gi
    Liveness:   exec [mysqladmin ping] delay=30s timeout=5s period=10s #success=1 #failure=3
    Readiness:  exec [mysql -h localhost -pStrongPass$^^$ -e SELECT 1] delay=5s timeout=1s period=2s #success=1 #failure=3
    Environment:
      MYSQL_ROOT_PASSWORD:         <set to the key 'password' in secret 'mariadb-pass-hmt2hb8m6g'>      Optional: false
      MYSQL_REPLICATION_USER:      <set to the key 'rep-user' in secret 'rep-user-bttd79k4t8'>          Optional: false
      MYSQL_REPLICATION_PASSWORD:  <set to the key 'rep-password' in secret 'rep-password-8bhkm96dbt'>  Optional: false
    Mounts:
      /etc/mysql/conf.d from mariadb-master-cnf (rw)
      /var/lib/mysql from mariadb-master-st (rw,path="mariadb")
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-7pn58 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  mariadb-master-st:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mariadb-master-st-mariadb-master-0
    ReadOnly:   false
  mariadb-master-cnf:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mariadb-master.cf
    Optional:  false
  default-token-7pn58:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-7pn58
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                  Message
  ----    ------     ----  ----                  -------
  Normal  Scheduled  32s   default-scheduler     Successfully assigned default/mariadb-master-0 to kubeworker1
  Normal  Pulled     31s   kubelet, kubeworker1  Container image "ohyoungjooung2/mariadb:10.1.14-master" already present on machine
  Normal  Created    30s   kubelet, kubeworker1  Created container mariadb-master
  Normal  Started    30s   kubelet, kubeworker1  Started container mariadb-master

5) mysql connection test.
root@mariadb-master-0:/# mysql -u root -p -h localhost
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 514
Server version: 10.1.14-MariaDB-1~jessie mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW MASTER STATUS\G;
*************************** 1. row ***************************
            File: master1-bin.000006
        Position: 329
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [(none)]> SELECT user,host,password from mysql.user;
+----------+------+-------------------------------------------+
| user     | host | password                                  |
+----------+------+-------------------------------------------+
| root     | %    | *D674B2C0175FB3763DD6952371BE0DA43805D876 |
| rep-user | %    | *7AA91BE9EE4FF62D8C576A7CA3B4CCBFD3F9EB19 |
+----------+------+-------------------------------------------+
2 rows in set (0.01 sec)




Conclusion)
In this small article, I introduce how to provisioing mariadb master server using pod.
In real production, database server's installation would be better on bare metal server, but this solution could be considered if someone like
all pod and container native.

Next). Two pod that replicate this master server(mariadb of course).

If you know more about statefulset, please visit https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/



No comments:

Post a Comment