Kubernetes Environment Setup
The Kubernetes adapter enables seamless command execution within Kubernetes pods, providing comprehensive cluster management, pod selection, and kubectl integration. It supports both direct pod execution and label-based pod selection with full namespace and context management.
Installation and Prerequisites
kubectl Installation
Ensure kubectl is installed and properly configured:
# Check kubectl installation
kubectl version --client
kubectl cluster-info
# Verify cluster connectivity
kubectl get nodes
kubectl get namespaces
The Kubernetes adapter automatically detects kubectl in common locations:
/usr/local/bin/kubectl
(Docker Desktop, official releases)/usr/bin/kubectl
(Linux package managers)/opt/homebrew/bin/kubectl
(Homebrew on macOS)
Cluster Configuration
Configure kubectl to connect to your cluster:
# Configure context for local cluster
kubectl config set-context local --cluster=local --user=local
# Use specific context
kubectl config use-context production
# View current context
kubectl config current-context
# List available contexts
kubectl config get-contexts
Basic Configuration
import { $ } from '@xec-sh/core';
// Basic pod execution
const result = await $({
adapterOptions: {
type: 'kubernetes',
pod: 'my-app-pod',
namespace: 'default'
}
})`ls -la /app`;
console.log(result.stdout);
Configuration Options
Adapter Configuration
Configure the Kubernetes adapter with various options:
import { KubernetesAdapter } from '@xec-sh/core';
const k8s = new KubernetesAdapter({
// kubectl configuration
kubectlPath: '/usr/local/bin/kubectl',
kubeconfig: '~/.kube/config',
context: 'production',
// Default namespace
namespace: 'app-namespace',
// Command timeout
kubectlTimeout: 30000,
// Default execution options
throwOnNonZeroExit: true,
encoding: 'utf8',
maxBuffer: 1024 * 1024 * 10 // 10MB
});
// Use the configured adapter
const result = await k8s.execute({
command: 'kubectl',
args: ['get', 'pods'],
adapterOptions: {
type: 'kubernetes',
pod: 'web-server'
}
});
Kubeconfig and Context Management
Manage multiple clusters and contexts:
// Use specific kubeconfig file
const k8sProd = new KubernetesAdapter({
kubeconfig: '~/.kube/production-config',
context: 'production-cluster',
namespace: 'production'
});
// Use default kubeconfig with specific context
const k8sDev = new KubernetesAdapter({
context: 'development',
namespace: 'dev'
});
// Override context per command
await $({
adapterOptions: {
type: 'kubernetes',
pod: 'test-pod',
namespace: 'staging'
}
})`echo "Running in staging"`;
Global Options
Configure global kubectl options:
const k8s = new KubernetesAdapter({
// Always use specific kubeconfig
kubeconfig: '/path/to/custom/kubeconfig',
// Always use specific context
context: 'my-cluster',
// Default namespace for all operations
namespace: 'my-namespace',
// Custom kubectl binary
kubectlPath: '/custom/path/kubectl'
});
// These options are applied to all kubectl commands
await k8s.execute({
command: 'whoami',
adapterOptions: {
type: 'kubernetes',
pod: 'app-pod'
}
});
Namespace Management
Default Namespace Configuration
Set default namespace for all operations:
const k8s = new KubernetesAdapter({
namespace: 'production'
});
// Uses 'production' namespace by default
await $({
adapterOptions: {
type: 'kubernetes',
pod: 'web-server'
}
})`hostname`;
// Override namespace per command
await $({
adapterOptions: {
type: 'kubernetes',
pod: 'test-pod',
namespace: 'staging' // Override default
}
})`env | grep NAMESPACE`;
Cross-Namespace Operations
Work with pods across different namespaces:
// Monitor multiple namespaces
const namespaces = ['production', 'staging', 'development'];
for (const ns of namespaces) {
const pods = await $({
adapterOptions: {
type: 'kubernetes',
pod: 'monitoring-pod',
namespace: ns
}
})`kubectl get pods -n ${ns} --field-selector=status.phase=Running`;
console.log(`Running pods in ${ns}:`, pods.stdout);
}