
https://hub.docker.com/r/flant/shell-operator
Shell-operator 是一个用于在 Kubernetes 集群中运行事件驱动脚本的工具。
该操作器不同于 prometheus-operator 或 kafka-operator 这类特定软件产品的操作器。Shell-operator 作为 Kubernetes 集群事件与 shell 脚本之间的集成层,将脚本视为由事件触发的钩子。可以将其理解为面向脚本的 operator-sdk。
Shell-operator 可用作更高级的 https://github.com/flant/addon-operator 的基础,后者支持 helm 图表和值存储。
Shell-operator 提供:
需具备 Kubernetes 集群,且 kubectl 已配置为与集群通信。
在集群中设置 Shell-operator 的步骤:
钩子是一种脚本,当使用 --config 选项执行时,会返回 JSON 格式的配置。了解https://github.com/flant/shell-operator/blob/master/HOOKS.md%E5%85%B3%E4%BA%8E%E9%92%A9%E5%AD%90%E7%9A%84%E4%BF%A1%E6%81%AF%E3%80%82
创建一个监控所有命名空间中 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
可使用预构建镜像 https://hub.docker.com/r/flant/shell-operator%EF%BC%88%E5%8C%85%E5%90%AB 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 https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
运行 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 绑定的钩子。https://github.com/flant/shell-operator/blob/master/HOOKS.md#onstartup%E5%85%B3%E4%BA%8E onStartup 绑定的信息。
示例 hook --config:
json{"onStartup":10}
此绑定用于定期运行钩子。调度可精确到秒级。https://github.com/flant/shell-operator/blob/master/HOOKS.md#schedule%E5%85%B3%E4%BA%8E schedule 绑定的信息。
包含 2 个调度的 hook --config 示例:
json{ "schedule": [ {"name":"每 10 分钟", "schedule":"0 */10 * * * *", "allowFailure":true }, {"name":"每周一 8:05", "schedule":"0 5 8 * * 1" } ] }
此绑定定义 Shell-operator 要监控的 Kubernetes 对象子集,以及用于过滤对象属性的 https://github.com/stedolan/jq/ 表达式。https://github.com/flant/shell-operator/blob/master/HOOKS.md#onKubernetesEvent%E5%85%B3%E4%BA%8E onKubernetesEvent 绑定的信息。
注意:目前不支持自定义资源监控,参见 issue https://github.com/flant/shell-operator/issues/14%E3%80%82
hook --config 示例:
json{ "onKubernetesEvent": [ {"name":"命名空间标签变化时执行", "kind": "namespace", "event":["update"], "jqFilter":".metadata.labels" }] }
Shell-operator 提供 /metrics 端点。更多信息参见 https://github.com/flant/shell-operator/blob/master/METRICS.md 文档。
更多示例可在 https://github.com/flant/shell-operator/blob/master/examples/ 目录中找到。
Apache License 2.0,参见 https://github.com/flant/shell-operator/blob/master/LICENSE%E3%80%82
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
docker search 限制
站内搜不到镜像
离线 save/load
插件要用 plugin install
WSL 拉取慢
安全与 digest
新手拉取配置
镜像合规机制
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
域名连通性排查
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务