maclof/kubernetes-client

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

0.31.0 2024-03-14 08:01 UTC

README

Build Status

一个用于管理 Kubernetes 集群的 PHP 客户端。

在运行于生产云集群上,使用 v1.9.6 版本进行过测试。

使用 Composer 进行安装

$ composer require maclof/kubernetes-client

支持的 API 功能

v1

  • 节点
  • 命名空间
  • Pod
  • 副本集
  • 复制控制器
  • 服务
  • 机密
  • 事件
  • 配置映射
  • 端点
  • 持久卷
  • 持久卷声明

batch/v1

  • 作业

batch/v1beta1

  • 定时作业

apps/v1

  • 部署

extensions/v1beta1

  • 守护进程集

networking.k8s.io/v1

  • 网络策略

networking.k8s.io/v1beta1

  • 入口

certmanager.k8s.io/v1alpha1

  • 证书
  • 颁发者

基本用法

<?php

require __DIR__ . '/vendor/autoload.php';

use Maclof\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();

使用 JSONPath

允许您查询状态数据。

$jobStartTime = $client->jobs()->find()->getJsonPath('$.status.startTime')[0];

身份验证示例

不安全的 HTTP

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

安全的 HTTPS (CA + 客户端证书验证)

use Maclof\Kubernetes\Client;
use Http\Adapter\Guzzle6\Client as Guzzle6Client;
$httpClient = Guzzle6Client::createWithConfig([
	'verify' => '/etc/kubernetes/ssl/ca.crt',
	'cert' => '/etc/kubernetes/ssl/client.crt',
	'ssl_key' => '/etc/kubernetes/ssl/client.key',
]);
$client = new Client([
	'master' => 'https://master.mycluster.com',
], null, $httpClient);

不安全的 HTTPS (禁用 CA 证书验证)

use Maclof\Kubernetes\Client;
use Http\Adapter\Guzzle6\Client as Guzzle6Client;
$httpClient = Guzzle6Client::createWithConfig([
	'verify' => false,
]);
$client = new Client([
	'master' => 'https://master.mycluster.com',
], null, $httpClient);

使用基本认证

use Maclof\Kubernetes\Client;
$client = new Client([
	'master' => 'https://master.mycluster.com',
	'username' => 'admin',
	'password' => 'abc123',
]);

使用服务账户

use Maclof\Kubernetes\Client;
use Http\Adapter\Guzzle6\Client as Guzzle6Client;
$httpClient = Guzzle6Client::createWithConfig([
	'verify' => '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
]);
$client = new Client([
	'master' => 'https://master.mycluster.com',
	'token' => '/var/run/secrets/kubernetes.io/serviceaccount/token',
], null, $httpClient);

解析 kubeconfig 文件

use Maclof\Kubernetes\Client;

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

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

// Example config that may be returned
// You would then feed these options into the http/kubernetes client constructors.
$config = [
	'master' => 'https://master.mycluster.com',
	'ca_cert' => '/temp/path/ca.crt',
	'client_cert' => '/temp/path/client.crt',
	'client_key' => '/temp/path/client.key',
];

扩展库

自定义仓库

use Maclof\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 Maclof\Kubernetes\Models\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);
}

删除复制控制器

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

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

use Maclof\Kubernetes\Models\DeleteOptions;

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

请参阅 API 文档以了解选项的解释

https://kubernetes.ac.cn/docs/api-reference/v1.6/#deleteoptions-v1-meta