Kubernetes Client
Control your Kubernetes resources from your code.
A PHP client for managing a Kubernetes cluster. Control your Kubernetes resources by manipulating manifests, as PHP array, through the Kubernetes HTTP API. The client supports all Kubernetes API V1.28 resources, but it's possible to define new resource usable with the client.
The library is Framework-free and can be used with any PHP app. It needs only a HttpClient library.
The library is able to detect automatically an HttpClient instance configured in your application.
Features
Framework Free
Not dependent to a framework or a tool, support any HttpClient library
PSR-7
Interoperable with any objects using the PSR-7 recommendation.
Friendly
Manipulate manifests as array
Extendable
Implements easily your custom Kubernetes resources.
Fork the project on GitHub
It is open source! Hosted, developed, and maintained on GitHub.
View GitHub Project
Support this project on Patreon
This project is free and will remain free, but its development is not. If you like it and help us maintain it and evolve it, don't hesitate to support us on Patreon.
Support it
Example
<?php
declare(strict_types=1);
namespace Acme;
require_once 'vendor/autoload.php';
use Teknoo\Kubernetes\Client;
use Teknoo\Kubernetes\Model\ReplicationController;
use function var_dump;
$client = new Client([
'master' => 'http://master.mycluster.com',
]);
// Find pods by label selector
var_dump(
$client->pods()
->setLabelSelector(
[
'name' => 'foo',
'version' => '1',
]
)->find()
);
// Both setLabelSelector and setFieldSelector can take an optional
// second parameter which lets you define inequality based selectors (ie using the != operator)
var_dump(
$client->pods()
->setLabelSelector(
[
'name' => 'bar',
],
[
'env' => 'staging',
]
)->find()
);
// Find pods by field selector
var_dump(
$client->pods()
->setFieldSelector(['metadata.name' => 'test'])
->find()
);
// Find first pod with label selector (same for field selector)
var_dump(
$client->pods()
->setLabelSelector(['name' => 'test'])
->first()
);
$replicationController = new ReplicationController([
'metadata' => [
'name' => 'nginx-test',
'labels' => [
'name' => 'nginx-test',
],
],
'spec' => [
'replicas' => 1,
'template' => [
'metadata' => [
'labels' => [
'name' => 'nginx-test',
],
],
'spec' => [
'containers' => [
[
'name' => 'nginx',
'image' => 'nginx',
'ports' => [
[
'containerPort' => 80,
'protocol' => 'TCP',
],
],
],
],
],
],
],
]);
$repository = $client->replicationControllers();
if ($repository->exists($replicationController->getMetadata('name'))) {
$repository->update($replicationController);
} else {
$repository->create($replicationController);
}
//or
$repository->apply($replicationController);
//to delete
$repository->delete($replicationController);