Laravel PHP K8s is a package that provides access to features offered by the excellent renoki-co/php-k8s package in Laravel. The underlying PHP K8s package is a PHP handler for the Kubernetes Cluster API. With it, you can automate the management of creating, deleting, updating, etc. individual Kubernetes resources directly from PHP.
Here is an example of how PHP K8s provides an object-oriented way to generate Kubernetes resources and configuration dynamically:
1use RenokiCo\PhpK8s\KubernetesCluster; 2 3// Create a new instance of KubernetesCluster 4$cluster = new KubernetesCluster('http://127.0.0.1:8080'); 5 6// Create a new NGINX service. 7$svc = $cluster->service() 8 ->setName('nginx') 9 ->setNamespace('frontend')10 ->setSelectors(['app' => 'frontend'])11 ->setPorts([12 [13 'protocol' => 'TCP',14 'port' => 80,15 'targetPort' => 8016 ],17 ])18 ->create();
Which would equal the following YAML configuration:
1apiVersion: v1 2kind: Service 3metadata: 4 name: nginx 5 namespace: frontend 6spec: 7 selector: 8 app: frontend 9 ports:10 - protocol: TCP11 port: 8012 targetPort: 80
The Laravel PHP package helps further with the above by handling connection configuration and allowing you to access the Kubernetes cluster. Here are some examples from the readme to get a taste of what you can do with this Laravel package:
1use RenokiCo\LaravelK8s\LaravelK8sFacade;2 3foreach (LaravelK8sFacade::getAllConfigMaps() as $cm) {4 // $cm->getName();5}
There are multiple ways to connect to the cluster instance, which means you can configure them and specifiy the connection type to use from the k8s.php
config:
1// Specify the cluster connection type and get the cluster2$cluster = LaravelK8sFacade::connection('http')->getCluster();3 4// Get the cluster using the default connection type5$cluster = LaravelK8sFacade::getCluster();
You can learn more about this package, get full installation instructions, and view the source code on GitHub. I'd also recommend reading through the renoki-co/php-k8s documentation to learn how to manage Kubernetes with this package.
0 comments:
Post a Comment
Thanks