Skip to content

Services Documentation

This document describes all services provided by the Kubernetes integration for programmatic control of your cluster.

Available Services

The integration provides the following services for controlling Kubernetes resources:

Service Description Supported Resources
Scale Workload Scale a workload to a specific number of replicas Deployments, StatefulSets
Start Workload Start a workload by scaling to specified replicas, or trigger CronJobs Deployments, StatefulSets, CronJobs
Stop Workload Stop a workload by scaling to 0 replicas Deployments, StatefulSets
Restart Workload Perform a rolling restart (equivalent to kubectl rollout restart) Deployments, StatefulSets, DaemonSets
Delete Job Delete one or more Jobs (useful for clearing failed Jobs) Jobs
Cordon Node Mark one or more nodes unschedulable (kubectl cordon) Nodes
Uncordon Node Mark one or more nodes schedulable again (kubectl uncordon) Nodes
Drain Node Cordon nodes and evict their pods, honouring PodDisruptionBudgets (kubectl drain) Nodes

Note: CronJobs are rejected by scale_workload and stop_workload. Use the switch entity to suspend/resume CronJobs, or use start_workload to trigger them.

All services are registered when Home Assistant starts, before any cluster is configured, and stay registered for the lifetime of Home Assistant. See Error Handling for what happens when a call is invalid or an operation fails.

Service Details

Scale Workload

Service: kubernetes.scale_workload

Scale one or more Kubernetes workloads (Deployments or StatefulSets) to a specific number of replicas.

Parameters:

  • workload_name (string or entity ID, optional): Single workload name or entity ID (e.g., switch.production_default_web_app)
  • workload_names (list or target selector, optional): Multiple workload names or entity IDs
  • namespace (string, optional): Kubernetes namespace (defaults to configured namespace)
  • replicas (integer, required): Target number of replicas (0 or more)
  • entry_id (string, optional): Config entry ID (defaults to the cluster that owns the targeted workloads, otherwise the first configured entry; must match the targets' cluster)

Supported Workloads: Deployments, StatefulSets

Note: CronJobs are not supported. If a CronJob is provided, the call fails with a validation error and no workload is scaled.

HPA-managed workloads: if a HorizontalPodAutoscaler targets any of the workloads and replicas is not 0, the whole call is rejected with a validation error — the HPA would revert the count within seconds. Change the HPA's minReplicas/maxReplicas instead. Scaling to 0 is allowed (the HPA controller leaves targets at 0 replicas alone). See HPA-managed workloads.

Examples:

# Scale a single deployment using entity ID
service: kubernetes.scale_workload
data:
  workload_name: switch.production_default_web_app
  replicas: 3

# Scale multiple workloads
service: kubernetes.scale_workload
data:
  workload_names:
    - switch.production_default_web_app
    - switch.production_default_api_server
  replicas: 2
  namespace: default

# Scale using direct workload name
service: kubernetes.scale_workload
data:
  workload_name: web-app
  namespace: production
  replicas: 5

Start Workload

Service: kubernetes.start_workload

Start one or more Kubernetes workloads by scaling them to the specified number of replicas, or trigger CronJobs (creates a job immediately).

Parameters:

  • workload_name (string or entity ID, optional): Single workload name or entity ID (e.g., switch.production_default_web_app)
  • workload_names (list or target selector, optional): Multiple workload names or entity IDs
  • namespace (string, optional): Kubernetes namespace (defaults to configured namespace)
  • replicas (integer, optional): Number of replicas for Deployments/StatefulSets (default: 1, ignored for CronJobs)
  • entry_id (string, optional): Config entry ID (defaults to the cluster that owns the targeted workloads, otherwise the first configured entry; must match the targets' cluster)

Supported Workloads: - Deployments: Scales to the specified number of replicas - StatefulSets: Scales to the specified number of replicas - CronJobs: Triggers the CronJob immediately (creates a job from the CronJob template)

Starting an HPA-managed Deployment/StatefulSet works, but it hands control back to the HPA, which then chooses the replica count instead of replicas.

Examples:

# Start a deployment with 2 replicas
service: kubernetes.start_workload
data:
  workload_name: switch.production_default_web_app
  replicas: 2

# Trigger a CronJob
service: kubernetes.start_workload
data:
  workload_name: switch.production_default_backup
  # replicas parameter is ignored for CronJobs

# Start multiple StatefulSets
service: kubernetes.start_workload
data:
  workload_names:
    - switch.production_database_database_primary
    - switch.production_database_database_replica
  replicas: 1
  namespace: database

Stop Workload

Service: kubernetes.stop_workload

Stop one or more Kubernetes workloads by scaling them to 0 replicas (Deployments/StatefulSets only).

Parameters:

  • workload_name (string or entity ID, optional): Single workload name or entity ID (e.g., switch.production_default_web_app)
  • workload_names (list or target selector, optional): Multiple workload names or entity IDs
  • namespace (string, optional): Kubernetes namespace (defaults to configured namespace)
  • entry_id (string, optional): Config entry ID (defaults to the cluster that owns the targeted workloads, otherwise the first configured entry; must match the targets' cluster)

Supported Workloads: Deployments, StatefulSets

Note: CronJobs are rejected by this service with a validation error. To suspend a CronJob, use the switch entity (switch.turn_off on the CronJob switch).

Examples:

# Stop a single deployment
service: kubernetes.stop_workload
data:
  workload_name: switch.production_default_web_app

# Stop multiple workloads
service: kubernetes.stop_workload
data:
  workload_names:
    - switch.production_development_development_api
    - switch.production_development_staging_api
  namespace: development

Restart Workload

Service: kubernetes.restart_workload

Perform a rolling restart of one or more Kubernetes workloads. This is equivalent to kubectl rollout restart — it patches spec.template.metadata.annotations with a kubectl.kubernetes.io/restartedAt timestamp, causing the controller to gradually recreate all pods.

Parameters:

  • workload_name (string or entity ID, optional): Single workload name or entity ID (e.g., switch.production_default_web_app)
  • workload_names (list or target selector, optional): Multiple workload names or entity IDs
  • namespace (string, optional): Kubernetes namespace (defaults to configured namespace)
  • entry_id (string, optional): Config entry ID (defaults to the cluster that owns the targeted workloads, otherwise the first configured entry; must match the targets' cluster)

Supported Workloads: Deployments, StatefulSets, DaemonSets

Note: CronJobs and Jobs are not supported. If an unsupported workload type is provided, the call fails with a validation error and no workload is restarted.

Examples:

# Restart a single deployment
service: kubernetes.restart_workload
data:
  workload_name: switch.production_default_web_app

# Restart multiple workloads
service: kubernetes.restart_workload
data:
  workload_names:
    - switch.production_default_web_app
    - switch.production_default_api_server
  namespace: default

# Restart using direct workload name
service: kubernetes.restart_workload
data:
  workload_name: web-app
  namespace: production

Delete Job

Service: kubernetes.delete_job

Delete one or more Kubernetes Jobs. This is useful for removing failed Jobs or cleaning up completed Jobs. Deletes the Job and its pods using a Background propagation policy (pods are also deleted).

Parameters:

  • job_name (string, optional): Single Job name
  • job_names (list, optional): Multiple Job names
  • namespace (string, optional): Kubernetes namespace (resolved from monitored data if Job exists, falls back to configured default)
  • entry_id (string, optional): Config entry ID (defaults to the first configured entry if not specified)

Supported Resources: Jobs

Note: This service deletes Jobs by name (not entity IDs). At least one of job_name or job_names must be provided. The Job's pods are automatically deleted as part of the background propagation policy.

Examples:

# Delete a single Job
service: kubernetes.delete_job
data:
  job_name: backup-job-abc123
  namespace: production

# Delete multiple Jobs
service: kubernetes.delete_job
data:
  job_names:
    - failed-job-1
    - failed-job-2
  namespace: production

# Delete using monitored data and default namespace
service: kubernetes.delete_job
data:
  job_name: backup-job-xyz789

Cordon Node / Uncordon Node

Services: kubernetes.cordon_node, kubernetes.uncordon_node

Cordon marks a node unschedulable (the equivalent of kubectl cordon): running pods keep running, but no new pods are scheduled onto the node. Uncordon reverses it. The same operation is also available as a per-node switch entity (on = schedulable, off = cordoned).

Parameters (both services):

  • node_name (string, optional): Single node name
  • node_names (list, optional): Multiple node names
  • entry_id (string, optional): Config entry ID (defaults to the first configured entry if not specified)

Note: Nodes are cluster-scoped — there is no namespace parameter. At least one of node_name or node_names must be provided. Requires the patch verb on nodes (included in manifests/full/).

Examples:

# Cordon a node before maintenance
service: kubernetes.cordon_node
data:
  node_name: worker-node-1

# Uncordon several nodes after maintenance
service: kubernetes.uncordon_node
data:
  node_names:
    - worker-node-1
    - worker-node-2

Drain Node

Service: kubernetes.drain_node

Drain cordons a node and evicts its pods through the Eviction API (also available as the Drain button in the sidebar panel's Nodes tab) — the equivalent of kubectl drain --ignore-daemonsets. Evictions respect PodDisruptionBudgets: an eviction a PDB blocks is retried every 5 seconds until timeout runs out. With several nodes in one call, every node is cordoned before the first pod is evicted, so evicted pods are not rescheduled onto another node of the same call.

Parameters:

  • node_name (string, optional): Single node name
  • node_names (list, optional): Multiple node names
  • ignore_daemonsets (boolean, optional, default true): Skip DaemonSet-managed pods. When false, a node running DaemonSet pods is not drained and the call fails for it, like kubectl drain without --ignore-daemonsets
  • delete_emptydir_data (boolean, optional, default false): Also evict pods that use emptyDir volumes; their data is lost. When false they are skipped
  • force (boolean, optional, default false): Also evict pods not managed by a controller (such as a ReplicaSet, StatefulSet or Job). Nothing recreates them, so they are gone for good. When false they are skipped
  • timeout (integer seconds, optional, default 120, max 3600): How long to retry PDB-blocked evictions. 0 tries each pod once
  • entry_id (string, optional): Config entry ID (defaults to the first configured entry if not specified)

Skipped pods (reported with a reason): mirror_pod (static pods), daemonset, completed (phase Succeeded/Failed), emptydir, unmanaged (no owner with controller: true; kubectl drain would refuse the drain instead, unless --force is given), and home_assistant — the pod Home Assistant itself runs in, when it runs inside the cluster. Evicting it would kill the service call before it returns; stop Home Assistant from your automation instead. That guard recognises the pod by its UID, taken from the ServiceAccount token mounted into it, so it works with hostNetwork: true, a custom hostname, and when draining another cluster. Without a mounted projected token (automountServiceAccountToken: false, or a legacy Secret token) it falls back to the hostname and the mounted namespace file. In that case it cannot find Home Assistant's pod with hostNetwork: true or a custom spec.hostname, and it may skip a pod with the same name and namespace on a node of another cluster.

Response: The service returns, per node, the pods whose eviction was accepted (evicted; they may still be terminating — the service does not wait for them), the skipped pods with their reason, and the pods still blocked by a PDB when the timeout ran out. Pods that are still blocked are reported, not raised; any other eviction error (e.g. a 403 because the RBAC lacks create on pods/eviction) fails the call after every pod was attempted.

{
  "worker-node-1": {
    "evicted": ["default/web-7d9f-abcde"],
    "skipped": [{"pod": "kube-system/kube-proxy-x1y2z", "reason": "daemonset"}],
    "blocked": ["db/postgres-0"]
  }
}

Note: Requires create on pods/eviction (included in manifests/full/ — re-apply the RBAC if you installed it before this service existed) and lists pods across all namespaces, so it does not work with namespace-scoped roles.

Example — drain a node for maintenance, wait until nothing is blocked, then hand over to the reboot:

script:
  drain_worker_1:
    sequence:
      - service: kubernetes.drain_node
        data:
          node_name: worker-node-1
          timeout: 300
        response_variable: drain
      - if:
          - condition: template
            value_template: "{{ drain['worker-node-1'].blocked | length > 0 }}"
        then:
          - stop: "Pods are still blocked by a PodDisruptionBudget"
            error: true
      # ... reboot the node through another integration, wait for its Ready
      # binary sensor, then:
      - service: kubernetes.uncordon_node
        data:
          node_name: worker-node-1

Using Entity IDs vs. Workload Names

The services accept both entity IDs and direct workload names:

Entity IDs (recommended for UI usage):

workload_name: switch.production_default_web_app
# or
workload_names:
  - switch.production_default_web_app
  - switch.production_default_api_server

Direct Names (for programmatic usage):

workload_name: web-app
# or
workload_names:
  - web-app
  - api-server

When using entity IDs, the service automatically extracts the workload name, namespace, and workload type from the entity attributes.

With several clusters configured, the four workload services run against the cluster that owns the targets: for entity IDs it is the entry that registered the switch, for raw workload names the cluster that has a workload with that name. A bare workload_name, and every workload_names item that is not a switch. entity ID, is first tried as the entity switch.<name>; only when no such switch resolves is it looked up as a raw name. An explicit entry_id must be loaded; raw names are then resolved only in that entry, and a bare name whose switch.<name> belongs to another cluster is looked up in the entry_id cluster instead. delete_job, cordon_node, uncordon_node and drain_node still default to the first configured entry.

Service Behavior

Error Handling

Services raise errors instead of failing silently — Home Assistant shows them in the UI when a service is called from a dashboard or the Developer Tools, and logs them (together with the failing automation) otherwise.

Invalid calls raise a validation error before anything is changed in the cluster:

  • No Kubernetes integration is configured, or the supplied entry_id does not match a loaded config entry
  • None of the given workload_name / workload_names resolve to a known workload (e.g. the entity ID does not exist, or the switch has no namespace/workload_type attributes)
  • delete_job / cordon_node / uncordon_node / drain_node are called without a usable name
  • A workload's type does not support the requested operation — for example scaling, stopping, or restarting a CronJob. The whole call is rejected; no target in the call is touched.
  • scale_workload targets an HPA-managed workload with replicas other than 0. The whole call is rejected; no target in the call is touched.
  • scale_workload / start_workload / stop_workload / restart_workload span more than one cluster: the targets belong to different config entries, an explicit entry_id contradicts the targets' cluster, or a raw workload name without entry_id exists in more than one cluster. The whole call is rejected; call the service once per cluster (or pass entry_id to pick the cluster for an ambiguous raw name).

Failed operations raise a regular error after every target has been attempted:

  • Multi-target calls do not abort at the first failure. Every workload, Job or node in the call is attempted, and a single error naming all failed targets (with the reason, where the API provided one) is raised at the end.
  • Targets that succeeded stay changed — a partial failure is reported, not rolled back.
  • Node cordon/uncordon/drain still refreshes the coordinator when at least one node changed, even if others failed.
  • drain_node reports pods still blocked by a PodDisruptionBudget in its response instead of raising; a raised error discards the response.

Asynchronous Operations

  • All scaling operations are asynchronous
  • Services return immediately after initiating the scaling operation
  • Use the entity states to monitor the actual scaling progress
  • Configure scale_verification_timeout to adjust how long to wait for operations

Workload Type Detection

The services automatically detect the workload type from entity attributes: - If an entity ID is provided (e.g., switch.production_default_web_app), the service reads the workload_type attribute - If a direct name is provided, the service attempts to determine the type from the coordinator data - CronJobs are automatically handled differently (triggered instead of scaled)

Permissions Required

Services require the following Kubernetes RBAC permissions:

For Deployments and StatefulSets: - apps/deployments: get, list, patch (for scaling and restart) - apps/statefulsets: get, list, patch (for scaling and restart)

For DaemonSets (restart only): - apps/daemonsets: get, list, patch

For CronJobs: - batch/cronjobs: get, list, watch, patch (suspend/resume via switch) - batch/jobs: get, list, watch, create (CronJob triggering via start_workload)

For Jobs (deletion): - batch/jobs: get, list, delete (delete_job service)

For Nodes (cordon/uncordon): - nodes: get, list, patch (cordon_node / uncordon_node / drain_node services) - pods/eviction: create, plus list on pods cluster-wide (drain_node)

General: - Access to the target namespaces

See the Setup Guide and RBAC Reference for detailed RBAC configuration.

CronJob Management

For CronJobs, the services work as follows:

  • start_workload: Triggers the CronJob immediately (creates a job from the CronJob template)
  • scale_workload: Not supported (the call is rejected with a validation error)
  • stop_workload: Not supported (the call is rejected with a validation error)

To suspend or resume CronJobs, use the switch entities: - Turn the switch ON to resume (unsuspend) a CronJob - Turn the switch OFF to suspend a CronJob

For more information about CronJob management, see the CronJobs Documentation.

Examples

Scale Multiple Deployments

automation:
  - alias: "Scale Down During Off Hours"
    trigger:
      - platform: time
        at: "22:00:00"
    action:
      service: kubernetes.scale_workload
      data:
        workload_names:
          - switch.production_default_web_app
          - switch.production_default_api_server
        replicas: 1
        namespace: default

Start Workloads on Schedule

automation:
  - alias: "Start Services in Morning"
    trigger:
      - platform: time
        at: "08:00:00"
    action:
      service: kubernetes.start_workload
      data:
        workload_names:
          - switch.production_default_web_app
          - switch.production_default_database
        replicas: 3

Trigger CronJob Manually

automation:
  - alias: "Manual Backup Trigger"
    trigger:
      - platform: event
        event_type: manual_backup_requested
    action:
      service: kubernetes.start_workload
      data:
        workload_name: switch.production_default_backup
        namespace: default

Scheduled Rolling Restart

automation:
  - alias: "Weekly rolling restart of production workloads"
    trigger:
      - platform: time
        at: "04:00:00"
    condition:
      condition: time
      weekday:
        - sun
    action:
      service: kubernetes.restart_workload
      data:
        workload_names:
          - switch.production_default_web_app
          - switch.production_default_api_server
        namespace: default

Stop All Workloads in Namespace

script:
  stop_all_workloads:
    sequence:
      - service: kubernetes.stop_workload
        data:
          workload_names:
            - switch.production_staging_app1
            - switch.production_staging_app2
            - switch.production_staging_app3
          namespace: staging