在 Drone 中使用 etcd 获取项目配置

上文有一个配置文件的发布问题待解决,于是考虑用轻量的 etcd 管理,并在发布的时候拉取配置,实践如下:

安装 etcd

  • docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
      etcd:
    # 官方镜像
    image: quay.io/coreos/etcd
    restart: always
    depends_on:
    - fluentd
    volumes:
    # 数据目录
    - /var/local/etcd:/etcd-data
    ports:
    # 开放端口用于 drone 构建过程访问
    - 2379:2379
    environment:
    # api 版本,不过服务器不需要指定改选项
    - ETCDCTL_API=3
    command:
    - /usr/local/bin/etcd
    - --data-dir=/etcd-data
    - --name
    - tool01
    # 通告节点地址,用于 e3w 访问
    - --initial-advertise-peer-urls
    - http://etcd:2380
    - --listen-peer-urls
    - http://0.0.0.0:2380
    # 通告节点接口地址,用于 e3w 访问
    - --advertise-client-urls
    - http://etcd:2379
    - --listen-client-urls
    - http://0.0.0.0:2379
    logging:
    driver: 'fluentd'
    options:
    fluentd-address: '127.0.0.1:24224'
    tag: 'etcd'

    # etcd 的 web 管理工具 https://github.com/soyking/e3w
    e3w:
    image: soyking/e3w
    restart: always
    depends_on:
    - fluentd
    - etcd
    volumes:
    # e3w 配置文件,后面给出文件内容
    - ./e3w.ini:/app/conf/config.default.ini
    ports:
    # 开方端口用于 nginx 反代,请在 nginx 上包一层认证
    - 9002:8080
    logging:
    driver: 'fluentd'
    options:
    fluentd-address: '127.0.0.1:24224'
    tag: 'e3w'
  • e3w.ini

    1
    2
    3
    4
    5
    6
    7
    8
    [app]
    port=8080
    auth=false

    [etcd]
    # 因为 e3w 使用 v3 版本 api 所以必须指定 root_key, 对应的 etcdctl 命令的访问方式也有所改变
    root_key=_
    addr=etcd:2379

使用

服务运行起来之后就可以通过 etcdctl 或者 e3w 设置配置了,这里我设置了一个 key 为 _/testing/app 的配置项。

接下来就可以需要 drone 中增加构建步骤

  • drone.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    pipeline:
    config-testing:
    # 使用官方镜像内置的 etcdctl 命令
    image: quay.io/coreos/etcd
    environment:
    # 使用 v3 版本 api
    - ETCDCTL_API=3
    commands:
    # 获取指定 api 路径下的指定 key 内容并输出到 .env 文件
    - etcdctl get _/testing/app --endpoints 'http://172.17.0.1:2379' --print-value-only > .env
    when:
    branch: master
  • 后续步骤就是将输出的配置文件通过 rsync 发布到目标服务器了

  • 付一张 e3w 界面截图: