nucleardog/k8s
Composer 安装命令:
composer require nucleardog/k8s
包简介
Kubernetes API library
README 文档
README
A work-in-progress library for developing against the Kubernetes API from PHP with higher level abstractions and richer resources.
Usage
Instantiation
The library depends heavily on the underlying kubernetes-client-php library. Instantiate that
then instantiate this library as a wrapper around it:
<?php
// Instantiate underlying client
$config = KubernetesClient\Config::LoadFromDefault();
$client = new KubernetesClient\Client($config);
// Instantiate this library
$k8s = new Nucleardog\K8s\Kubernetes($client);
Reading Resources
Types are typically identified by their plural name as registered in Kubernetes (e.g., if you
would use kubectl get services -> services). Resources are initially filtered by this, then
you may further filter the resources from there.
<?php
// Fetch all services
$services = $k8s->services->all();
// Fetch a single service by name
$service = $k8s->services->find('my-service');
// Fetch a single service by name, throwing an exception if not found
$service = $k8s->services->findOrFail('my-service');
// Fetch a service by its labels
$service = $k8s->services->whereLabels(new Collection([
'app.kubernetes.io/name' => 'my-service',
]));
Creating Resources
Any resource can be created by passing the required fields to create(). The resource is not
actually submitted to kubernetes until you call save:
<?php
// Data can be specified during creation
$my_service = $k8s->services->create([
'metadata' => [
'name' => 'my-service',
'labels' => [
'app.kubernetes.io/name' => 'example-project',
],
],
'spec' => [
'ports' => [],
'selector' => [
'app.kubernetes.io/name' => 'example-project',
],
],
]);
// Or by setting fields on the object afterwards
$my_service['spec']['ports'][0] = [
'name' => 'http',
'port' => 80,
'protocol' => 'TCP',
'targetPort' => 80,
];
// You must save the resource to actually have these changes reflected anywhere.
$my_service->save();
Modifying Resources
To update values on a resource, simply load it, make the modifications, then call save:
<?php
// Load the resource
$service = $k8s->services->findOrFail('my-service');
// Change some stuff
$service['spec']['selector']['app.kubernetes.io/part-of'] = 'examples';
// Save it
$service->save();
You may also delete resources in a similar way:
<?php
// Load the resource
$service = $k8s->services->findOrFail('my-service');
// Remove it
$service->delete();
Watching Resources
Watches can be registered on any resource type. The library will handle decoding te event into a resource instance for you:
<?php
$k8s->services->on(Nucleardog\K8s\Watcher\EventType::ADDED)->call(
function(Nucleardog\K8s\EventType $event, Nucleardog\K8s\Kind\Pod $pod) {
echo sprintf('Pod Added: Namespace=%s Name=%s', $pod->namespace, $pod->name).PHP_EOL;
},
);
$k8s->watcher->start()->listen();
If a resource class does not exist, you will instead get an instance of Nucleardog\K8s\Resources\Resource.
Namespaces
All calls can be preceeded by a call to namespace() to override the default namespace.
<?php
$service = $k8s->namespace('kube-system')->services->findOrFail('traefik');
You may also override the namespace to use when none is specified by setting it in the options during library instantiation:
<?php
// Instantiate underlying client
$config = KubernetesClient\Config::LoadFromDefault();
$client = new KubernetesClient\Client($config);
// Instantiate optiosn
$options = new Nucleardog\K8s\KubernetesOptions(
namespace: 'my-namespace',
);
// Instantiate this library
$k8s = new Nucleardog\K8s\Kubernetes($client, $options);
Applying Files
This library can load resource definitions from JSON and YAML files.
<?php
$resources = $k8s->json()->fromFile('my-file.json');
//$resources = $k8s->yaml()->fromFile('my-file.yaml');
// These also support fromStream and fromString
Once loaded, you can "apply" the resources:
<?php
$resources->each(fn($resource) => $resource->apply());
Or "delete" the rsources:
<?php
$resources->each(fn($resource) => $resource->delete());
Examples
See the examples for additional information.
Thanks
- Travis Glenn Hansen for kubernetes-client-php which is the "no nonsense PHP client" handling all of the low level authentication and communication concerns so we can add all this nonsense on top.
Legal
Copyright 2024 Adam Pippin hello@adampippin.ca
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
nucleardog/k8s 适用场景与选型建议
nucleardog/k8s 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nucleardog/k8s 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nucleardog/k8s 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-02-07