Kubernetes Integration
EMB provides native Kubernetes integration, allowing you to run tasks directly on pods in your cluster and manage deployments.
Kubernetes Executor
Section titled “Kubernetes Executor”The kubernetes executor runs tasks on pods in your Kubernetes cluster, complementing the local and container (Docker Compose) executors.
tasks: migrate: script: npm run migrate executors: - kubernetesWhen to Use Kubernetes Executor
Section titled “When to Use Kubernetes Executor”Use the kubernetes executor when you need to:
- Run tasks on a deployed instance of your application
- Execute database migrations on production/staging environments
- Debug issues in a running cluster
- Run one-off commands without local setup
tasks: db-migrate: description: Run database migrations on the cluster executors: - kubernetes script: npm run migrate
console: description: Open Rails console on production interactive: true executors: - kubernetes script: rails consoleRunning Tasks with Kubernetes
Section titled “Running Tasks with Kubernetes”Use the --executor flag to run tasks on Kubernetes:
# Run migration on the clusteremb run db-migrate --executor kubernetesConfiguration
Section titled “Configuration”Project-Level Defaults
Section titled “Project-Level Defaults”Configure Kubernetes defaults in your .emb.yml:
defaults: kubernetes: namespace: staging # Default namespace selectorLabel: app.kubernetes.io/component # Label for pod selection (default)Component Configuration
Section titled “Component Configuration”Override settings per component:
kubernetes: selector: app=api,tier=backend # Custom label selector container: main # For multi-container podsConfiguration Options
Section titled “Configuration Options”Project Level (defaults.kubernetes)
Section titled “Project Level (defaults.kubernetes)”| Option | Description | Default |
|---|---|---|
namespace | Default Kubernetes namespace | default |
selectorLabel | Label name used to find component pods | app.kubernetes.io/component |
Component Level (kubernetes)
Section titled “Component Level (kubernetes)”| Option | Description |
|---|---|
selector | Custom label selector (e.g., app=api,env=prod) |
container | Container name for multi-container pods |
Namespace Resolution
Section titled “Namespace Resolution”EMB resolves the namespace in this order of precedence:
- Environment variable:
K8S_NAMESPACE - Configuration:
defaults.kubernetes.namespace - Default:
default
# Set environment variableexport K8S_NAMESPACE=productionemb run migrate --executor kubernetes
# Or configure in .emb.yml# defaults:# kubernetes:# namespace: productionPod Selection
Section titled “Pod Selection”By default, EMB finds pods using the label app.kubernetes.io/component={component-name}:
# For component "api", EMB looks for pods with:# app.kubernetes.io/component=apiCustom Label Selector
Section titled “Custom Label Selector”If your pods use different labels, configure a custom selector:
# Component with custom selectorkubernetes: selector: app=my-api,environment=productionCustom Selector Label
Section titled “Custom Selector Label”To change the default label name project-wide:
defaults: kubernetes: selectorLabel: app # Now uses: app={component-name}Multi-Container Pods
Section titled “Multi-Container Pods”For pods with multiple containers (sidecars, init containers, etc.), specify which container to use:
kubernetes: container: main # Execute in the 'main' container, not the sidecarIf not specified and the pod has multiple containers, EMB will error with a helpful message listing available containers.
Kubernetes Commands
Section titled “Kubernetes Commands”EMB provides commands for interacting with your Kubernetes deployments:
emb kubernetes shell
Section titled “emb kubernetes shell”Open a shell in a running pod:
emb kubernetes shell <COMPONENT> [OPTIONS]Options:
-n, --namespace <name>- Target namespace-s, --shell <shell>- Shell to use (default:/bin/sh)
Examples:
emb kubernetes shell apiemb kubernetes shell api --namespace productionemb kubernetes shell api --shell /bin/bashemb kubernetes logs
Section titled “emb kubernetes logs”View logs from pods:
emb kubernetes logs <COMPONENT> [OPTIONS]Options:
-n, --namespace <name>- Target namespace-f, --follow- Follow log output--tail <lines>- Number of lines to show
Examples:
emb kubernetes logs apiemb kubernetes logs api --followemb kubernetes logs api --namespace production --tail 100emb kubernetes ps
Section titled “emb kubernetes ps”List pods for a deployment:
emb kubernetes ps <COMPONENT> [OPTIONS]Options:
-n, --namespace <name>- Target namespace
emb kubernetes restart
Section titled “emb kubernetes restart”Restart pods for a deployment:
emb kubernetes restart <COMPONENT> [OPTIONS]Options:
-n, --namespace <name>- Target namespace
Interactive Tasks
Section titled “Interactive Tasks”For tasks requiring user input, mark them as interactive:
tasks: console: description: Open application console interactive: true executors: - kubernetes script: rails consoleThis ensures:
- TTY is allocated for the pod exec
- stdin is connected for interactive input
- SIGINT (Ctrl+C) is properly handled
Example: Full Configuration
Section titled “Example: Full Configuration”project: name: myapp
defaults: kubernetes: namespace: ${env:K8S_NAMESPACE:-staging} selectorLabel: app.kubernetes.io/component
components: api: kubernetes: container: app # Multi-container pod tasks: migrate: description: Run database migrations executors: - kubernetes script: npm run db:migrate
console: description: Open Rails console interactive: true executors: - kubernetes script: rails console
worker: kubernetes: selector: app=worker,tier=background tasks: process: description: Process pending jobs executors: - kubernetes script: npm run jobs:processTroubleshooting
Section titled “Troubleshooting”No ready pods found
Section titled “No ready pods found”Error: No ready pods found for component "api" in namespace "production"Solutions:
- Check pod status:
kubectl get pods -n production -l app.kubernetes.io/component=api - Verify the label selector matches your pod labels
- Ensure pods are in Ready state
Multiple containers error
Section titled “Multiple containers error”Error: Pod "api-xyz" has multiple containers, explicit container config requiredSolution: Add container to your component’s kubernetes config:
kubernetes: container: mainContainer not found
Section titled “Container not found”Error: Container "main" not found in pod "api-xyz"Solution: Check available containers and update your config:
kubectl get pod api-xyz -o jsonpath='{.spec.containers[*].name}'