zoukankan      html  css  js  c++  java
  • Apache Dolphin Scheduler

    Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统。简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeeper,自身的服务模块包括:api, alert, master, worker(有一个 logger 服务,运行在 worker 中)等。详细部署可以参考:Docker 部署 Dolphin Scheduler

    官方提供了构建 Docker 镜像的 Dockerfile,位于项目的 docker/build/ 目录下,本文以 v1.3.8 版本为例,讲解 Dockerfile 内的具体内容

    Dockerfile 流程

    v1.3.8 docker/build/ 目录结构:

    docker/build
    ├── checkpoint.sh
    ├── conf
    │   └── dolphinscheduler
    │       ├── alert.properties.tpl
    │       ├── application-api.properties.tpl
    │       ├── common.properties.tpl
    │       ├── datasource.properties.tpl
    │       ├── env
    │       │   └── dolphinscheduler_env.sh.tpl
    │       ├── logback
    │       │   ├── logback-alert.xml
    │       │   ├── logback-api.xml
    │       │   ├── logback-master.xml
    │       │   └── logback-worker.xml
    │       ├── master.properties.tpl
    │       ├── quartz.properties.tpl
    │       ├── supervisor
    │       │   └── supervisor.ini
    │       ├── worker.properties.tpl
    │       └── zookeeper.properties.tpl
    ├── Dockerfile
    ├── hooks
    │   ├── build
    │   ├── build.bat
    │   ├── push
    │   └── push.bat
    ├── startup-init-conf.sh
    └── startup.sh
    

    Dockerfile 流程:

    1. 引用基础镜像,设置环境变量
    2. 安装所需的软件依赖,设置时区
    3. 添加 DS 的项目文件到容器中,设置工作目录
    4. 将所需的脚本和配置文件拷贝到容器内指定目录,并进行预先的处理
    5. 暴露网络端口
    6. 使用 Tini 进程管理器启动 startup.sh

    Dockerfile:

    FROM openjdk:8-jre-slim-buster
    
    # 需要在运行时使用 --build-arg <参数名>=<值> 覆盖 VERSION 变量
    ARG VERSION
    
    # 使得 apt-get 在安装过程不询问问题
    ARG DEBIAN_FRONTEND=noninteractive
    
    # 时区选择上海
    ENV TZ Asia/Shanghai
    # 修改编码集,解决 Docker 容器乱码
    ENV LANG C.UTF-8
    
    # 自定义环境变量,用于标识当前是 Docker 环境,在 dolphinscheduler-daemon.sh 中会引用该变量
    ENV DOCKER true
    
    ENV DOLPHINSCHEDULER_HOME /opt/dolphinscheduler
    
    # 设置 Debian 的源,这段代码默认是注释的,为了顺利下载依赖,需要放开注释
    RUN { 
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free"; 
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free"; 
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free"; 
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free"; 
    } > /etc/apt/sources.list
    
    # 使用 apt-get update 更新软件列表
    # 使用 --no-install-recommends 来避免安装非必须的文件,以减小镜像的体积
    RUN apt-get update && 
        apt-get install -y --no-install-recommends tzdata dos2unix python supervisor procps psmisc netcat sudo tini && 
        echo "Asia/Shanghai" > /etc/timezone && 
        rm -f /etc/localtime && 
        dpkg-reconfigure tzdata && 
        rm -rf /var/lib/apt/lists/* /tmp/*
    
    # 需要将 Maven 构建的压缩包 放在 Dockerfile 同级目录,使用 ADD 命令会将 tar 压缩包解压到容器内
    ADD ./apache-dolphinscheduler-${VERSION}-bin.tar.gz /opt/
    
    # 使用软链接将解压后的文件夹链接到 /opt/dolphinscheduler
    RUN ln -s -r /opt/apache-dolphinscheduler-${VERSION}-bin /opt/dolphinscheduler
    # 设置工作目录
    WORKDIR /opt/apache-dolphinscheduler-${VERSION}-bin
    
    # 拷贝配置文件到容器内指定位置
    COPY ./checkpoint.sh /root/checkpoint.sh
    COPY ./startup-init-conf.sh /root/startup-init-conf.sh
    COPY ./startup.sh /root/startup.sh
    COPY ./conf/dolphinscheduler/*.tpl /opt/dolphinscheduler/conf/
    COPY ./conf/dolphinscheduler/logback/* /opt/dolphinscheduler/conf/
    COPY ./conf/dolphinscheduler/supervisor/supervisor.ini /etc/supervisor/conf.d/
    COPY ./conf/dolphinscheduler/env/dolphinscheduler_env.sh.tpl /opt/dolphinscheduler/conf/env/
    
    # 使用 sed 替换配置中的文件后缀
    # 使用 dos2unix 将脚本中 
     转换为
    ,避免脚本执行错误
    # 删除 /bin/sh 使用 bash
    # echo PS1="\w \$ " >> ~/.bashrc:修改命令提示符设置(PS1) 格式
    # Set disable_coredump false:重新启用核心转储,以解决容器内使用 sudo 导致的 Operation not permitted 问题
    RUN sed -i 's/*.conf$/*.ini/' /etc/supervisor/supervisord.conf && 
        dos2unix /root/checkpoint.sh && 
        dos2unix /root/startup-init-conf.sh && 
        dos2unix /root/startup.sh && 
        dos2unix /opt/dolphinscheduler/script/*.sh && 
        dos2unix /opt/dolphinscheduler/bin/*.sh && 
        rm -f /bin/sh && 
        ln -s /bin/bash /bin/sh && 
        mkdir -p /tmp/xls && 
        echo PS1="\w \$ " >> ~/.bashrc && 
        echo "Set disable_coredump false" >> /etc/sudo.conf
    
    # 暴露端口
    EXPOSE 5678 1234 12345 50051
    
    # 使用 Tini 启动 /root/startup.sh
    ENTRYPOINT ["/usr/bin/tini", "--", "/root/startup.sh"]
    

    使用的基础镜像是:openjdk:8-jre-slim-buster

    使用到了 ARG 和 ENV 设置环境变量:ARG 设置的环境变量仅在构建期间可用,在将来容器运行时是不会存在的

    ENV DOCKER true 是为了标识当前是 Docker 环境,在dolphinscheduler-daemon.sh 中会引用该变量,添加 JVM 参数:-XX:-UseContainerSupport,这允许JVM 从主机读取 cgroup 限制,例如可用的 CPU 和 RAM,并进行相应的配置。这样当容器超过内存限制时,会抛出 OOM 异常,而不是杀死容器

    主要安装了这些依赖:tzdata dos2unix python supervisor procps psmisc netcat sudo tini

    使用 echo "Asia/Shanghai" > /etc/timezone && rm -f /etc/localtime && dpkg-reconfigure tzdata && 设置容器时区

    因为已经完成依赖安装了所以可以使用 rm -rf /var/lib/apt/lists/* /tmp/* 删除软件列表和临时目录,减小压镜像体积

    Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台 daemon,并监控进程状态,它的配置文件是:/etc/supervisor/supervisord.conf,配置中包含下面的配置,默认引入 /etc/supervisor/conf.d/ 下的所有 .conf 文件

    [include]
    files = /etc/supervisor/conf.d/*.conf
    

    Dockerfile 将自定义的配置文件拷贝到容器内指定的目录,其中:将conf/dolphinscheduler/supervisor/supervisor.ini 拷贝到 /etc/supervisor/conf.d/目录下,supervisor.ini 是 .ini 而 Supervisor 默认配置是 .conf,为了使 Supervisor 能够读取到自定义的配置,使用命令 sed -i 's/*.conf$/*.ini/' /etc/supervisor/supervisord.conf*.conf 替换为 *.ini,就变成了下面的样子

    [include]
    files = /etc/supervisor/conf.d/*.ini
    

    Windows格式文件的换行符为 ,而Unix&Linux文件的换行符为 ,dos2unix命令其实就是将文件中的 转换为

    使用 Tini 进程管理器,它是一个最小化到 init 系统,运行在容器内部,用于启动一个子进程,并等待进程退出时清理僵尸和执行信号转发,它是一个替代庞大复杂的 systemd 体系的解决方案

    startup.sh 主要做了这些事:

    1. 检查数据库连接
    2. 初始化数据库,执行 script/create-dolphinscheduler.sh,最终运行 org.apache.dolphinscheduler.dao.upgrade.shell.CreateDolphinScheduler Java 程序
    3. 检查 zookeeper 连接
    4. 初始化配置文件:执行 startup-init-conf.sh ,这个脚本定义了各个参数的默认配置,遍历所有 的 tpl 文件,填充配置的值,输出填充后的配置文件
    5. 读取执行 Shell 时传入的第一个参数 ($1),根据这个判断需要启动哪些服务
    6. 创建 logs 目录
    7. 启动 supervisord,supervisord 会间接读取 supervisor.ini 的配置,这里定义了每个服务启动的方式,通过调用 dolphinscheduler-daemon.sh 执行具体的启动逻辑

    构建镜像

    使用 打包 apache-dolphinscheduler-${VERSION}-bin.tar.gz 这里的 VERSION 为 1.3.8-SNAPSHOT

    mvn -U clean package -Prelease -Dmaven.test.skip=true
    

    将压缩包拷贝到 docker/build/ 下,使用 --build-arg 传入版本,构建 Docker 镜像

    docker build --build-arg VERSION=1.3.8-SNAPSHOT -t apache/dolphinscheduler:1.3.8-SNAPSHOT .
    

    参考资料

    When building from Dockerfile, Debian/Ubuntu package install debconf Noninteractive install not allowed
    Ubuntu的安装参数DEBIAN_FRONTEND详解
    容器环境的JVM内存设置最佳实践
    apt-get install 的参数(add-apt-repository)
    Dockerfile设置时区
    Docker 中如何设置 container 的时区
    Docker tini进程管理器
    Supervisor使用详解
    SED 简明教程
    linux系统终端命令提示符设置(PS1)记录
    sudo: setrlimit(RLIMIT_CORE): Operation not permitted
    What does the curly-brace syntax ${var%.*} mean?
    linux bash shell之变量替换::=句法、=句法、:-句法、-句法、=?句法、?句法、:+句法、+句法

  • 相关阅读:
    shell:bash基本特性
    python_day02
    python_day01
    centos环境下安装python3以及pip3
    http1到http3的巨大变化
    HTTP协议
    bootstrap
    JQuery
    BOM与DOM
    23种设计模式的几种常见的设计模式
  • 原文地址:https://www.cnblogs.com/aaronlinv/p/15308369.html
Copyright © 2011-2022 走看看