Skip to main content

kubernetes

当验证Kubernetes配置时,分析YAML清单语法,检查资源定义规范,验证部署配置。验证集群资源,设计安全策略,和最佳实践。

Source facts

Repository
microwind/ai-skills
Last source activity
March 26, 2026 at 14:58
Detected SKILL.md language
Chinese
Stars
83
Forks
24

Install options

The review-first prompt is selected by default. You can switch to a direct command or download a local copy.

Review the source files

Read SKILL.md and any companion files shown by SkillsMP before deciding whether to install.

File Explorer
5 files

Showing SKILL.md

SKILL.md
Source instructions · Read-only preview
name
Kubernetes验证器
description
当验证Kubernetes配置时,分析YAML清单语法,检查资源定义规范,验证部署配置。验证集群资源,设计安全策略,和最佳实践。
license
MIT
# Kubernetes验证器技能 ## 概述 Kubernetes配置验证是确保部署成功的关键步骤。无效的Kubernetes清单会静默部署然后神秘失败。在部署前进行全面验证可以避免生产环境问题。 **核心原则**: 好的配置验证应该覆盖语法、语义和最佳实践,确保部署的稳定性和安全性。坏的配置会导致部署失败、资源浪费和安全风险。 ## 何时使用 **始终:** - 部署到Kubernetes集群之前 - 验证YAML清单文件时 - 检查资源配置定义时 - 调试Pod启动失败时 - 规划资源限制策略时 - 审查部署配置时 **触发短语:** - "这个Kubernetes配置有效吗?" - "验证K8s清单文件" - "检查Pod定义" - "审查部署配置" - "为什么无法部署?" - "验证服务网格配置" ## Kubernetes验证功能 ### YAML语法验证 - 语法结构检查 - 必填字段验证 - 数据类型验证 - 嵌套结构检查 ### 资源定义验证 - API版本检查 - 资源规格验证 - 字段完整性检查 - 引用关系验证 ### 最佳实践检查 - 资源限制设置 - 健康检查配置 - 安全策略验证 - 镜像拉取策略检查 ### 配置分析 - 服务发现配置 - 存储挂载验证 - 环境变量检查 - 密钥管理验证 ## 常见Kubernetes配置问题 ### 缺失资源限制 ``` 问题: Pod没有设置CPU和内存限制 错误示例: apiVersion: v1 kind: Pod spec: containers: - name: app image: nginx # 没有resources字段 后果: - Pod可能耗尽所有可用资源 - 影响其他Pod正常运行 - 集群变得不稳定 解决方案: 1. 设置resources.requests确保调度 2. 设置resources.limits防止资源耗尽 3. 根据应用特点合理配置 4. 监控资源使用情况并调整 ``` ### 缺失健康检查 ``` 问题: Deployment没有配置存活性和就绪性探针 错误示例: apiVersion: apps/v1 kind: Deployment spec: template: spec: containers: - name: app image: nginx # 没有livenessProbe和readinessProbe 后果: - 故障Pod仍被标记为健康 - 流量被发送到故障Pod - 用户体验差 解决方案: 1. 配置livenessProbe检测容器健康 2. 配置readinessProbe检测服务就绪 3. 设置合适的检查间隔和阈值 4. 配置startupProbe处理启动慢的应用 ``` ### 镜像配置错误 ``` 问题: 镜像不存在或仓库地址错误 错误示例: spec: containers: - name: app image: nonexistent/app:latest # 镜像不存在 后果: - Pod无法启动 - ImagePullBackOff错误 - 服务不可用 解决方案: 1. 验证镜像在仓库中存在 2. 使用正确的仓库地址和命名空间 3. 避免使用latest标签 4. 配置正确的镜像拉取策略 ``` ### 安全配置不当 ``` 问题: 容器以特权模式运行或使用root用户 错误示例: spec: containers: - name: app securityContext: privileged: true # 特权模式 runAsUser: 0 # root用户 后果: - 容器获得主机权限 - 安全风险增加 - 可能影响主机稳定性 解决方案: 1. 避免使用特权模式 2. 使用非root用户运行 3. 配置只读根文件系统 4. 限制Linux capabilities ``` ## 代码实现示例 ### Kubernetes配置验证器 ```python import yaml import json import re from typing import List, Dict, Any, Optional, Tuple from dataclasses import dataclass from pathlib import Path @dataclass class ValidationIssue: """验证问题""" severity: str # critical, high, medium, low type: str resource: str field: str message: str suggestion: str line_number: Optional[int] = None @dataclass class ValidationResult: """验证结果""" is_valid: bool issues: List[ValidationIssue] warnings: List[ValidationIssue] summary: Dict[str, Any] class KubernetesConfigValidator: def __init__(self): self.issues: List[ValidationIssue] = [] self.warnings: List[ValidationIssue] = [] # Kubernetes资源必填字段定义 self.required_fields = { 'Pod': { 'apiVersion': True, 'kind': True, 'metadata': True, 'spec': True, 'spec.containers': True }, 'Deployment': { 'apiVersion': True, 'kind': True, 'metadata': True, 'spec': True, 'spec.selector': True, 'spec.template': True }, 'Service': { 'apiVersion': True, 'kind': True, 'metadata': True, 'spec': True, 'spec.selector': True, 'spec.ports': True } } # 最佳实践规则 self.best_practice_rules = { 'resource_limits': 'medium', 'health_checks': 'medium', 'image_tag': 'low', 'security_context': 'high', 'replicas': 'medium' } def validate_yaml_file(self, file_path: str) -> ValidationResult: """验证YAML文件""" try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() return self.validate_yaml_content(content, file_path) except FileNotFoundError: self.issues.append(ValidationIssue( severity='critical', type='file_error', resource='', field='', message=f'文件不存在: {file_path}', suggestion='检查文件路径是否正确' )) return self._generate_result() except Exception as e: self.issues.append(ValidationIssue( severity='critical', type='file_error', resource='', field='', message=f'读取文件失败: {e}', suggestion='检查文件权限和格式' )) return self._generate_result() def validate_yaml_content(self, content: str, file_name: str = '') -> ValidationResult: """验证YAML内容""" try: # 重置问题列表 self.issues = [] self.warnings = [] # 解析YAML documents = yaml.safe_load_all(content) for doc in documents: if doc is None: continue # 验证单个文档 self.validate_document(doc, file_name) return self._generate_result() except yaml.YAMLError as e: self.issues.append(ValidationIssue( severity='critical', type='syntax_error', resource='', field='', message=f'YAML语法错误: {e}', suggestion='检查YAML语法,注意缩进和格式' )) return self._generate_result() def validate_document(self, doc: Dict[str, Any], file_name: str) -> None: """验证单个文档""" if not isinstance(doc, dict): self.issues.append(ValidationIssue( severity='critical', type='structure_error', resource='', field='', message='文档必须是字典类型', suggestion='检查YAML文档结构' )) return # 获取资源类型 kind = doc.get('kind', '') api_version = doc.get('apiVersion', '') if not kind: self.issues.append(ValidationIssue( severity='critical', type='missing_field', resource='', field='kind', message='缺少kind字段', suggestion='添加资源类型,如Pod、Deployment等' )) return # 验证必填字段 self.validate_required_fields(doc, kind) # 验证特定资源类型 if kind == 'Pod': self.validate_pod(doc) elif kind == 'Deployment': self.validate_deployment(doc) elif kind == 'Service': self.validate_service(doc) # 验证最佳实践 self.validate_best_practices(doc, kind) def validate_required_fields(self, doc: Dict[str, Any], kind: str) -> None: """验证必填字段""" required = self.required_fields.get(kind, {}) for field, is_required in required.items(): if is_required: value = self.get_nested_field(doc, field) if value is None: self.issues.append(ValidationIssue( severity='critical', type='missing_field', resource=kind, field=field, message=f'缺少必填字段: {field}', suggestion=f'添加{field}字段' )) def get_nested_field(self, doc: Dict[str, Any], field_path: str) -> Any: """获取嵌套字段值""" keys = field_path.split('.') value = doc for key in keys: if isinstance(value, dict) and key in value: value = value[key] else: return None return value def validate_pod(self, doc: Dict[str, Any]) -> None: """验证Pod配置""" spec = doc.get('spec', {}) containers = spec.get('containers', []) if not containers: self.issues.append(ValidationIssue( severity='critical', type='missing_field', resource='Pod', field='spec.containers', message='Pod必须包含至少一个容器', suggestion='添加容器配置' )) return for i, container in enumerate(containers): container_name = container.get('name', f'container-{i}') # 验证容器名称 if not container.get('name'): self.issues.append(ValidationIssue( severity='high', type='missing_field', resource='Pod', field=f'spec.containers[{i}].name', message='容器缺少名称', suggestion='为容器设置唯一名称' )) # 验证镜像 image = container.get('image', '') if not image: self.issues.append(ValidationIssue( severity='critical', type='missing_field', resource='Pod', field=f'spec.containers[{i}].image', message='容器缺少镜像', suggestion='指定容器镜像' )) elif ':latest' in image: self.warnings.append(ValidationIssue( severity='medium', type='best_practice', resource='Pod', field=f'spec.containers[{i}].image', message='使用latest镜像标签', suggestion='使用具体的版本标签确保部署一致性' )) # 验证资源配置 resources = container.get('resources', {}) if not resources: self.warnings.append(ValidationIssue( severity='medium', type='best_practice', resource='Pod', field=f'spec.containers[{i}].resources', message='容器没有设置资源配置', suggestion='设置CPU和内存的requests和limits' )) else: # 检查资源请求 requests = resources.get('requests', {}) if not requests: self.warnings.append(ValidationIssue( severity='medium', type='best_practice', resource='Pod', field=f'spec.containers[{i}].resources.requests', message='容器没有设置资源请求', suggestion='设置CPU和内存请求以确保调度' )) # 检查资源限制 limits = resources.get('limits', {}) if not limits: self.warnings.append(ValidationIssue( severity='medium', type='best_practice', resource='Pod', field=f'spec.containers[{i}].resources.limits', message='容器没有设置资源限制', suggestion='设置CPU和内存限制防止资源耗尽' )) # 验证健康检查 liveness_probe = container.get('livenessProbe') readiness_probe = container.get('readinessProbe') if not liveness_probe and not readiness_probe: self.warnings.append(ValidationIssue( severity='medium', type='best_practice', resource='Pod', field=f'spec.containers[{i}].probes', message='容器没有配置健康检查', suggestion='添加livenessProbe和readinessProbe' )) # 验证安全上下文 security_context = container.get('securityContext', {}) if security_context.get('privileged', False): self.issues.append(ValidationIssue( severity='high', type='security', resource='Pod', field=f'spec.containers[{i}].securityContext.privileged', message='容器以特权模式运行', suggestion='避免使用特权模式,最小化权限' )) if security_context.get('runAsUser') == 0: self.warnings.append(ValidationIssue( severity='medium', type='security', resource='Pod', field=f'spec.containers[{i}].securityContext.runAsUser', message='容器以root用户运行', suggestion='使用非root用户运行容器' )) def validate_deployment(self, doc: Dict[str, Any]) -> None: """验证Deployment配置""" spec = doc.get('spec', {}) template = spec.get('template', {}) pod_spec = template.get('spec', {}) # 验证副本数 replicas = spec.get('replicas', 1) if replicas == 1: self.warnings.append(ValidationIssue( severity='medium', type='availability', resource='Deployment', field='spec.replicas', message='副本数为1,存在单点故障风险', suggestion='增加副本数提高可用性' )) # 验证选择器匹配 selector = spec.get('selector', {}) match_labels = selector.get('matchLabels', {})
View on GitHub
This SKILL.md is very large, so SkillsMP previews the first section here. View on GitHub