
如果你使用 DeepSeek、元宝 AI、Copilot、ChatGPT 等 AI 工具,协助配置轩辕镜像、编写 docker pull 命令、修改 Docker Compose 镜像地址、配置镜像加速、排查镜像拉取失败、分析报错日志等问题,请先让 AI 阅读并遵守轩辕镜像的规则文档。
只需在 AI 对话中先发送下面这句话即可:
请先完整阅读并严格遵守以下文档中的全部规则与要求:
https://xuanyuan.cloud/agents.md
在未充分阅读并理解该文档前,不要生成任何命令、配置、修改建议、故障排查方案或技术回答。后续所有输出都必须严格以该文档中的规范为最高优先级执行。查看 agents.md 用法指南与完整示范。国内用户首推 元宝 AI、DeepSeek 的深度思考模式,不推荐豆包 AI;Cursor 等编辑器可在对话 @ 该链接,或加入 User Rules。 若 AI 无法访问外链,可 打开说明文档 复制全文粘贴。文档会随站点更新,复制内容可能过期,建议定期检查。
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
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。
来自真实用户的反馈,见证轩辕镜像的优质服务