初始化 Doris 集群

本文介绍如何对 Kubernetes 上的 Doris 集群进行初始化 root, admin 账号和密码设置,以及批量自动执行 SQL 语句对数据库进行初始化。

Note
  • 以下功能只在 Doris 集群创建后第一次执行起作用,执行完以后再修改不会生效。
  • 初始化过程不依赖 root 帐户,创建 Doris 集群后可以随意手动修改 root 帐户密码。

配置 DorisInitializer

通过配置 DorisInitializer CR 来配置 Doris 集群的帐号密码、SQL 初始化行为。

A basic DorisInitializer CR sample

doris-initializer.yaml

apiVersion: al-assad.github.io/v1beta1
kind: DorisInitializer
metadata:
  name: basic-init
spec:
  # Target doris cluster name
  cluster: basic
  image: tnir/mysqlclient:1.4.6

  # The number of retries for the initialization script
  maxRetry: 3

  # Changes the default root & admin user password for Doris.(optional)
  rootPassword: ""
  adminPassword: ""

  # Doris initialization sql script.(optional)
  initSqlScript: |
    CREATE DATABASE IF NOT EXISTS DEMO;
    USE DEMO;
    CREATE TABLE IF NOT EXISTS demo.example_tbl(
      `user_id` LARGEINT NOT NULL,
      `date` DATE NOT NULL,
      `city` VARCHAR(20),
      `age` SMALLINT COMMENT,
      `sex` TINYINT COMMENT,
      `last_visit_date` DATETIME REPLACE DEFAULT "1970-01-01 00:00:00",
      `cost` BIGINT SUM DEFAULT "0",
      `max_dwell_time` INT MAX DEFAULT "0",
      `min_dwell_time` INT MIN DEFAULT "99999"
    )
      AGGREGATE KEY(`user_id`, `date`, `city`, `age`, `sex`)
      DISTRIBUTED BY HASH(`user_id`) BUCKETS 1
      PROPERTIES (
        "replication_allocation" = "tag.location.default: 1"
      );
    INSERT INTO example_tbl VALUES
      (1, '2021-01-01', 'Vienna', 18, 1, '2021-01-01 00:00:00', 20, 10, 10),
      (2, '2021-01-02', 'Frankfurt', 20, 1, '2021-04-01 6:00:00', 15, 2, 2),
      (3, '2021-05-02', 'Milan', 32, 0, '2021-07-01 23:00:00', 30, 11, 11),
      (4, '2021-06-12', 'Vienna', 22, 0, '2021-08-02 08:00:00', 11, 6, 6);    
A advanced DorisInitializer CR sample

doris-initializer.yaml

apiVersion: al-assad.github.io/v1beta1
kind: DorisInitializer
metadata:
  name: basic-init
spec:
  ## Target doris cluster name
  cluster: basic

  ## Doris initializer image, it's actually a mysql client image with python runtime.
  image: tnir/mysqlclient:1.4.6

  ## ImagePullPolicy of Doris monitor Pods
  ## Ref: https://kubernetes.io/docs/concepts/configuration/overview/#container-images
  # imagePullPolicy: IfNotPresent

  ## Ref: https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod
  # imagePullSecrets:
  # - name: secretName

  ## Specifies the service account for prometheus/grafana/loki/promtail components.
  # serviceAccount: ""

  ## Tolerations are applied to Doris cluster pods, allowing (but do not require) pods to be scheduled onto nodes
  ##  with matching taints.
  ## Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
  # tolerations: []

  ## Describes the compute resource requirements.
  ## Ref: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
  # requests:
  #   cpu: 100m
  #   memory: 100Mi
  # limits:
  #   cpu: 2000m
  #   memory: 2Gi

  # The number of retries for the initialization script.
  maxRetry: 3

  # Changes the default root & admin user password for Doris.(optional)
  rootPassword: ""
  adminPassword: ""

  # Doris initialization sql script content.(optional)
  initSqlScript: ""
Note
建议在 ${cluster_name} 目录下组织 Doris 集群的配置,并将其另存为 ${cluster_name}/doris-initializer.yaml

设置 Doris 集群的名称

${cluster_name}/doris-initializer.yaml 文件中,修改spec.cluster 字段为对应 DorisCluster CR 的 metadata.name:

spec:
  cluster: ${cluster_name}

设置初始化账号密码

集群创建时默认会创建 rootadmin 账号,但是密码为空,这会带来一些安全性问题。可以通过如下步骤设置初始密码。

spec:
  # ...
  rootPassword: "your password"
  adminPassword: "your password"

设置初始化 SQL 脚本

集群在初始化过程还可以自动执行 initSqlScript 中的 SQL 语句用于初始化,该功能可以用于默认给集群创建一些 database 或者 table,并且执行一些用户权限管理类的操作。

spec:
  # ...
  initSqlScript: |
    CREATE DATABASE app;
    GRANT ALL PRIVILEGES ON app.* TO 'developer'@'%';    

执行初始化

kubectl apply -f ${cluster_name}/doris-initializer.yaml --namespace=${namespace}

以上命令会自动创建一个初始化的 Job,初始化完成后 Pod 状态会变成 Completed。

查看初始化任务运行情况:

kubectl get dorisinitializer ${dorisinitializer.metadata.name} -n ${namespace} -o yaml