关于 kubernetes-flux
为使用 Flux 和 kubectl 管理 Kubernetes 集群的 Agent 提供操作与排障指南。涵盖 GitOps 引导、状态检查、资源排查和集群管理,并说明危险操作确认及 Secret 值遮蔽规则。
主要功能
- 01Flux GitOps 引导与协调
- 02检查集群资源和部署状态
- 03排查 Pod、服务与事件
- 04扩缩容、回滚及端口转发
- 05危险操作需确认且遮蔽 Secret 值
适用场景
- 01排查故障 Pod 和容器
- 02监控部署发布及回滚
- 03调查服务网络与集群事件
- 04通过 Flux 同步 Git 仓库配置
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ oimiragieo/ agent-studio --skill kubernetes-flux安装后在 Claude Code 中输入/kubernetes-flux确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
commands/kubernetes-flux.mdhooks/post-execute.cjspre-execute.cjsreferences/research-requirements.mdrules/kubernetes-flux.mdschemas/input.schema.jsonoutput.schema.jsonscripts/main.cjstemplates/implementation-template.md详细介绍
Kubernetes Flux 技能
安装
此技能会调用 Flux CLI。请安装:
- macOS/Linux(Homebrew):
brew install fluxcd/tap/flux - macOS/Linux(脚本):
curl -s https://fluxcd.io/install.sh | sudo bash - Windows(winget):
winget install -e --id FluxCD.Flux - Windows(Chocolatey):
choco install flux - 自定义目录:
curl -s https://fluxcd.io/install.sh | bash -s ~/.local/bin
验证:flux --version。然后按需使用 flux bootstrap 部署控制器。
速查表与最佳实践
引导: flux bootstrap git --url=ssh://git@host/repo.git --path=clusters/my-cluster;按需使用 --branch、--interval、--private-key-file 或 --token-auth。
状态: flux check — 检查控制器/CRD;flux get all -A — 查看所有资源;flux get kustomizations;flux tree kustomization <name> — 查看受管理的对象。
技巧: 使用 flux get sources git 和 flux get kustomizations 查看同步状态。按需协调:flux reconcile kustomization <name> --with-source。使用安装脚本时,通过 FLUX_VERSION 固定版本。使用 GitOps 时,应用清单优先使用 Git 而非 Helm。
认证与培训
Kubernetes: CKA / CKAD(Linux Foundation)。Flux: GitOps with Flux (LFS269)。技能数据: 引导、协调、状态(flux check、flux get all)、树状视图;GitOps 工作流。
钩子与工作流
建议的钩子: 应用前:flux check。推送后(推送到 Flux 使用的 Git 仓库):可选的协调触发器。与 devops 配合使用(始终适用)以管理 GitOps 集群。
工作流: 与 devops 配合使用(始终适用)。流程:引导或协调;使用 flux get all、flux tree kustomization 进行调试。另请参阅 gitops-workflow 技能和企业工作流。
概述
此技能通过 kubectl 提供全面的 Kubernetes 集群管理功能,使 AI Agent 能够检查、排查问题并管理 Kubernetes 资源。
适用场景
- 调试应用 Pod 和容器
- 监控部署的滚动更新和状态
- 分析服务网络和端点
- 调查集群事件和错误
- 排查性能问题
- 管理应用扩缩容
- 为本地开发进行端口转发
要求
- 已安装并配置 kubectl
- 有效的 KUBECONFIG 文件或默认上下文
- 集群访问凭据
- 适当的 RBAC 权限
快速参考
# Get pods in current namespace
kubectl get pods
# Get pods in specific namespace
kubectl get pods -n production
# Get pods with labels
kubectl get pods -l app=web -n production
# Describe a pod
kubectl describe pod my-app-123 -n default
# Get pod logs
kubectl logs my-app-123 -n default
# Get logs with tail
kubectl logs my-app-123 -n default --tail=100
# Get logs since time
kubectl logs my-app-123 -n default --since=1h
# List recent events
kubectl get events -n default --sort-by='.lastTimestamp' | tail -20
# Watch events in real-time
kubectl get events -n default -w
资源发现
Pod
# List all pods
kubectl get pods -n <namespace>
# List pods with wide output
kubectl get pods -n <namespace> -o wide
# List pods across all namespaces
kubectl get pods -A
# Filter by label
kubectl get pods -l app=nginx -n <namespace>
部署
# List deployments
kubectl get deployments -n <namespace>
# Get deployment details
kubectl describe deployment <name> -n <namespace>
# Check rollout status
kubectl rollout status deployment/<name> -n <namespace>
服务
# List services
kubectl get svc -n <namespace>
# Describe service
kubectl describe svc <name> -n <namespace>
# Get endpoints
kubectl get endpoints <name> -n <namespace>
ConfigMap 和 Secret
# List ConfigMaps
kubectl get configmaps -n <namespace>
# Describe ConfigMap
kubectl describe configmap <name> -n <namespace>
# Get ConfigMap data
kubectl get configmap <name> -n <namespace> -o yaml
# List Secrets (names only)
kubectl get secrets -n <namespace>
# Describe Secret (values masked)
kubectl describe secret <name> -n <namespace>
命名空间
# List namespaces
kubectl get namespaces
# Get namespace details
kubectl describe namespace <name>
故障排查
Pod 调试
# Describe pod for events and conditions
kubectl describe pod <name> -n <namespace>
# Get pod logs
kubectl logs <pod-name> -n <namespace>
# Get logs from specific container
kubectl logs <pod-name> -c <container-name> -n <namespace>
# Get previous container logs (after crash)
kubectl logs <pod-name> -n <namespace> --previous
# Exec into pod
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
# Run command in pod
kubectl exec <pod-name> -n <namespace> -- ls -la /app
事件
# List events sorted by time
kubectl get events -n <namespace> --sort-by='.lastTimestamp'
# Filter warning events
kubectl get events -n <namespace> --field-selector type=Warning
# Watch events live
kubectl get events -n <namespace> -w
管理操作
扩缩容
# Scale deployment
kubectl scale deployment <name> --replicas=5 -n <namespace>
# Autoscale deployment
kubectl autoscale deployment <name> --min=2 --max=10 --cpu-percent=80 -n <namespace>
滚动更新
# Check rollout status
kubectl rollout status deployment/<name> -n <namespace>
# View rollout history
kubectl rollout history deployment/<name> -n <namespace>
# Rollback to previous version
kubectl rollout undo deployment/<name> -n <namespace>
# Rollback to specific revision
kubectl rollout undo deployment/<name> --to-revision=2 -n <namespace>
端口转发
# Forward local port to pod
kubectl port-forward <pod-name> 8080:80 -n <namespace>
# Forward to service
kubectl port-forward svc/<service-name> 8080:80 -n <namespace>
上下文管理
# Get current context
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context <context-name>
# Set default namespace
kubectl config set-context --current --namespace=<namespace>
常见工作流
排查故障 Pod
# 1. Find the problematic pod
kubectl get pods -n production
# 2. Describe for events
kubectl describe pod <pod-name> -n production
# 3. Check events
kubectl get events -n production --sort-by='.lastTimestamp' | tail -20
# 4. Get logs
kubectl logs <pod-name> -n production --tail=200
监控部署滚动更新
# 1. Check deployment status
kubectl get deployments -n production
# 2. Watch rollout
kubectl rollout status deployment/<name> -n production
# 3. Watch pods
kubectl get pods -l app=<app-name> -n production -w
调试服务连通性
# 1. Check service
kubectl describe svc <name> -n <namespace>
# 2. Check endpoints
kubectl get endpoints <name> -n <namespace>
# 3. Check backing pods
kubectl get pods -l <service-selector> -n <namespace>
# 4. Port forward for testing
kubectl port-forward svc/<name> 8080:80 -n <namespace>
安全功能
禁止的操作
以下操作具有危险性,需要确认:
kubectl delete命令- 具有破坏性的 exec 命令(rm、dd、mkfs)
- 将生产环境中的副本数扩缩至 0
输出脱敏
始终对 Secret 值进行脱敏。仅显示元数据。
错误处理
| 错误 | 原因 | 解决方法 |
|---|---|---|
kubectl not found | 未安装 | 安装 kubectl |
Unable to connect | 无法访问集群 | 检查网络/VPN |
Forbidden | RBAC 权限不足 | 申请权限 |
NotFound | 资源缺失 | 验证名称/命名空间 |
context deadline exceeded | 超时 | 检查集群健康状态 |
相关
- kubectl 文档:https://kubernetes.io/docs/reference/kubectl/
- Kubernetes API:https://kubernetes.io/docs/reference/kubernetes-api/
记忆协议(必须遵守)
开始前:
cat .claude/context/memory/learnings.md
**完成后:**记录发现的任何新模式或例外情况。
假设任务可能中断:你的上下文可能会重置。如果没有记录在记忆中,就等于没有发生。