AndriyKalashnykov / k8s-mysql-init-data

Initializing MySQL Database with data on Kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docker Image CI Hits License: MIT

Initializing MySQL Database with data on Kubernetes

initContainers can be used to download SQL dump file and restore it to the MySQL database, which is hosted in another container

# other config removed for brevity.

spec:
  initContainers:
    - name: fetch
      image: akalashnykov/wget:1.0
      command:
        [
          "wget",
          "--no-check-certificate",
          "https://sample-videos.com/sql/Sample-SQL-File-1000rows.sql",
          "-O",
          "/docker-entrypoint-initdb.d/dump.sql",
        ]
      volumeMounts:
        - mountPath: /docker-entrypoint-initdb.d
          name: dump
    - name: mysql
  containers:
    - name: mysql
      image: mysql:8.0.19
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 3306
          name: mysql  
volumes:
  - name: dump
    emptyDir: {}

# other config removed for brevity.

MySQL configuration

  • Logins - ./k8s/mysql-cm.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mysql-config
      labels:
        app: mysql
    data:
      MYSQL_DATABASE: "your_database"
      MYSQL_USER: "your_user"
      default_auth: |
        [mysqld]
        default_authentication_plugin=mysql_native_password
  • Passwords - ./k8s/mysql-secret.yaml

    apiVersion: v1
    kind: Secret
    metadata:
      name: mysql-secrets
    type: Opaque
    data:
      MYSQL_PASSWORD: eW91cl9wYXNzd29yZA==
      MYSQL_ROOT_PASSWORD: cm9vdF9wYXNzd29yZA==

    Generate passwords

    cd ./scripts
    ./encode-pwd.sh

Pre-requisites

Optional - Start Minikube Kubernetes cluster

cd scripts
./start-cluster.sh

Build Docker image for MySQL client pod

Edit set-env.sh

vi ./set-env.sh

Set Docker registry credentials

# 1 - if deploying to local Minikube cluster, 0 - otherwise
export IS_MINIKUBE=0

export DOCKER_LOGIN=
export DOCKER_PWD=
export DOCKER_REGISTRY=registry-1.docker.io

Build Docker image

cd scripts
./build-images.sh

Push Docker image

cd scripts
./push-images.sh

Deploy

cd scripts
./deploy.sh

The above command creates a Pod that hosts two containers: the init container - fetch and the application container - mysql.

The init container is responsible for downloading the SQL file that contains the database dump. We use the akalahnykov/wget:1.0 image because we only need the wget command. The destination directory for the downloaded SQL is the directory used by the MySQL image to execute SQL files (/docker-entrypoint-initdb.d). This behavior is built into the MySQL image that we use in the application container. The init container mounts /docker-entrypoint-initdb.d to an emptyDir volume. Because both containers are hosted on the same Pod, they share the same volume and MySQL database container has access to the SQL dump file placed on the emptyDir volume.

The imported table user_details is in your_database schema.

Query table with imported data

Connect to MySQL server pod

kubectl exec -it mysql -n mysql /bin/bash

$ mysql -u root -D mysql -proot_password
mysql> SELECT * FROM your_database.user_details;
...
1000 rows in set (0.01 sec)
mysql> exit

or connect to MySQL from mysql-client pod

kubectl exec -it mysql-client -n mysql /bin/ash

$ mysql -h mysql -u your_user -D your_database -pyour_password
MySQL [your_database]> SELECT * FROM your_database.user_details;
...
1000 rows in set (0.01 sec)
mysql> exit

Cleanup

cd scripts
./undeploy.sh

Optional - Stop Minikube Kubernetes cluster

cd scripts
./stop-cluster.sh

About

Initializing MySQL Database with data on Kubernetes

License:MIT License


Languages

Language:Shell 100.0%