Skip to main content

mariadb

ben.wangzAbout 1 min

mariadb

prepare

  1. k8s is ready
  2. argocd is ready and logged in
  3. (optional) ingress is ready
    • only required by phpmyadmin in the tests
  4. (optional) cert-manager is ready
    • the clusterissuer named self-signed-ca-issuer is ready
    • only required by phpmyadmin in the tests
  5. (optional) kube-prometheus-stack is ready
    • only required by with-metrics feature
    • the kube-prometheus-stack is installed in the monitor namespace
    • the serviceMonitorNamespaceSelector is {}
    • the matchLabels of serviceMonitorSelector is release: kube-prometheus-stack
    • grafana is exposed by ingress
      • https://grafana.dev.geekcity.tech:32443/

installation

  1. prepare mariadb.yaml
    • simple
      apiVersion: argoproj.io/v1alpha1
      kind: Application
      metadata:
        name: mariadb
      spec:
        syncPolicy:
          syncOptions:
          - CreateNamespace=true
        project: default
        source:
          repoURL: https://charts.bitnami.com/bitnami
          chart: mariadb
          targetRevision: 16.3.2
          helm:
            releaseName: mariadb
            values: |
              architecture: standalone
              auth:
                database: geekcity
                username: ben.wangz
                existingSecret: mariadb-credentials
              primary:
                extraFlags: "--character-set-server=utf8mb4 --collation-server=utf8mb4_bin"
                persistence:
                  enabled: false
              secondary:
                replicaCount: 1
                persistence:
                  enabled: false
              image:
                registry: docker.io
                pullPolicy: IfNotPresent
              volumePermissions:
                enabled: false
                image:
                  registry: docker.io
                  pullPolicy: IfNotPresent
              metrics:
                enabled: false
                image:
                  registry: docker.io
                  pullPolicy: IfNotPresent
        destination:
          server: https://kubernetes.default.svc
          namespace: database
      
      
  2. prepare credentials secret
    • kubectl get namespaces database > /dev/null 2>&1 || kubectl create namespace database
      kubectl -n database create secret generic mariadb-credentials \
          --from-literal=mariadb-root-password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 16) \
          --from-literal=mariadb-replication-password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 16) \
          --from-literal=mariadb-password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 16)
      
  3. apply to k8s
    • kubectl -n argocd apply -f mariadb.yaml
      
  4. sync by argocd
    • argocd app sync argocd/mariadb
      
  5. expose interface
    1. prepare mariadb-expose.yaml
      • apiVersion: v1
        kind: Service
        metadata:
          labels:
            app.kubernetes.io/component: primary
            app.kubernetes.io/instance: mariadb
          name: mariadb-expose
        spec:
          ports:
          - name: mysql
            port: 3306
            protocol: TCP
            targetPort: mysql
            nodePort: 32306
          selector:
            app.kubernetes.io/component: primary
            app.kubernetes.io/instance: mariadb
            app.kubernetes.io/name: mariadb
          type: NodePort
        
        
    2. apply to k8s
      • kubectl -n database apply -f mariadb-expose.yaml
        

tests with cli

  1. with root user
    • ROOT_PASSWORD=$(kubectl -n database get secret mariadb-credentials -o jsonpath='{.data.mariadb-root-password}' | base64 -d)
      podman run --rm \
          -e MYSQL_PWD=${ROOT_PASSWORD} \
          -it docker.io/library/mariadb:11.2.2-jammy \
          mariadb \
          --host host.containers.internal \
          --port 32306 \
          --user root \
          --database mysql \
          --execute 'show databases'
      
  2. with normal user
    • PASSWORD=$(kubectl -n database get secret mariadb-credentials -o jsonpath='{.data.mariadb-password}' | base64 -d)
      podman run --rm \
          -e MYSQL_PWD=${PASSWORD} \
          -it docker.io/library/mariadb:11.2.2-jammy \
          mariadb \
          --host host.containers.internal \
          --port 32306 \
          --user ben.wangz \
          --database geekcity \
          --execute 'show databases'
      

test with phpmyadmin

  1. prepare phpmyadmin.yaml
    • apiVersion: argoproj.io/v1alpha1
      kind: Application
      metadata:
        name: phpmyadmin
      spec:
        syncPolicy:
          syncOptions:
          - CreateNamespace=true
        project: default
        source:
          repoURL: https://charts.bitnami.com/bitnami
          chart: phpmyadmin
          targetRevision: 14.5.2
          helm:
            releaseName: phpmyadmin
            values: |
              image:
                registry: docker.io
                pullPolicy: IfNotPresent
              replicas: 1
              ingress:
                enabled: true
                annotations:
                  cert-manager.io/cluster-issuer: self-signed-ca-issuer
                  nginx.ingress.kubernetes.io/rewrite-target: /$1
                hostname: phpmyadmin.dev.geekcity.tech
                ingressClassName: nginx
                path: /?(.*)
                tls: true
              metrics:
                enabled: false
                image:
                  registry: docker.io
                  pullPolicy: IfNotPresent
        destination:
          server: https://kubernetes.default.svc
          namespace: database
      
      
  2. apply to k8s
    • kubectl -n argocd apply -f phpmyadmin.yaml
      
  3. sync by argocd
    • argocd app sync argocd/phpmyadmin
      
  4. open with browser: https://phpmyadmin.dev.geekcity.tech:32443
    • phpmyadmin.dev.geekcity.tech should be resolved to nginx-ingress
      • for example, add $K8S_MASTER_IP phpmyadmin.dev.geekcity.tech to /etc/hosts
  5. login
    • server: mariadb.database:3306
    • username: root
      • password
        • kubectl -n database get secret mariadb-credentials -o jsonpath='{.data.mariadb-root-password}' | base64 -d
          
    • username: ben.wangz
      • password
        • kubectl -n database get secret mariadb-credentials -o jsonpath='{.data.mariadb-password}' | base64 -d
          

dashboard from grafana

  • only for with-metrics feature
  • open with browser and login grafana
  • import dashboard
    • https://grafana.dev.geekcity.tech:32443/dashboard/import
    • import dashboard with uid 14057