``yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{api_name}} namespace: {{namespace}} labels: app: {{api_name}} spec: replicas: {{min_replicas}} selector: matchLabels: app: {{api_name}} strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 25% maxSurge: 25% template: metadata: labels: app: {{api_name}} spec: terminationGracePeriodSeconds: 30 securityContext: runAsNonRoot: true runAsUser: 10001 fsGroup: 10001 affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchLabels: app: {{api_name}} topologyKey: kubernetes.io/hostname containers: - name: {{api_name}} image: {{container_image}} ports: - containerPort: {{port}} name: http resources: requests: cpu: "{{cpu_request_m}}m" memory: "{{memory_request_mib}}Mi" limits: cpu: "{{cpu_request_m}}m" memory: "{{memory_request_mib}}Mi" livenessProbe: httpGet: path: /healthz port: http initialDelaySeconds: 15 periodSeconds: 20 timeoutSeconds: 5 failureThreshold: 3 readinessProbe: httpGet: path: /ready port: http initialDelaySeconds: 5 periodSeconds: 10 timeoutSeconds: 3 failureThreshold: 3 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL --- apiVersion: v1 kind: Service metadata: name: {{api_name}} namespace: {{namespace}} labels: app: {{api_name}} spec: selector: app: {{api_name}} ports: - protocol: TCP port: {{port}} targetPort: http type: ClusterIP --- apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: {{api_name}}-hpa namespace: {{namespace}} spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: {{api_name}} minReplicas: {{min_replicas}} maxReplicas: {{max_replicas}} metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: {{target_cpu_utilization}} --- apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: {{api_name}}-pdb namespace: {{namespace}} labels: app: {{api_name}} spec: minAvailable: 1 selector: matchLabels: app: {{api_name}} ``
GitOps Integration:
For Helm, these manifests would reside in the templates/ directory of a chart. Dynamic values like api_name, container_image, resource requests, and replica counts would be templated using {{ .Values.key }}. The values.yaml file would define defaults, with environment-specific overrides managed via dedicated values-{{env}}.yaml files or --set flags during deployment. With Kustomize, these manifests form the base/ configuration. Overlays for different environments (e.g., dev/, prod/) would then patch the base. For instance, a prod/kustomization.yaml could adjust replica counts, image tags, or resource limits using patches or replicas fields, keeping the base configuration clean and reusable across environments.
Rollout/Rollback Strategy:
The RollingUpdate strategy in the Deployment ensures new pods are brought online and become ready before older pods are terminated, maintaining service availability during updates. The Pod Disruption Budget (PDB) further protects against voluntary disruptions, ensuring a minimum number of pods remain available. During a rollout, continuous monitoring of pod readiness, error rates, and latency is critical. If issues are detected, a rollback is initiated by reverting the container_image tag in the Deployment manifest to a known stable version and reapplying the configuration. Kubernetes' RollingUpdate handles the transition back. For urgent rollbacks, kubectl rollout undo deployment/{{api_name}} can quickly revert to the previous revision. Post-rollback, re-validation through monitoring confirms service stability.