<@ULVA73B9P> Why my kubernetes worker is down if i...
# ask-marvin
h
@Marvin Why my kubernetes worker is down if i close the terminal with command
prefect worker start --pool 'test'
. Can i have the kubernetes worker always on
m
thought for 155.3 seconds
Closing the terminal stops the process that’s running your worker. When you start a worker with
prefect worker start --pool 'test'
in a shell, that process lives only as long as the terminal/session does. You’ve got a few good options to keep a Kubernetes worker “always on”: Option A (recommended): run the worker inside your Kubernetes cluster - Deploy the Prefect Kubernetes worker as a Kubernetes Deployment (via Helm). Kubernetes will keep it running and restart it if it dies, independent of your terminal. - High-level steps: 1) Add/update the Helm repo
Copy code
helm repo add prefect <https://prefecthq.github.io/prefect-helm>
  helm repo update
2) Create a namespace (optional)
Copy code
kubectl create namespace prefect
3) Provide your Prefect API credentials (e.g., for Prefect Cloud) as a secret
Copy code
kubectl create secret generic prefect-api-key \
    --namespace prefect \
    --from-literal=key=<YOUR_PREFECT_API_KEY>
4) Install the worker chart and configure it (work pool name, API URL/Key, etc.) via values.yaml
Copy code
helm install prefect-worker prefect/prefect-worker --namespace prefect -f values.yaml
- Docs: - Deploy a worker using Helm - Kubernetes how-to guide - Manage work pools Option B: run it as a background service on a VM - If you prefer to run the worker off-cluster (e.g., a VM with kubeconfig that can reach your cluster), use a process supervisor so it survives logouts and restarts: - systemd service (reliable, restarts on failure) - or tmux/screen/nohup for simpler use - Examples: - Quick and simple:
Copy code
nohup prefect worker start --pool test >prefect-worker.log 2>&1 &
    disown
- More robust (systemd) — create a unit like:
Copy code
[Unit]
    Description=Prefect Kubernetes Worker
    After=network.target

    [Service]
    Environment=PREFECT_API_URL=<your_api_url>
    Environment=PREFECT_API_KEY=<your_api_key>
    ExecStart=/usr/bin/env bash -lc 'prefect worker start --pool test'
    Restart=always
    RestartSec=5

    [Install]
    WantedBy=multi-user.target
Then:
Copy code
sudo systemctl daemon-reload
    sudo systemctl enable --now prefect-worker
A couple of checks/tips: - Make sure your work pool exists and is type “kubernetes”:
Copy code
prefect work-pool create "test" --type kubernetes --paused
  prefect work-pool resume "test"
Docs: Manage work pools - The worker process will connect to Prefect using
PREFECT_API_URL
and (for Cloud)
PREFECT_API_KEY
. Set those in your Helm values or service environment. - Workers are stateless in Prefect 3.x; you can restart/replace them without impacting running flow runs. Would you like help with a concrete Helm values.yaml for your environment, or do you prefer the systemd/tmux route?