1. The Helm Package Manager & Ansible Orchestration
Helm is a graduated CNCF project that packages related Kubernetes resources into versioned bundles called Charts:
- `Chart.yaml`: Metadata declaring chart name, version, and dependencies.
- `values.yaml`: Default configuration variables.
- `templates/`: Jinja2-style Go templates rendered into Kubernetes manifests.
Ansible's kubernetes.core.helm module allows platform engineers to manage Helm chart lifecycles declaratively:
- name: Deploy Redis Cache Cluster via Official Bitnami Helm Chart
kubernetes.core.helm:
name: redis-cluster
chart_ref: oci://registry-1.docker.io/bitnamicharts/redis
release_namespace: cache-tier
create_namespace: true
state: present
values:
architecture: replication
auth:
enabled: true
password: "{{ vault_redis_password }}"
replica:
replicaCount: 3
persistence:
enabled: true
size: 10Gi2. Deploying a Cloud-Native Observability Stack
A production Kubernetes cluster requires comprehensive telemetry: metrics, logs, and alerts.
Applications & Nodes
| (Prometheus Scrape via ServiceMonitor)
v
+-----------------------------------------------------------------+
| Prometheus Time-Series Database |
| - Collects CPU, memory, network, and custom app metrics |
| - Evaluates PromQL alerting rules |
+-----------------------------------------------------------------+
|
+--------------------------------+
| |
v v
+-----------------------+ +-----------------------------------+
| Alertmanager | | Grafana Dashboards |
| (Dispatches alerts | | (Visualizes cluster topology, |
| to Slack / PagerDuty)| | pod health, and traffic spikes) |
+-----------------------+ +-----------------------------------+Using Ansible, deploying the entire kube-prometheus-stack Helm chart (Prometheus Operator, Grafana, Alertmanager, node-exporter) requires a single declarative task:
- name: Deploy Complete Kube-Prometheus Observability Stack
kubernetes.core.helm:
name: kube-prometheus-stack
chart_repo_url: https://prometheus-community.github.io/helm-charts
chart_ref: kube-prometheus-stack
release_namespace: monitoring
create_namespace: true
state: present
values:
grafana:
adminPassword: "{{ vault_grafana_password }}"
prometheus:
prometheusSpec:
retention: 30d
storageSpec:
volumeClaimTemplate:
spec:
resources:
requests:
storage: 50Gi3. Zero-Downtime Node Maintenance Workflows
Performing kernel upgrades or hardware maintenance on worker nodes must never disrupt running services. Ansible automates the safe three-stage maintenance routine:
Stage 1: Cordon Node Stage 2: Drain Pods
[ Node marked unschedulable ] ----> [ Evicts pods via Eviction API; ]
(New pods will not land here) [ respects PodDisruptionBudgets ]
|
v
Stage 3: Upgrade & Uncordon [ Pods reschedule safely onto ]
[ Host patched, rebooted, ] <--- [ neighboring healthy nodes ]
[ marked schedulable again ]Ansible Node Drain Playbook
---
- name: Safely Drain and Upgrade Kubernetes Worker Node
hosts: localhost
connection: local
vars:
target_node: "worker-node-03"
tasks:
- name: Cordon Node (Mark Unschedulable)
kubernetes.core.k8s_drain:
name: "{{ target_node }}"
state: cordon
- name: Drain Pods Safely Using the Eviction API
kubernetes.core.k8s_drain:
name: "{{ target_node }}"
state: drain
delete_emptydir_data: true
ignore_daemonsets: true
force: false # Enforces PodDisruptionBudget limits!
- name: Execute Host OS Kernel Patching via SSH
ansible.builtin.command: "ssh root@{{ target_node }} 'apt-get update && apt-get dist-upgrade -y && reboot'"
async: 600
poll: 0
- name: Wait for Node to Recover and Return to Ready State
kubernetes.core.k8s_info:
kind: Node
name: "{{ target_node }}"
register: node_status
until: "node_status.resources[0].status.conditions | selectattr('type', 'equalto', 'Ready') | map(attribute='status') | first == 'True'"
retries: 30
delay: 10
- name: Uncordon Node (Resume Normal Scheduling)
kubernetes.core.k8s_drain:
name: "{{ target_node }}"
state: uncordon