
flant/shell-operator
Shell-operator 是一个用于在 Kubernetes 集群中运行事件驱动脚本的工具。
该操作器不同于 prometheus-operator 或 kafka-operator 这类特定软件产品的操作器。Shell-operator 作为 Kubernetes 集群事件与 shell 脚本之间的集成层,将脚本视为由事件触发的钩子。可以将其理解为面向脚本的 operator-sdk。
Shell-operator 可用作更高级的 Addon-operator 的基础,后者支持 helm 图表和值存储。
Shell-operator 提供:
需具备 Kubernetes 集群,且 kubectl 已配置为与集群通信。
在集群中设置 Shell-operator 的步骤:
钩子是一种脚本,当使用 --config 选项执行时,会返回 JSON 格式的配置。了解更多关于钩子的信息。
创建一个监控所有命名空间中 Pod 的小型操作器,当有新 Pod 时记录其名称。
使用 "onKubernetesEvent" 绑定告知 shell-operator 要监控的对象。创建 pods-hook.sh 文件,内容如下:
bash#!/usr/bin/env bash if [[ $1 == "--config" ]] ; then cat <<EOF {"onKubernetesEvent": [ {"kind":"Pod", "event":["add"] } ]} EOF else podName=$(jq -r .[0].resourceName $BINDING_CONTEXT_PATH) echo "Pod '${podName}' added" fi
使 pods-hook.sh 可执行:
shellchmod +x pods-hook.sh
可使用预构建镜像 flant/shell-operator:latest(包含 bash、python、kubectl、jq 和 shell-operator 二进制文件)构建自定义镜像。只需在 Dockerfile 中将钩子添加到 /hooks 目录。
在 pods-hook.sh 所在目录创建以下 Dockerfile:
dockerfileFROM flant/shell-operator:latest ADD pods-hook.sh /hooks
构建镜像(根据 Docker 仓库修改镜像标签):
shelldocker build -t "registry.mycompany.com/shell-operator:monitor-pods" .
将镜像推送到 Kubernetes 集群可访问的 Docker 仓库:
shelldocker push registry.mycompany.com/shell-operator:monitor-pods
需监控所有命名空间的 Pod,需为 shell-operator 创建特定 RBAC 定义:
shellkubectl create namespace example-monitor-pods && kubectl create serviceaccount monitor-pods-acc \ --namespace example-monitor-pods && kubectl create clusterrole monitor-pods --verb=get,watch,list --resource=pods && kubectl create clusterrolebinding monitor-pods \ --clusterrole=monitor-pods \ --serviceaccount=example-monitor-pods:monitor-pods-acc
Shell-operator 可部署为 Pod。创建 shell-operator-pod.yaml 文件,内容如下:
yamlapiVersion: v1 kind: Pod metadata: name: shell-operator spec: containers: - name: shell-operator image: registry.mycompany.com/shell-operator:monitor-pods imagePullPolicy: Always serviceAccountName: monitor-pods-acc
应用 shell-operator-pod.yaml 文件启动 shell-operator:
shellkubectl -n example-monitor-pods apply -f shell-operator-pod.yaml
例如,部署 kubernetes-dashboard 触发 onKuberneteEvent:
shellkubectl apply -f [***]
运行 kubectl -n example-monitor-pods logs po/shell-operator,可看到钩子打印 dashboard Pod 名称:
... INFO : QUEUE add TASK_HOOK_RUN@KUBE_EVENTS pods-hook.sh INFO : TASK_RUN HookRun@KUBE_EVENTS pods-hook.sh INFO : Running hook 'pods-hook.sh' binding 'KUBE_EVENTS' ... Pod 'kubernetes-dashboard-769df5545f-99xsb' added ...
删除创建的对象:
shellkubectl delete ns example-monitor-pods && kubectl delete clusterrole monitor-pods && kubectl delete clusterrolebinding monitor-pods
此示例也可在 /examples 目录中找到:monitor-pods。
此绑定仅包含一个参数:执行顺序。启动时加载钩子,然后按参数定义的顺序执行带有 onStartup 绑定的钩子。了解更多关于 onStartup 绑定的信息。
示例 hook --config:
json{"onStartup":10}
此绑定用于定期运行钩子。调度可精确到秒级。了解更多关于 schedule 绑定的信息。
包含 2 个调度的 hook --config 示例:
json{ "schedule": [ {"name":"每 10 分钟", "schedule":"0 */10 * * * *", "allowFailure":true }, {"name":"每周一 8:05", "schedule":"0 5 8 * * 1" } ] }
此绑定定义 Shell-operator 要监控的 Kubernetes 对象子集,以及用于过滤对象属性的 jq 表达式。了解更多关于 onKubernetesEvent 绑定的信息。
注意:目前不支持自定义资源监控,参见 issue #14。
hook --config 示例:
json{ "onKubernetesEvent": [ {"name":"命名空间标签变化时执行", "kind": "namespace", "event":["update"], "jqFilter":".metadata.labels" }] }
Shell-operator 提供 /metrics 端点。更多信息参见 METRICS 文档。
更多示例可在 examples 目录中找到。
Apache License 2.0,参见 LICENSE。
manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务