其他

StackStorm 介绍与入门

2017-07-12 作者:Philo Linux中国
StackStorm2 (ST2)是一种 DevOps 工具,包括顺序简单的工作流、 Mistral 工作流、触发器等。
-- Philo


本文导航
转载自 | http://www.philo.top/2017/04/30/IntroductionStackStorm1/
 作者 | Philo


背景

在日常运维工作中经常使用的脚本为了提供给别人用,为了降低出错概率与操作复杂度,所以这里介绍一下 StackStorm2 的最基本使用与安装。

在日常开发工作中很多积累下来的知识需要脚本化->自动化,在这一过程中需要做到知识可自动执行节省培训时间。

请先通读之后再做实践。

介绍

StackStorm2 (ST2)是一种 DevOps 工具,包括顺序简单的工作流、 Mistral 工作流、触发器等。

在下面这个列表中列出一些笔者认为能够快速直观的学习 ST2 的官方文档列表。 按照顺序读可以快速掌握使用方法方便接下来继续学习。可以节约很多时间。

  1. 功能介绍阅读[1]

  2. ST2 全局关系图,在学习的时候适合来回做参考[2]

  3. ST2 命令掌握,cli是最强大的,图形界面适合给别人调用你写的功能[3]

  4. 开始尝试写 action,写完了之后使用 st2 reload --register-all 注册你写的代码[4]

  5. 开始尝试把 action 结合起来一起运行[5]

快速安装用的 Dockerfile

请查看链接:https://github.com/lijianying10/FixLinux/tree/master/st2

构建流程描述:https://github.com/lijianying10/FixLinux/blob/master/st2/Dockerfile

  1. FROM ubuntu:16.04

  2. COPY st2ctl st2.conf supervisord.conf docker-entrypoint.sh

  3. RUN apt-get update && \

  4. # 安装基本组件

  5. apt-get install -y build-essential wget gnupg-curl curl sudo apache2-utils vim apt-utils supervisor && \

  6. # 安装 ST2

  7. os=ubuntu dist=xenial curl -s https://packagecloud.io/install/repositories/StackStorm/stable/script.deb.sh | sudo bash && \

  8. apt-get update && \

  9. apt-get install -y st2  && \

  10. # 安装nginx

  11. apt-key adv --fetch-keys http://nginx.org/keys/nginx_signing.key && \

  12. echo 'deb http://nginx.org/packages/ubuntu/ xenial nginx' >> /etc/apt/sources.list.d/nginx.list && \

  13. apt-get update && \

  14. apt-get install -y st2web nginx && \

  15. rm /etc/nginx/conf.d/default.conf && \

  16. cp /usr/share/doc/st2/conf/nginx/st2.conf /etc/nginx/conf.d/ && \

  17. # 安装kubectl tini

  18. curl -o /bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.6.1/bin/linux/amd64/kubectl && \

  19. chmod +x /bin/kubectl && \

  20. wget https://github.com/krallin/tini/releases/download/v0.14.0/tini-amd64 -O /bin/tini && \

  21. chmod +x /bin/tini && \

  22. # 复制配置文件

  23. mv /st2.conf /etc/st2/ && \

  24. mv supervisord.conf /etc/supervisor/supervisord.conf && \

  25. mv /st2ctl /usr/bin/st2ctl && \

  26. chmod +x /usr/bin/st2ctl && \

  27. # 安装python pip

  28. curl -SsL https://bootstrap.pypa.io/get-pip.py | python && \

  29. # 安装docker client

  30. pip install && \

  31. wget https://get.docker.com/builds/Linux/x86_64/docker-17.05.0-ce.tgz && \

  32. tar xf docker-17.05.0-ce.tgz && \

  33. mv docker/docker /bin/docker && \

  34. rm -rf docker docker-17.05.0-ce.tgz

  35. ENTRYPOINT ["/bin/tini", "--"]

  36. CMD bash /docker-entrypoint.sh

注意: 笔者裁剪掉了 chatops 以及 Mistral 的原因是在前期刚学习的时候并不能用上。还能节省大量配置时间。

在构建中加入了 entrypoint 脚本,是启动的时候执行的脚本流程描述如下:

启动流程描述: https://github.com/lijianying10/FixLinux/blob/master/st2/docker-entrypoint.sh

  1. # 生成SSH key

  2. echo generate ssh key

  3. ssh-keygen -f /root/.ssh/id_rsa -P "" && cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

  4. # 生成 ST2 账号密码

  5. echo generate user

  6. printf "%s\n" "${USER_NAME:?Need to set USER_NAME non-empty}"

  7. printf "%s\n" "${USER_PASSWORD:?Need to set USER_PASSWORD non-empty}"

  8. echo $USER_PASSWORD | sudo htpasswd -i /etc/st2/htpasswd $USER_NAME

  9. # 生成 证书

  10. echo generate cert

  11. sudo mkdir -p /etc/ssl/st2

  12. sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/st2/st2.key -out /etc/ssl/st2/st2.crt \

  13. -days 365 -nodes -subj "/C=US/ST=California/L=Palo Alto/O=StackStorm/OU=Information \

  14. Technology/CN=$(hostname)"

  15. # 检查环境变量是否完备

  16. printf "%s\n" "${CONN_RMQ:?Need to set CONN_RMQ non-empty}"

  17. printf "%s\n" "${MONGO_HOST:?Need to set MONGO_HOST non-empty}"

  18. printf "%s\n" "${MONGO_DB:?Need to set MONGO_DB non-empty}"

  19. printf "%s\n" "${MONGO_PORT:?Need to set MONGO_PORT non-empty}"

  20. # 生成st2配置文件

  21. cat >> /etc/st2/st2.conf <<EOF

  22. [system_user]

  23. user = root

  24. ssh_key_file = /root/.ssh/id_rsa

  25. [messaging]

  26. url = $CONN_RMQ

  27. [ssh_runner]

  28. remote_dir = /tmp

  29. [database]

  30. host = $MONGO_HOST

  31. port = $MONGO_PORT

  32. db_name = $MONGO_DB

  33. EOF

  34. # 启动supervisor

  35. /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

简单上手

容器化一次性安装启动(无负担安装最快上手)

  1. docker network create stn

  2. docker run -itd --hostname st2-mongo  --name st2-mongo  -v /var/lib/mongo:/data/db --net=stn daocloud.io/library/mongo:3.4.3

  3. docker run -itd --hostname st2-etcd --name st2-etcd --net=stn index.tenxcloud.com/coreos/etcd:2.3.1 /etcd -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 -advertise-client-urls http://127.0.0.1:2379,http://127.0.0.1:4001

  4. docker run -itd --hostname st2-rabbit --name st2-rabbit -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=123456 --net=stn daocloud.io/library/rabbitmq:3.6.9

  5. docker run -itd --hostname st2 --name st2 --net=stn -e USER_NAME=admin -e USER_PASSWORD=123456 -e CONN_RMQ=amqp://root:123456@st2-rabbit.stn:5672/ -e MONGO_HOST=st2-mongo.stn -e MONGO_DB=st2 -e MONGO_PORT=27017 -e ETCD_ENDPOINT=http://st2-etcd.stn:2379 -p 80:80 -p 443:443 index.tenxcloud.com/philo/stackstorm:2.2.1

注意: 笔者使用了 daocloud 和 tenxcloud 提供的 dockerhub 为大家提供服务。

注意: 笔者这持久化了 mongodb 的存储,如果不需要可删除挂载。

注意: etcd 用来存储全局变量。用作分布式锁。在未来使用 ST2 调度 kubernetes 中会介绍如何使用。

第一个action

经过之前的文档基本概念的了解,相信读者已经对系统概念有基本了解。 Action 是任务执行的最小单元。成功的学会 Shell 和Python 的 Action 和简单的 WorkFlow 就能够应付非常多的工作。

第一个shell action:

  1. #!/usr/bin/env bash

  2. SERVER=$1

  3. MESSAGE=$2

  4. echo ${SERVER} ${MESSAGE}

第一个action yaml 申明:

  1. ---

  2. name: "my_first_action"

  3. runner_type: "local-shell-cmd"

  4. description: "first automate"

  5. enabled: true

  6. entry_point: "first.sh"

  7. parameters:

  8.    server:

  9.        type: "string"

  10.        description: "server address"

  11.        required: true

  12.        position: 0

  13.    message:

  14.        type: "string"

  15.        description: "the information"

  16.        required: true

  17.        position: 1

注意: 在开始复制文档之前看清楚 pack 是如何组织架构开发的。运行后在容器中查看文件夹/opt/stackstorm/packs/packs来学习如何开发一个 pack 是最直观最有效的。

第一个 workflow:

注意: workflow 在 simple action 中只能串行执行,并且只有成功失败两个路径可以走。

有两个文件组成 workflow.yaml 在这里开发 workflow chain。

第二个文件是 xx.meta.yaml 申明 workflow。

case 请看参考文档 5。

总结

经过查看 Dockerimage 是如何构建与运行的,快速在你的服务器上运行 StackStorm2,到简单编写 Action 和 workflow 之后您就已经入门了 StackStorm2。 在未来的文章中我会继续更新更高级的应用方法。


推荐文章

< 左右滑动查看相关文章 >

点击图片、输入文章 ID 或识别二维码直达



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存