dealroadshow/k8s-framework
Laravel-friendly framework for building and managing Kubernetes resources and deployments in PHP. Provides abstractions and helpers to define manifests, apply changes, and integrate k8s workflows into your application and tooling.
Installation
composer require dealroadshow/k8s-framework
Add the service provider to config/app.php:
Dealroadshow\K8sFramework\K8sFrameworkServiceProvider::class,
First Manifest
Define a basic Kubernetes resource in PHP (e.g., app/Kubernetes/Deployment.php):
<?php
namespace App\Kubernetes;
use Dealroadshow\K8sFramework\Manifest;
class Deployment extends Manifest
{
public function getMetadata(): array
{
return [
'name' => 'my-app',
'namespace' => 'default',
];
}
public function getSpec(): array
{
return [
'containers' => [
[
'name' => 'app',
'image' => 'nginx:latest',
],
],
];
}
}
Generate YAML Use the CLI command to render the manifest:
php artisan k8s:render App\Kubernetes\Deployment
Outputs YAML to storage/app/k8s/.
Define a Service Manifest
class Service extends Manifest
{
public function getMetadata(): array
{
return [
'name' => 'my-service',
'namespace' => 'default',
'labels' => ['app' => 'my-app'],
];
}
public function getSpec(): array
{
return [
'selector' => ['app' => 'my-app'],
'ports' => [
['port' => 80, 'targetPort' => 8080],
],
];
}
}
Apply to Cluster
Use the k8s:apply command:
php artisan k8s:apply App\Kubernetes\Service
Dynamic Manifests Use environment variables or config files to parameterize manifests:
public function getSpec(): array
{
return [
'containers' => [
[
'name' => 'app',
'image' => env('APP_IMAGE', 'nginx:latest'),
'env' => [
['name' => 'ENVIRONMENT', 'value' => env('APP_ENV')],
],
],
],
];
}
Helm-like Templating Combine multiple manifests into a "release":
class Release
{
public function getManifests(): array
{
return [
new Deployment(),
new Service(),
new Ingress(),
];
}
}
Render all manifests at once:
php artisan k8s:render Release --all
Validation Use Laravel’s validation rules to enforce Kubernetes constraints:
use Illuminate\Support\Facades\Validator;
public function getSpec(): array
{
$spec = [
'containers' => [
['name' => 'app', 'image' => 'nginx:latest'],
],
];
Validator::make($spec, [
'containers.*.image' => 'required|string',
])->validate();
return $spec;
}
Namespaces Dynamically set namespaces based on environment:
public function getMetadata(): array
{
return [
'namespace' => env('K8S_NAMESPACE', 'default'),
];
}
Secrets and ConfigMaps
Load secrets from Laravel’s .env or encrypted storage:
public function getSpec(): array
{
return [
'envFrom' => [
['secretRef' => ['name' => env('SECRET_NAME')]],
],
];
}
YAML Formatting
// Bad: Missing key for array item
'ports' => [['port' => 80]], // Missing 'targetPort'
// Good
'ports' => [['port' => 80, 'targetPort' => 8080]],
Namespace Conflicts
Deployment), the last one rendered will overwrite previous ones. Use unique names or labels to avoid clashes.Kubernetes API Limits
kubectl’s --dry-run=client for validation.Dry Runs
Use the --dry-run flag to preview changes without applying:
php artisan k8s:apply App\Kubernetes\Deployment --dry-run
Logging
Enable debug mode in config/k8s-framework.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/k8s-framework.log.
Manifest Validation Validate manifests against the Kubernetes schema using a tool like kubeval in your CI pipeline.
Custom Resource Definitions (CRDs)
Extend the Manifest class to support custom resources:
class CustomResource extends Manifest
{
protected $kind = 'MyCustomResource'; // Override default 'Deployment'
protected $apiVersion = 'mygroup.example/v1';
// ...
}
Pre/Post Hooks Add lifecycle hooks for pre-rendering or post-apply actions:
class Deployment extends Manifest
{
public function onBeforeRender()
{
// Modify spec before rendering
$this->spec['replicas'] = env('REPLICAS', 1);
}
public function onAfterApply()
{
// Log or trigger side effects
\Log::info('Deployment applied: ' . $this->metadata['name']);
}
}
Plugin System Register custom plugins for additional functionality (e.g., Helm chart integration):
// In a service provider
$this->app->make('k8s-framework')->extend(function ($framework) {
$framework->addPlugin(new HelmPlugin());
});
How can I help you explore Laravel packages today?