StackStorm 介紹與入門

Philo發表於2017-07-12

背景

在日常運維工作中經常使用的指令碼為了提供給別人用,為了降低出錯機率與操作複雜度,所以這裡介紹一下 StackStorm2 的最基本使用與安裝。

在日常開發工作中很多積累下來的知識需要指令碼化->自動化,在這一過程中需要做到知識可自動執行節省培訓時間。

請先通讀之後再做實踐。

介紹

StackStorm 介紹與入門

StackStorm2 (ST2)是一種 DevOps 工具,包括順序簡單的工作流、 Mistral 工作流、觸發器等。

在下面這個列表中列出一些筆者認為能夠快速直觀的學習 ST2 的官方文件列表。 按照順序讀可以快速掌握使用方法方便接下來繼續學習。可以節約很多時間。

  1. 功能介紹閱讀
  2. ST2 全域性關係圖,在學習的時候適合來回做參考
  3. ST2 命令掌握,cli是最強大的,圖形介面適合給別人呼叫你寫的功能
  4. 開始嘗試寫 action,寫完了之後使用 st2 reload --register-all 註冊你寫的程式碼
  5. 開始嘗試把 action 結合起來一起執行

快速安裝用的 Dockerfile

請檢視連結:https://github.com/lijianying10/FixLinux/tree/master/st2

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

FROM ubuntu:16.04
COPY st2ctl st2.conf supervisord.conf docker-entrypoint.sh 
RUN apt-get update && \
# 安裝基本元件
apt-get install -y build-essential wget gnupg-curl curl sudo apache2-utils vim apt-utils supervisor && \

# 安裝 ST2
os=ubuntu dist=xenial curl -s https://packagecloud.io/install/repositories/StackStorm/stable/script.deb.sh | sudo bash && \
apt-get update && \
apt-get install -y st2  && \

# 安裝nginx 
apt-key adv --fetch-keys http://nginx.org/keys/nginx_signing.key && \
echo 'deb http://nginx.org/packages/ubuntu/ xenial nginx' >> /etc/apt/sources.list.d/nginx.list && \
apt-get update && \
apt-get install -y st2web nginx && \
rm /etc/nginx/conf.d/default.conf && \
cp /usr/share/doc/st2/conf/nginx/st2.conf /etc/nginx/conf.d/ && \

# 安裝kubectl tini
curl -o /bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.6.1/bin/linux/amd64/kubectl && \
chmod +x /bin/kubectl && \
wget https://github.com/krallin/tini/releases/download/v0.14.0/tini-amd64 -O /bin/tini && \
chmod +x /bin/tini && \

# 複製配置檔案
mv /st2.conf /etc/st2/ && \
mv supervisord.conf /etc/supervisor/supervisord.conf && \
mv /st2ctl /usr/bin/st2ctl && \
chmod +x /usr/bin/st2ctl && \

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

# 安裝docker client
pip install && \
 wget https://get.docker.com/builds/Linux/x86_64/docker-17.05.0-ce.tgz && \
 tar xf docker-17.05.0-ce.tgz && \
mv docker/docker /bin/docker && \
rm -rf docker docker-17.05.0-ce.tgz

ENTRYPOINT ["/bin/tini", "--"]
CMD bash /docker-entrypoint.sh

注意: 筆者裁剪掉了 chatops 以及 Mistral 的原因是在前期剛學習的時候並不能用上。還能節省大量配置時間。

在構建中加入了 entrypoint 指令碼,是啟動的時候執行的指令碼流程描述如下:

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

# 生成SSH key
echo generate ssh key
ssh-keygen -f /root/.ssh/id_rsa -P "" && cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

# 生成 ST2 賬號密碼
echo generate user
printf "%s\n" "${USER_NAME:?Need to set USER_NAME non-empty}"
printf "%s\n" "${USER_PASSWORD:?Need to set USER_PASSWORD non-empty}"
echo $USER_PASSWORD | sudo htpasswd -i /etc/st2/htpasswd $USER_NAME

# 生成 證照
echo generate cert
sudo mkdir -p /etc/ssl/st2
sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/st2/st2.key -out /etc/ssl/st2/st2.crt \
-days 365 -nodes -subj "/C=US/ST=California/L=Palo Alto/O=StackStorm/OU=Information \
Technology/CN=$(hostname)"

# 檢查環境變數是否完備
printf "%s\n" "${CONN_RMQ:?Need to set CONN_RMQ non-empty}"
printf "%s\n" "${MONGO_HOST:?Need to set MONGO_HOST non-empty}"
printf "%s\n" "${MONGO_DB:?Need to set MONGO_DB non-empty}"
printf "%s\n" "${MONGO_PORT:?Need to set MONGO_PORT non-empty}"

# 生成st2配置檔案
cat >> /etc/st2/st2.conf <<EOF
[system_user]
user = root
ssh_key_file = /root/.ssh/id_rsa
[messaging]
url = $CONN_RMQ
[ssh_runner]
remote_dir = /tmp
[database]
host = $MONGO_HOST
port = $MONGO_PORT
db_name = $MONGO_DB
EOF

# 啟動supervisor
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

簡單上手

容器化一次性安裝啟動(無負擔安裝最快上手)

docker network create stn
docker run -itd --hostname st2-mongo  --name st2-mongo  -v /var/lib/mongo:/data/db --net=stn daocloud.io/library/mongo:3.4.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
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
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:

#!/usr/bin/env bash

SERVER=$1
MESSAGE=$2
echo ${SERVER} ${MESSAGE}

第一個action yaml 申明:

---
name: "my_first_action"
runner_type: "local-shell-cmd"
description: "first automate"
enabled: true
entry_point: "first.sh"
parameters:
    server:
        type: "string"
        description: "server address"
        required: true
        position: 0
    message:
        type: "string"
        description: "the information"
        required: true
        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。 在未來的文章中我會繼續更新更高階的應用方法。

相關文章