参考了elasticsearch官网上给出的es部署文件,为MySQL和mongodb做的改写。
配合自定义的镜像包,实现了自动化创建用户、授权、备份、对接监控等功能。

MySQL:

version: '3.8'
services:
  mysql-pre-install:
    env_file:
      - .env
    image: ${REPO_PREFIX}/mysql-privatization:${STACK_VERSION}
    container_name: mysql-pre-install
    user: "0"
    volumes:
      - '${BASE_DIR}/mysql/prometheus-mysqld-exporter:/etc/init.d/prometheus-mysqld-exporter'
      - '${BASE_DIR}/mysql/.my_prom.cnf:/app/prometheus-mysqld-exporter/.my_prom.cnf'
      - '${BASE_DIR}/mysql/mysql-cron:/etc/cron.d/mysql-cron'
      - '${BASE_DIR}/mysql/my.cnf:/etc/mysql/my.cnf'
      - '${BASE_DIR}/mysql/customerized.sql:/app/customerized.sql'
    command: >
      bash -c '
        echo "Writting my.cnf.";
        echo -ne \
        "#添加mysql 配置\n"\
        > /etc/mysql/my.cnf;
        echo "Writting SQLs.";
        echo -ne \
        "#删除root远程用户\n"\
        "drop user if exists root@'${MYSQL_ACCOUNTS_HOSTSTRING}';\n"\
        "#管理员用户\n"\
        "create user if not exists '${MYSQL_MGTUSER}'@'${MYSQL_ACCOUNTS_HOSTSTRING}' identified by '${MYSQL_MGTPASS}';\n"\
        "grant all privileges on *.* to '${MYSQL_MGTUSER}'@'${MYSQL_ACCOUNTS_HOSTSTRING}' with grant option;\n"\
        "#archery用户\n"\
        "#备份用户\n"\
        "#监控用户\n"\
        "#删除多余数据库\n"\
        "drop database if exists mysql_test;\n"\
        "flush privileges;\n"\
        > /app/customerized.sql;
        echo "Configure exporter.";
        #配置mysqld_exporter服务
        #echo "$(sed '24d' /etc/init.d/prometheus-mysqld-exporter)" > /etc/init.d/prometheus-mysqld-exporter;
        #echo "$(sed '24i\ARGS=new' /etc/init.d/prometheus-mysqld-exporter)" > /etc/init.d/prometheus-mysqld-exporter;
        echo -ne \
        "[client]\n"\
        "host=${MYSQL_HOST}\n"\
        "user=${MYSQL_EXPUSER}\n"\
        "password=${MYSQL_EXPPASS}\n"\
        > /app/prometheus-mysqld-exporter/.my_prom.cnf;
        echo "Configure crontab.";
        echo -ne \
        "#(在这里添加crontab:xtrabackup/logswitch1)\n"\
        > /etc/cron.d/mysql-cron;
        chmod 0644 /etc/cron.d/mysql-cron;
        echo "All done!";
      '
    healthcheck:
      test: ["CMD-SHELL", "[ -d /etc/cron.d ]"]
      interval: 10s
      timeout: 10s
      retries: 3


  mysql:
    env_file:
      - .env
    image: ${REPO_PREFIX}/mysql-privatization:${STACK_VERSION}
    restart: always
    container_name: mysql
    user: "0"
    volumes:
      - '${BASE_DIR}/mysql/.my_prom.cnf:/app/prometheus-mysqld-exporter/.my_prom.cnf'
      - '${BASE_DIR}/mysql/mysql-cron:/etc/cron.d/mysql-cron'
      - '${BASE_DIR}/mysql/prometheus-mysqld-exporter:/etc/init.d/prometheus-mysqld-exporter'
      - '${BASE_DIR}/mysql/data:/var/lib/mysql'
      - '${BASE_DIR}/mysql/log:/var/log/mysql'
      - '${BASE_DIR}/mysql/my.cnf:/etc/mysql/my.cnf'
    ports:
      - ${MYSQL_PORT}:3306
    environment:
      TZ: Asia/Shanghai
    healthcheck:
      test: ["CMD-SHELL", "curl --silent localhost:3306 >/dev/null || exit 1"]
      interval: 10s
      timeout: 10s
      retries: 3
      
  mysql-post-install:
    depends_on:
      mysql:
        condition: service_healthy
    env_file:
      - .env
    image: ${REPO_PREFIX}/mysql-privatization:${STACK_VERSION}
    container_name: mysql-post-install
    user: "0"
    volumes:
      - '${BASE_DIR}/mysql/prometheus-mysqld-exporter:/etc/init.d/prometheus-mysqld-exporter'
      - '${BASE_DIR}/mysql/.my_prom.cnf:/app/prometheus-mysqld-exporter/.my_prom.cnf'
      - '${BASE_DIR}/mysql/my.cnf:/etc/mysql/my.cnf'
      - '${BASE_DIR}/mysql/customerized.sql:/app/customerized.sql'
      - '${BASE_DIR}/mysql/customerized.err:/app/customerized.err'
    command: >
      bash -c '
        echo "Waitting for MySQL availability.";
        until mysqladmin -uroot -p${MYSQL_ROOT_PASSWORD} ping -h"${MYSQL_HOST}" --silent; do sleep 5;done;
        echo "Executing SQLs.";
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -h"${MYSQL_HOST}" -P3306 < /app/customerized.sql 1>>/app/customerized.log 2>>/app/customerized.err;
        echo "All done!";
      '
    healthcheck:
      test: ["CMD-SHELL", "[ -f /app/customerized.sql ]"]
      interval: 10s
      timeout: 10s
      retries: 3

mongodb:

version: '3.3'
services:
  mongodb-pre-install:
    env_file:
      - .env
    image: ${REPO_PREFIX}/mongodb-privatization:${STACK_VERSION}
    container_name: mongodb-pre-install
    user: "0"
    volumes:
      - '${BASE_DIR}/mongodb/prometheus-mongodb-exporter:/etc/init.d/prometheus-mongodb-exporter'
      - '${BASE_DIR}/mongodb/prometheus-mongodb-exporter.template:/etc/init.d/prometheus-mongodb-exporter.template:ro'
      - '${BASE_DIR}/mongodb/mongodb-cron:/etc/cron.d/mongodb-cron'
      - '${BASE_DIR}/mongodb/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js'
    command: >
      bash -c '
        echo "Writting init js.";
        echo -ne \
        "db.createUser({\n"\
        "    user: '\'${MONGO_MGTUSER}\'',\n"\
        "    pwd: '\'${MONGO_MGTPASS}\'',\n"\
        "    roles: [\n"\
        "        {\n"\
        "            role: 'readWrite',\n"\
        "            db: 'testDB',\n"\
        "        },\n"\
        "    ],\n"\
        "});\n"\
        > /docker-entrypoint-initdb.d/init-mongo.js;
        echo "Configure exporter.";
        sed '25s/URL_PLACEHOLDER/\"xxxxx\"/' /etc/init.d/prometheus-mongodb-exporter.template > /etc/init.d/prometheus-mongodb-exporter;
        echo "Configure crontab.";
        echo -ne \
        "#(在这里添加crontab:xtrabackup/logswitch1)\n"\
        > /etc/cron.d/mongodb-cron;
        echo "All done!";
        '
    healthcheck:
      test: ["CMD-SHELL", "[ -f /etc/cron.d/mongodb-cron ]"]
      interval: 10s
      timeout: 10s
      retries: 3

  mongodb:
    env_file:
      - .env
    image: ${REPO_PREFIX}/mongodb-privatization:${STACK_VERSION}
    restart: always
    container_name: mongodb
    user: "0"
    privileged: true
    volumes:
      - '${BASE_DIR}/mongodb/prometheus-mongodb-exporter:/etc/init.d/prometheus-mongodb-exporter'
      - '${BASE_DIR}/mongodb/prometheus-mongodb-exporter.template:/etc/init.d/prometheus-mongodb-exporter.template:ro'
      - '${BASE_DIR}/mongodb/mongodb-cron:/etc/cron.d/mongodb-cron'
      - '${BASE_DIR}/mongodb/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js'
      - '${BASE_DIR}/mongodb/data/db:/data/db'
      - '${BASE_DIR}/mongodb/data/configdb:/data/configdb'
    ports:
      - ${MONGO_PORT}:27017
    healthcheck:
      test: ["CMD-SHELL", "curl --silent localhost:27017 >/dev/null || exit 1"]
      interval: 10s
      timeout: 10s
      retries: 3

Grainy
1 声望1 粉丝