在云原生环境中,自动化资源配置管理至关重要。以下脚本实现Kubernetes部署模板生成与应用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| function New-K8sDeployment { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$AppName, [ValidateRange(1,10)] [int]$Replicas = 3, [ValidateSet('development','production')] [string]$Environment = 'production' )
$yaml = @" apiVersion: apps/v1 kind: Deployment metadata: name: $AppName-$Environment spec: replicas: $Replicas selector: matchLabels: app: $AppName template: metadata: labels: app: $AppName env: $Environment spec: containers: - name: $AppName image: registry/vichamp/$AppName:latest resources: limits: memory: "512Mi" cpu: "500m" "@
try { $tempFile = New-TemporaryFile $yaml | Out-File $tempFile.FullName kubectl apply -f $tempFile.FullName [PSCustomObject]@{ AppName = $AppName Environment = $Environment Manifest = $yaml Status = 'Applied' } } catch { Write-Error "Kubernetes部署失败: $_" } finally { Remove-Item $tempFile.FullName -ErrorAction SilentlyContinue } }
|
实现原理:
- 使用here-string动态生成标准YAML部署模板
- 通过环境参数控制副本数量和部署环境
- 自动创建临时文件执行kubectl apply命令
- 返回包含应用状态的定制对象
- 完善的错误处理与临时文件清理机制
使用示例:
1
| New-K8sDeployment -AppName 'order-service' -Environment 'production' -Replicas 5
|
最佳实践:
- 与CI/CD流水线集成实现自动部署
- 添加资源请求/限制验证逻辑
- 实现部署历史版本回滚功能
- 集成Prometheus监控指标
注意事项:
• 需要配置kubectl访问权限
• 建议添加YAML语法验证
• 生产环境需设置严格的资源限制