dasann/kubernetes-client

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

v0.27.4 2023-03-20 18:48 UTC

This package is auto-updated.

Last update: 2024-09-20 22:43:21 UTC


README

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

最后在1.9.6版本上测试过,在生产云托管集群上。

使用Composer安装

$ composer require dasann/kubernetes-client

支持的API功能

v1

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

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 Dasann\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 Dasann\Kubernetes\Client;
$client = new Client([
	'master' => 'http://master.mycluster.com',
]);

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

use Da\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 Dasann\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 Dasann\Kubernetes\Client;
$client = new Client([
	'master' => 'https://master.mycluster.com',
	'username' => 'admin',
	'password' => 'abc123',
]);

使用服务账户

use Dasann\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 Dasann\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 Dasann\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 Dasann\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 Dasann\Kubernetes\Models\DeleteOptions;

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

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

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