自动配置DolphinScheduler的脚本

wen 实用脚本 25

本文目录导读:

自动配置DolphinScheduler的脚本

  1. 自动配置DolphinScheduler脚本
  2. 使用说明

我来为您提供一个自动配置DolphinScheduler的Shell脚本,这个脚本支持单机和集群两种部署模式。

自动配置DolphinScheduler脚本

#!/bin/bash
# DolphinScheduler自动配置脚本
# 支持单机和集群部署
set -e
# ==================== 配置参数 ====================
# 版本信息
DS_VERSION="3.2.1"
DS_DOWNLOAD_URL="https://dlcdn.apache.org/dolphinscheduler/${DS_VERSION}/apache-dolphinscheduler-${DS_VERSION}-bin.tar.gz"
# 安装路径
INSTALL_DIR="/opt/dolphinscheduler"
# 数据库配置(默认使用PostgreSQL)
DB_TYPE="postgresql"
DB_HOST="localhost"
DB_PORT="5432"
DB_NAME="dolphinscheduler"
DB_USER="dolphinscheduler"
DB_PASSWORD="dolphinscheduler"
# Zookeeper配置
ZK_CONNECT_STRING="localhost:2181"
# 注册中心类型(zookeeper or postgresql)
REGISTRY_TYPE="zookeeper"
# 部署模式:standalone 或 cluster
DEPLOY_MODE="standalone"
# 其他配置
DS_USER="dolphinscheduler"
DS_GROUP="dolphinscheduler"
PYTHON_HOME="/usr/bin/python3"
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
# ==================== 颜色定义 ====================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ==================== 辅助函数 ====================
info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
    echo -e "${RED}[ERROR]${NC} $1"
    exit 1
}
success() {
    echo -e "${BLUE}[SUCCESS]${NC} $1"
}
# ==================== 环境检查 ====================
check_environment() {
    info "检查运行环境..."
    # 检查root权限
    if [[ $EUID -ne 0 ]]; then
        error "此脚本需要root权限运行"
    fi
    # 检查Java
    if [[ -z "$(which java)" ]]; then
        error "未找到Java环境,请先安装Java 8或11"
    fi
    # 检查Python
    if [[ ! -f "$PYTHON_HOME" ]]; then
        warn "Python3路径 $PYTHON_HOME 不存在,尝试自动查找"
        PYTHON_HOME=$(which python3 2>/dev/null || which python 2>/dev/null)
        if [[ -z "$PYTHON_HOME" ]]; then
            error "未找到Python环境"
        fi
    fi
    # 检查系统包
    for cmd in wget tar sed awk; do
        if [[ -z "$(which $cmd)" ]]; then
            error "未找到 $cmd 命令,请安装相应的系统包"
        fi
    done
    success "环境检查通过"
}
# ==================== 系统准备 ====================
prepare_system() {
    info "准备系统环境..."
    # 创建用户和组
    if ! grep -q "^${DS_GROUP}:" /etc/group; then
        groupadd $DS_GROUP
    fi
    if ! id -u $DS_USER >/dev/null 2>&1; then
        useradd -g $DS_GROUP -m -s /bin/bash $DS_USER
    fi
    # 创建目录
    mkdir -p $INSTALL_DIR
    mkdir -p ${INSTALL_DIR}/logs
    mkdir -p ${INSTALL_DIR}/conf
    # 安装必要的系统包(Debian/Ubuntu)
    if [[ -f /etc/debian_version ]]; then
        apt-get update -y
        apt-get install -y wget tar sed awk python3 python3-pip
    # 安装必要的系统包(CentOS/RHEL)
    elif [[ -f /etc/redhat-release ]]; then
        yum install -y wget tar sed awk python3 python3-pip
    fi
    success "系统环境准备完成"
}
# ==================== 下载和解压 ====================
download_and_extract() {
    info "下载并解压DolphinScheduler..."
    local tmp_dir=$(mktemp -d)
    cd $tmp_dir
    if [[ ! -f "apache-dolphinscheduler-${DS_VERSION}-bin.tar.gz" ]]; then
        wget --progress=bar:force $DS_DOWNLOAD_URL 2>&1 | tail -f
    fi
    tar -xzf "apache-dolphinscheduler-${DS_VERSION}-bin.tar.gz" -C $INSTALL_DIR --strip-components=1
    # 设置权限
    chown -R $DS_USER:$DS_GROUP $INSTALL_DIR
    chmod -R 755 $INSTALL_DIR
    cd /
    rm -rf $tmp_dir
    success "下载和解压完成"
}
# ==================== 配置数据库 ====================
configure_database() {
    info "配置数据库..."
    local ds_home=$INSTALL_DIR
    # 修改 application.yaml
    local config_file="${ds_home}/conf/application.yaml"
    if [[ -f "$config_file" ]]; then
        # PostgreSQL配置
        sed -i "s|#.*spring.datasource.url.*jdbc:postgresql://.*|spring.datasource.url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}|" $config_file
        sed -i "s|#.*spring.datasource.username.*|spring.datasource.username: ${DB_USER}|" $config_file
        sed -i "s|#.*spring.datasource.password.*|spring.datasource.password: ${DB_PASSWORD}|" $config_file
        sed -i "s|#.*spring.datasource.driver-class-name.*|spring.datasource.driver-class-name: org.postgresql.Driver|" $config_file
    else
        warn "未找到application.yaml,将使用默认配置"
    fi
    success "数据库配置完成"
}
# ==================== 配置注册中心 ====================
configure_registry() {
    info "配置注册中心..."
    local config_file="${INSTALL_DIR}/conf/application.yaml"
    if [[ "$REGISTRY_TYPE" == "zookeeper" ]]; then
        # 配置Zookeeper
        if [[ -f "$config_file" ]]; then
            sed -i "s|#.*registry.zookeeper.connect-string.*|registry.zookeeper.connect-string: ${ZK_CONNECT_STRING}|" $config_file
            sed -i "s|#.*registry.type.*|registry.type: zookeeper|" $config_file
        fi
    elif [[ "$REGISTRY_TYPE" == "postgresql" ]]; then
        # 使用PostgreSQL作为注册中心
        if [[ -f "$config_file" ]]; then
            sed -i "s|#.*registry.type.*|registry.type: postgresql|" $config_file
        fi
    fi
    success "注册中心配置完成"
}
# ==================== 配置集群 ====================
configure_cluster() {
    if [[ "$DEPLOY_MODE" == "cluster" ]]; then
        info "配置集群模式..."
        # 配置Master节点
        local master_config="${INSTALL_DIR}/conf/env/dolphinscheduler_env.sh"
        if [[ -f "$master_config" ]]; then
            # 设置Master配置
            cat >> $master_config << EOF
# Master配置
export MASTER_LISTEN_PORT=5678
export MASTER_EXEC_THREADS=10
export MASTER_TASK_EXEC_THREADS=20
export MASTER_HEARTBEAT_INTERVAL=10
export MASTER_TASK_COMMIT_RETRY_TIMES=5
export MASTER_TASK_COMMIT_INTERVAL=1000
export MASTER_MAX_CPULOAD_AVG=-1
export MASTER_RESERVED_MEMORY=0.3
# Worker配置
export WORKER_LISTEN_PORT=1234
export WORKER_EXEC_THREADS=10
export WORKER_GROUP=default
export WORKER_HEARTBEAT_INTERVAL=10
EOF
        fi
        # 配置其他节点的连接信息
        # 这里可以根据实际需求配置其他节点
    fi
    success "集群配置完成"
}
# ==================== 初始化数据库 ====================
initialize_database() {
    info "初始化数据库..."
    cd $INSTALL_DIR
    # 执行数据库初始化脚本
    if [[ -f "tools/bin/upgrade-schema.sh" ]]; then
        sudo -u $DS_USER bash tools/bin/upgrade-schema.sh
        success "数据库初始化完成"
    else
        warn "未找到数据库初始化脚本,将使用自动初始化"
        # 使用内置的初始化功能
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start standalone-server &
        sleep 10
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh stop standalone-server
    fi
}
# ==================== 启动服务 ====================
start_service() {
    info "启动DolphinScheduler服务..."
    cd $INSTALL_DIR
    if [[ "$DEPLOY_MODE" == "standalone" ]]; then
        # 单机模式启动
        info "以单机模式启动..."
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start standalone-server
        # 等待服务启动
        sleep 5
        # 检查服务状态
        if sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh status standalone-server | grep -q "running"; then
            success "DolphinScheduler单机模式启动成功"
        else
            error "DolphinScheduler启动失败,请检查日志"
        fi
    else
        # 集群模式启动
        info "以集群模式启动..."
        # 启动Master节点
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start master-server
        # 启动Worker节点
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start worker-server
        # 启动API服务
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start api-server
        # 启动Alert服务
        sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh start alert-server
        sleep 5
        # 检查服务状态
        info "检查各服务状态..."
        for service in master-server worker-server api-server alert-server; do
            if sudo -u $DS_USER bash bin/dolphinscheduler-daemon.sh status $service | grep -q "running"; then
                success "$service 启动成功"
            else
                warn "$service 启动失败,请检查日志"
            fi
        done
    fi
}
# ==================== 显示配置信息 ====================
display_info() {
    echo ""
    echo -e "${BLUE}========================================${NC}"
    echo -e "${GREEN}DolphinScheduler 配置信息${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo -e "安装路径:      ${INSTALL_DIR}"
    echo -e "版本:          ${DS_VERSION}"
    echo -e "部署模式:      ${DEPLOY_MODE}"
    echo -e "数据库类型:    ${DB_TYPE}"
    echo -e "数据库地址:    ${DB_HOST}:${DB_PORT}"
    echo -e "数据库名称:    ${DB_NAME}"
    echo -e "注册中心:      ${REGISTRY_TYPE}"
    if [[ "$REGISTRY_TYPE" == "zookeeper" ]]; then
        echo -e "Zookeeper地址: ${ZK_CONNECT_STRING}"
    fi
    echo ""
    echo -e "Web UI访问地址: http://$(hostname -I | awk '{print $1}'):12345/dolphinscheduler"
    echo -e "默认管理员:    admin"
    echo -e "默认密码:      dolphinscheduler123"
    echo -e "${BLUE}========================================${NC}"
}
# ==================== 主函数 ====================
main() {
    echo -e "${BLUE}========================================${NC}"
    echo -e "${GREEN}DolphinScheduler 自动配置脚本${NC}"
    echo -e "${BLUE}========================================${NC}"
    # 处理命令行参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            --mode)
                DEPLOY_MODE="$2"
                shift 2
                ;;
            --db-host)
                DB_HOST="$2"
                shift 2
                ;;
            --db-port)
                DB_PORT="$2"
                shift 2
                ;;
            --db-name)
                DB_NAME="$2"
                shift 2
                ;;
            --db-user)
                DB_USER="$2"
                shift 2
                ;;
            --db-password)
                DB_PASSWORD="$2"
                shift 2
                ;;
            --zk-string)
                ZK_CONNECT_STRING="$2"
                REGISTRY_TYPE="zookeeper"
                shift 2
                ;;
            --help)
                echo "用法: $0 [选项]"
                echo "选项:"
                echo "  --mode <standalone|cluster>    部署模式"
                echo "  --db-host <host>               数据库主机"
                echo "  --db-port <port>               数据库端口"
                echo "  --db-name <name>               数据库名称"
                echo "  --db-user <user>               数据库用户"
                echo "  --db-password <password>       数据库密码"
                echo "  --zk-string <string>           Zookeeper连接字符串"
                echo "  --help                         显示帮助信息"
                exit 0
                ;;
            *)
                error "未知参数: $1"
                ;;
        esac
    done
    # 执行安装流程
    check_environment
    prepare_system
    download_and_extract
    configure_database
    configure_registry
    configure_cluster
    initialize_database
    start_service
    display_info
    success "DolphinScheduler 安装和配置完成!"
}
# 执行主函数
main "$@"

使用说明

基本用法

单机模式部署(默认):

# 直接运行
sudo bash install_dolphinscheduler.sh
# 或指定参数
sudo bash install_dolphinscheduler.sh --mode standalone

集群模式部署:

# 在Master节点运行
sudo bash install_dolphinscheduler.sh \
  --mode cluster \
  --db-host 192.168.1.100 \
  --db-port 5432 \
  --db-name dolphinscheduler \
  --db-user dolphinscheduler \
  --db-password your_password \
  --zk-string 192.168.1.100:2181,192.168.1.101:2181

自定义参数

# 完整参数示例
sudo bash install_dolphinscheduler.sh \
  --mode cluster \
  --db-host postgresql.example.com \
  --db-port 5432 \
  --db-name ds_prod \
  --db-user ds_admin \
  --db-password SecuredPass123! \
  --zk-string zk1:2181,zk2:2181,zk3:2181

前提条件

  • 操作系统: Linux(Ubuntu 20.04+/CentOS 7+/Debian 10+)
  • Java: JDK 8 或 11
  • Python: Python 3.6+
  • 数据库: PostgreSQL 或 MySQL
  • Zookeeper: 3.4.6+(集群模式需要)
  • 网络: 开放必要端口

注意事项

  1. 首次运行:确保脚本具有执行权限
  2. 数据库准备:需要提前创建好数据库
  3. 端口占用:确保以下端口未被占用:
    • 12345(Web UI)
    • 5678(Master)
    • 1234(Worker)
  4. 日志查看:如有问题查看 ${INSTALL_DIR}/logs/ 目录下的日志

验证安装

安装完成后,访问:

  • Web UI: http://<server-ip>:12345/dolphinscheduler
  • 默认账号: admin / dolphinscheduler123

这个脚本会自动处理大多数配置,您只需要根据实际环境调整数据库和Zookeeper的配置即可。

抱歉,评论功能暂时关闭!