Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Features] support initdb when create cluster #3665

Closed
ruijun2002 opened this issue Jun 9, 2023 · 3 comments · Fixed by #4182
Closed

[Features] support initdb when create cluster #3665

ruijun2002 opened this issue Jun 9, 2023 · 3 comments · Fixed by #4182

Comments

@ruijun2002
Copy link

ruijun2002 commented Jun 9, 2023

When setting up a database cluster, it is often necessary to initialize the databases, database users, and even the database schema. To achieve this, an OpsRequest defining the scripts to be executed during database initialization is required.

Here is an example,

apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: myops
  namespace: default
spec:
  # cluster name
  clusterRef: pg-cluster
  type: DataScript
  # After a period of time, the ops is automatically deleted
  ttlSecondsAfterSucceed: 600
  
  # Waiting for cluster is ready and exit when times out
  ttlsecondsBeforeAbort: 3600

  scriptSpec: 
      componentName: postgres
      script: |
        CREATE USER u1 IDENTIFIED BY "pwd";
        CREATE DATABASE test;
        USE test;
        CREATE TABLE IF NOT EXISTS model (
            id BIGSERIAL PRIMARY KEY,
            user_id VARCHAR(50) NOT NULL,
            q TEXT NOT NULL,
        );
        CREATE INDEX IF NOT EXISTS model_userId_index ON model USING HASH (user_id);

The user can also specify a secret from an sql script file created by kubectl create secret generic my-secret --from-file=path/init.sql command. This can hide sensitive information such as passwords. Here is an example,

apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: myops
  namespace: default
spec:
  clusterRef: pg-cluster
  type: DataScript
  ttlSecondsAfterSucceed: 600
  ttlsecondsBeforeAbort: 3600

  scriptSpec: 
     componentName: postgres
      scriptFrom:
        secretRefs:
          - name: my-secret
            key: init.sql
@shanshanying
Copy link
Contributor

It is more than init, we shall support create database, index, and users when cluster is ready (status is running), through a more generic way. Such as wrap these scripts through OpsRequest.

@shanshanying
Copy link
Contributor

APIs is under discussion and will be provided later.

@shanshanying
Copy link
Contributor

shanshanying commented Jul 14, 2023

test-datascript.zip
in the attachment are examples to test testscript ops:

  1. create configmap from file: mysql/createdb.txt, pg/createdb.txt
    using command `kubectl create cm --from-file
  2. create secret from file: mysql/createdb.txt, pg/createdb.txt
    using command kubectl create secret generic <name> --from-file <filename>
  3. create a cluster for mysql
  4. create ops for mysql using command kubectl apply -f mysql/mysqlops.yaml
  5. create a cluster for pg
  6. create ops for pg using command kubectl apply -f pg/pgops.yaml

We introduce a new API ttlSecondsBeforeAbort, so an ops can be created when cluster is still creating and this ops will wait till timeout for cluster to become running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants