teknoo/kubernetes-client

一个简单而优雅的客户端,用于访问和控制 Kubernetes 集群。


README

Latest Stable Version Latest Unstable Version Total Downloads License PHPStan

一个用于管理 Kubernetes 集群的 PHP 客户端。通过操作 Manifests(以 PHP 数组形式)通过 Kubernetes HTTP API 来控制 Kubernetes 资源。客户端支持所有 Kubernetes API V1.28 资源,但也可以定义新的资源供客户端使用。这是一个从 Maclof Kuebrnetes 库 分支出来的重构。

支持的 API 功能

v1

  • Config Maps
  • 删除选项
  • 端点
  • 端点
  • 事件
  • 命名空间
  • 节点
  • 持久卷
  • 持久卷声明
  • Pods
  • 配额
  • 副本集
  • 副本控制器
  • 机密
  • 服务账户
  • 服务

autoscaling/v2

  • 水平 pod 自动缩放器

batch/v1

  • CronJobs
  • 作业

batch/v1beta1

  • Cron Jobs

apps/v1

  • 守护进程集
  • 部署
  • 副本集

extensions/v1beta1

  • 守护进程集

networking.k8s.io/v1

  • 入口
  • 网络策略

certmanager.k8s.io/v1

  • 证书
  • 颁发者

rbac.authorization.k8s.io/v1

  • 集群角色
  • 集群角色绑定
  • 角色
  • 角色绑定

hnc.x-k8s.io/v1

  • 子命名空间锚点

基本用法

use Teknoo\Kubernetes\Client;

$client = new Client([
	'master' => 'http://master.mycluster.com',
]);

// Find pods by label selector
$pods = $client->pods()
    ->setLabelSelector(
        [
            'name'    => 'test',
            'version' => 'a',
        ]
    )->find();

// Both setLabelSelector and setFieldSelector can take an optional
// second parameter which lets you define inequality based selectors (ie using the != operator)
$pods = $client->pods()
    ->setLabelSelector(
        ['name' => 'test'], 
	    ['env' => 'staging']
    )->find();

// Find pods by field selector
$pods = $client->pods()->setFieldSelector(['metadata.name' => 'test'])->find();

// Find first pod with label selector (same for field selector)
$pod = $client->pods()->setLabelSelector(['name' => 'test'])->first();

身份验证示例

不安全的 HTTP

use Teknoo\Kubernetes\Client;
$client = new Client([
	'master' => 'http://master.mycluster.com',
]);

从 kubeconfig 文件连接

use Teknoo\Kubernetes\Client;

// Parsing from the file data directly
$client = Client::loadFromKubeConfig('kubeconfig yaml data');

// Parsing from the file path
$client = Client::loadFromKubeConfigFile('~/.kube/config.yml');

扩展库

自定义仓库

use Teknoo\Kubernetes\Client;

$repositories = new RepositoryRegistry();
$repositories['things'] = MyApp\Kubernetes\Repository\ThingRepository::class;

$client = new Client(
    [
        'master' => 'https://master.mycluster.com',
    ], 
    $repositories
);

$client->things(); //ThingRepository

使用示例

创建/更新副本控制器

以下示例使用数组指定副本控制器的属性。您可以指定属性为数组、JSON 编码的字符串或 YAML 编码的字符串。模型构造函数的第二个参数是数据类型,默认为数组。

use Teknoo\Kubernetes\Model\ReplicationController;

$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',
							],
						],
					],
				],
			],
		],
	],
]);

if ($client->replicationControllers()->exists($replicationController->getMetadata('name'))) {
	$client->replicationControllers()->update($replicationController);
} else {
	$client->replicationControllers()->create($replicationController);
}
$client->replicationControllers()->apply($replicationController);
// or

删除副本控制器

$replicationController = $client->replicationControllers()->setLabelSelector(['name' => 'nginx-test'])->first();
$client->replicationControllers()->delete($replicationController);

您还可以在执行删除时指定选项,例如执行 级联删除

use Teknoo\Kubernetes\Model\DeleteOptions;

$client->replicationControllers()->delete(
	$replicationController,
	new DeleteOptions(['propagationPolicy' => 'Background'])
);

支持此项目

此项目是免费的,并将保持免费。它完全由 EIRL 的活动支持。如果您喜欢它并帮助我维护和改进它,请不要犹豫,在 PatreonGithub 上支持我。

谢谢 :) Richard。

致谢

EIRL Richard Déloge - https://deloge.io - 主开发者。Teknoo Software SASU - https://teknoo.software

关于 Teknoo Software

Teknoo Software 是一家 PHP 软件编辑公司,由 Richard Déloge 创立,作为 EIRL Richard Déloge 的一部分。Teknoo Software 的目标:为我们合作伙伴和社区提供一套高质量的服务或软件,分享知识和技能。

许可证

Kubernetes 客户端根据 MIT 许可证授权 - 有关详细信息,请参阅许可证文件夹。

安装 & 要求

要使用 composer 安装此库,请运行此命令

composer require teknoo/kubernetes-client

此库需要

* PHP 8.1+
* A PHP autoloader (Composer is recommended)
* Symfony/Yaml.
* Illuminate/Collections

贡献 :)

欢迎您为此项目贡献力量。 在Github上Fork它